I am building a variable resolution ruler control for a data visualization program. I want it to show axes tick labels depending on zoom level.
For exemple, with a very wide zoom, it would plot only [100, 200, 300] tick labels. Then if I close zoom, it would show, say, [10, 15, 20, 25] labels.
The numbers would always be multiple of 5 or 10. So, a partial “rounding grid” would be [1, 5, 10, 50, 100, 500, 1000, 5000], but that would grow infinitely for both sides (including decimal numbers.
The question is: given an arbitrary positive floating point number, how could I ‘floor’ it to the first smaller number in the grid?
(This would allow me to set the granularity of the tick labels according to the plotter’s zoom level/scale).
Mind that:
- The grid is “infinite” (that’s why I didn’t use sorting and other list methods);
-
There is no fixed set of values to the floating point number, except it’s always positive and non-zero.
-
(EDIT) The resolution IS NOT SYMMETRIC, the tick intervals are, say, [1,5,10,50,100,500,…], the increment multipliers being, alternately, 2 and 5!
Thanks for reading
If the most significant digit is<5, use 1 and (k-1) zeros where k is the length of the number, else use 1 and (k-1) zeros. The following function works.