Skip to content

Commit

Permalink
fix(zoom): Correct zoom in rendering on 0 coord.
Browse files Browse the repository at this point in the history
When mouse over the coordinates after zooming in, the guide line has
incorrect coordinates.

Fix #169
Close #170
  • Loading branch information
cnabro authored and netil committed Oct 24, 2017
1 parent 0b7538b commit f61b792
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 17 additions & 0 deletions spec/api.zoom-spec.js
Expand Up @@ -45,6 +45,23 @@ describe("API zoom", function() {
expect(Math.round(domain[0])).to.be.equal(target[0]);
expect(Math.round(domain[1])).to.be.equal(target[1]);
});

it("should be zoomed and showing focus grid properly when target contained minus value", () => {
const target = [-2, 3]; // zoom in cotaining minus value

chart.zoom(target);

const zoomScale = chart.internal.zoomScale;

// If target contained minus value should not be null
expect(zoomScale).to.not.be.null;

const domain = chart.internal.zoomScale.domain();

// domain value must be above than target
expect(Math.round(domain[0])).to.be.above(target[0]);
expect(Math.round(domain[1])).to.be.above(target[1]);
});
});

describe("zoom line chart #2", () => {
Expand Down
17 changes: 10 additions & 7 deletions src/interactions/zoom.js
Expand Up @@ -103,14 +103,17 @@ extend(ChartInternal.prototype, {
// .on("dblclick.zoom", null);

if ($$.zoomScale) {
const range1 = $$.zoomScale.domain()[0];
const range2 = $$.x.domain()[0];
const delta = 0.015;

// reset scale when zoom is out as initial
if (range1 <= range2 || (range1 - delta) <= range2) {
$$.zoomScale = null;
const zoomDomain = $$.zoomScale.domain();
const xDomain = $$.x.domain();
const delta = 0.015; // arbitrary value

// check if the zoomed chart is fully shown, then reset scale when zoom is out as initial
if (
(zoomDomain[0] <= xDomain[0] || (zoomDomain[0] - delta) <= xDomain[0]) &&
(xDomain[1] <= zoomDomain[1] || xDomain[1] <= (zoomDomain[1] - delta))
) {
$$.xAxis.scale($$.x);
$$.zoomScale = null;
}
}

Expand Down

0 comments on commit f61b792

Please sign in to comment.