4

Scenario:

Drawing a graph. Have data points which range from A to B, and want to decide on a granularity for drawing the axis scales. Eg, for 134 to 151 the scale might run from 130 to 155, to start and end on "round" numbers in the decimal system. But the numbers might run from 134.31 to 134.35, in which case a scale from 130 to 135 would (visually) compress out the "significance" in the data -- it would be better to draw the scale from 134 to 135, or maybe even 134.3 to 134.4. And the data values might instead run from 0.013431 to 0.013435, or from 1343100 to 1343500.

So I'm trying to figure out an elegant way to calculate the "granularity" to round the low bound down to and the upper bound up to, to produce a "pleasing" chart. One could just "hack" it somehow, but that produces little confidence that "odd" cases will be handled well.

Any ideas?

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • 1
    Possible dup of http://stackoverflow.com/questions/361681/algorithm-for-nice-grid-line-intervals-on-a-graph, which has a lot more good answers. – Geoffrey Zheng Apr 25 '15 at 02:51

2 Answers2

2

Just an idea:

  1. Add about 10% to your range, tune this figure empirically
  2. Divide size of range by number of tick marks you want to have
  3. Take the base 10 logarithm of that number
  4. Multiply the result by three, then round to the nearest integer
  5. The remainder modulo 3 will tell you whether you want the least significant decimal to change in steps of 1, 2, or 5
  6. The result of an integer division by 3 will tell you the power of ten to use
  7. Take the (extended) range and compute the extremal tick points it contains, according to the tick frequencey just computed
  8. Ensure that all data points actually lie within that range, add ticks if not
  9. If needed, add minor ticks by decreasing the integer above by one
MvG
  • 57,380
  • 22
  • 148
  • 276
  • +1 -- Yeah, the base 10 log is something that hadn't occurred to me. And the rest of your algorithm has some interesting points to ponder. – Hot Licks Nov 15 '12 at 23:10
0

I found a very helpful calculation which is very similar to the axis scale of excel graphs:

enter image description here

It is written for excel but I used and transformed it into objective-c code for setting up my graph axis.

JFS
  • 2,992
  • 3
  • 37
  • 48