Reasonable optimized chart scaling

前端 未结 6 1622
耶瑟儿~
耶瑟儿~ 2021-01-30 13:43

I need to make a chart with an optimized y axis maximum value.

The current method I have of making charts simply uses the maximum value of all the graphs, then

6条回答
  •  青春惊慌失措
    2021-01-30 14:16

    You could use div and mod. For example.

    Let's say you want your chart to round up by increments of 20 (just to make it more a more arbitrary number than your typical "10" value).

    So I would assume that 1, 11, 18 would all round up to 20. But 21, 33, 38 would round to 40.

    To come up with the right value do the following:

    Where divisor = your rounding increment.
    
    divisor = 20
    multiple = maxValue / divisor;  // Do an integer divide here. 
    if (maxValue modulus divisor > 0)
       multiple++;
    
    graphMax = multiple * maxValue;
    

    So now let's plugin real numbers:

    divisor = 20;
    multiple = 33 / 20; (integer divide)
    so multiple = 1
    if (33 modulus 20 > 0)  (it is.. it equals 13) 
       multiple++;
    
    so multiple = 2;
    graphMax = multiple (2) * maxValue (20);
    graphMax = 40;
    

提交回复
热议问题