-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieChart.yaml.txt
More file actions
1344 lines (1303 loc) · 55.6 KB
/
Copy pathPieChart.yaml.txt
File metadata and controls
1344 lines (1303 loc) · 55.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
ComponentDefinitions:
cmpPieChart:
DefinitionType: CanvasComponent
CustomProperties:
AccessibleLabel:
PropertyKind: Input
DisplayName: Accessible Label
Description: Screen reader label for the chart
RaiseOnReset: true
DataType: Text
Default: ="Pie chart"
Caption:
PropertyKind: Input
DisplayName: Caption
Description: Optional text displayed below the chart; leave blank to hide
RaiseOnReset: true
DataType: Text
Default: ="Description of what Pie Chart represents"
CaptionColor:
PropertyKind: Input
DisplayName: Caption Color
Description: CSS color for the caption text
RaiseOnReset: true
DataType: Text
Default: ="black"
CenterLabelColor:
PropertyKind: Input
DisplayName: Center Label Color
Description: color (CSS) of center label
RaiseOnReset: true
DataType: Text
Default: ="black"
CenterLabelText:
PropertyKind: Input
DisplayName: Center Label Text
Description: Overrides the center label; blank shows total value
RaiseOnReset: true
DataType: Text
Default: ="Center Label"
ChartBorderColor:
PropertyKind: Input
DisplayName: Chart Border Color
Description: Border color used when Chart Shadow is DropShadow.None
RaiseOnReset: true
DataType: Text
Default: ="#E0E0E0"
ChartDimension:
PropertyKind: Output
DisplayName: Chart Dimension
Description: width and height of Pie
DataType: Number
ChartFill:
PropertyKind: Input
DisplayName: Chart Fill
Description: Background fill color used for the chart container and donut hole
RaiseOnReset: true
DataType: Text
Default: ="#FAFAFA"
ChartShadow:
PropertyKind: Input
DisplayName: Chart Shadow
Description: Drop shadow style for the chart container (DropShadow enum)
RaiseOnReset: true
DataType: Text
Default: =DropShadow.Semibold
ChartTitle:
PropertyKind: Input
DisplayName: Chart Title
Description: Text displayed as the chart heading
RaiseOnReset: true
DataType: Text
Default: ="Pie Chart Title"
ChartX:
PropertyKind: Output
DisplayName: Chart X
Description: upper left corner X of pie (excluding title and caption)
DataType: Number
ChartY:
PropertyKind: Output
DisplayName: Chart Y
Description: upper left corner Y of pie (excluding title and caption)
DataType: Number
DonutHolePercent:
PropertyKind: Input
DisplayName: Donut Hole Percent
Description: 0-100 inner radius percentage for donut chart mode; 0 disables donut hole
RaiseOnReset: true
DataType: Number
Default: =If(IsBlank(Self.Caption), 0, 39)
EmptyStateText:
PropertyKind: Input
DisplayName: Empty State Text
Description: Message shown when Items is empty or total is zero
RaiseOnReset: true
DataType: Text
Default: ="No data"
EndRotation:
PropertyKind: Input
DisplayName: End Rotation
Description: Degrees at which the last slice ends; 360 = full circle, use 180 for gauge
RaiseOnReset: true
DataType: Number
Default: =360
ExplodeDistance:
PropertyKind: Input
DisplayName: Explode Distance
Description: How far exploded slices are pulled from center in viewBox units (0-100 space)
RaiseOnReset: true
DataType: Number
Default: =5
HasSelection:
PropertyKind: Output
DisplayName: Has Selection
Description: True when a slice is currently selected
DataType: Boolean
IsItemsEmpty:
PropertyKind: Output
DisplayName: Is Items Empty
Description: True when Items is empty or total value is zero
DataType: Boolean
IsPrinting:
PropertyKind: Input
DisplayName: Is Printing
Description: Set to Screen.Printing to hide non-printable portions
RaiseOnReset: true
DataType: Boolean
Default: =false
Items:
PropertyKind: Input
DisplayName: Items
Description: >-
Data table for slices.
Required fields: Key (Text) and Value (Number).
Alternate field names supported: X for Key, Y for Value.
Optional fields: Order (Number), Label (Text), Color (Text, CSS hex), Explode (Boolean).
Define Items using a superset schema (include both X/Y and Key/Value fields) so formulas using Coalesce or conditional field selection can safely reference either schema without non-existent field errors. Missing optional values are defaulted during reset.
RaiseOnReset: true
DataType: Table
Default: |-
=Table(
{ Order: 1, X: "A", Y: 15, Key: "A", Value: 15, Label: "Alpha", Color: "", Explode: false },
{ Order: 2, X: "B", Y: 25, Key: "B", Value: 25, Label: "Beta", Color: "", Explode: true },
{ Order: 3, X: "C", Y: 60, Key: "C", Value: 60, Label: "Gamma", Color: "", Explode: false }
)
LegendData:
PropertyKind: Output
DisplayName: Legend Data
Description: >-
Table of legend records for use by an external legend component.
Fields: Order (Number), Key (Text), Color (Color), Value (Number), Percent (Number), IsSelected (Boolean).
DataType: Table
LegendSortMode:
PropertyKind: Input
DisplayName: Legend Sort Mode
Description: >-
Controls legend output order.
Allowed values: MatchChart, Alphabetical, ByValue
RaiseOnReset: true
DataType: Text
Default: ="MatchChart"
MinLabelAngle:
PropertyKind: Input
DisplayName: Min Label Angle
Description: Minimum slice angle in degrees required before a wedge label is rendered
RaiseOnReset: true
DataType: Number
Default: =10
OnSliceSelect:
PropertyKind: Event
DisplayName: On Slice Select
Description: Fires when a slice is selected via the button grid
ReturnType: None
Default: =
PaddingBottom:
PropertyKind: Input
DisplayName: Padding Bottom
Description: Space between bottom component edge and content
RaiseOnReset: true
DataType: Number
Default: =6
PaddingLeft:
PropertyKind: Input
DisplayName: Padding Left
Description: Space between left component edge and content
RaiseOnReset: true
DataType: Number
Default: =6
PaddingRight:
PropertyKind: Input
DisplayName: Padding Right
Description: Space between right component edge and content
RaiseOnReset: true
DataType: Number
Default: =6
PaddingTop:
PropertyKind: Input
DisplayName: Padding Top
Description: Space between top component edge and content
RaiseOnReset: true
DataType: Number
Default: =6
Palette:
PropertyKind: Input
DisplayName: Palette
Description: Table of color strings cycled when a slice has no Color value
RaiseOnReset: true
DataType: Table
Default: |-
=Table(
{ Value: "#2563EB" },
{ Value: "#10B981" },
{ Value: "#F59E0B" },
{ Value: "#EF4444" },
{ Value: "#8B5CF6" },
{ Value: "#EC4899" }
)
PercentFormat:
PropertyKind: Input
DisplayName: Percent Format
Description: Power Fx numeric format string used when rendering percentages in wedge labels, tooltips, and legend values
RaiseOnReset: true
DataType: Text
Default: ="[$-en-US]0.0"
SelectedKey:
PropertyKind: Input
DisplayName: Selected Key
Description: The Key value of the currently selected slice (in)
RaiseOnReset: true
DataType: Text
Default: =""
SelectedSlice:
PropertyKind: Output
DisplayName: Selected Slice
Description: Full record of the currently selected slice from Items
DataType: Record
ShowCenterLabel:
PropertyKind: Input
DisplayName: Show Center Label
Description: Controls whether the center value label is visible
RaiseOnReset: true
DataType: Boolean
Default: =true
ShowWedgeLabel:
PropertyKind: Input
DisplayName: Show Wedge Label
Description: Display labels inside wedges when they are large enough
RaiseOnReset: true
DataType: Boolean
Default: =true
SliceCount:
PropertyKind: Output
DisplayName: Slice Count
Description: Number of slices in Items
DataType: Number
SortByValue:
PropertyKind: Input
DisplayName: Sort By Value
Description: When true, default ordering follows Value (descending); otherwise source sequence is used
RaiseOnReset: true
DataType: Boolean
Default: =false
StartRotation:
PropertyKind: Input
DisplayName: Start Rotation
Description: Clockwise degrees from 12 o'clock where the first slice begins. 0 = top (12 o'clock), 90 = right (3 o'clock).
RaiseOnReset: true
DataType: Number
Default: =15
StrokeColor:
PropertyKind: Input
DisplayName: Stroke Color
Description: CSS color string for the border between slices
RaiseOnReset: true
DataType: Text
Default: ="white"
StrokeWidth:
PropertyKind: Input
DisplayName: Stroke Width
Description: Width of the border between slices in viewBox units
RaiseOnReset: true
DataType: Number
Default: =1
SvgMarkup:
PropertyKind: Output
DisplayName: SVG Markup
Description: The raw SVG string used to render the chart
DataType: Text
TotalValue:
PropertyKind: Output
DisplayName: Total Value
Description: Sum of all slice Values in Items
DataType: Number
WedgeLabelColor:
PropertyKind: Input
DisplayName: Wedge Label Color
Description: color (CSS) of wedge labels
RaiseOnReset: true
DataType: Text
Default: ="black" //"white"
WedgeLabelsOutside:
PropertyKind: Input
DisplayName: Wedge Labels Outside
Description: should labels be inside (false) or outside pie (true)
RaiseOnReset: true
DataType: Boolean
Default: =true
Properties:
ChartDimension: =cmpPieChart_imgChart.Width
ChartX: =cmpPieChart_imgChart.X
ChartY: =cmpPieChart_imgChart.Y
HasSelection: =varHoveredKey <> ""
Height: =400
IsItemsEmpty: |-
=IsEmpty(varItemsSorted) Or
Sum(varItemsSorted, Coalesce(Value, 0)) = 0
LegendData: |-
=With(
{
_legendBase:
AddColumns(
varSlices,
Percent,
If(
WedgeTotal > 0,
Value / WedgeTotal,
0
),
PercentText,
Text(
If(
WedgeTotal > 0,
Value / WedgeTotal,
0
),
Coalesce(Self.PercentFormat, "[$-en-US]0.0%")
),
ValueText,
Text(Value) & " (" &
Text(
If(
WedgeTotal > 0,
Value / WedgeTotal,
0
),
Coalesce(Self.PercentFormat, "[$-en-US]0.0%")
) & ")",
IsSelected,
Key = varHoveredKey
)
},
Switch(
Coalesce(Self.LegendSortMode, "MatchChart"),
"Alphabetical",
SortByColumns(_legendBase, "Key", SortOrder.Ascending, "Order", SortOrder.Ascending),
"ByValue",
SortByColumns(_legendBase, "Value", SortOrder.Descending, "Order", SortOrder.Ascending),
SortByColumns(_legendBase, "Order", SortOrder.Ascending)
)
)
OnReset: |-
=// only show reset button for dev work
Set(debugResetVisible, true);
// ====
// SECTION 0: SELECTION STATE
// ====
If(
Coalesce(Self.SelectedKey, "") <> "",
Set(varHoveredKey, Self.SelectedKey),
Set(varHoveredKey, Coalesce(varHoveredKey, ""))
);
// ====
// SECTION 1: PADDING AND TEXT HEIGHTS
// ====
Set(varPadT, Coalesce(Self.PaddingTop, 6));
Set(varPadB, Coalesce(Self.PaddingBottom, 6));
Set(varPadL, Coalesce(Self.PaddingLeft, 6));
Set(varPadR, Coalesce(Self.PaddingRight, 6));
Set(varTitleH, If(IsBlank(Self.ChartTitle), 0, 24));
Set(varCaptionH, If(IsBlank(Self.Caption), 0, 30));
// ====
// SECTION 2: CHART GEOMETRY
// ====
With(
{
_usableW: Self.Width - varPadL - varPadR,
_usableH: Self.Height - varPadT - varPadB - varTitleH - varCaptionH
},
Set(ChartSize, Min(_usableW, _usableH));
Set(ChartCenterX, varPadL + ChartSize / 2);
Set(ChartCenterY, varPadT + varTitleH + ChartSize / 2)
);
Set(PieRadius, ChartSize / 2);
// ====
// SECTION 3: GRID PARAMETERS FOR BUTTON HIT LAYER
// ====
Set(gridN, Min(15, Max(10, RoundDown(ChartSize / 20, 0))));
Set(gridM, gridN);
Set(sampleSize, 7);
Set(threshold, 0.4);
// ====
// SECTION 4: INPUT NORMALIZATION (Key/Value) + WEDGE MATH SCALARS
// ====
Set(
varItemsNormalized,
ForAll(
Sequence(CountRows(Self.Items)) As row,
With(
{ _item: Index(Self.Items, row.Value) },
{
RowNumber: row.Value,
Order: Coalesce(_item.Order, row.Value),
Key: Coalesce(Text(_item.Key), Text(_item.X), Text(row.Value)),
Value: Coalesce(_item.Value, Coalesce(_item.Y, 0)),
Label: Coalesce(Text(_item.Label), Text(_item.Key), Text(_item.X), ""),
Color: Coalesce(_item.Color, ""),
Explode: Coalesce(_item.Explode, false)
}
)
)
);
Set(
varItemsSorted,
If(
Coalesce(Self.SortByValue, false),
SortByColumns(varItemsNormalized, "Value", SortOrder.Descending, "Order", SortOrder.Ascending),
SortByColumns(varItemsNormalized, "Order", SortOrder.Ascending)
)
);
Set(WedgeTotal, Sum(varItemsSorted, Coalesce(Value, 0)));
If(WedgeTotal = 0, Set(WedgeTotal, 1));
Set(WedgeStartRad, Coalesce(Self.StartRotation, 0) * Pi() / 180);
Set(ArcScale, Coalesce(Self.EndRotation, 360) / 360);
// ====
// SECTION 5: SVG RADII + OUTSIDE LABEL MODE + DONUT SETTINGS
// ====
Set(varIsOutside, Coalesce(Self.WedgeLabelsOutside, false));
Set(varSvgR,
If(varIsOutside,
Max(10, 30 - Coalesce(Self.ExplodeDistance, 5)),
Max(10, 48 - Coalesce(Self.ExplodeDistance, 5))
)
);
Set(varSvgRTxt,
If(varIsOutside,
Max(10, 18 - Coalesce(Self.ExplodeDistance, 5)),
Max(10, 30 - Coalesce(Self.ExplodeDistance, 5))
)
);
Set(varLabelR,
If(varIsOutside, varSvgR + 2, varSvgR * 0.65)
);
Set(varLabelFS, If(varIsOutside, "4", "5"));
Set(varDonutPct, Max(0, Min(100, Coalesce(Self.DonutHolePercent, 0))));
Set(varHoleR, varSvgR * varDonutPct / 100);
Set(varHoleStartAngle, WedgeStartRad);
Set(varHoleEndAngle, WedgeStartRad + 2 * Pi() * ArcScale);
// ====
// SECTION 6: BUILD varSlices
// ====
Set(palCount, CountRows(Self.Palette));
Set(
varSlices,
ForAll(
varItemsSorted As SortedRec,
With(
{
_cumSum:
Coalesce(
Sum(
Filter(varItemsSorted As SliceRecord, SliceRecord.Order < SortedRec.Order),
Coalesce(ThisRecord.Value, 0)
),
0
)
},
With(
{
_value: SortedRec.Value,
_valText: Text(SortedRec.Value),
_explode: SortedRec.Explode,
_explodeDist: Coalesce(Self.ExplodeDistance, 5),
_startAngle: WedgeStartRad
+ (_cumSum / If(WedgeTotal = 0, 1, WedgeTotal))
* 2 * Pi() * ArcScale,
_color:
Coalesce(
If(SortedRec.Color <> "", SortedRec.Color, Blank()),
Index(
Self.Palette,
Mod(SortedRec.Order - 1, If(palCount = 0, 1, palCount)) + 1
).Value,
"#D1D5DB"
)
},
With(
{
_sliceArc: (_value / If(WedgeTotal = 0, 1, WedgeTotal))
* 2 * Pi() * ArcScale,
_endAngle: WedgeStartRad
+ (_cumSum / If(WedgeTotal = 0, 1, WedgeTotal))
* 2 * Pi() * ArcScale
+ (_value / If(WedgeTotal = 0, 1, WedgeTotal))
* 2 * Pi() * ArcScale
},
With(
{
_midAngle: WedgeStartRad
+ (_cumSum / If(WedgeTotal = 0, 1, WedgeTotal))
* 2 * Pi() * ArcScale
+ (_value / If(WedgeTotal = 0, 1, WedgeTotal))
* Pi() * ArcScale
},
With(
{
_offsetX: If(_explode, _explodeDist * Sin(_midAngle), 0),
_offsetY: If(_explode, -(_explodeDist * Cos(_midAngle)), 0)
},
With(
{
_tooltip:
_valText & " (" &
Text(
If(WedgeTotal > 0, _value / WedgeTotal * 100, 0),
Coalesce(Self.PercentFormat, "[$-en-US]0.0")
) & "%)"
},
With(
{
_label: Coalesce(SortedRec.Label, SortedRec.Key & " " & _tooltip),
_lxn: 50 + varLabelR * Sin(_midAngle) + _offsetX,
_lyn: 50 - varLabelR * Cos(_midAngle) + _offsetY
},
{
Order: SortedRec.Order,
Key: SortedRec.Key,
Label: Substitute(Substitute(Substitute(_label, "&", "&"), "<", "<"), ">", ">"),
Value: _value,
RGBColor: _color,
Explode: _explode,
Color: ColorValue(_color),
StartAngle: _startAngle,
EndAngle: _endAngle,
MidAngle: _midAngle,
NormStart: Mod(_startAngle, 2 * Pi()),
NormEnd: Mod(_endAngle, 2 * Pi()),
OffsetX: _offsetX,
OffsetY: _offsetY,
X1: Text(50 + varSvgR * Sin(_startAngle), "[$-en-US]0.0000"),
Y1: Text(50 - varSvgR * Cos(_startAngle), "[$-en-US]0.0000"),
X2: Text(50 + varSvgR * Sin(_endAngle), "[$-en-US]0.0000"),
Y2: Text(50 - varSvgR * Cos(_endAngle), "[$-en-US]0.0000"),
MidX: Text(50 + varSvgRTxt * Sin(_midAngle) + _offsetX, "[$-en-US]0.0000"),
MidY: Text(50 - varSvgRTxt * Cos(_midAngle) + _offsetY, "[$-en-US]0.0000"),
LabelX: Text(_lxn, "[$-en-US]0.0000"),
LabelY: Text(_lyn, "[$-en-US]0.0000"),
LabelXN: _lxn,
LabelYN: _lyn,
tooltip: SortedRec.Key & ": " & _tooltip
}
)
)
)
)
)
)
)
)
);
// ====
// SECTION 6B: OUTSIDE LABEL PLACEMENT -- EDGE AVOIDANCE + COLLISION AVOIDANCE
// ====
// Only active when WedgeLabelsOutside = true; inside mode passes through unchanged.
//
// STEP 1 -- EDGE AVOIDANCE:
// Estimate text width as Len(Label) * varCharW viewBox units.
// varCharW = 2.3: approximate character width at font-size 4.
// varMaxX = 97: right safe boundary. varMinX = 3: left safe boundary.
//
// Right side (text-anchor=start): if anchorX + textW > varMaxX,
// shift anchor left by HALF the overflow distance only.
// Never pull past x=52.
//
// Left side (text-anchor=end): if anchorX - textW < varMinX,
// push anchor right by HALF the underflow distance only.
// Never push past x=48.
//
// Y is NOT recomputed -- natural LabelYN preserved for rank-push.
//
// STEP 2 -- COLLISION AVOIDANCE:
// Split into right/left sides, sort top-to-bottom by natural LabelYN.
// Stack base starts half a spacing unit ABOVE the topmost natural Y so
// labels near the top of the pie can spread upward as well as downward.
// _rank = 0-based count of labels above this one on the same side.
// AdjLabelY = Max(naturalY, stackBase + rank * varLabelSpacing).
// varLabelSpacing = 5 (slightly tighter than before for better fit).
Set(varCharW, 2.3);
Set(varMaxX, 97);
Set(varMinX, 3);
Set(varLabelSpacing, 5);
Set(
varSlicesAdjusted,
If(
Not(varIsOutside),
AddColumns(
varSlices,
AdjLabelX, LabelXN,
AdjLabelY, LabelYN,
LabelAnchor, "middle"
),
With(
{
_edgeAdj:
AddColumns(
varSlices,
_isRight, Sin(MidAngle) >= 0,
_edgeX,
If(
Sin(MidAngle) >= 0,
// Right side: shift left by half the overflow only
If(
LabelXN + Len(Label) * varCharW > varMaxX,
Max(52, LabelXN - (LabelXN + Len(Label) * varCharW - varMaxX) * 0.5),
LabelXN
),
// Left side: shift right by half the underflow only
If(
LabelXN - Len(Label) * varCharW < varMinX,
Min(48, LabelXN + (varMinX - (LabelXN - Len(Label) * varCharW)) * 0.5),
LabelXN
)
)
)
},
With(
{
_right: Sort(Filter(_edgeAdj, _isRight), LabelYN, SortOrder.Ascending),
_left: Sort(Filter(_edgeAdj, !_isRight), LabelYN, SortOrder.Ascending)
},
With(
{
// Stack base starts half a spacing above topmost label
// so the top of the stack can spread upward too
_rightBase: First(_right).LabelYN - varLabelSpacing * 0.5,
_leftBase: First(_left).LabelYN - varLabelSpacing * 0.5
},
Ungroup(
Table(
{
Value:
AddColumns(
AddColumns(
_right,
_rank,
CountRows(Filter(_right, LabelYN < ThisRecord.LabelYN))
),
AdjLabelX, _edgeX,
AdjLabelY, Max(LabelYN, _rightBase + _rank * varLabelSpacing),
LabelAnchor, "start"
)
},
{
Value:
AddColumns(
AddColumns(
_left,
_rank,
CountRows(Filter(_left, LabelYN < ThisRecord.LabelYN))
),
AdjLabelX, _edgeX,
AdjLabelY, Max(LabelYN, _leftBase + _rank * varLabelSpacing),
LabelAnchor, "end"
)
}
),
Value
)
)
)
)
)
);
// ====
// SECTION 7: BUILD varSVG
// ====
Set(
varSVG,
If(
Or(WedgeTotal <= 0, IsEmpty(varSlices)),
// Empty state SVG
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'>" &
"<circle cx='50' cy='50' r='48' fill='#F3F4F6' stroke='#E5E7EB' stroke-width='1'/>" &
"<path d='M 50 50 L 50 2 A 48 48 0 0 1 91.6 26 Z' fill='#E5E7EB' stroke='#F9FAFB' stroke-width='1.5'/>" &
"<path d='M 50 50 L 91.6 26 A 48 48 0 0 1 91.6 74 Z' fill='#DBEAFE' stroke='#F9FAFB' stroke-width='1.5'/>" &
"<path d='M 50 50 L 91.6 74 A 48 48 0 0 1 50 98 Z' fill='#E5E7EB' stroke='#F9FAFB' stroke-width='1.5'/>" &
"<path d='M 50 50 L 50 98 A 48 48 0 0 1 8.4 74 Z' fill='#DBEAFE' stroke='#F9FAFB' stroke-width='1.5'/>" &
"<path d='M 50 50 L 8.4 74 A 48 48 0 0 1 8.4 26 Z' fill='#E5E7EB' stroke='#F9FAFB' stroke-width='1.5'/>" &
"<path d='M 50 50 L 8.4 26 A 48 48 0 0 1 50 2 Z' fill='#DBEAFE' stroke='#F9FAFB' stroke-width='1.5'/>" &
If(
varHoleR > 0,
"<circle cx='50' cy='50' r='" & Text(varHoleR, "[$-en-US]0.0###") &
"' fill='" & Coalesce(Self.ChartFill, "#FAFAFA") & "'/>",
""
) &
"</svg>",
// Data SVG
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'>" &
Concat(
varSlices,
"<path transform='translate(" &
Text(OffsetX, "[$-en-US]0.0000") & "," &
Text(OffsetY, "[$-en-US]0.0000") &
")' d='M 50 50 L " &
X1 & " " & Y1 &
" A " & Text(varSvgR, "[$-en-US]0.0###") & " " &
Text(varSvgR, "[$-en-US]0.0###") & " 0 " &
If((EndAngle - StartAngle) > Pi(), "1", "0") &
" 1 " & X2 & " " & Y2 &
" Z' fill='" & RGBColor &
"' stroke='" & Coalesce(Self.StrokeColor, "white") &
"' stroke-width='" & Text(Coalesce(Self.StrokeWidth, 1), "[$-en-US]0.0#") &
"'/>"
) &
If(
Coalesce(Self.ShowWedgeLabel, false),
Concat(
varSlicesAdjusted,
If(
(EndAngle - StartAngle) >= (Max(0, Coalesce(Self.MinLabelAngle, 10)) * Pi() / 180),
"<text x='" & Text(AdjLabelX, "[$-en-US]0.0000") &
"' y='" & Text(AdjLabelY, "[$-en-US]0.0000") &
"' text-anchor='" & LabelAnchor &
"' dominant-baseline='middle'" &
" fill='" & Coalesce(Self.WedgeLabelColor, "white") &
"' font-size='" & varLabelFS &
"' font-family='Segoe UI' font-weight='600'>" &
Label &
"</text>",
""
)
),
""
) &
If(
varHoleR > 0,
If(
ArcScale >= 1,
"<circle cx='50' cy='50' r='" & Text(varHoleR, "[$-en-US]0.0###") &
"' fill='" & Coalesce(Self.ChartFill, "#FAFAFA") & "'/>",
"<path d='M 50 50 L " &
Text(50 + varHoleR * Sin(varHoleStartAngle), "[$-en-US]0.0000") & " " &
Text(50 - varHoleR * Cos(varHoleStartAngle), "[$-en-US]0.0000") &
" A " & Text(varHoleR, "[$-en-US]0.0###") & " " &
Text(varHoleR, "[$-en-US]0.0###") & " 0 " &
If((varHoleEndAngle - varHoleStartAngle) > Pi(), "1", "0") & " 1 " &
Text(50 + varHoleR * Sin(varHoleEndAngle), "[$-en-US]0.0000") & " " &
Text(50 - varHoleR * Cos(varHoleEndAngle), "[$-en-US]0.0000") &
" Z' fill='" & Coalesce(Self.ChartFill, "#FAFAFA") & "'/>"
),
""
) &
"</svg>"
)
);
// make sure varHoveredKey matches an actual key, otherwise clear it
If(IsBlank(LookUp(Self.Items, Key = varHoveredKey || X = varHoveredKey)), Set(varHoveredKey, ""));
// ====
// SECTION 8: BUILD varButtonSlices
// ====
Set(btnW, 2 * PieRadius / gridM);
Set(btnH, 2 * PieRadius / gridN);
Set(gridOX, ChartCenterX - PieRadius);
Set(gridOY, ChartCenterY - PieRadius);
Set(PieRadiusAdjusted, PieRadius * varSvgR / 50);
Set(r2, PieRadiusAdjusted * PieRadiusAdjusted);
Set(
varButtonSlices,
ForAll(
Sequence(gridN * gridM) As cell,
With(
{
_i: RoundDown((cell.Value - 1) / gridM, 0),
_j: Mod(cell.Value - 1, gridM),
_xMin: gridOX + Mod(cell.Value - 1, gridM) * btnW,
_yMin: gridOY + RoundDown((cell.Value - 1) / gridM, 0) * btnH
},
// ====
// IMPLEMENTATION A: single center-point Atan2 per cell
// ====
/*
With(
{
_sx: _xMin + btnW / 2,
_sy: _yMin + btnH / 2
},
With(
{
_dx: _sx - ChartCenterX,
_ndy: ChartCenterY - _sy,
_d2: Power(_sx - ChartCenterX, 2) + Power(ChartCenterY - _sy, 2)
},
If(
_d2 <= r2 And _d2 > 0,
With(
{
_angle: Mod(
If(
_ndy > 0, Atan(_dx / _ndy),
_ndy < 0 And _dx >= 0, Atan(_dx / _ndy) + Pi(),
_ndy < 0, Atan(_dx / _ndy) - Pi(),
_dx > 0, Pi() / 2,
-Pi() / 2
) + 2 * Pi(),
2 * Pi()
)
},
{
Ni: _i,
Mj: _j,
SliceOrder:
Coalesce(
LookUp(
varSlices,
If(
NormStart < NormEnd,
_angle >= NormStart And _angle < NormEnd,
_angle >= NormStart Or _angle < NormEnd
)
).Order,
Blank()
)
}
),
{ Ni: _i, Mj: _j, SliceOrder: Blank() }
)
)
)
*/
// ====
// IMPLEMENTATION B: sub-sample majority vote [ACTIVE]
// ====
With(
{
_hits:
ForAll(
Sequence(sampleSize * sampleSize) As s,
{
wedge:
With(
{
_sx: _xMin
+ (RoundDown((s.Value - 1) / sampleSize, 0) + 0.5)
* btnW / sampleSize,
_sy: _yMin
+ (Mod(s.Value - 1, sampleSize) + 0.5)
* btnH / sampleSize
},
With(
{
_dx: _sx - ChartCenterX,
_ndy: ChartCenterY - _sy,
_d2: Power(_sx - ChartCenterX, 2)
+ Power(ChartCenterY - _sy, 2)
},
If(
_d2 <= r2 And _d2 > 0,
With(
{
_angle: Mod(
If(
_ndy > 0, Atan(_dx / _ndy),
_ndy < 0 And _dx >= 0, Atan(_dx / _ndy) + Pi(),
_ndy < 0, Atan(_dx / _ndy) - Pi(),
_dx > 0, Pi() / 2,
-Pi() / 2
) + 2 * Pi(),
2 * Pi()
)
},
Coalesce(
LookUp(
varSlices,
If(
NormStart < NormEnd,
_angle >= NormStart And _angle < NormEnd,
_angle >= NormStart Or _angle < NormEnd
)
).Order,
-1
)
),
-1
)
)
)
}
)
},
With(
{ _valid: CountIf(_hits, wedge <> -1) },
If(
_valid = 0,
{ Ni: _i, Mj: _j, SliceOrder: Blank() },
With(
{
_best: First(
Sort(
AddColumns(
varSlices,
_cnt,
CountIf(_hits, wedge = Order)
),
_cnt,
SortOrder.Descending
)
)
},
{
Ni: _i,
Mj: _j,
SliceOrder:
If(
_best._cnt / _valid >= threshold,
_best.Order,
Blank()
)
}
)
)
)
)
)
)
)
SelectedSlice: =LookUp(varSlices, Key = varHoveredKey)
SliceCount: =CountRows(varItemsSorted)
SvgMarkup: =varSVG
TotalValue: =Sum(varItemsSorted, Coalesce(Value, 0))
Width: =400
Children:
- cmpPieChart_conRoot:
Control: GroupContainer@1.5.0
Variant: ManualLayout
Properties:
Height: =Parent.Height
Width: =Parent.Width
Children:
- cmpPieChart_conStatic:
Control: GroupContainer@1.5.0
Variant: ManualLayout
Properties:
Height: =Parent.Height
Width: =Parent.Width
Children:
- cmpPieChart_conBackground:
Control: GroupContainer@1.5.0
Variant: ManualLayout
Properties:
BorderColor: |-
=If(