BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
Problem: given the start and end (min & max) of a range values to be graphed, determine the 'best' tick mark interval.
Simple solution: (end - start + 1) / noOfTicks = interval.
For: 0 .. 9 by 10 := ( 9 - 0 + 1 ) / 10 = 1; gives ticks at 0,1,2,3,4,5,6,7,8,9.
But: 0.123 .. 8.672 by 10 := ( 8.672 - 0.123 +1 ) / 10 = 0.9549; gives ticks at: 0.123, 1.0779, 2.0328, 2.9877, 3.9426, 4.8975, 5.8524, 6.8073, 7.7622, 8.7171. Not so nice!
Okay. So do a little rounding. But to what boundaries?
Eg. rounding to the nearest integer works above, but how about if the inputs are: 0.022 .. 0.086 by 10?
0.022, 0.0294, 0.0368, 0.0442, 0.0516, 0.059, 0.0664, 0.0738, 0.0812, 0.0886
Here, by inspection, we can conclude that the 'best' tick marks would probably be 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09.
But that's only 8 ticks, and we had to change the calculation from +1 to +0.01. So the formula needs to be: r = ( end - start ); r += r/n; interval = r / n; some rounding formula?
Or 1,123,345,456 .. 1,124,246,357.5 by 11?
1123345456, 1123427356.13636, 1123509256.27273, 1123591156.40909, 1123673056.54546, 1123754956.68182, 1123836856.81818, 1123918756.95455, 1124000657.09091, 1124082557.22727, 1124164457.36364
Erm?
I know; its simple stuff that's been solved many times; but I've failed to see a fully generic pattern; nor find reference to one.
Thoughts; pointers; admonishments?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple calculation. (A solution; can it be improved/broken?)
by BrowserUk (Patriarch) on May 09, 2015 at 12:56 UTC | |
|
Re: Simple calculation.
by Anonymous Monk on May 09, 2015 at 07:26 UTC | |
by BrowserUk (Patriarch) on May 09, 2015 at 09:01 UTC | |
by Anonymous Monk on May 09, 2015 at 10:26 UTC | |
by Anonymous Monk on May 09, 2015 at 12:46 UTC | |
|
Re: calculate the best Interval between tick marks (dollars cents)
by Anonymous Monk on May 09, 2015 at 07:17 UTC | |
by BrowserUk (Patriarch) on May 09, 2015 at 08:50 UTC |