Axis tick mark algorithm

Hi all,

I've been struggling trying to come up with a suitable algorithm for determining tick marks on an axis based on the range of values the axis should display.

For example, if my initial data set contains the values

[5,20,29,51,60,63,79,95]

I can get the Range of 90

I can specify I want, say, 5 tick marks on my axis, so my interval between marks is 90/5 = 18

so I could create an axis with values displayed of 0,18,36,54,72,90

but what I'd like to display here is 0,25,50,75,100 (ie more useful/readable values)

the axis will also have to cope with floats and negative values, so my data set could be....

[-5,-2.5,3.9,50.2, 80.9]

and if I specified ten tick marks Id want output of something like:

-10,0,10,20,30,40,50,60,70,80

If anyone has any ideas or can point me towards some source which does this kind of calculation I'd be very grateful.

Thanks in advance,

Dylan

[971 byte] By [Dylberta] at [2007-10-2 15:07:04]
# 1

In general, since the factor of 10 are 1,2 and 5, you want to place tic marks at units that are either 1, 2 or 5 "units" apart, where "unit" is some power of ten like .01, .1, 1, 10, 100 etc.

Secondly, if your picture has 1000 tic marks, there will be too many of them. If there is only one tic mark, or maybe two, you havent given the person enough to look at.

This argues that there should be some reasonable number of tic mark as a target that you are shooting for, like 10 or 20 or something like that.

SO - given any range, that you want to cover, and given any "unit", and given any choice of multiplier (1, 2 or 5) you could calculate how many tics it would take to cover that range.

Given a target number of tics that you think is optimal you should be able to very quickly find the unit and multiplier that gives you the optimal number of tics.

For extra beauty, draw tics that land on multiples of 10 long, draw tics that land on multiples of 5 medium and draw ones that land on anything else short.

Enjoy!

marlin314a at 2007-7-13 13:59:05 > top of Java-index,Other Topics,Algorithms...