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:
- If range > 3 years, choose major=1 year, minor = 3month
- If range > 1 year, choose major=3 month, minor=1 month
- If range > 3 months, choose major=month, minor=nothing (I was considering week however since month is not divisible by week it looks odd)
- If range > 1 week, choose major=1 day, minor=nothing
- If range < 1 day, choose hours according to the nice numbers algorithm
- If range < 1 hour, choose minutes
- If range < 1 minute, choose seconds
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