Skip to content

Commit c98a9be

Browse files
committed
[FIX] pie chart: prevent overlapping shown values
The `show value` option for pie chart was displaying values no matter the size of the chart or the size of the pie slice of the value. This commit adds a check to prevent the value from being shown if the slice is too small, and that the value will be displayed outside of it. closes #6387 Task: 4639810 Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
1 parent 0f8b349 commit c98a9be

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/components/figures/chart/chartJs/chartjs_show_values_plugin.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,26 @@ function drawPieChartValues(
168168
const midAngle = (startAngle + endAngle) / 2;
169169
const midRadius = (innerRadius + outerRadius) / 2;
170170
const x = bar.x + midRadius * Math.cos(midAngle);
171-
const y = bar.y + midRadius * Math.sin(midAngle) + 7;
171+
const y = bar.y + midRadius * Math.sin(midAngle);
172+
const displayValue = options.callback(value, dataset, i);
173+
174+
const textHeight = 12; // ChartJS default
175+
const textWidth = computeTextWidth(ctx, displayValue, { fontSize: textHeight }, "px");
176+
177+
const radius = outerRadius - innerRadius;
178+
// Check if the text fits in the slice. Not perfect, but good enough heuristic.
179+
if (textWidth >= radius || radius < textHeight) {
180+
continue;
181+
}
182+
const sliceAngle = endAngle - startAngle;
183+
const midWidth = 2 * midRadius * Math.tan(sliceAngle / 2);
184+
if (sliceAngle < Math.PI / 2 && (textWidth >= midWidth || midWidth < textHeight)) {
185+
continue;
186+
}
172187

173188
ctx.fillStyle = chartFontColor(options.background);
174189
ctx.strokeStyle = options.background || "#ffffff";
175190

176-
const displayValue = options.callback(value, dataset, i);
177191
drawTextWithBackground(displayValue, x, y, ctx);
178192
}
179193
}

0 commit comments

Comments
 (0)