Skip to content

Commit

Permalink
fix(axis): fix undefined error reading generatedTicks
Browse files Browse the repository at this point in the history
fix getting axis.generatedTicks property when isn't available

Ref naver#2786
  • Loading branch information
netil committed Jul 19, 2022
1 parent 4176cf1 commit daec175
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ChartInternal/Axis/AxisRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export default class AxisRenderer {
* @private
*/
getGeneratedTicks(count: number): (Date|number)[] {
const len = this.generatedTicks.length - 1;
const len = this.generatedTicks?.length - 1;
let res = this.generatedTicks;

if (len > count) {
Expand Down
1 change: 0 additions & 1 deletion src/ChartInternal/internals/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export default {
const {axis, config, scale, state, $el: {grid, main}} = $$;
const isRotated = config.axis_rotated;
const pos = d => Math.ceil(scale.y(d));

const gridValues =
axis.y.getGeneratedTicks(config.grid_y_ticks) || $$.scale.y.ticks(config.grid_y_ticks);

Expand Down
62 changes: 58 additions & 4 deletions test/internals/grid-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/* eslint-disable */
import {expect} from "chai";
import {select as d3Select} from "d3-selection";
import { utils } from "mocha";
import {$AXIS, $COMMON, $EVENT, $FOCUS, $GRID} from "../../src/config/classes";
import util from "../assets/util";

Expand Down Expand Up @@ -155,7 +156,7 @@ describe("GRID", function() {

it("should show grids depending on y axis ticks", () => {
const ygrids = chart.$.main.select(`.${$GRID.ygrids}`);
const expectedYs = [];
const expectedYs: number[] = [];

ygrids.selectAll(`.${$GRID.ygrid}`).each(function(d, i) {
expectedYs[i] = +d3Select(this).attr("y1");
Expand All @@ -181,9 +182,9 @@ describe("GRID", function() {
args = {
data: {
columns: [
["data1", 100, 50, 150, 200, 100, 350, 58, 210, 80, 126],
["data2", 305, 350, 55, 25, 335, 29, 258, 310, 180, 226],
["data3", 223, 121, 259, 247, 53, 159, 95, 111, 307, 337]
["data1", 100, 50, 150, 200, 100, 350, 58, 210, 80, 126],
["data2", 305, 350, 55, 25, 335, 29, 258, 310, 180, 226],
["data3", 223, 121, 259, 247, 53, 159, 95, 111, 307, 337]
],
type: "line",
labels: true
Expand All @@ -208,6 +209,59 @@ describe("GRID", function() {

expect(gridLen).to.be.equal(tickLen);
});

it("shouldn't be throw error", () => {
try {
util.generate({
"data": {
"json": [
{ "data_periodo": "2018-05-01", "int_exists": 0 },
{ "data_periodo": "2018-06-01", "int_exists": 0 }
],
"keys": {
"x": "data_periodo",
"value": [
"int_exists"
]
},
"type": "line"
},
"axis": {
"y": {
"tick": {
"format": null,
"count": 1
},
"show": false,
"max": 1,
"min": 0.1
},
},
"grid": {
"y": {
"show": true,
"lines": [
{
"value": 0,
"position": "start",
"text": "assenza"
},
{
"value": 1,
"position": "start",
"text": "presenza"
}
]
}
}
});
} catch (e) {
// it shouldn't be thrown
expect(false).to.be.true;
}

expect(true).to.be.ok;
});
});

describe("front option", () => {
Expand Down

0 comments on commit daec175

Please sign in to comment.