Skip to content

Commit

Permalink
fix(axis): Fix translate NaN error
Browse files Browse the repository at this point in the history
Check the given x tick value to get correct scale.

Fix #2924
  • Loading branch information
netil authored and netil committed Oct 28, 2022
1 parent b22b63d commit b8396a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ChartInternal/Axis/AxisRendererHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
));
};
}

Expand Down
38 changes: 38 additions & 0 deletions test/internals/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit b8396a0

Please sign in to comment.