Skip to content

Commit

Permalink
Fix zoom out of chart with cursor outside chart container (#2893)
Browse files Browse the repository at this point in the history
  • Loading branch information
crcarlo authored Sep 5, 2024
1 parent 7135e47 commit 37c6448
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-bananas-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-zoom-container": patch
---

Fix: #2761 zoom out zooming in if cursor outside chart container
88 changes: 88 additions & 0 deletions packages/victory-zoom-container/src/zoom-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { RawZoomHelpers } from "./zoom-helpers";

describe("RawZoomHelpers.getMinimumDomain", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("should be calculating the minimum domain", () => {
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({
x: [0, 100],
}));
expect(
RawZoomHelpers.getMinimumDomain(30, { minimumZoom: true }, "x"),
).toStrictEqual([29.95, 30.05]);
});
});

describe("RawZoomHelpers.getScaledDomain", () => {
it("should scale the domain correctly with a zoom in factor", () => {
expectToBeCloseToArray(
RawZoomHelpers.getScaledDomain([0, 100], 0.9, 0.5),
[5, 95],
);
});
it("should scale the domain correctly with a zoom out factor", () => {
expectToBeCloseToArray(
RawZoomHelpers.getScaledDomain([0, 100], 1.1, 0.5),
[-5, 105],
);
});
});

describe("RawZoomHelpers.scale", () => {
afterEach(() => {
jest.restoreAllMocks();
});

it("should get the correct domain", () => {
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({
x: [0, 100],
}));
jest.spyOn(RawZoomHelpers, "getScalePercent").mockImplementation(() => 0.5);
jest
.spyOn(RawZoomHelpers, "getMinimumDomain")
.mockImplementation(() => [29.955, 30.045]);

expectToBeCloseToArray(
RawZoomHelpers.scale([0, 100], { deltaY: -1 }, {}, "x"),
[0.166, 99.833],
);
});

it("should't change the domain when zooming out with max zoom out", () => {
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({
x: [0, 100],
}));
jest.spyOn(RawZoomHelpers, "getScalePercent").mockImplementation(() => 0.1);
jest
.spyOn(RawZoomHelpers, "getMinimumDomain")
.mockImplementation(() => [29.955, 30.045]);

expectToBeCloseToArray(
RawZoomHelpers.scale([0, 100], { deltaY: 1 }, {}, "x"),
[0, 100],
);
});

it("should't change the domain when zooming out with max zoom out with the cursor outside the container boundary", () => {
jest.spyOn(RawZoomHelpers, "getDomain").mockImplementation(() => ({
x: [0, 100],
}));
jest
.spyOn(RawZoomHelpers, "getScalePercent")
.mockImplementation(() => -0.1);
jest
.spyOn(RawZoomHelpers, "getMinimumDomain")
.mockImplementation(() => [29.955, 30.045]);

expectToBeCloseToArray(
RawZoomHelpers.scale([0, 100], { deltaY: 1 }, {}, "x"),
[0, 100],
);
});
});

function expectToBeCloseToArray(actual, expected) {
expect(actual.length).toBe(expected.length);
actual.forEach((x, i) => expect(x).toBeCloseTo(expected[i]));
}
4 changes: 2 additions & 2 deletions packages/victory-zoom-container/src/zoom-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const RawZoomHelpers = {
const [from, to] = currentDomain;
const range = Math.abs(to - from);
const diff = range - range * factor;
const newMin = Number(from) + diff * percent;
const newMax = Number(to) - diff * (1 - percent);
const newMin = Number(from) + diff * Math.max(percent, 0);
const newMax = Number(to) - diff * Math.max(1 - percent, 0);
return [Math.min(newMin, newMax), Math.max(newMin, newMax)];
},

Expand Down

0 comments on commit 37c6448

Please sign in to comment.