ChartJS Zoom Granularity

563 Views Asked by At

The data I'm working with has a 'floor' granularity of 'Days'. Anything more fine-grain (e.g. hours) does not make sense. However the ChartJS plugin allows the user to keep zooming to finer scales.

How can I detect that the zoom level has now hit 'Days' and prevent any further zoom-in? I would like to allow zooming from year -> day level but nothing past this.

1

There are 1 best solutions below

0
On BEST ANSWER

I solved this with what I consider a hack:

  1. Hook into the onZoomStart callback. (docs)
  2. Read the values (dates in millis) of the x-axis Ticks (docs)
  3. Check if the duration between any two consecutive dates is less than 1day.

Notes:

  • If (3) is true then the chart is already at the finest desired granularity
  • If event.deltaY > 0 the event is a zoom-out, else zoom-in
    • I only wanted to limit zoom-in, so if the event is a zoom-out I immediately return true