Skip to content

Commit

Permalink
fix(text): Correct text vertical align
Browse files Browse the repository at this point in the history
Fix multiline vertical position.

Fix #982
  • Loading branch information
netil committed Jul 18, 2019
1 parent 26c1fa5 commit 6debb55
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
46 changes: 46 additions & 0 deletions spec/shape/shape.arc-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,52 @@ describe("SHAPE ARC", () => {
});
});

describe("check for multiline text position", () => {
let chart;
let args = {
data: {
columns: [
["data1", 50],
["data2", 50]
],
type: "donut"
},
donut: {
title: "Title 1\nTitle 2"
}
};

beforeEach(() => {
chart = util.generate(args);
});

const checkAtMiddle = done => {
const arc = chart.$.arc.node().getBoundingClientRect();
const title = chart.$.arc.select("text").node().getBoundingClientRect();

const titlePos = title.top - arc.top + (title.height / 2);

expect(titlePos).to.be.closeTo(arc.height / 2, 2);
done && done();
};

it("check for two lined text position", done => {
setTimeout(() => {
checkAtMiddle(done);
}, 100);
});

it("set option args.donut.title", () => {
args.donut.title = "Title 1\nTitle 2\nTitle 3";
});

it("check for three lined text position", done => {
setTimeout(() => {
checkAtMiddle(done);
}, 100);
});
});

describe("check for data loading", () => {
it("Interaction of chart when initialized with 0 and .load()", done => {
const chart = util.generate({
Expand Down
6 changes: 4 additions & 2 deletions src/internals/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ const sanitise = str => (isString(str) ? str.replace(/</g, "&lt;").replace(/>/g,
* @param {d3Selection} node Text node
* @param {String} text Text value string
* @param {Array} dy dy value for multilined text
* @param {Boolean} toMiddle To be alingned vertically middle
* @private
*/
const setTextValue = (node, text, dy = [-1, 1]) => {
const setTextValue = (node, text, dy = [-1, 1], toMiddle = false) => {
if (!node || !isString(text)) {
return;
}
Expand All @@ -96,14 +97,15 @@ const setTextValue = (node, text, dy = [-1, 1]) => {

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

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

multiline.forEach((v, i) => {
node.append("tspan")
.attr("x", 0)
.attr("dy", `${i === 0 ? dy[0] : dy[1]}em`)
.attr("dy", `${i === 0 ? dy[0] * len : dy[1]}em`)
.text(v);
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/shape/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,17 @@ extend(ChartInternal.prototype, {
const value = updated ? updated.value : null;
const ratio = $$.getRatio("arc", updated);
const id = d.data.id;
const hasGauge = $$.hasType("gauge");
const isUnderThreshold = !(
!$$.hasType("gauge") && !$$.meetsArcLabelThreshold(ratio)
!hasGauge && !$$.meetsArcLabelThreshold(ratio)
);

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

setTextValue(node, text);
setTextValue(node, text, [-1, 1], hasGauge);
}
});
}
Expand Down Expand Up @@ -420,7 +421,7 @@ extend(ChartInternal.prototype, {
.style("font-size", "27px");
}

setTextValue(text, title, hasGauge ? undefined : [-1.3, 1.3]);
setTextValue(text, title, hasGauge ? undefined : [-0.6, 1.35], true);
}
},

Expand Down
2 changes: 1 addition & 1 deletion src/shape/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ extend(ChartInternal.prototype, {
.attr("dy", ".5em")
.call(selection => {
selection.each(function(d) {
setTextValue(d3Select(this), d, [-1.2, 1.2]);
setTextValue(d3Select(this), d, [-1.2, 1.2], true);
});
})
.datum((d, i) => ({index: i}))
Expand Down

0 comments on commit 6debb55

Please sign in to comment.