Skip to content

Commit

Permalink
fix(axis): Fix axis label overlap when has no data
Browse files Browse the repository at this point in the history
Make calculate tick width size when has no data.

Fix #2974
  • Loading branch information
netil authored Nov 28, 2022
1 parent 54ee419 commit 643357b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ChartInternal/Axis/Axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ class Axis {
const currentTickMax = current.maxTickWidths[id];
let maxWidth = 0;

if (withoutRecompute || !config[`axis_${id}_show`] || $$.filterTargetsToShow().length === 0) {
if (withoutRecompute || !config[`axis_${id}_show`] || (currentTickMax.size > 0 && $$.filterTargetsToShow().length === 0)) {
return currentTickMax.size;
}

Expand Down
36 changes: 34 additions & 2 deletions test/internals/axis-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2582,7 +2582,8 @@ describe("AXIS", function() {
},
axis: {
y: {
label: "Your Y Axis"
label: "Your Y Axis",
position: ""
}
}
};
Expand All @@ -2591,8 +2592,39 @@ describe("AXIS", function() {
it("<clipPath> width shouldn't truncate y axis tick when has no data", () => {
const rect = chart.internal.$el.defs.selectAll("clipPath[id$='yaxis'] rect");

expect(+rect.attr("width")).to.be.equal(50);
expect(+rect.attr("width")).to.be.equal(60);
});

it("set options: axis.y.label", () => {
args.axis.y.label = {
text: "Your Y Axis",
position: "outer-middle"
};

args.axis.y2 = {
show: true,
label: args.axis.y.label
}
});

it("label text shouldn't be overlapped with tick text", () => {
const y = chart.$.main.select(`.${$AXIS.axisY}`);
const y2 = chart.$.main.select(`.${$AXIS.axisY2}`);

[y, y2].forEach((v, i) => {
const labelRect = v.select("text").node().getBoundingClientRect();
const tickRect = v.select(".tick:nth-child(7)").node().getBoundingClientRect();

// y axis
if (i === 0) {
expect(labelRect.x + labelRect.width < tickRect.x).to.be.true;

// y2 axis
} else {
expect(labelRect.x > tickRect.x + tickRect.width).to.be.true
}
});
})
});

describe("Axes tick culling", () => {
Expand Down

0 comments on commit 643357b

Please sign in to comment.