Choosing an attractive linear scale for a graph's Y Axis

后端 未结 13 1448
予麋鹿
予麋鹿 2020-12-07 08:42

I\'m writing a bit of code to display a bar (or line) graph in our software. Everything\'s going fine. The thing that\'s got me stumped is labeling the Y axis.

The

13条回答
  •  半阙折子戏
    2020-12-07 08:56

    Try this code. I've used it in a few charting scenarios and it works well. It's pretty fast too.

    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;
        }
    }
    

提交回复
热议问题