Algorithm for determining labels and positions along the Y axis?

Given an array of y values, for example [-3400, -1000, 500, 1200, 3790], how do you define “good” Y-axis labels and put them in a grid?

           ^
---(6,000)-|---
           |
---(4,000)-|---
           |
---(2,000)-|---
           |
<------(0)-+---------------------------------------->
           |
--(-2,000)-|---
           |
--(-4,000)-|---
           V
+3
source share
1 answer

You can do this in the following lines:

  • Determine the number of labels you want to have ( n). As a result, there may not be many shortcuts, but it will be close. I will have it n = 6.
  • Decide which numbers you think are "beautiful." You want to see only steps, such as 1, 2, 5, 10, 20, 50, ... (in this case, pretty numbers 1, 2 and 5) or steps 1, 2, 4, 5, 6, 8, 10 ,... good too? I will consider only 1, 2 and 5.
  • (min) (max) , . (min = -3400, max = 3790)
  • , uglyStep = (max - min) / (n - 2). . (uglyStep = 1797)
  • uglyStep magnitude = 10 ^ floor(log10(uglyStep)). (magnitude = 1000)
  • , . uglyStep. prettyStep. (prettyStep = 2000)
  • bottom = floor(min / prettyStep) * prettyStep top = ceil(max / prettyStep) * prettyStep. , / , C- . (bottom = -4000, top = 4000)
  • bottom top, prettyStep, . (-4000, -2000, 0, 2000, 4000)

, , min max bottom top.

, , . min = 0 max = 3002 0, 500, 1000, 1500, 2000, 2500, 3000, 3500, max = 3005 0, 1000, 2000, 3000, 4000.

+4

All Articles