I am looking for a good algorithm to generate nice major/minor tick intervals for a DateTime axis on custom charting component. I have read this previous question here, which discusses a nice way to calculate major/minor ticks and minimum/maximum values for a numerical axis. However, it is difficult to translate this into date axis calculations.
The reason is that some date ranges are in different bases to others, some are not even consistent time frames. Consider an axis range in several minutes. This calculation is fairly easy as you can divide the minutes up using a similar algorithm to that presented above. However what do you do when the same axis is asked to present data in the Years/Months, or Months/Week range?
The requirement for this axis are that it can calculate major/minor tick marks so that the chart is never too cluttered, with input data ranges in the milliseconds right up to months and years. It is intended to be used on a realtime chart so static presentation is not as important as ability to update quickly. My simplistic algorithm does this:
etc...
As you can see its a lot of if-statements and possible to miss something out. In this approach I often end up with glitches at certain dates and I was wondering if there was an easy or common way to approach this problem.
Best regards, Andrew
min_dist_major
) and for minor (min_dist_minor
) tick. Those are constants and have values in pixels. min_dist_major
when shown on monitor. min_dist_minor
when shown on monitor. If minor tick can't be found then it does not exist. You can expand your array with other ticks, such as 2 and 5 of every type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With