chartjs / chartjs-plugin-zoom

Zoom and pan plugin for Chart.js
MIT License
579 stars 322 forks source link

minRange for Category #796

Open mBreyerBG opened 8 months ago

mBreyerBG commented 8 months 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 6 months ago

+1

alimertcakar commented 2 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 1 month ago

+1