I am looking for a good algorithm for creating good major / minor tick intervals for the DateTime axis on a custom chart component. Here I read this previous question , which discusses a good way to calculate the major / minor ticks and the min / max values for the numerical axis. However, it is difficult to translate into calculations by date.
The reason is that some date ranges are in different bases for others, some are not even coordinated time frames. Consider a range of axles in a few minutes. This calculation is quite simple, since you can split the minutes using the same algorithm that was presented above. However, what do you do when the same axis is asked to present data in the range Years / Months or Months / Week?
The requirement for this axis is that it can calculate the main / minor marks so that the chart is never too cluttered, with input ranges in milliseconds up to months and years. It is intended for use on real-time charts, so a static presentation is not as important as being able to update quickly. My simplified algorithm does this:
- If range> 3 years, select major = 1 year, minor = 3month
- If range> 1 year, select major = 3 month, minor = 1 month
- If the range is> 3 months, select major = month, minor = nothing (I looked at the week, however, since the month is not divided by weeks, it looks weird).
- If range> 1 week, select major = 1 day, minor = nothing
- If the range is <1 day, select a watch according to the algorithm with good numbers
- If range <1 hour, select minutes
- If range <1 minute, select seconds
etc...
As you can see its many if statements and it’s possible to skip something. In this approach, I often find myself in a quandary on certain dates, and I was wondering if there is an easy way to find this problem.
Regards, Andrew
source