Skip to content

Commit

Permalink
fix(zoom): Correct zoom.range() to work
Browse files Browse the repository at this point in the history
- Use zoom.min/max for the range setting
- Add test codes for zoom.min/max/range

Fix #290
Close #291
  • Loading branch information
netil authored Feb 14, 2018
1 parent 63350d1 commit 6830e5c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
54 changes: 53 additions & 1 deletion spec/api/api.zoom-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,57 @@ describe("API zoom", function() {
});
});

// @TODO zoom.max/min/range
describe("zoom.min/max/range()", () => {
chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250]
]
},
zoom: {
enabled: true
}
});

it("should be updated the minimum zoom range", () => {
const range = chart.zoom.min(-1);
const zoomRange = chart.zoom([-1, 1]);

expect(Math.round(zoomRange[0])).to.be.equal(range);
expect(+chart.internal.main.select(`.${CLASS.axisX} .tick`).attr("transform").match(/\d+/)[0]).to.be.above(250);
});

it("should be updated the maximum zoom range", () => {
const range = chart.zoom.max(6);
const zoomRange = chart.zoom([4, 6]);

expect(Math.round(zoomRange[1])).to.be.equal(range);

const tick = chart.internal.main.selectAll(`.${CLASS.axisX} .tick`);
expect(+tick.filter(`:nth-child(${tick.size() + 1})`).attr("transform").match(/\d+/)[0]).to.be.below(500);
});

it("should be updated zoom range", () => {
const main = chart.internal.main;
const range = chart.zoom.range({
min: -2,
max: 7
});

// check the min range
let zoomRange = chart.zoom([-2, 1]);

expect(Math.round(zoomRange[0])).to.be.equal(range.min);
expect(+main.select(`.${CLASS.axisX} .tick`).attr("transform").match(/\d+/)[0]).to.be.above(350);

// check the max range
zoomRange = chart.zoom([5, 7]);

expect(Math.round(zoomRange[1])).to.be.equal(range.max);

const tick = main.selectAll(`.${CLASS.axisX} .tick`);

expect(+tick.filter(`:nth-child(${tick.size() + 1})`).attr("transform").match(/\d+/)[0]).to.be.below(5);
});
});
});
10 changes: 5 additions & 5 deletions src/api/api.zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ extend(zoom, {
* });
*/
range: function(range) {
const domain = this.domain;
const zoom = this.zoom;

if (isObject(range)) {
isDefined(range.max) && domain.max(range.max);
isDefined(range.min) && domain.min(range.min);
isDefined(range.min) && zoom.min(range.min);
isDefined(range.max) && zoom.max(range.max);
}

return {
max: domain.max(),
min: domain.min()
min: zoom.min(),
max: zoom.max()
};
}
});
Expand Down

0 comments on commit 6830e5c

Please sign in to comment.