Skip to content

Commit

Permalink
fix(pie): Correct multiline label text
Browse files Browse the repository at this point in the history
- Add condition to check if text has been updated
- For multilined text, reset html before update

Fix #784
  • Loading branch information
netil committed Mar 15, 2019
1 parent a65028b commit 9bacb9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
5 changes: 5 additions & 0 deletions spec/shape/shape.arc-spec.js
Expand Up @@ -441,6 +441,11 @@ describe("SHAPE ARC", () => {

it("should be multilined in pie", () => {
checkMultiline(chart.$.arc);

chart.toggle("data1");
chart.toggle("data1");

checkMultiline(chart.$.arc);
});

it("set options data.type='donut'", () => {
Expand Down
28 changes: 18 additions & 10 deletions src/shape/arc.js
Expand Up @@ -204,22 +204,30 @@ extend(ChartInternal.prototype, {
);

if (isUnderThreshold) {
const nodeText = node.text();
const text = (
$$.getArcLabelFormat() || $$.defaultArcValueFormat
)(value, ratio, id).toString();

if (text.indexOf("\n") === -1) {
node.text(text);
nodeText !== text && node.text(text);
} else {
const multiline = text.split("\n");
const len = multiline.length - 1;

multiline.forEach((v, i) => {
node.append("tspan")
.attr("x", 0)
.attr("dy", `${i === 0 ? -len : 1}em`)
.text(v);
});
const diff = [nodeText, text].map(v => v.replace(/[\s\n]/g, ""));

if (diff[0] !== diff[1]) {
const multiline = text.split("\n");
const len = multiline.length - 1;

// reset possible text
node.html("");

multiline.forEach((v, i) => {
node.append("tspan")
.attr("x", 0)
.attr("dy", `${i === 0 ? -len : 1}em`)
.text(v);
});
}
}
}
});
Expand Down

0 comments on commit 9bacb9b

Please sign in to comment.