From a8ec411f893458925428e1b09ec5d4f13e28c863 Mon Sep 17 00:00:00 2001 From: Jae Sung Park Date: Wed, 14 Apr 2021 16:35:51 +0900 Subject: [PATCH] fix(axis): Fix axis.x.padding value setting Refactor .getXAxisPadding() to handle axis.x.padding option, when value given is number type. Fix #2038 --- src/ChartInternal/Axis/Axis.ts | 19 +++++----------- test/internals/axis-spec.ts | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/ChartInternal/Axis/Axis.ts b/src/ChartInternal/Axis/Axis.ts index 59d8b345b..061d30e0a 100644 --- a/src/ChartInternal/Axis/Axis.ts +++ b/src/ChartInternal/Axis/Axis.ts @@ -737,29 +737,20 @@ class Axis { */ getXAxisPadding(tickCount) { const $$ = this.owner; - let padding = $$.config.axis_x_padding; - - if (isEmpty(padding)) { - padding = {left: 0, right: 0}; - } else { - padding.left = padding.left || 0; - padding.right = padding.right || 0; - } + let {left = 0, right = 0} = $$.config.axis_x_padding; + let padding = {left, right}; if ($$.axis.isTimeSeries()) { const firstX = +$$.getXDomainMin($$.data.targets); const lastX = +$$.getXDomainMax($$.data.targets); const timeDiff = lastX - firstX; - - const range = timeDiff + padding.left + padding.right; - let left = 0; - let right = 0; + const range = timeDiff + left + right; if (tickCount && range) { const relativeTickWidth = (timeDiff / tickCount) / range; - left = padding.left / range / relativeTickWidth; - right = padding.right / range / relativeTickWidth; + left = left / range / relativeTickWidth; + right = right / range / relativeTickWidth; } padding = {left, right}; diff --git a/test/internals/axis-spec.ts b/test/internals/axis-spec.ts index fc6bf3732..e071999c0 100644 --- a/test/internals/axis-spec.ts +++ b/test/internals/axis-spec.ts @@ -2469,6 +2469,46 @@ describe("AXIS", function() { expect(tickValues.every(v => v % 1 === 0)).to.be.true; expect(translateX).to.be.closeTo(30.5, 1); }); + + it("should work with axis.x.padding=10 option", done => { + const option = { + data: { + x: "x", + columns: [ + ["x", "2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04", "2020-01-05", "2020-01-06"], + ["data1", 30, 200, 100, 400, 150, 250], + ["data2", 130, 340, 200, 500, 250, 350] + ], + type: "line" + }, + axis: { + x: { + type: "timeseries", + tick: { + format: "%Y-%m-%d" + }, + padding: 100 + } + }, + subchart: { + show: true, + init: { + range: [ + +new Date("2020-01-02 00:00:00"), + +new Date("2020-01-03 00:00:00") + ] + } + }, + onafterinit: function() { + // reaching at this point, means no issue happened + expect(true).to.be.ok; + + done(); + } + }; + + util.generate(option); + }); }); describe("axis min/max", () => {