-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusunburstchart.pas
1029 lines (932 loc) · 32.5 KB
/
usunburstchart.pas
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
(******************************************************************************)
(* uSunburstChart 28.07.2023 *)
(* *)
(* Version : 0.05 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Realises a SunBurstchart that is configurable as much as *)
(* possible. *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* 0.02 - rewrite with pointers -> more stable and faster. *)
(* load/save *)
(* 0.03 - support multi line captions *)
(* OnDeleteElementsUserData *)
(* 0.04 - OnAfterSegmentPaint *)
(* Fix Crash when selecting multiple elements *)
(* 0.05 - change datatype of TSunBurstChartElement.value to *)
(* uint64 *)
(* *)
(******************************************************************************)
Unit usunburstchart;
{$MODE ObjFPC}{$H+}
Interface
Uses
Classes, SysUtils, Controls, Graphics, ufifo;
Const
(*
* History: 1 = Initialversion
* 2 = TSunBurstChartElement.value (integer -> uint64)
*)
SunBurstChartFileVersion: integer = 2;
Type
TPointArray = Array Of TPoint;
TColorSet = Record
BrushColor: TColor;
PenColor: TColor;
PenWitdh: integer;
FontColor: TColor;
End;
(*
* Needed to give write access to Selected elements ..
*)
PSunBurstChartElement = ^TSunBurstChartElement;
TSunBurstChartElement = Record
// Everything that a user is allowed to access to
Caption: String;
Color: TColorSet;
SelectedColor: TColorSet;
Value: UInt64;
UserData: Pointer;
Selected: Boolean; // If True, then Selected Brush / Selected will be used (will not be stored!)
// Everything that the user is not allowed to access (can and will be overwriten by TSunburstChart)
Child: PSunBurstChartElement; // Pointer to first Child
PrevSibling: PSunBurstChartElement; // Pointer to previos Sibling (nil if first child)
NextSibling: PSunBurstChartElement; // Pointer to next Sibling
Parent: PSunBurstChartElement; // Pointer Back to Parent only needed when deleting Elements
AbsStartAngle: Single;
AbsEndAngle: Single;
Level: Integer; // 0 = most inner circle
LevelRadius: Single; // is more or less constant, but with this and some constraints the width could be dynamic ..
End;
TOnSaveUserData = Procedure(Sender: TObject; Const Stream: TStream; aUserData: Pointer) Of Object;
TOnLoadUserData = Function(Sender: TObject; Const Stream: TStream): Pointer Of Object;
TOnDeleteElementsUserData = Procedure(Sender: TObject; aUserData: Pointer) Of Object;
TOnSegmentPaint = Procedure(Sender: TObject; Const aElement: PSunBurstChartElement) Of Object;
TPSunBurstChartElementFifo = Specialize TBufferedFifo < PSunBurstChartElement > ;
{ TSunburstChart }
TSunburstChart = Class(TGraphicControl)
private
fRoot: PSunBurstChartElement; // Pointer to Root Element
fSelectedStack: Array Of PSunBurstChartElement; // only used during Paint
fSelectedStackCnt: integer; // only used during Paint
FAngleOffset: Single;
fInitialArc: Single;
fPieCenter: Tpoint;
fPieRadius: Single;
fLevelMargin: integer;
fChanged: Boolean;
// Helper for iterator pattern
fIterator: PSunBurstChartElement;
fIteratorFifo: TPSunBurstChartElementFifo;
(*
* Variables for callback handlings
*)
fOnAfterSegmentPaint: TOnSegmentPaint;
fOnAfterPaint: TNotifyEvent;
fOnBeforeSegmentPaint: TOnSegmentPaint;
fOnDeleteElementsUserData: TOnDeleteElementsUserData; // Only needed if UserData Pointer is <> nil
fOnLoadUserData: TOnLoadUserData; // Only needed if UserData Pointer is <> nil
fOnSaveUserData: TOnSaveUserData; // Only needed if UserData Pointer is <> nil
Procedure setAngleOffset(AValue: Single);
Procedure SetInitialArc(AValue: Single);
Procedure SetPieCenter(AValue: Tpoint);
Function CalcMaxlevelCount(): Integer;
Procedure RenderElement(Const aElement: PSunBurstChartElement; RenderAll: Boolean);
Procedure SetPieRadius(AValue: Single);
Procedure SetLevelMargin(AValue: integer);
Procedure CalcAllMetaData(); // Calc Level, Angle informations for each Element accessable by Root
Procedure FreeChild(Var Child: PSunBurstChartElement);
protected
Procedure Paint; override;
public
Property Changed: Boolean read fChanged;
Property PieCenter: Tpoint read fPieCenter write SetPieCenter; // According to the compiler this can not be published :-(
Constructor Create(AOwner: TComponent); override;
Destructor Destroy(); override;
Function LoadFromStream(Const Stream: TStream): Boolean;
Function LoadFromFile(aFilename: String): Boolean;
Function SaveToStream(Const Stream: TStream): Boolean;
Function SaveToFile(aFilename: String): Boolean;
Function AddChildElement(Const aParent: PSunBurstChartElement; Child: TSunBurstChartElement): PSunBurstChartElement; // if aParent = nil, child will always be added as sibling to root
Function AddSiblingElement(Const aElement: PSunBurstChartElement; Sibling: TSunBurstChartElement): PSunBurstChartElement;
Function DelElement(Const aElement: PSunBurstChartElement): boolean;
Procedure Clear; // Removes all elements at once
Procedure DeselectAll; // Iterates through all Childs and "desectes" them (but without changing the iterator values !
// Gives the Segment which collides with x,y
// false if none exist
// Returns a pointer to give write access to the Element!
Function GetSegmentAtPos(x, y: integer; Out aElement: PSunBurstChartElement): Boolean;
(*
* Iterator Pattern to walk all Childs
*)
Function Iterator: PSunBurstChartElement;
Function IterFirst: PSunBurstChartElement;
Function IterNext: PSunBurstChartElement;
published
Property AngleOffset: Single read FAngleOffset write setAngleOffset; // in Radian
Property Color;
Property InitialArc: Single read fInitialArc write SetInitialArc; // in Radian
Property LevelMargin: integer read fLevelMargin write SetLevelMargin;
Property PieRadius: Single read fPieRadius write SetPieRadius;
Property ShowHint;
Property OnAfterSegmentPaint: TOnSegmentPaint read fOnAfterSegmentPaint write fOnAfterSegmentPaint;
Property OnAfterPaint: TNotifyEvent read fOnAfterPaint write fOnAfterPaint;
Property OnBeforeSegmentPaint: TOnSegmentPaint read fOnBeforeSegmentPaint write fOnBeforeSegmentPaint;
Property OnClick;
Property OnDeleteElementsUserData: TOnDeleteElementsUserData read fOnDeleteElementsUserData write fOnDeleteElementsUserData;
Property OnDblClick;
Property OnLoadUserData: TOnLoadUserData read fOnLoadUserData write fOnLoadUserData;
Property OnMouseUp;
Property OnMouseMove;
Property OnMouseDown;
Property OnSaveUserData: TOnSaveUserData read fOnSaveUserData write fOnSaveUserData;
Property OnShowHint;
Property OnResize;
End;
(*
* Use this function to get a proper initialized TSunBurstChartElement !
*)
Function DefaultSunBurstChartElement(): TSunBurstChartElement;
Implementation
Uses
math;
Const
Epsilon = 0.0001;
Procedure Nop(); // Debug only
Begin
End;
Function DefaultSunBurstChartElement(): TSunBurstChartElement;
Begin
// The values the user is allowed to access to
result.Caption := '';
result.Color.BrushColor := clGray;
result.Color.PenColor := clblack;
result.Color.PenWitdh := 1;
result.Color.FontColor := clBlack;
result.SelectedColor.BrushColor := clNavy;
result.SelectedColor.PenColor := clred;
result.SelectedColor.PenWitdh := 4;
result.SelectedColor.FontColor := clWhite;
result.Value := 1;
result.UserData := Nil;
result.Selected := false;
// The values the user shall not change / or edit, will be calculated through TSunburstChart
result.Child := Nil;
result.PrevSibling := Nil;
result.NextSibling := Nil;
result.Parent := Nil;
result.AbsStartAngle := 0;
result.AbsEndAngle := 0;
result.Level := 0;
result.LevelRadius := 0;
End;
{ TSunburstChart }
Constructor TSunburstChart.Create(AOwner: TComponent);
Begin
Inherited Create(AOwner);
fOnAfterSegmentPaint := Nil;
fOnAfterPaint := Nil;
fOnBeforeSegmentPaint := Nil;
fOnDeleteElementsUserData := Nil;
fOnLoadUserData := Nil;
fOnSaveUserData := Nil;
fChanged := false;
Width := 300;
Height := 300;
fPieRadius := 140;
Color := clwhite;
fPieCenter := point(150, 150);
fInitialArc := 2 * pi;
fAngleOffset := 0;
fLevelMargin := 0;
fRoot := Nil;
fSelectedStack := Nil;
fSelectedStackCnt := 0;
fIterator := Nil;
fIteratorFifo := TPSunBurstChartElementFifo.create(16);
End;
Procedure TSunburstChart.Clear;
Begin
fChanged := false;
FreeChild(froot);
fRoot := Nil;
InitialArc := 2 * pi;
AngleOffset := 0;
LevelMargin := 0;
Color := clwhite;
Invalidate;
End;
Destructor TSunburstChart.Destroy;
Begin
Clear();
setlength(fSelectedStack, 0);
fIteratorFifo.free;
Inherited Destroy;
End;
Function TSunburstChart.SaveToStream(Const Stream: TStream): Boolean;
Function GetSiblingCount(Const aElement: PSunBurstChartElement): Integer;
Var
p: PSunBurstChartElement;
Begin
result := 0;
If Not assigned(aElement) Then exit;
p := aElement;
While assigned(p) Do Begin
inc(result);
p := p^.NextSibling;
End;
End;
Function ElementsToStream(Const aElement: PSunBurstChartElement): Boolean;
Var
Siblings: Integer;
b: UInt8;
p: PSunBurstChartElement;
Begin
If Not Assigned(aElement) Then Begin
Siblings := 0;
stream.Write(Siblings, SizeOf(Siblings));
result := true;
exit;
End;
result := false;
Siblings := GetSiblingCount(aElement);
stream.Write(Siblings, SizeOf(Siblings));
p := aElement;
While Assigned(p) Do Begin
stream.WriteAnsiString(p^.Caption);
stream.Write(p^.Color, SizeOf(p^.Color));
stream.Write(p^.SelectedColor, SizeOf(p^.SelectedColor));
stream.Write(p^.Value, SizeOf(p^.Value));
If assigned(p^.UserData) Then Begin
b := 1;
stream.Write(b, SizeOf(b));
If Not assigned(OnSaveUserData) Then Begin
Raise exception.create('Error, TChild with defined userdata, but no OnSaveUserData property set.');
exit;
End;
OnSaveUserData(self, Stream, p^.UserData);
End
Else Begin
b := 0;
stream.Write(b, SizeOf(b));
End;
ElementsToStream(p^.Child);
p := p^.NextSibling;
End;
result := true;
End;
Var
c: TColor;
Begin
result := false;
// Global Settings
stream.Write(SunBurstChartFileVersion, sizeof(SunBurstChartFileVersion));
stream.write(FAngleOffset, sizeof(FAngleOffset));
c := Color;
stream.write(C, sizeof(C));
stream.write(fInitialArc, sizeof(fInitialArc));
stream.write(fPieCenter, sizeof(fPieCenter));
stream.write(fPieRadius, sizeof(fPieRadius));
stream.write(fLevelMargin, sizeof(fLevelMargin));
result := ElementsToStream(fRoot);
If result Then fChanged := false;
End;
Function TSunburstChart.LoadFromStream(Const Stream: TStream): Boolean;
Var
LoadedFileVersion: integer;
Function ElementFromStream(): TSunBurstChartElement;
Var
b: uint8;
i: integer;
Begin
result := DefaultSunBurstChartElement();
result.caption := Stream.ReadAnsiString;
stream.Read(result.Color, SizeOf(result.Color));
stream.Read(result.SelectedColor, SizeOf(result.SelectedColor));
If LoadedFileVersion = 1 Then Begin
i := 1;
stream.Read(i, SizeOf(i));
result.Value := i;
End
Else Begin
stream.Read(result.Value, SizeOf(result.Value));
End;
b := 2;
stream.Read(b, SizeOf(b));
If b = 1 Then Begin
If Not assigned(OnLoadUserData) Then Begin
Raise exception.create('Error, stream with defined userdata, but no OnLoadUserData property set.');
exit;
End;
result.UserData := OnLoadUserData(self, Stream);
End;
End;
Function ElementsFromStream(aParent: PSunBurstChartElement): boolean;
Var
Siblings, i: Integer;
Element: TSunBurstChartElement;
nParent: PSunBurstChartElement;
Begin
result := false;
Siblings := 0;
stream.Read(Siblings, SizeOf(Siblings));
For i := 0 To Siblings - 1 Do Begin
Element := ElementFromStream();
nParent := AddChildElement(aParent, Element);
result := ElementsFromStream(nParent);
If Not result Then exit;
End;
result := true;
End;
Var
c: TColor;
Begin
result := false;
Clear;
LoadedFileVersion := 0;
stream.Read(LoadedFileVersion, sizeof(LoadedFileVersion));
If (LoadedFileVersion > SunBurstChartFileVersion) Or (LoadedFileVersion <= 0) Then exit; // Da ist offensichtlich was falsch ..
stream.Read(FAngleOffset, sizeof(FAngleOffset));
c := Color;
stream.Read(C, sizeof(C));
stream.Read(fInitialArc, sizeof(fInitialArc));
stream.Read(fPieCenter, sizeof(fPieCenter));
stream.Read(fPieRadius, sizeof(fPieRadius));
stream.Read(fLevelMargin, sizeof(fLevelMargin));
result := ElementsFromStream(Nil);
If result Then fChanged := false;
End;
Function TSunburstChart.SaveToFile(aFilename: String): Boolean;
Var
fs: TFileStream;
Begin
result := false;
Try
fs := TFileStream.Create(aFilename, fmCreate Or fmOpenWrite);
result := SaveToStream(fs);
fs.free;
Except
Raise;
// Nichts, das Result ist ja schon false..
End;
End;
Function TSunburstChart.LoadFromFile(aFilename: String): Boolean;
Var
fs: TFileStream;
Begin
result := false;
Try
fs := TFileStream.Create(aFilename, fmOpenRead);
result := LoadFromStream(fs);
fs.free;
Invalidate;
Except
Raise;
// Nichts, das Result ist ja schon false..
End;
End;
Function TSunburstChart.AddChildElement(Const aParent: PSunBurstChartElement; Child: TSunBurstChartElement
): PSunBurstChartElement;
Var
p: PSunBurstChartElement;
Begin
result := Nil;
If aParent = Nil Then Begin
If assigned(fRoot) Then Begin
If assigned(fRoot^.NextSibling) Then Begin
p := fRoot^.NextSibling;
While assigned(p^.NextSibling) Do Begin
p := p^.NextSibling;
End;
new(p^.NextSibling);
p^.NextSibling^ := Child;
p^.NextSibling^.Parent := Nil;
p^.NextSibling^.PrevSibling := p;
result := p^.NextSibling;
End
Else Begin
new(fRoot^.NextSibling);
fRoot^.NextSibling^ := Child;
fRoot^.NextSibling^.Parent := Nil;
fRoot^.NextSibling^.PrevSibling := fRoot;
result := fRoot^.NextSibling;
End;
End
Else Begin
new(fRoot);
froot^ := Child;
froot^.Parent := Nil;
froot^.PrevSibling := Nil;
result := fRoot;
End;
End
Else Begin
If assigned(aParent^.Child) Then Begin
p := aParent^.Child;
While assigned(p^.NextSibling) Do Begin
p := p^.NextSibling;
End;
new(p^.NextSibling);
p^.NextSibling^ := Child;
p^.NextSibling^.Parent := p^.Parent;
p^.NextSibling^.PrevSibling := p;
result := p^.NextSibling;
End
Else Begin
new(aParent^.Child);
aParent^.Child^ := Child;
aParent^.Child^.Parent := aParent;
aParent^.Child^.PrevSibling := Nil;
result := aParent^.Child;
End;
End;
fChanged := true;
Invalidate;
End;
Function TSunburstChart.AddSiblingElement(Const aElement: PSunBurstChartElement;
Sibling: TSunBurstChartElement): PSunBurstChartElement;
Var
p: PSunBurstChartElement;
Begin
result := Nil;
If Not assigned(aElement) Then exit;
If assigned(aElement^.NextSibling) Then Begin
p := aElement^.NextSibling;
While assigned(p^.NextSibling) Do Begin
p := p^.NextSibling;
End;
new(p^.NextSibling);
p^.NextSibling^ := Sibling;
p^.NextSibling^.PrevSibling := p;
p^.NextSibling^.Parent := p^.Parent;
result := p^.NextSibling;
End
Else Begin
new(aElement^.NextSibling);
aElement^.NextSibling^ := Sibling;
aElement^.NextSibling^.PrevSibling := aElement;
aElement^.NextSibling^.Parent := aElement^.Parent;
result := aElement^.NextSibling;
End;
fChanged := true;
Invalidate;
End;
Function TSunburstChart.DelElement(Const aElement: PSunBurstChartElement): boolean;
Var
p: PSunBurstChartElement;
Begin
result := false;
If Not assigned(aElement) Then exit;
If assigned(aElement^.PrevSibling) Then Begin
// Das Element ist irgendwo mitten in der Siblingkette
p := aElement;
// Aushängen des Elementes aus der Siblingkette
p^.PrevSibling^.NextSibling := p^.NextSibling;
If assigned(p^.NextSibling) Then Begin
p^.NextSibling^.PrevSibling := p^.PrevSibling;
End;
// Alles Unterhalb fliegt sowieso raus
FreeChild(p^.Child);
// Das Eigentliche element Freigeben
If assigned(p^.UserData) Then Begin
If assigned(OnDeleteElementsUserData) Then Begin
OnDeleteElementsUserData(self, p^.UserData);
End
Else Begin
Raise exception.create('Error, freeing PSunBurstChartElement with userdata, but no OnDeleteElementsUserData set.');
End;
End;
dispose(p);
End
Else Begin
// Das Element ist das Erste in der Kette
If aElement^.Parent = Nil Then Begin
p := aElement;
//p^.Parent^.Child := p^.NextSibling; -- Gibt es in der 1. Ebene ja nicht.
If assigned(p^.NextSibling) Then Begin
p^.NextSibling^.PrevSibling := Nil;
End;
If aElement = fRoot Then froot := froot^.NextSibling;
FreeChild(p^.Child);
If assigned(p^.UserData) Then Begin
If assigned(OnDeleteElementsUserData) Then Begin
OnDeleteElementsUserData(self, p^.UserData);
End
Else Begin
Raise exception.create('Error, freeing PSunBurstChartElement with userdata, but no OnDeleteElementsUserData set.');
End;
End;
dispose(p);
Invalidate;
End
Else Begin
p := aElement;
p^.Parent^.Child := p^.NextSibling;
If assigned(p^.NextSibling) Then Begin
p^.NextSibling^.PrevSibling := Nil;
End;
FreeChild(p^.Child);
If assigned(p^.UserData) Then Begin
If assigned(OnDeleteElementsUserData) Then Begin
OnDeleteElementsUserData(self, p^.UserData);
End
Else Begin
Raise exception.create('Error, freeing PSunBurstChartElement with userdata, but no OnDeleteElementsUserData set.');
End;
End;
dispose(p);
End;
End;
fChanged := true;
Invalidate;
End;
Procedure TSunburstChart.DeselectAll;
Procedure DeSel(Const aElement: PSunBurstChartElement);
Begin
If Not assigned(aElement) Then exit;
aElement^.Selected := false;
DeSel(aElement^.Child);
DeSel(aElement^.NextSibling);
End;
Begin
Desel(fRoot);
//fChanged := true; -- Selected flag is not stored so no change !
Invalidate;
End;
Function TSunburstChart.GetSegmentAtPos(x, y: integer; Out aElement: PSunBurstChartElement
): Boolean;
Const
TwoPi = 2 * Pi;
Function AngleInRange(Const Angle: Single; StartAngle, EndAngle: Single): boolean;
Begin
// Clamp all Angles into the range of 0 .. 2 * pi
While StartAngle < 0 Do
StartAngle := StartAngle + TwoPi;
While StartAngle > TwoPi Do
StartAngle := StartAngle - TwoPi;
While EndAngle < 0 Do
EndAngle := EndAngle + TwoPi;
While EndAngle > TwoPi + Epsilon Do
EndAngle := EndAngle - TwoPi;
If EndAngle < StartAngle Then Begin
// If the zero crossing is within the Range
result := (angle >= StartAngle) Or (angle <= EndAngle);
End
Else Begin
result := (Angle >= StartAngle) And (angle <= EndAngle);
End;
End;
Var
Level: Integer;
angle: Single;
Function Search(Const asElement: PSunBurstChartElement): Boolean;
Begin
result := false;
If Not assigned(asElement) Then exit;
result := (asElement^.Level = Level) And AngleInRange(angle, asElement^.AbsStartAngle, asElement^.AbsEndAngle);
If result Then Begin
aElement := asElement;
End
Else Begin
If asElement^.Level < Level Then Begin
result := search(asElement^.Child);
If result Then exit;
End;
If asElement^.Level <= Level Then Begin
result := search(asElement^.NextSibling);
If result Then exit;
End;
End;
End;
Var
tx, ty: integer;
Begin
result := false;
aElement := Nil;
If Not assigned(fRoot) Then exit;
// 0. Alle Elemente initialisieren, sollten diese noch nie gerendert worden sein
CalcAllMetaData();
// 1. Bestimmen der Polarkoordinaten von X,Y
tx := x - PieCenter.x;
ty := PieCenter.y - y; // Man Bedenke die Y-Achse ist invertiert damit der Mathematische Drehsinn stimmt ;)
// TODO: Abfangen Div by 0, wenn fRoot[0].LevelRadius + LevelMargin = 0 !
Level := trunc(sqrt(sqr(tx) + sqr(ty)) / (fRoot[0].LevelRadius + fLevelMargin)); // Berechnen des Aktuellen Ringes
angle := ArcTan2(ty, tx); // -pi .. pi
If angle < 0 Then angle := angle + 2 * pi; // 0 .. 2 * pi (so wie alle internen Koordiaten abgespeichert sind !
// 2. Suchen ob es ein Segment gibt, welches in diesen Koordinaten liegt !
result := Search(fRoot);
End;
Function TSunburstChart.Iterator: PSunBurstChartElement;
Begin
result := fIterator;
End;
Function TSunburstChart.IterFirst: PSunBurstChartElement;
Procedure Push(Const aElement: PSunBurstChartElement);
Begin
If Not Assigned(aElement) Then exit;
fIteratorFifo.Push(aElement);
push(aElement^.NextSibling);
push(aElement^.Child);
End;
Begin
// TODO: Not shure if this is the best way of implementing a iteration pattern
// as long as nobody deletes a element everything should be fine otherwise
// you will get lots of dangling references ..
fIterator := Nil;
fIteratorFifo.clear;
Push(fRoot);
If fIteratorFifo.Count <> 0 Then Begin
fIterator := fIteratorFifo.Pop;
End;
result := fIterator;
End;
Function TSunburstChart.IterNext: PSunBurstChartElement;
Begin
If fIteratorFifo.Count <> 0 Then Begin
fIterator := fIteratorFifo.Pop;
End
Else Begin
fIterator := Nil;
End;
result := fIterator;
End;
Procedure TSunburstChart.Paint;
Var
i: integer;
Begin
Inherited Paint; // Call the fOnPaint, ifneeded
// Erase Background
Canvas.Brush.Color := Color;
Canvas.Pen.Width := 1;
Canvas.Rectangle(-1, -1, Width + 1, Height + 1);
If Not assigned(froot) Then exit;
CalcAllMetaData;
fSelectedStackCnt := 0;
RenderElement(froot, True);
// Re Render all Selected Parts so that their border can overpaint the non selected areas
For i := 0 To fSelectedStackCnt - 1 Do Begin
RenderElement(fSelectedStack[i], false);
End;
If assigned(fOnAfterPaint) Then Begin
FonAfterPaint(Self);
End;
End;
Procedure TSunburstChart.RenderElement(Const aElement: PSunBurstChartElement;
RenderAll: Boolean);
Procedure PlotTextAtPos(p: Tpoint);
Var
Lines: TStringArray;
t, i: Integer;
{$IFDEF Windows}
s: String;
{$ENDIF}
Begin
If pos(LineEnding, aElement^.Caption) <> 0 Then Begin
{$IFDEF Windows}
s := aElement^.Caption;
s := StringReplace(s, #10, '', [rfReplaceAll]);
lines := s.Split([#13]);
{$ELSE}
lines := aElement^.Caption.Split([LineEnding]);
{$ENDIF}
t := p.y - (canvas.TextHeight('8') * length(Lines)) Div 2;
// TODO: This implements a "center" layout, in theory you could provide left and right as well..
For i := 0 To high(lines) Do Begin
canvas.TextOut(
p.x - (Canvas.TextWidth(lines[i]) Div 2),
t + i * Canvas.TextHeight('8'),
lines[i]
);
End;
End
Else Begin
canvas.TextOut(
p.x - Canvas.TextWidth(aElement^.Caption) Div 2,
p.Y - Canvas.TextHeight(aElement^.Caption) Div 2,
aElement^.Caption
);
End;
End;
Var
c, s, a: extended;
cnt, i: Integer;
PolyPoints: Array Of TPoint;
InnerRadius, OuterRadius: Single;
Begin
If Not assigned(aElement) Then exit;
If assigned(fOnBeforeSegmentPaint) Then Begin
fOnBeforeSegmentPaint(self, aElement);
End;
PolyPoints := Nil;
// 1. Rendern des Segmentes
If aElement^.Selected Then Begin
Canvas.Brush.Color := aElement^.SelectedColor.BrushColor;
canvas.Pen.Color := aElement^.SelectedColor.PenColor;
canvas.Pen.Width := aElement^.SelectedColor.PenWitdh;
canvas.Font.Color := aElement^.SelectedColor.FontColor;
If RenderAll Then Begin
inc(fSelectedStackCnt);
If fSelectedStackCnt > high(fSelectedStack) Then Begin
// In case where typically only 1 element is selected
// This is totally fine. In case with lots and lots selected
// elements, this is really high cost at the very first rendering ..
setlength(fSelectedStack, fSelectedStackCnt);
End;
fSelectedStack[fSelectedStackCnt - 1] := aElement;
End;
End
Else Begin
Canvas.Brush.Color := aElement^.Color.BrushColor;
canvas.Pen.Color := aElement^.Color.PenColor;
canvas.Pen.Width := aElement^.Color.PenWitdh;
canvas.Font.Color := aElement^.Color.FontColor;
End;
InnerRadius := aElement^.Level * (aElement^.LevelRadius + fLevelMargin);
OuterRadius := (aElement^.Level + 1) * (aElement^.LevelRadius);
If InnerRadius = 0 Then Begin
If abs(aElement^.AbsEndAngle - aElement^.AbsStartAngle - 2 * pi) <= Epsilon Then Begin
// Ein Vollkreis
Canvas.Ellipse(
round(PieCenter.x - OuterRadius),
round(PieCenter.Y - OuterRadius),
round(PieCenter.x + OuterRadius),
round(PieCenter.Y + OuterRadius)
);
PlotTextAtPos(PieCenter);
End
Else Begin
// Ein "Torten" Element
cnt := max(3, round(((aElement^.AbsEndAngle - aElement^.AbsStartAngle) * OuterRadius) / 10));
setlength(PolyPoints, cnt + 2);
PolyPoints[0] := PieCenter;
For i := 0 To cnt Do Begin
a := (aElement^.AbsEndAngle - aElement^.AbsStartAngle) * i / cnt + aElement^.AbsStartAngle;
SinCos(a, s, c);
PolyPoints[i + 1] := point(round(PieCenter.x + OuterRadius * c), round(PieCenter.Y - OuterRadius * s));
End;
Canvas.Polygon(PolyPoints);
a := (aElement^.AbsEndAngle + aElement^.AbsStartAngle) / 2;
SinCos(a, s, c);
PlotTextAtPos(point(round(PieCenter.x + OuterRadius * c / 2), round(PieCenter.Y - OuterRadius * s / 2)));
End;
End
Else Begin
// TODO: Wenn das Segment ein Vollkreis ist solte es als Ring und nicht als Ring mit 0-Linie gemalt werden.
// \-> Dieser "Bug" / "glitch" tritt nur auf, wenn BrushColor <> PenColor, sonst stimmt es *g*
// Ein Segment "außen" also als Ring
cnt := max(3, round(((aElement^.AbsEndAngle - aElement^.AbsStartAngle) * OuterRadius) / 10));
setlength(PolyPoints, 2 * (cnt + 1));
For i := 0 To cnt Do Begin
a := (aElement^.AbsEndAngle - aElement^.AbsStartAngle) * i / cnt + aElement^.AbsStartAngle;
SinCos(a, s, c);
PolyPoints[i] := point(round(PieCenter.x + InnerRadius * c), round(PieCenter.Y - InnerRadius * s));
PolyPoints[2 * (cnt + 1) - 1 - i] := point(round(PieCenter.x + OuterRadius * c), round(PieCenter.Y - OuterRadius * s));
End;
Canvas.Polygon(PolyPoints);
a := (aElement^.AbsEndAngle + aElement^.AbsStartAngle) / 2;
SinCos(a, s, c);
PlotTextAtPos(point(round(PieCenter.x + (InnerRadius + OuterRadius) * c / 2), round(PieCenter.Y - (InnerRadius + OuterRadius) * s / 2)));
End;
If assigned(fOnAfterSegmentPaint) Then Begin
fOnAfterSegmentPaint(self, aElement);
End;
// 2. Rekursiver Abstieg
If RenderAll Then Begin
RenderElement(aElement^.Child, true);
RenderElement(aElement^.NextSibling, true);
End;
End;
Procedure TSunburstChart.SetPieCenter(AValue: Tpoint);
Begin
If fPieCenter = AValue Then Exit;
fPieCenter := AValue;
fChanged := true;
Invalidate;
End;
Function TSunburstChart.CalcMaxLevelCount: Integer;
Function GetDepthOf(aElement: PSunBurstChartElement): integer;
Var
p: PSunBurstChartElement;
Begin
If assigned(aElement) Then Begin
result := 1 + GetDepthOf(aElement^.Child);
p := aElement^.NextSibling;
While assigned(p) Do Begin
result := max(result, 1 + GetDepthOf(p^.Child));
p := p^.NextSibling;
End;
End
Else Begin
result := 0;
End;
End;
Begin
result := GetDepthOf(fRoot);
End;
Procedure TSunburstChart.SetPieRadius(AValue: Single);
Begin
If fPieRadius = AValue Then Exit;
fPieRadius := AValue;
fChanged := true;
Invalidate;
End;
Procedure TSunburstChart.SetLevelMargin(AValue: integer);
Begin
If fLevelMargin = AValue Then Exit;
fLevelMargin := AValue;
fChanged := true;
Invalidate;
End;
Procedure TSunburstChart.CalcAllMetaData;
Var
LevelRadius: Single;
Function GetSiblingValueSum(Const aElement: PSunBurstChartElement): UInt64;
Var
p: PSunBurstChartElement;
Begin
result := 0;
p := aElement;
While Assigned(p) Do Begin
result := result + p^.Value;
p := p^.NextSibling;
End;
End;
Procedure CalcAllMetaDataSub(Const aElement: PSunBurstChartElement; Level: integer; absStartAngle, absEndAngle: Single);
Var
ElementSum: UInt64;
p: PSunBurstChartElement;
ChildAngleDist, ChildStartAngle, AngleDist: Single;
Begin
If Not assigned(aElement) Then exit;
ElementSum := GetSiblingValueSum(aElement);
p := aElement;
AngleDist := absEndAngle - absStartAngle;
ChildStartAngle := absStartAngle;
While assigned(p) Do Begin
ChildAngleDist := AngleDist * p^.Value / max(1, ElementSum);
p^.AbsStartAngle := ChildStartAngle;
p^.AbsEndAngle := ChildStartAngle + ChildAngleDist;
p^.Level := Level;
p^.LevelRadius := LevelRadius;
CalcAllMetaDataSub(p^.Child, Level + 1, p^.AbsStartAngle, p^.AbsEndAngle);
ChildStartAngle := p^.AbsEndAngle;
p := p^.NextSibling;
End;
End;
Var
LevelCount: integer;
Begin
LevelCount := CalcMaxlevelCount();
If LevelCount = 0 Then exit; // Das ist quasi die Prüfung auf fRoot <> Nil
LevelRadius := (PieRadius - fLevelMargin * (LevelCount - 1)) / LevelCount;
CalcAllMetaDataSub(fRoot, 0, AngleOffset, InitialArc + AngleOffset);
End;
Procedure TSunburstChart.FreeChild(Var Child: PSunBurstChartElement);
Begin
If Not assigned(Child) Then exit;
FreeChild(child^.Child);
FreeChild(child^.NextSibling);
If assigned(Child^.UserData) Then Begin
If assigned(OnDeleteElementsUserData) Then Begin