Better label positioning on pie chart

This commit is contained in:
2022-07-09 19:19:45 +02:00
parent 77043f731f
commit 618a2299dd
+2 -7
View File
@@ -66,16 +66,11 @@ export const PieChart = ({ parts, labels, total, totalLabel }) => {
g.lineTo(0, 0)
g.stroke()
const { width: w } = g.measureText(label)
const textRadius = Math.sqrt(w ** 2 + 16 ** 2) / 2
g.fillStyle = `hsl(${((hashCode(`${i} ${seed}`) % 0xff) * 360) / 0xff}, 80%, 15%)`
g.fillText(
label,
Math.cos(acc + angle / 2 - 0.5 * Math.PI) *
(width * 0.5 * 0.85 + textRadius * 0.8),
Math.sin(acc + angle / 2 - 0.5 * Math.PI) *
(width * 0.5 * 0.85 + textRadius * 0.8)
Math.cos(acc + angle / 2 - 0.5 * Math.PI) * (width * 0.5 * 0.9125),
Math.sin(acc + angle / 2 - 0.5 * Math.PI) * (width * 0.5 * 0.9125)
)
acc += angle