From b8396a077ce4e7c1e0e354e18911e9c6dfe007c8 Mon Sep 17 00:00:00 2001 From: Jae Sung Park Date: Thu, 27 Oct 2022 12:47:49 +0900 Subject: [PATCH] fix(axis): Fix translate NaN error Check the given x tick value to get correct scale. Fix #2924 --- src/ChartInternal/Axis/AxisRendererHelper.ts | 6 ++-- test/internals/axis-spec.ts | 38 ++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/ChartInternal/Axis/AxisRendererHelper.ts b/src/ChartInternal/Axis/AxisRendererHelper.ts index da890b569..f45d2da75 100644 --- a/src/ChartInternal/Axis/AxisRendererHelper.ts +++ b/src/ChartInternal/Axis/AxisRendererHelper.ts @@ -4,7 +4,7 @@ * @ignore */ import {getScale} from "../internals/scale"; -import {isDefined, isNumber, isString} from "../../module/util"; +import {isDefined, isNumber, isString, isValue} from "../../module/util"; import type {d3Selection} from "../../../types/types"; export default class AxisRendererHelper { @@ -75,7 +75,9 @@ export default class AxisRendererHelper { value => `translate(0,${value})`; return (selection, scale) => { - selection.attr("transform", d => fn(Math.ceil(scale(d)))); + selection.attr("transform", d => ( + isValue(d) ? fn(Math.ceil(scale(d))) : null + )); }; } diff --git a/test/internals/axis-spec.ts b/test/internals/axis-spec.ts index ae14a5e33..7f3ed86e5 100644 --- a/test/internals/axis-spec.ts +++ b/test/internals/axis-spec.ts @@ -3,6 +3,7 @@ * billboard.js project is licensed under the MIT license */ /* eslint-disable */ +// @ts-nocheck import {expect} from "chai"; import {select as d3Select} from "d3-selection"; import {format as d3Format} from "d3-format"; @@ -2854,6 +2855,43 @@ describe("AXIS", function() { }); }); + describe("Axis tick.values", () => { + before(() => { + args = { + data: { + x: "x", + columns: [ + ["x", "2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06"], + ["data1", 30, 200, 100, 400, 150, 250] + ] + }, + axis: { + x: { + type: "timeseries", + tick: { + format: "%Y-%m-%d", + fit: false, + values: [ + "2022-01-01", + "2022-01-03",, + "2022-01-05" + ] + } + } + } + }; + }); + + it("tick transform translate shoudn't contain NaN value.", () => { + chart.$.main.selectAll(".tick") + .each(function() { + const hasNaN = this.getAttribute("transform")?.indexOf("NaN") >= 0; + + expect(hasNaN).to.be.false; + }); + }); + }); + describe("axis min/max", () => { before(() => { args = {