问题描述
继:http://stackoverflow.com/questions/326679/choosing-an-attractive-linear-scale-for-a-graphs-y-axis
我相信这部分的问题没有得到回答,但似乎我不能评论或扩展的问题,所以我已经创建了一个新的
值-100,0,100 5蜱:
- 下限= -100
- 上限= 100
- 范围= 100--100 = 200
- 在刻度范围= 40
- 除以10 ^ 2为0.4,转化为0.4,这给40(10 ^ 2相乘)。
- 在新下界= 40 *圆(-100/40)= -80
- 在新的上限= 40 *轮(1 +100 / 40)= 120
或
- 在新下界= 40 *楼(-100/40)= -120
- 在新的上限= 40 *地板(1 +100 / 40)= 120
现在的范围已经在40各增加至240(一个额外的滴答!),用5蜱。这将需要6个步骤,以填补新的范围!
解决方案?
我用下面的code。它产生于人类的观众很好地间隔的步骤和迎合穿过零范围。
公共静态类AxisUtil{ 公共静态浮动CalculateStepSize(浮动范围,浮动targetSteps) { //计算在步长的初始猜测 浮tempStep =范围/ targetSteps; //获取步长的大小 浮磁=(浮点)Math.Floor(Math.Log10(tempStep)); 浮动magPow =(浮点)Math.Pow(10,MAG); //计算新的步长的最显著位 浮magMsd =(int)的(tempStep / magPow + 0.5); //促进来自MSD 1,2,或5 如果(magMsd> 5.0) magMsd = 10.0f; 否则,如果(magMsd> 2.0) magMsd = 5.0F; 否则,如果(magMsd> 1.0) magMsd = 2.0f; 返回magMsd * magPow; }}
Further to: http://stackoverflow.com/questions/326679/choosing-an-attractive-linear-scale-for-a-graphs-y-axis
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;
}
}
这篇关于选择一个有吸引力的线性比例为图形的Y轴 - 更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!