Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating Date Time Axis minimum, maximum and major minor tick interval

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

like image 816
Dr. Andrew Burnett-Thompson Avatar asked Nov 05 '22 08:11

Dr. Andrew Burnett-Thompson


1 Answers

  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.

like image 194
Dialecticus Avatar answered Nov 09 '22 05:11

Dialecticus