Skip to content

Commit c779724

Browse files
committed
Support text formating in pie slices
1 parent 1a6de7b commit c779724

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

modules/draw/TPiePainter.mjs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeTranslate, DrawOptions } from '../base/BasePainter.mjs';
1+
import { makeTranslate, DrawOptions, floatToString } from '../base/BasePainter.mjs';
22
import { ObjectPainter } from '../base/ObjectPainter.mjs';
33
import { ensureTCanvas } from '../gpad/TCanvasPainter.mjs';
44
import { addMoveHandler } from '../gui/utils.mjs';
@@ -14,13 +14,17 @@ class TPiePainter extends ObjectPainter {
1414
#is3d; // if 3d mode enabled
1515
#lblor; // how to draw labels
1616
#sort; // sorting order of pies
17+
#samecolor; // use same color for labels
1718

1819
/** @summary Decode options */
1920
decodeOptions(opt) {
2021
const d = new DrawOptions(opt);
2122
this.#is3d = d.check('3D');
2223
this.#lblor = 0;
2324
this.#sort = 0;
25+
this.#samecolor = false;
26+
if (d.check('SC'))
27+
this.#samecolor = true; // around
2428
if (d.check('T'))
2529
this.#lblor = 2; // around
2630
if (d.check('R'))
@@ -76,8 +80,9 @@ class TPiePainter extends ObjectPainter {
7680
pixelHeight = this.axisToSvg('y', pie.fY - pie.fHeight) - yc;
7781
}
7882

79-
const textatt = this.createAttText({ attr: pie }),
80-
rx = this.axisToSvg('x', pie.fX + radX) - xc,
83+
this.createAttText({ attr: pie });
84+
85+
const rx = this.axisToSvg('x', pie.fX + radX) - xc,
8186
ry = this.axisToSvg('y', pie.fY - radY) - yc,
8287
dist_to_15pi = a => {
8388
while (a < 0.5*Math.PI)
@@ -212,15 +217,38 @@ class TPiePainter extends ObjectPainter {
212217
.call(this.fillatt.func);
213218
}
214219

220+
const frac = total ? slice.fValue / total : 0;
221+
let tmptxt = pie.fLabelFormat;
222+
tmptxt = tmptxt.replaceAll("%txt", slice.fTitle);
223+
tmptxt = tmptxt.replaceAll("%val", floatToString(slice.fValue, pie.fValueFormat));
224+
tmptxt = tmptxt.replaceAll("%frac", floatToString(frac, pie.fFractionFormat));
225+
tmptxt = tmptxt.replaceAll("%perc", floatToString(frac*100, pie.fPercentFormat) + '%');
226+
227+
215228
const arg = { draw_g: g,
216-
x: rx * 1.05 * Math.cos(mid_angle),
217-
y: ry * 1.05 * Math.sin(mid_angle),
229+
x: rx * (1 + pie.fLabelsOffset) * Math.cos(mid_angle),
230+
y: ry * (1 + pie.fLabelsOffset) * Math.sin(mid_angle),
218231
latex: 1,
219232
align: 22,
220-
text: slice.fTitle,
221-
color: this.textatt.color };
233+
text: tmptxt };
222234

223-
if ((arg.x >= 0) && (arg.y >= 0)) {
235+
if (this.#samecolor)
236+
arg.color = this.getColor(slice.fFillColor);
237+
238+
if (this.#lblor === 1) {
239+
// radial positioning of the labels
240+
arg.rotate = Math.atan2(arg.y, arg.x) / Math.PI * 180;
241+
if (arg.x > 0) {
242+
arg.align = 12;
243+
} else {
244+
arg.align = 32;
245+
arg.rotate += 180;
246+
}
247+
} else if (this.#lblor === 2) {
248+
// in the slice
249+
arg.align = 23;
250+
arg.rotate = Math.atan2(arg.y, arg.x) / Math.PI * 180 + 90;
251+
} else if ((arg.x >= 0) && (arg.y >= 0)) {
224252
arg.align = 13;
225253
if (this.#is3d)
226254
arg.y += pixelHeight;

0 commit comments

Comments
 (0)