Skip to content

Commit

Permalink
fix(axis): Fix for multi axes data bound
Browse files Browse the repository at this point in the history
When data are bound to use different axis, it should maintain its axis domain value.

Ref #1233 (comment)
  • Loading branch information
netil authored Mar 11, 2020
1 parent 1bc3f20 commit 3f8afba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
27 changes: 27 additions & 0 deletions spec/internals/domain-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,32 @@ describe("DOMAIN", function() {
expect(yDomain).to.be.deep.equal(y2Domain);
});

it("set option data.axes", () => {
args.data.axes = {
data2: "y2"
};
});

describe("check the domain value after the data toggle", () => {
const checkDomain = (dataId, axisId, done) => {
const domain = chart.internal[axisId].domain();

// when
chart.toggle(dataId);

setTimeout(() => {
expect(chart.internal[axisId].domain()).to.be.deep.equal(domain);
done();
}, 300);
};

it("y Axis domain should maintain", done => {
checkDomain("data1", "y", done);
});

it("y2 Axis domain should maintain", done => {
checkDomain("data2", "y2", done);
});
});
});
});
29 changes: 23 additions & 6 deletions src/internals/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ extend(ChartInternal.prototype, {
return this.getYDomainMinMax(targets, "max");
},

/**
* Check if hidden targets bound to the given axis id
* @return {Boolean}
* @private
*/
isHiddenTargetWithYDomain(id) {
const $$ = this;

return $$.hiddenTargetIds
.some(v => $$.axis.getId(v) === id);
},

getYDomain(targets, axisId, xDomain) {
const $$ = this;
const config = $$.config;
Expand All @@ -78,12 +90,17 @@ extend(ChartInternal.prototype, {
const targetsByAxisId = targets.filter(t => $$.axis.getId(t.id) === axisId);
const yTargets = xDomain ? $$.filterByXDomain(targetsByAxisId, xDomain) : targetsByAxisId;

if (yTargets.length === 0) { // use domain of the other axis if target of axisId is none
return axisId === "y2" ?
$$.y.domain() :
// When all data bounds to y2, y Axis domain is called prior y2.
// So, it needs to call to get y2 domain here
$$.getYDomain(targets, "y2", xDomain);
if (yTargets.length === 0) {
if ($$.isHiddenTargetWithYDomain(axisId)) {
return $$[axisId].domain();
} else {
// use domain of the other axis if target of axisId is none
return axisId === "y2" ?
$$.y.domain() :
// When all data bounds to y2, y Axis domain is called prior y2.
// So, it needs to call to get y2 domain here
$$.getYDomain(targets, "y2", xDomain);
}
}

const yMin = config[`${pfx}_min`];
Expand Down

0 comments on commit 3f8afba

Please sign in to comment.