Skip to content

Commit

Permalink
fix(axis): fix handling x padding value
Browse files Browse the repository at this point in the history
Make sure to handle correctly when axis.x.padding value is
given as number or as object.

Fix #2038
  • Loading branch information
netil committed Apr 16, 2021
1 parent 0a48fd6 commit 0a42768
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/ChartInternal/Axis/Axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,11 @@ class Axis {
* @returns {object} Padding object values with 'left' & 'right' key
* @private
*/
getXAxisPadding(tickCount) {
getXAxisPadding(tickCount: number): {left: number, right: number} {
const $$ = this.owner;
let {left = 0, right = 0} = $$.config.axis_x_padding;
let padding = {left, right};
const padding = $$.config.axis_x_padding;
let {left = 0, right = 0} = isNumber(padding) ?
{left: padding, right: padding} : padding;

if ($$.axis.isTimeSeries()) {
const firstX = +$$.getXDomainMin($$.data.targets);
Expand All @@ -752,11 +753,9 @@ class Axis {
left = left / range / relativeTickWidth;
right = right / range / relativeTickWidth;
}

padding = {left, right};
}

return padding;
return {left, right};
}

updateLabels(withTransition) {
Expand Down
53 changes: 53 additions & 0 deletions test/internals/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,59 @@ describe("AXIS", function() {

util.generate(option);
});

it("set options", () => {
args = {
data: {
x: "x",
columns: [
["x", 1, 2, 3, 4, 5],
["data1", 30, 200, 100, 400, 150],
],
type: "line"
},
axis: {
x: {
type: "category",
tick: {
rotate: 5
},
padding: 10
}
}
}
});

it("check if axis.x.padding correctly set when is given as number value.", () => {
const {state} = chart.internal;
const padding = args.axis.x.padding;

expect(state.axis.x.padding).to.be.deep.equal({left: padding, right: padding});
});

it("set options axis.x.padding={left: 5}", () => {
args.axis.x.padding = {left: 5};
});

it("check if axis.x.padding correctly set when is given as 'left' key only.", () => {
const {state} = chart.internal;
const padding = args.axis.x.padding;

padding.right = 0;

expect(state.axis.x.padding).to.be.deep.equal(padding);
});

it("set options axis.x.padding={left: 15, right: 5}", () => {
args.axis.x.padding = {left: 15, right: 5};
});

it("check if axis.x.padding correctly set when is given as object type.", () => {
const {state} = chart.internal;
const padding = args.axis.x.padding;

expect(state.axis.x.padding).to.be.deep.equal(padding);
});
});

describe("axis min/max", () => {
Expand Down

0 comments on commit 0a42768

Please sign in to comment.