Skip to content

Commit

Permalink
Avoid zooming in when zooming out over certain areas of chart (#574)
Browse files Browse the repository at this point in the history
* Avoid zooming in when zooming out over certain areas of chart

Zooming out when hovering over y-axis labels or legend would cause `minPercent` to be negative or past 100%, causing the chart to zoom in instead.

* Update src/scale.types.js

Co-authored-by: Jukka Kurkela <jukka.kurkela@gmail.com>
  • Loading branch information
0b10011 and kurkle committed Sep 20, 2021
1 parent 786c34f commit cf1966d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/scale.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ function zoomDelta(scale, zoom, center) {
const newRange = range * (zoom - 1);

const centerPoint = scale.isHorizontal() ? center.x : center.y;
const minPercent = (scale.getValueForPixel(centerPoint) - scale.min) / range || 0;
// `scale.getValueForPixel()` can return a value less than the `scale.min` or
// greater than `scale.max` when `centerPoint` is outside chartArea.
const minPercent = Math.max(0, Math.min(1,
(scale.getValueForPixel(centerPoint) - scale.min) / range || 0
));

const maxPercent = 1 - minPercent;

return {
Expand Down

0 comments on commit cf1966d

Please sign in to comment.