Skip to content

Commit

Permalink
fix(bar): Bar's width resize according the zoom scale
Browse files Browse the repository at this point in the history
- Fix .getBarW() to update width on zoom interaction
- Make to hold event rect elements in $$.eventRect
- Make to not bind zoom event when subchart is enabled
- Make xx() to handle direct data value argument

Fix #1185
  • Loading branch information
netil authored Jan 3, 2020
1 parent 2ab23b9 commit 93184a2
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 30 deletions.
53 changes: 53 additions & 0 deletions spec/api/api.zoom-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,57 @@ describe("API zoom", function() {
}, 300);
});
});

describe("bar's width based on ratio", () => {
before(() => {
chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250]
],
type: "bar"
},
bar: {
width: {
ratio: 0.8
}
},
zoom: {
enabled: true
}
});
});

it("check bar's width for zoom in/out API call", done => {
const len = [];

chart.$.bar.bars.each(function() {
len.push(this.getTotalLength());
});

// when
chart.zoom([1, 2]);

new Promise((resolve, reject) => {
setTimeout(() => {
chart.$.bar.bars.each(function(d, i) {
expect(this.getTotalLength()).to.be.greaterThan(len[i]);
});

resolve();
}, 300);
}).then(() => {
// when
chart.unzoom();
}).then(() => {
setTimeout(() => {
chart.$.bar.bars.each(function(d, i) {
expect(this.getTotalLength()).to.be.equal(len[i]);
});

done();
}, 300);
});
});
});
});
104 changes: 81 additions & 23 deletions spec/interactions/zoom-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ describe("ZOOM", function() {
});

it("region area should be resized on zoom", done => {
const main = chart.$.main;
const main = chart.$.main;
const regionRect = main.select(`.${CLASS.region}-0 rect`);
const lineWidth = chart.$.line.lines.node().getBBox().width;

Expand Down Expand Up @@ -734,8 +734,8 @@ describe("ZOOM", function() {
x: {
type: "category",
tick: {
rotate: 40,
multiline: false
rotate: 40,
multiline: false
}
}
},
Expand Down Expand Up @@ -766,11 +766,6 @@ describe("ZOOM", function() {
clientY: 137
});

const expected = {
x: [7.738255334651066, 84.52408366017097],
y: [0, 101.2]
};

["x", "y"].forEach(id => {
const domain = chart.internal[id].domain();
const org = orgDomain[id];
Expand All @@ -790,26 +785,26 @@ describe("ZOOM", function() {
it("set options", () => {
args = {
data: {
columns: [
["data1", 30, 350, 200],
["data2", 130, 100, 10],
["data3", 230, 153, 85]
],
types: {
data1: "scatter",
columns: [
["data1", 30, 350, 200],
["data2", 130, 100, 10],
["data3", 230, 153, 85]
],
types: {
data1: "scatter",
},
labels: true
labels: true
},
bubble: {
maxR: 50
maxR: 50
},
axis: {
x: {
type: "category"
},
y: {
max: 450
}
x: {
type: "category"
},
y: {
max: 450
}
},
zoom: {
enabled: true
Expand Down Expand Up @@ -914,4 +909,67 @@ describe("ZOOM", function() {
expect(tickTexts.size()).to.be.equal(args.axis.y.tick.culling.max);
});
});

describe("bar's width based on ratio", () => {
before(() => {
args = {
data: {
x: "x",
columns: [
["x", "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
["data1", 30, 200, 100, 400, 150, 250]
],
type: "bar"
},
bar: {
width: {
ratio: 0.8
}
},
axis: {
x: {
type: "timeseries"
}
},
zoom: {
enabled: {
type: "wheel"
}
}
}
});

it("check bar's width during wheel zoom in/out", () => {
const eventRect = chart.$.main.select(`.${CLASS.eventRect}-2`).node();
const len = [];

chart.$.bar.bars.each(function() {
len.push(this.getTotalLength());
});

// when zoom in
util.fireEvent(eventRect, "wheel", {
deltaX: 0,
deltaY: -100,
clientX: 159,
clientY: 137
});

chart.$.bar.bars.each(function(d, i) {
expect(this.getTotalLength()).to.be.greaterThan(len[i]);
});

// when zoom out
util.fireEvent(eventRect, "wheel", {
deltaX: 0,
deltaY: 100,
clientX: 159,
clientY: 137
});

chart.$.bar.bars.each(function(d, i) {
expect(this.getTotalLength()).to.be.equal(len[i]);
});
});
});
});
3 changes: 2 additions & 1 deletion src/interactions/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ extend(ChartInternal.prototype, {
.merge(eventRectUpdate);
}

$$.eventRect = eventRectUpdate;
$$.updateEventRect(eventRectUpdate);

if ($$.inputType === "touch" && !$$.svg.on("touchstart.eventRect") && !$$.hasArcType()) {
Expand Down Expand Up @@ -193,7 +194,7 @@ extend(ChartInternal.prototype, {
const $$ = this;
const config = $$.config;
const xScale = $$.zoomScale || $$.x;
const eventRectData = eventRectUpdate || $$.eventRect.data();// set update selection if null
const eventRectData = eventRectUpdate || $$.eventRect.data(); // set update selection if null
const isRotated = config.axis_rotated;
let x;
let y;
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ extend(ChartInternal.prototype, {
const eventRects = $$.main.select(`.${CLASS.eventRects}`);

if (zoomEnabled && bind) {
$$.bindZoomOnEventRect(eventRects, zoomEnabled.type);
// Do not bind zoom event when subchart is shown
!$$.config.subchart_show &&
$$.bindZoomOnEventRect(eventRects, zoomEnabled.type);
} else if (bind === false) {
$$.api.unzoom();

Expand Down
14 changes: 10 additions & 4 deletions src/internals/ChartInternal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {transition as d3Transition} from "d3-transition";
import Axis from "../axis/Axis";
import CLASS from "../config/classes";
import {document, window} from "../internals/browser";
import {notEmpty, asHalfPixel, getOption, isValue, isArray, isFunction, isString, isNumber, isObject, callFn, sortValue} from "./util";
import {notEmpty, asHalfPixel, getOption, isArray, isFunction, isNumber, isObject, isString, isValue, callFn, sortValue} from "./util";

/**
* Internal chart class.
Expand Down Expand Up @@ -979,11 +979,17 @@ export default class ChartInternal {
return this.hasDataLabel() ? "1" : "0";
}

/**
* Get the zoom or unzoomed scaled value
* @param {Date|Number|Object} d Data value
* @private
*/
xx(d) {
const fn = this.config.zoom_enabled && this.zoomScale ?
this.zoomScale : this.x;
const $$ = this;
const fn = $$.config.zoom_enabled && $$.zoomScale ?
$$.zoomScale : this.x;

return d ? fn(d.x) : null;
return d ? fn(isValue(d.x) ? d.x : d) : null;
}

xv(d) {
Expand Down
4 changes: 3 additions & 1 deletion src/shape/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ extend(ChartInternal.prototype, {
getBarW(axis, barTargetsNum) {
const $$ = this;
const config = $$.config;
const tickInterval = axis.tickInterval($$.getMaxDataCount());
const maxDataCount = $$.getMaxDataCount();
const isGrouped = config.data_groups.length;
const tickInterval = ($$.zoomScale || $$) && !$$.isCategorized() ?
$$.xx($$.subX.domain()[1]) / maxDataCount : axis.tickInterval(maxDataCount);
let result;

const getWidth = id => {
Expand Down

0 comments on commit 93184a2

Please sign in to comment.