chartjs / chartjs-plugin-zoom

Zoom and pan plugin for Chart.js
MIT License
598 stars 327 forks source link

minRange for Category #796

Closed mBreyerBG closed 2 weeks ago

mBreyerBG commented 1 year ago

I have a barChart with categories. i am using drag to zoom. How can i define a minimum amount of categories, just like e.g: limits.x.minRange would work? Is this possible?! Right now i can zoom, so that only one category is visible and fills the whole chart. i want like minimum of 4 categories to be visible

jgrayson-apl commented 10 months ago

+1

alimertcakar commented 7 months ago

+1

There is supposed to be a prop for this but nothing worked. You can try this as a workaround:

           const minDesiredElems = 10;
           onZoomStart: ({ event, chart }) => {
            const isZoomIn = event.deltaY < 0;
            const hasLessThanDesired = chart.scales.xAxes.ticks?.length <= minDesiredElems;
            // reset it back in case of a big step
            if (chart.scales.xAxes.ticks?.length <= minDesiredElems-1 && isZoomIn) {
              chart.zoom(chart, { x: minDesiredElems });
            }
            // skip zoom event
            if (isZoomIn && hasLessThanDesired) return false;
          }

(This isn't really well tested code, but somewhat works)

fecipo commented 6 months ago

+1