Skip to content

Commit

Permalink
feat(Arc): Implement multiline donut title
Browse files Browse the repository at this point in the history
Add line break functionality using '\n' key

Fix #71
Close #130
  • Loading branch information
netil authored Aug 30, 2017
1 parent 354d7e2 commit b816dc0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
10 changes: 5 additions & 5 deletions spec/arc-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* billboard.js project is licensed under the MIT license
*/
/* eslint-disable */
import CLASS from "../src/config/classes";
import util from "./assets/util";

describe("ARC", () => {
Expand Down Expand Up @@ -97,9 +98,8 @@ describe("ARC", () => {
expect(arcs.data3.attr("d").indexOf("NaN")).to.be.equal(-1);
});

it("should set padAngle value", () => {
it("check for donut's options", () => {
const value = 0.05;

const chart = util.generate({
data: {
columns: [
Expand All @@ -109,13 +109,13 @@ describe("ARC", () => {
type: "donut"
},
donut: {
title: "text1\ntext2\ntext3",
padAngle: value
}
});

const padAngle = chart.internal.pie.padAngle();

expect(padAngle()).to.be.equal(value);
expect(chart.internal.pie.padAngle()()).to.be.equal(value);
expect(chart.internal.main.selectAll(`text.${CLASS.chartArcsTitle} tspan`).size()).to.be.equal(3);
});
});

Expand Down
9 changes: 6 additions & 3 deletions src/config/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2167,7 +2167,7 @@ export default class Options {
* @property {Number|Function} [donut.label.ratio=undefined] Set ratio of labels position.
* @property {Boolean} [donut.expand=true] Enable or disable expanding donut pieces.
* @property {Number} [donut.width] Set width of donut chart.
* @property {String} [donut.title=""] Set title of donut chart.
* @property {String} [donut.title=""] Set title of donut chart. Use `\n` character to enter line break.
* @property {Number} [donut.padAngle=0] Set padding between data.
* @example
* donut: {
Expand All @@ -2188,8 +2188,11 @@ export default class Options {
* },
* expand: false,
* width: 10,
* title: "Donut Title",
* padAngle: 0.2
* padAngle: 0.2,
* title: "Donut Title"
*
* // title with line break
* title: "Title1\nTitle2"
* }
*/
donut_label_show: true,
Expand Down
44 changes: 40 additions & 4 deletions src/internals/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,46 @@ extend(ChartInternal.prototype, {
.attr("class", CLASS.chartArcs)
.attr("transform", $$.getTranslate("arc"));

$$.arcs.append("text")
.attr("class", CLASS.chartArcsTitle)
.style("text-anchor", "middle")
.text($$.getArcTitle());
$$.setArcTitle();
},

/**
* Set arc title text
* @private
*/
setArcTitle() {
const $$ = this;
const title = $$.getArcTitle();

if (title) {
const multiline = title.split("\n");
const text = $$.arcs.append("text")
.attr("class", CLASS.chartArcsTitle)
.style("text-anchor", "middle");

// if is multiline text
if (multiline.length > 1) {
const fontSize = +text.style("font-size").replace("px", "");
const height = Math.floor(
text.text(".").node()
.getBBox().height, text.text("")
);

multiline.forEach((v, i) =>
text.insert("tspan")
.text(v)
.attr("x", 0)
.attr("dy", i ? height : 0)
);

text.attr("y", `-${
fontSize * (multiline.length - 2) ||
fontSize / 2
}`);
} else {
text.text(title);
}
}
},

redrawArc(duration, durationForExit, withTransform) {
Expand Down

0 comments on commit b816dc0

Please sign in to comment.