Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zoom out of chart with cursor outside chart container #2893

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading