6

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

Community
  • 1
  • 1
Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178

1 Answers1

2
  1. Have a sorted array of all possible ticks: 1 second, 1 minute, 1 hour, 1 day, 1 month, 1 year.
  2. Decide what is the minimum distance for major (min_dist_major) and for minor (min_dist_minor) tick. Those are constants and have values in pixels.
  3. Find the smallest major tick that is larger than min_dist_major when shown on monitor.
  4. Then find the smallest minor tick that is less than major tick but larger than 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.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Great solution, let me try that. I suppose for sub-second, and greater than 1 year you could use numeric tick calculation: multiples of 1, 2, 5 – Dr. Andrew Burnett-Thompson Dec 27 '11 at 22:26
  • You can use any values you like, but 1, 2 and 5 are most common. For months that would be 1, 2, 3, 4 and 6, and for days along with 1, 2 and 5 I would add 10 and 15 as well. The problem is if you don't know the upper limit. Just add 10, 20, 50, 100, 200, 500 years and you're probably fine :) – Dialecticus Dec 28 '11 at 00:09
  • One problem I have is to have the major tick divisible by minor tick. This is mainly due to how other parts of my software calculate the coordinates for tick marks. 3-Month/week was a bad combination for instance. What I might do instead is disable minor for certain tick combinations. Going to try your sln, give me a few days as it is Christmas an all ;-) – Dr. Andrew Burnett-Thompson Dec 28 '11 at 22:01
  • Major tick does not have to be divisible by minor. You just have to handle the situation when it isn't. You could reset minor ticks to begin at the start of every major. Or you could just place both types every which way and not worry where will they fall. Of course you could also decide that minor ticks are exactly a half or a quarter of a major and be done with it. – Dialecticus Dec 28 '11 at 22:42
  • Ok, I've implemented this and it works very well. The problem was I had a custom class called DateSpan which handled Months and Years (in addition to timeSpan), however this was faulty. I've refactored my code to use TimeSpan but with some extension methods with tick constants to handle Month and Year. Factoring out the special cases it works fine, I no longer need Major to be divisible by minor. Thanks :-) – Dr. Andrew Burnett-Thompson Jan 02 '12 at 21:58