Choosing an attractive linear scale for a graph’s Y Axis - more
Further to: Choosing an attractive linear scale for a graph's Y Axis
And what to do when some of the points are negative?
I believe this part of the question was not answered but it seems I can't comment or extend that question so 开发者_开发百科I've created a new one
Values -100, 0, 100 with 5 ticks:
- lower bound = -100
- upper bound = 100
- range = 100--100 = 200
- tick range = 40
- Divide by 10^2 for 0.4, translates to 0.4, which gives (multiplied by 10^2) 40.
- new lower bound = 40 * round(-100/40) = -80
- new upper bound = 40 * round(1+100/40) = 120
or
- new lower bound = 40 * floor(-100/40) = -120
- new upper bound = 40 * floor(1+100/40) = 120
Now the range has been increased to 240 (an extra tick!), with 5 ticks at 40 each. it will take 6 steps to fill the new range!
Solution?
I use the following code. It produces nicely-spaced steps for human viewers and caters for ranges that pass through zero.
public static class AxisUtil
{
public static float CalculateStepSize(float range, float targetSteps)
{
// calculate an initial guess at step size
float tempStep = range/targetSteps;
// get the magnitude of the step size
float mag = (float)Math.Floor(Math.Log10(tempStep));
float magPow = (float)Math.Pow(10, mag);
// calculate most significant digit of the new step size
float magMsd = (int)(tempStep/magPow + 0.5);
// promote the MSD to either 1, 2, or 5
if (magMsd > 5.0)
magMsd = 10.0f;
else if (magMsd > 2.0)
magMsd = 5.0f;
else if (magMsd > 1.0)
magMsd = 2.0f;
return magMsd*magPow;
}
}
精彩评论