-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathucirclepackchart.pas
836 lines (755 loc) · 24.3 KB
/
ucirclepackchart.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
(******************************************************************************)
(* uCirclePackChart 08.08.2023 *)
(* *)
(* Version : 0.03 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : Realises a circlepacking component *)
(* *)
(* Inspired by https://github.com/pmenzel/packCircles *)
(* *)
(* 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 - Add: NeedReEvaluation *)
(* FIX: OnAfterPaint missing, Memleak *)
(* 0.03 - switch value from integer to single *)
(* *)
(******************************************************************************)
Unit ucirclepackchart;
{$MODE ObjFPC}{$H+}
Interface
Uses
Classes, SysUtils, Controls, Graphics;
Const
CirclePackFileVersoin: integer = 1;
Type
TFloatPoint = Record
x, y: Single;
End;
TColorSet = Record
BrushColor: TColor;
PenColor: TColor;
PenWitdh: integer;
FontColor: TColor;
End;
PCircle = ^TCircle;
TCircle = Record
// Everything that a user is allowed to access to
Caption: String;
Color: TColorSet;
SelectedColor: TColorSet;
Value: Single; // The Value that determines the Diameter of the circle
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 TPackedCircleChart)
Center: TFloatPoint; // Location where the circle should be if unscaled
ScaledRadius: Single; // Scalled radius of the Circle Proportional to Value
ScaledCenter: TFloatPoint; // Scaled Location where to render the Circle
(*
* During calculation of the circles positions the outmost circle of circles is stored in the sibling pointers
*)
PrevSibling: PCircle; // Pointer to previos Sibling (nil if first child)
NextSibling: PCircle; // Pointer to next Sibling
Next: PCircle; // Pointers to every "next" created circle -> will form a list of all circles in the order they where created
End;
TCircles = Array Of TCircle;
TOnSaveUserData = Procedure(Sender: TObject; Const Stream: TStream; aUserData: Pointer) Of Object;
TOnLoadUserData = Function(Sender: TObject; Const Stream: TStream): Pointer Of Object;
TOnDeleteCirclesUserData = Procedure(Sender: TObject; aUserData: Pointer) Of Object;
TOnCirclePaint = Procedure(Sender: TObject; Const aElement: PCircle) Of Object;
{ TPackedCircleChart }
TPackedCircleChart = Class(TGraphicControl)
private
fbb_topright, fbb_bottomleft: TFloatPoint;
fFirstCircle: PCircle; // Einsprung in das 1. Element
fLastCircle: PCircle;
FIterator: PCircle;
fChanged: Boolean;
FCircleCount: Integer;
fNeedRecalculation: Boolean; // True nach mindestens einem Add / Delete
fOnAfterCirclePaint: TOnCirclePaint;
fOnAfterPaint: TNotifyEvent;
fOnBeforeCirclePaint: TOnCirclePaint;
fOnDeleteCirclesUserData: TOnDeleteCirclesUserData;
fOnLoadUserData: TOnLoadUserData;
fOnSaveUserData: TOnSaveUserData;
Procedure ReCalculateIfNeeded;
Procedure CalcCirclesPositionsFromValue;
Procedure ScaleCirclesPositionsToClientRect; // Rechnet ScaledCenter und Radius entsprechend um, so dass alle Kreice in ClientRect passen (nur nach CalcCirclesPositionsFromValue aufrufen !)
Procedure FreeUserData(Const aCircle: PCircle);
protected
Procedure Paint; override;
Procedure DoOnResize; override; // call OnResize
public
Property Changed: Boolean read fChanged;
Property CircleCount: Integer read FCircleCount;
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 AddCircle(Const aCircle: TCircle): boolean;
Function DelCircle(Const aCircle: PCircle): Boolean;
Procedure Clear;
Procedure DeselectAll; // Iterates through all Childs and "desectes" them but without changing the iterator values !
// Gives the Circle which collides with x,y
// false if none exist
// Returns a pointer to give write access to the circle
Function GetCircleAtPos(x, y: integer; Out aElement: PCircle): Boolean;
(*
* Iterator Pattern to walk all Circles
*)
Function Iterator: PCircle;
Function IterFirst: PCircle;
Function IterNext: PCircle;
Procedure NeedReEvaluation; // Call this routine if you manually changed the value of a already included cirlce
published
Property Color;
Property ShowHint;
Property OnAfterCirclePaint: TOnCirclePaint read fOnAfterCirclePaint write fOnAfterCirclePaint;
Property OnAfterPaint: TNotifyEvent read fOnAfterPaint write fOnAfterPaint;
Property OnBeforeCircletPaint: TOnCirclePaint read fOnBeforeCirclePaint write fOnBeforeCirclePaint;
Property OnClick;
Property OnDeleteCirclesUserData: TOnDeleteCirclesUserData read fOnDeleteCirclesUserData write fOnDeleteCirclesUserData;
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;
Function DefaultCircle(): TCircle;
Implementation
Uses math;
Const
Epsilon = 1E-6;
Function FloatPoint(x, y: Single): TFloatPoint;
Begin
result.x := x;
result.y := y;
End;
Function DefaultCircle(): TCircle;
Begin
result.Caption := '';
result.Color.BrushColor := clWhite;
result.Color.PenColor := clBlack;
result.Color.PenWitdh := 1;
result.Color.FontColor := clBlack;
result.SelectedColor.BrushColor := clWhite;
result.SelectedColor.PenColor := clred;
result.SelectedColor.PenWitdh := 1;
result.SelectedColor.FontColor := clBlack;
result.Value := 1;
result.Center := FloatPoint(0, 0);
result.UserData := Nil;
result.Selected := false;
result.ScaledRadius := -1;
result.ScaledCenter := FloatPoint(0, 0);
result.PrevSibling := Nil;
result.NextSibling := Nil;
result.Next := Nil;
End;
Function clamp(aValue, aMin, aMax: Single): Single;
Begin
If aValue < aMin Then Begin
result := aMin;
End
Else Begin
If aValue > aMax Then Begin
result := aMax;
End
Else Begin
result := aValue;
End;
End;
End;
Operator + (a, b: TFloatPoint): TFloatPoint;
Begin
result.x := a.x + b.x;
result.y := a.y + b.y;
End;
{ TPackedCircleChart }
Constructor TPackedCircleChart.Create(AOwner: TComponent);
Begin
Inherited Create(AOwner);
Color := clWhite;
fChanged := false;
fNeedRecalculation := false;
fOnAfterCirclePaint := Nil;
fOnAfterPaint := Nil;
fOnBeforeCirclePaint := Nil;
fOnDeleteCirclesUserData := Nil;
fOnLoadUserData := Nil;
fOnSaveUserData := Nil;
fFirstCircle := Nil;
Clear;
End;
Destructor TPackedCircleChart.Destroy;
Begin
Clear;
Inherited Destroy;
End;
Procedure TPackedCircleChart.Clear;
Var
i, j: PCircle;
Begin
i := fFirstCircle;
While assigned(i) Do Begin
j := i;
i := i^.Next;
FreeUserData(j);
Dispose(j);
End;
FCircleCount := 0;
fFirstCircle := Nil;
fLastCircle := Nil;
FIterator := Nil;
fNeedRecalculation := false;
Invalidate;
End;
Function TPackedCircleChart.LoadFromStream(Const Stream: TStream): Boolean;
Var
LoadedFileVersion: integer;
j, i: integer;
c: TCircle;
b: UInt8;
Begin
result := false;
Clear;
LoadedFileVersion := 0;
stream.Read(LoadedFileVersion, sizeof(LoadedFileVersion));
If (LoadedFileVersion > CirclePackFileVersoin) Or (LoadedFileVersion <= 0) Then exit; // Da ist offensichtlich was falsch ..
j := 0;
stream.Read(j, SizeOf(j));
For i := 0 To j - 1 Do Begin
c := DefaultCircle();
c.Caption := stream.ReadAnsiString;
stream.Read(c.Color, SizeOf(c.Color));
stream.Read(c.SelectedColor, SizeOf(c.SelectedColor));
stream.Read(c.Value, SizeOf(c.Value));
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;
c.UserData := OnLoadUserData(self, Stream);
End;
AddCircle(c);
End;
result := true;
End;
Function TPackedCircleChart.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 TPackedCircleChart.SaveToStream(Const Stream: TStream): Boolean;
Var
i: PCircle;
b: UInt8;
Begin
result := false;
// Global Settings
stream.Write(CirclePackFileVersoin, sizeof(CirclePackFileVersoin));
stream.Write(FCircleCount, sizeof(FCircleCount));
i := fFirstCircle;
While assigned(i) Do Begin
stream.WriteAnsiString(i^.Caption);
stream.Write(i^.Color, SizeOf(i^.Color));
stream.Write(i^.SelectedColor, SizeOf(i^.SelectedColor));
stream.Write(i^.Value, SizeOf(i^.Value));
If assigned(i^.UserData) Then Begin
b := 1;
stream.Write(b, SizeOf(b));
If Not assigned(OnSaveUserData) Then Begin
Raise exception.create('Error, TCircle with defined userdata, but no OnSaveUserData property set.');
exit;
End;
OnSaveUserData(self, Stream, i^.UserData);
End
Else Begin
b := 0;
stream.Write(b, SizeOf(b));
End;
// TODO: Include more fields..
i := i^.Next;
End;
result := true;
End;
Function TPackedCircleChart.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 TPackedCircleChart.AddCircle(Const aCircle: TCircle): boolean;
Var
p: PCircle;
Begin
result := false;
If assigned(fFirstCircle) Then Begin
new(p);
p^ := aCircle;
p^.Next := Nil;
fLastCircle^.Next := p;
fLastCircle := p;
End
Else Begin
New(fFirstCircle);
fFirstCircle^ := aCircle;
fFirstCircle^.Next := Nil; // Egal wie das Ein / Aushängen in die Lineare liste stellen wir hier sicher sonst knallts ggf.
fLastCircle := fFirstCircle;
End;
inc(FCircleCount);
fNeedRecalculation := true;
result := true;
End;
Function TPackedCircleChart.DelCircle(Const aCircle: PCircle): Boolean;
Var
i: PCircle;
Begin
result := false;
If fFirstCircle = aCircle Then Begin
fFirstCircle := fFirstCircle^.Next;
FreeUserData(aCircle);
Dispose(aCircle);
dec(FCircleCount);
fNeedRecalculation := true;
result := true;
End
Else Begin
i := fFirstCircle;
While assigned(i) And (i^.Next <> aCircle) Do Begin
i := i^.Next;
End;
If assigned(i) And (i^.Next = aCircle) Then Begin
If aCircle = fLastCircle Then Begin
fLastCircle := i;
fLastCircle^.Next := Nil;
FreeUserData(aCircle);
Dispose(aCircle);
dec(FCircleCount);
fNeedRecalculation := true;
result := true;
End
Else Begin
i^.Next := aCircle^.Next; // Aushängen von ACircle
FreeUserData(aCircle);
Dispose(aCircle);
dec(FCircleCount);
fNeedRecalculation := true;
result := true;
End;
End;
End;
End;
Procedure TPackedCircleChart.ReCalculateIfNeeded;
Begin
If fNeedRecalculation Then Begin
CalcCirclesPositionsFromValue;
ScaleCirclesPositionsToClientRect
End;
fNeedRecalculation := false;
End;
Procedure TPackedCircleChart.CalcCirclesPositionsFromValue;
Procedure bound(Const n: PCircle); // Update the Global bounding box of all circles
Begin
fbb_bottomleft.x := MIN(n^.center.x - abs(n^.Value), fbb_bottomleft.x);
fbb_bottomleft.y := MIN(n^.center.y - abs(n^.Value), fbb_bottomleft.y);
fbb_topright.x := MAX(n^.center.x + abs(n^.Value), fbb_topright.x);
fbb_topright.y := MAX(n^.center.y + abs(n^.Value), fbb_topright.y);
End;
Procedure place(Const a, b, c: PCircle); // Place C near b, c
Var
da, db, dx, dy, dc, cos_, theta, x, h: Double;
Begin
da := abs(b^.Value) + abs(c^.Value);
db := abs(a^.Value) + abs(c^.Value);
dx := b^.center.x - a^.center.x;
dy := b^.center.y - a^.center.y;
dc := sqrt(dx * dx + dy * dy);
If (dc > 0.0) Then Begin
cos_ := (db * db + dc * dc - da * da) / (2 * db * dc);
cos_ := clamp(cos_, -1, 1);
theta := ArcCos(cos_);
x := cos_ * db;
h := sin(theta) * db;
dx := dx / dc;
dy := dy / dc;
c^.center.x := a^.center.x + x * dx + h * dy;
c^.center.y := a^.center.y + x * dy - h * dx;
End
Else Begin
c^.center.x := a^.center.x + db;
c^.center.y := a^.center.y;
End;
End;
Function distance(a: PCircle): Double;
Begin
result := sqrt(sqr(a^.Center.x) + sqr(a^.Center.y));
End;
Function intersects(a, b: PCircle): boolean; // True if a intersects b
Var
dx, dy, dr: Double;
Begin
dx := b^.Center.x - a^.Center.x;
dy := b^.Center.y - a^.Center.y;
dr := abs(a^.Value) + abs(b^.Value);
If (sqr(dr) - epsilon > sqr(dx) + sqr(dy)) Then // overlap is bigger than epsilon
result := true
Else
result := false;
End;
Procedure splice(a, b: PCircle);
Begin
a^.NextSibling := b;
b^.PrevSibling := a;
End;
Procedure insert(a, b: PCircle);
Var
c: PCircle;
Begin
c := a^.NextSibling;
a^.NextSibling := b;
b^.PrevSibling := a;
b^.NextSibling := c;
If assigned(c) Then c^.PrevSibling := b;
End;
Var
j, k, a, b, c, n, nearestnode: PCircle;
isect, skip: Boolean;
sj, sk, nearestdist, dist_n: Double;
Begin
(*
* Berechnet alle Positionen aller Kreise aufgrund ihrer "Values" neu
*)
// Reset Bounding Box
fbb_topright := FloatPoint(0, 0);
fbb_bottomleft := FloatPoint(0, 0);
(* Create first circle. *)
a := fFirstCircle;
b := Nil;
c := Nil;
a^.center.x := -1 * abs(a^.Value);
bound(a);
(* Create second circle. *)
If Not assigned(a^.Next) Then Begin
a^.NextSibling := Nil;
a^.PrevSibling := Nil;
exit;
End;
b := a^.Next;
b^.Center.x := abs(b^.Value);
b^.Center.y := 0;
bound(b);
(* Create third circle. *)
If Not assigned(b^.Next) Then Begin
a^.NextSibling := b;
a^.PrevSibling := b;
b^.NextSibling := a;
b^.PrevSibling := a;
exit;
End;
c := b^.Next;
place(a, b, c);
bound(c);
// make initial chain of a <-> b <-> c
a^.NextSibling := c;
a^.PrevSibling := b;
b^.NextSibling := a;
b^.PrevSibling := c;
c^.NextSibling := b;
c^.PrevSibling := a;
If Not assigned(c^.Next) Then Begin
exit;
End;
b := c;
(* add remaining nodes *)
skip := false;
c := c^.Next;
While assigned(c) Do Begin
// Determine the node a in the chain, which is nearest to the center
// The new node c will be placed NextSibling to a (unless overlap occurs)
// NB: This search is only done the first time for each new node, i.e.
// not again after splicing
If (Not skip) Then Begin
n := a;
nearestnode := n;
nearestdist := MaxSingle;
Repeat
dist_n := distance(n);
If (dist_n < nearestdist) Then Begin
nearestdist := dist_n;
nearestnode := n;
End;
n := n^.NextSibling;
Until (n = a);
a := nearestnode;
b := nearestnode^.NextSibling;
End;
(* a corresponds to C_m, and b corresponds to C_n in the paper *)
place(a, b, c);
(* for debugging: initial placement of c that may ovelap *)
isect := false;
j := b^.NextSibling;
k := a^.PrevSibling;
sj := abs(b^.Value);
sk := abs(a^.Value);
//j = b.NextSibling, k = a.previous, sj = b._.r, sk = a._.r;
Repeat
If (sj <= sk) Then Begin
If (intersects(j, c)) Then Begin
splice(a, j);
b := j;
skip := true;
isect := true;
break;
End;
sj := sj + abs(j^.Value);
j := j^.NextSibling;
End
Else Begin
If (intersects(k, c)) Then Begin
splice(k, b);
a := k;
skip := true;
isect := true;
break;
End;
sk := sk + abs(k^.Value);
k := k^.PrevSibling;
End;
Until (j = k^.NextSibling);
If (Not isect) Then Begin
(* Update node chain. *)
insert(a, c);
b := c;
bound(c);
skip := false;
c := c^.Next;
End;
End;
End;
Procedure TPackedCircleChart.ScaleCirclesPositionsToClientRect;
Var
DimX, Dimy: Integer;
maxx, maxy, minx, miny, cx, cy, aScalex, aScaley, aScale: Single;
i: PCircle;
Begin
If Not assigned(fFirstCircle) Then exit;
maxx := fbb_topright.x;
maxy := fbb_topright.y;
minx := fbb_bottomleft.x;
miny := fbb_bottomleft.y;
// The Unscaled dimension
DimX := ceil(maxx - minx);
Dimy := ceil(maxy - miny);
// The unscaled Center
cx := (maxx + minx) / 2;
cy := (maxy + miny) / 2;
// All Radii and positions need now to be scaled in that way, that dimx and dimy will fit width / height
aScalex := Width / (DimX);
aScaley := Height / (Dimy);
aScale := min(ascalex, ascaley); // We use the same scaling in x- / y-Direction
cx := round(cx * aScale);
cy := round(cy * aScale);
i := fFirstCircle;
While assigned(i) Do Begin
// First scale all to (0,0)
i^.ScaledCenter := FloatPoint(aScale * i^.Center.x, aScale * i^.Center.y);
// Second translate to fit into Window
i^.ScaledCenter := i^.ScaledCenter + FloatPoint(width / 2 - cx, height / 2 - cy);
// // Last Scale down the radius
i^.ScaledRadius := aScale * abs(i^.Value);
i := i^.Next;
End;
End;
Procedure TPackedCircleChart.FreeUserData(Const aCircle: PCircle);
Begin
If assigned(aCircle^.UserData) Then Begin
If Assigned(fOnDeleteCirclesUserData) Then Begin
OnDeleteCirclesUserData(self, aCircle^.UserData);
End
Else Begin
Raise exception.create('Error, freeing PCircle with userdata, but no OnDeleteCirclesUserData set.');
End;
End;
End;
Procedure TPackedCircleChart.Paint;
Procedure PlotTextAtPos(Const p: Tpoint; Const aText: String);
Var
Lines: TStringArray;
t, i: Integer;
{$IFDEF Windows}
s: String;
{$ENDIF}
Begin
If pos(LineEnding, aText) <> 0 Then Begin
{$IFDEF Windows}
s := aText;
s := StringReplace(s, #10, '', [rfReplaceAll]);
lines := s.Split([#13]);
{$ELSE}
lines := aText.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(aText) Div 2,
p.Y - Canvas.TextHeight(aText) Div 2,
aText
);
End;
End;
Var
i: PCircle;
Begin
Inherited Paint;
ReCalculateIfNeeded;
// Clear background
canvas.Brush.Color := Color;
canvas.Pen.Color := Color;
canvas.Rectangle(0, 0, Width, Height);
i := fFirstCircle;
While assigned(i) Do Begin
If assigned(fOnBeforeCirclePaint) Then Begin
fOnBeforeCirclePaint(self, i);
End;
// 1. Rendern des Segmentes
If i^.Selected Then Begin
Canvas.Brush.Color := i^.SelectedColor.BrushColor;
canvas.Pen.Color := i^.SelectedColor.PenColor;
canvas.Pen.Width := i^.SelectedColor.PenWitdh;
canvas.Font.Color := i^.SelectedColor.FontColor;
End
Else Begin
Canvas.Brush.Color := i^.Color.BrushColor;
canvas.Pen.Color := i^.Color.PenColor;
canvas.Pen.Width := i^.Color.PenWitdh;
canvas.Font.Color := i^.Color.FontColor;
End;
canvas.Ellipse(
round(i^.ScaledCenter.X - i^.ScaledRadius), round(i^.ScaledCenter.Y - i^.ScaledRadius),
round(i^.ScaledCenter.X + i^.ScaledRadius), round(i^.ScaledCenter.Y + i^.ScaledRadius)
);
PlotTextAtPos(point(round(i^.ScaledCenter.X), round(i^.ScaledCenter.y)), i^.Caption);
i := i^.Next;
End;
If assigned(OnAfterPaint) Then Begin
OnAfterPaint(Self);
End;
End;
Procedure TPackedCircleChart.DoOnResize;
Begin
If fNeedRecalculation Then Begin
ReCalculateIfNeeded();
End
Else Begin
ScaleCirclesPositionsToClientRect;
End;
Invalidate;
Inherited DoOnResize;
End;
Procedure TPackedCircleChart.DeselectAll;
Var
i: PCircle;
Begin
i := fFirstCircle;
While assigned(i) Do Begin
i^.Selected := false;
i := i^.Next;
End;
Invalidate;
End;
Function TPackedCircleChart.GetCircleAtPos(x, y: integer; Out aElement: PCircle
): Boolean;
Var
i: PCircle;
dx, dy: Single;
Begin
ReCalculateIfNeeded();
result := false;
i := fFirstCircle;
While assigned(i) Do Begin
dx := x - i^.ScaledCenter.x;
dy := y - i^.ScaledCenter.y;
If sqr(i^.ScaledRadius) >= sqr(dx) + sqr(dy) Then Begin
result := true;
aElement := i;
exit;
End;
i := i^.Next;
End;
End;
Function TPackedCircleChart.Iterator: PCircle;
Begin
result := FIterator;
End;
Function TPackedCircleChart.IterFirst: PCircle;
Begin
FIterator := fFirstCircle;
result := FIterator;
End;
Function TPackedCircleChart.IterNext: PCircle;
Begin
If assigned(FIterator) Then Begin
FIterator := FIterator^.Next;
result := FIterator;
End
Else Begin
result := Nil;
End;
End;
Procedure TPackedCircleChart.NeedReEvaluation;
Begin
fNeedRecalculation := true;
Invalidate;
End;
End.