Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ChartInternal/internals/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ export default {
node.attr("transform", `translate(${pos.x} ${pos.y}) ${rotateString}`);
} else {
node.attr("x", pos.x).attr("y", pos.y);

// the position is on the text itself: drop the transform that the parent
// got when the label was multiline, or both would be applied
hasTreemap && (this.parentNode as SVGGElement)?.removeAttribute("transform");
}

config.data_labels.border &&
Expand Down
58 changes: 58 additions & 0 deletions test/shape/treemap-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,64 @@ describe("TREEMAP", () => {
done(1);
}, 100);
}));

it("should not keep the position of the previous line count on redraw", () => new Promise(done => {
let multiLine = true;
const columns = [["A", 660], ["B", 377], ["C", 368], ["D", 240], ["E", 105]];
const options = () => ({
data: {
columns,
type: "treemap",
labels: {
centered: true
}
},
transition: {
duration: 0
},
treemap: {
tile: "binary",
label: {
format: (value, ratio, id) => multiLine ? `${id}\n${value}` : id
}
}
});
// where the labels are really drawn, relative to the chart
const getRects = target => {
const {left, top} = target.$.svg.node().getBoundingClientRect();

return [...target.$.svg.node().querySelectorAll("text.bb-text")].map(t => {
const r = t.getBoundingClientRect();

return [Math.round(r.left - left), Math.round(r.top - top)];
});
};
const treemap = util.generate(options());

// two lines -> one line
multiLine = false;
treemap.load({columns});

setTimeout(() => {
const fresh = util.generate({...options(), bindto: "#chart2"});

expect(getRects(treemap)).to.be.deep.equal(getRects(fresh));

// one line -> two lines
multiLine = true;
treemap.load({columns});
fresh.load({columns});

setTimeout(() => {
expect(getRects(treemap)).to.be.deep.equal(getRects(fresh));

treemap.destroy();
fresh.destroy();
document.getElementById("chart2")?.remove();
done(1);
}, 100);
}, 100);
}));
});

describe("with centered labels disabled", () => {
Expand Down
Loading