A centroid is the geometric center, or center of mass, of a shape or region. For an arc, D3 calculates it as a two-value array containing horizontal and vertical coordinates relative to the chart origin. section 4.4.3 section 4.5
Use an arc centroid to place labels accurately:
- Configure the arc generator with its start and end angles. section 4.4.3
- Call centroid() to obtain the label position. section 4.5
- Assign the returned coordinates to an SVG text element and center its text with text-anchor and dominant-baseline. section 4.4.3
const centroid = arcGenerator .startAngle(0) .endAngle(angleDaysWithPrecipitation_rad) .centroid();
This listing shows the compact D3 method chain for calculating an arc centroid from its angular bounds. section 4.4.3
Labeling a donut arc
For an arc representing 35% of days with precipitation, calculate its centroid and use the two returned values as the x and y coordinates of a white percentage label. Apply the same pattern to every donut-chart arc. section 4.4.3 section 5.1.4






