Skip to content

Commit 0446045

Browse files
theiliadcal-smith
authored andcommittedDec 9, 2019
feat(core, angular, react, vue): allow customizations to pie labels & the donut center number (#427)
1 parent e115968 commit 0446045

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed
 

‎packages/core/src/components/graphs/donut.ts

+24-19
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,36 @@ import { Tools } from "../../tools";
77
import { select } from "d3-selection";
88
import { interpolateNumber } from "d3-interpolate";
99

10-
const donutCenterNumberTween = (d3Ref, newNumber: number) => {
11-
// Remove commas from the current value string, and convert to an int
12-
const currentValue = parseInt(d3Ref.text().replace(/[, ]+/g, ""), 10) || 0;
13-
const i = interpolateNumber(currentValue, newNumber);
14-
15-
const formatInterpolatedValue = number => Math.floor(number).toLocaleString();
16-
17-
return t => d3Ref.text(formatInterpolatedValue(i(t)));
18-
};
19-
2010
export class Donut extends Pie {
2111
type = "donut";
2212

2313
render(animate = true) {
2414
// Call render() from Pie
2515
super.render(animate);
2616

17+
const self = this;
18+
2719
const svg = DOMUtils.appendOrSelect(this.getContainerSVG(), "g.center");
2820
const options = this.model.getOptions();
2921

3022
// Compute the outer radius needed
3123
const radius = this.computeRadius();
3224

33-
let donutCenterFigure = Tools.getProperty(options, "center", "number");
34-
if (!donutCenterFigure) {
35-
donutCenterFigure = this.getDataList().reduce((accumulator, d) => {
36-
return accumulator + d.value;
37-
}, 0);
38-
}
39-
4025
// Add the number shown in the center of the donut
4126
DOMUtils.appendOrSelect(svg, "text.donut-figure")
4227
.attr("text-anchor", "middle")
4328
.style("font-size", () => options.donut.center.numberFontSize(radius))
4429
.transition(this.services.transitions.getTransition("donut-figure-enter-update", animate))
4530
.tween("text", function() {
46-
return donutCenterNumberTween(select(this), donutCenterFigure);
31+
return self.centerNumberTween(select(this));
4732
});
4833

4934
// Add the label below the number in the center of the donut
5035
DOMUtils.appendOrSelect(svg, "text.donut-title")
5136
.attr("text-anchor", "middle")
5237
.style("font-size", () => options.donut.center.titleFontSize(radius))
5338
.attr("y", options.donut.center.titleYPosition(radius))
54-
.text(options.donut.center.label);
39+
.text(Tools.getProperty(options, "donut", "center", "label"));
5540
}
5641

5742
getInnerRadius() {
@@ -60,4 +45,24 @@ export class Donut extends Pie {
6045

6146
return radius * (3 / 4);
6247
}
48+
49+
centerNumberTween(d3Ref) {
50+
const options = this.model.getOptions();
51+
52+
let donutCenterFigure = Tools.getProperty(options, "donut", "center", "number");
53+
if (!donutCenterFigure) {
54+
donutCenterFigure = this.getDataList().reduce((accumulator, d) => {
55+
return accumulator + d.value;
56+
}, 0);
57+
}
58+
59+
// Remove commas from the current value string, and convert to an int
60+
const currentValue = parseInt(d3Ref.text().replace(/[, ]+/g, ""), 10) || 0;
61+
const i = interpolateNumber(currentValue, donutCenterFigure);
62+
63+
return t => {
64+
const { numberFormatter } = options.donut.center;
65+
d3Ref.text(numberFormatter(i(t)));
66+
};
67+
}
6368
}

‎packages/core/src/components/graphs/pie.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ export class Pie extends Component {
124124
const calloutData = [];
125125
enteringLabels.merge(labels)
126126
.style("text-anchor", "middle")
127-
.text(d => Tools.convertValueToPercentage(d.data.value, dataList) + "%")
127+
.text(d => {
128+
if (options.pie.labels.formatter) {
129+
return options.pie.labels.formatter(d);
130+
}
131+
132+
return Tools.convertValueToPercentage(d.data.value, dataList) + "%";
133+
})
128134
// Calculate dimensions in order to transform
129135
.datum(function(d) {
130136
const textLength = this.getComputedTextLength();

‎packages/core/src/configuration.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ const pieChart: PieChartOptions = Tools.merge({}, chart, {
202202
offsetY: 12,
203203
horizontalLineLength: 8,
204204
textMargin: 2
205+
},
206+
labels: {
207+
formatter: null
205208
}
206209
}
207210
} as PieChartOptions);
@@ -214,7 +217,8 @@ const donutChart: DonutChartOptions = Tools.merge({}, pieChart, {
214217
center: {
215218
numberFontSize: radius => Math.min((radius / 100) * 24, 24) + "px",
216219
titleFontSize: radius => Math.min((radius / 100) * 15, 15) + "px",
217-
titleYPosition: radius => Math.min((radius / 80) * 20, 20)
220+
titleYPosition: radius => Math.min((radius / 80) * 20, 20),
221+
numberFormatter: number => Math.floor(number).toLocaleString()
218222
}
219223
}
220224
} as DonutChartOptions);

‎packages/core/src/interfaces/charts.ts

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ export interface PieChartOptions extends BaseChartOptions {
129129
offsetY?: number;
130130
horizontalLineLength?: number;
131131
textMargin?: number;
132+
},
133+
labels?: {
134+
formatter?: Function;
132135
}
133136
};
134137
}
@@ -144,6 +147,7 @@ export interface DonutChartOptions extends PieChartOptions {
144147
numberFontSize?: Function;
145148
titleFontSize?: Function;
146149
titleYPosition?: Function;
150+
numberFormatter?: Function;
147151
};
148152
};
149153
}

0 commit comments

Comments
 (0)
Please sign in to comment.