-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathDataLabels.php
1613 lines (1478 loc) · 49 KB
/
DataLabels.php
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
<?php
/**
* Copyright (C) 2015-2023 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <graham@goat1000.com>
*/
namespace Goat1000\SVGGraph;
class DataLabels {
protected $graph;
protected $have_filters = false;
private $labels = [];
private $max_values = [];
private $min_values = [];
private $start_indices = [];
private $end_indices = [];
private $peak_indices = [];
private $trough_indices = [];
private $directions = [];
private $last = [];
private $max_labels = 1000;
private $coords = null;
private $semantic_classes;
private $units_before;
private $units;
private $filter;
private $same_size;
private $callback;
/**
* Details of each label type
*/
private $types_info = [
'box' => ['shape' => 'boxLabel', 'tail' => false, 'pad' => true],
'bubble' => ['shape' => 'bubbleLabel', 'tail' => true, 'pad' => true],
'circle' => ['shape' => 'circleLabel', 'tail' => false, 'pad' => true],
'line' => ['shape' => 'lineLabel', 'tail' => true, 'pad' => false],
'line2' => ['shape' => 'lineLabel2', 'tail' => true, 'pad' => true],
'linebox' => ['shape' => 'boxLineLabel', 'tail' => true, 'pad' => true],
'linecircle' => ['shape' => 'circleLineLabel', 'tail' => true, 'pad' => true],
'linesquare' => ['shape' => 'squareLineLabel', 'tail' => true, 'pad' => true],
'plain' => ['shape' => null, 'tail' => false, 'pad' => false],
'square' => ['shape' => 'squareLabel', 'tail' => false, 'pad' => true],
];
/**
* Mapping between label style array members and options
*/
protected $style_map = [
'type' => 'data_label_type',
'font' => 'data_label_font',
'font_size' => 'data_label_font_size',
'font_adjust' => 'data_label_font_adjust',
'font_weight' => 'data_label_font_weight',
'colour' => 'data_label_colour',
'altcolour' => 'data_label_colour_outside',
'back_colour' => 'data_label_back_colour',
'back_altcolour' => 'data_label_back_colour_outside',
'space' => 'data_label_space',
'angle' => 'data_label_angle',
'pad_x' => 'data_label_padding_x',
'pad_y' => 'data_label_padding_y',
'round' => 'data_label_round',
'stroke' => 'data_label_outline_colour',
'stroke_width' => 'data_label_outline_thickness',
'fill' => 'data_label_fill',
'tail_width' => 'data_label_tail_width',
'tail_length' => 'data_label_tail_length',
'tail_end' => 'data_label_tail_end',
'tail_end_angle' => 'data_label_tail_end_angle',
'tail_end_width' => 'data_label_tail_end_width',
'shadow_opacity' => 'data_label_shadow_opacity',
'opacity' => 'data_label_opacity',
'line_spacing' => 'data_label_line_spacing',
'align' => 'data_label_align',
];
/**
* Options that are colours, so need translation
*/
protected $colour_options = [
'colour', 'altcolour', 'back_colour', 'back_altcolour', 'stroke', 'fill',
];
function __construct(&$graph)
{
$this->graph =& $graph;
$this->filter = $graph->getOption('data_label_filter');
$this->have_filters = (!empty($this->filter) && $this->filter !== 'all');
$max_labels = $graph->getOption('data_label_max_count');
if($max_labels >= 1)
$this->max_labels = (int)$max_labels;
$this->semantic_classes = $graph->getOption('semantic_classes');
$this->units_before = $graph->getOption('units_before_label');
$this->units = $graph->getOption('units_label');
$this->same_size = $graph->getOption('data_label_same_size');
$this->callback = $graph->getOption('data_label_callback');
}
/**
* Adds a label to the list
*/
public function addLabel($dataset, $index, &$item, $x, $y, $w, $h,
$id = null, $content = null, $fade_in = null, $click = null)
{
if(!isset($this->labels[$dataset]))
$this->labels[$dataset] = [];
$this->labels[$dataset][$index] = [
'item' => $item, 'id' => $id, 'content' => $content,
'x' => $x, 'y' => $y, 'width' => $w, 'height' => $h,
'fade' => $fade_in, 'click' => $click,
];
if($this->have_filters)
$this->setupFilters($dataset, $index, $item->value);
}
/**
* Adds a content (non-data) label
*/
public function addContentLabel($dataset, $index, $x, $y, $w, $h, $content)
{
if(!isset($this->labels[$dataset]))
$this->labels[$dataset] = [];
$this->labels[$dataset][$index] = [
'item' => null, 'id' => null, 'content' => $content,
'x' => $x, 'y' => $y, 'width' => $w, 'height' => $h,
'fade' => null, 'click' => null,
];
}
/**
* Adds a user-defined label from a label option
*/
public function addUserLabel($label_array)
{
if(!isset($this->labels['_user']))
$this->labels['_user'] = [];
if(!isset($label_array[0]) || !isset($label_array[1]) || !isset($label_array[2]))
throw new \Exception('Malformed label option - required fields missing');
$x = $label_array[0];
$y = $label_array[1];
$content = $label_array[2];
$w = 0;
$h = 0;
// merge the options with required fields
$this->labels['_user'][] = array_merge($label_array, [
'item' => null, 'id' => null, 'content' => $content,
'x' => $x, 'y' => $y, 'width' => $w, 'height' => $h,
'fade' => null, 'click' => null,
]);
}
/**
* Returns label details
*/
public function getLabel($dataset, $index)
{
if(isset($this->labels[$dataset][$index]))
return $this->labels[$dataset][$index];
return null;
}
/**
* Updates filter information from label
*/
protected function setupFilters($dataset, $index, $value)
{
// set up filtering info
if(!isset($this->max_values[$dataset]) ||
$this->max_values[$dataset] < $value)
$this->max_values[$dataset] = $value;
if(!isset($this->min_values[$dataset]) ||
$this->min_values[$dataset] > $value)
$this->min_values[$dataset] = $value;
if(!isset($this->start_indices[$dataset]) ||
$this->start_indices[$dataset] > $index)
$this->start_indices[$dataset] = $index;
if(!isset($this->end_indices[$dataset]) ||
$this->end_indices[$dataset] < $index)
$this->end_indices[$dataset] = $index;
// peaks and troughs are a bit more complicated
if(!isset($this->last[$dataset])) {
$this->last[$dataset] = [$index, $value];
$this->directions[$dataset] = null;
$this->peak_indices[$dataset] = [];
$this->trough_indices[$dataset] = [];
return;
}
if($this->last[$dataset][1] != $value) {
$last = $this->last[$dataset];
$diff = $value - $last[1];
$direction = ($diff > 0);
if($this->directions[$dataset] !== null &&
$direction !== $this->directions[$dataset]) {
if($diff > 0)
$this->trough_indices[$dataset][] = $last[0];
else
$this->peak_indices[$dataset][] = $last[0];
}
$this->last[$dataset] = [$index, $value];
$this->directions[$dataset] = $direction;
}
}
/**
* Load user-defined labels
*/
public function load(&$settings)
{
if(!is_array($settings['label']) || !isset($settings['label'][0]))
throw new \Exception('Malformed label option');
if(!is_array($settings['label'][0])) {
$this->addUserLabel($settings['label']);
$this->coords = new Coords($this->graph);
return;
}
$count = 0;
foreach($settings['label'] as $label) {
$this->addUserLabel($label);
++$count;
}
if($count)
$this->coords = new Coords($this->graph);
}
/**
* Returns all the labels as a string
*/
public function getLabels()
{
$filter_count = is_array($this->filter) ? count($this->filter) : 1;
$label_list = [];
foreach($this->labels as $dataset => $label_set) {
$set_filter = 'all';
if(is_numeric($dataset)) {
$set_filter = is_array($this->filter) ?
$this->filter[$dataset % $filter_count] : $this->filter;
}
$count = 0;
foreach($label_set as $i => $label) {
if($this->filter($set_filter, $dataset, $label, $i)) {
$content = $this->getLabelText($dataset, $label);
if($content !== null && $content != '') {
list($w, $h) = $this->measureLabel($content, $dataset, $i, $label);
$label_list[] = compact('content', 'w', 'h', 'dataset', 'i', 'label');
if(++$count >= $this->max_labels)
break;
}
}
}
}
$this->setLabelSizes($label_list);
$labels = '';
foreach($label_list as $l) {
$labels .= $this->drawLabel($l['content'], $l['w'], $l['h'],
$l['dataset'], $l['i'], $l['label']);
}
if($labels != '') {
$group = [];
if($this->semantic_classes)
$group['class'] = 'data-labels';
$labels = $this->graph->element('g', $group, null, $labels);
}
return $labels;
}
/**
* Adjusts the label sizes
*/
protected function setLabelSizes(&$labels)
{
if(!$this->same_size)
return;
// globally equal sizes
if(!is_array($this->same_size)) {
$max_w = 0;
$max_h = 0;
foreach($labels as $l) {
if(is_numeric($l['dataset'])) {
if($l['w'] > $max_w)
$max_w = $l['w'];
if($l['h'] > $max_h)
$max_h = $l['h'];
}
}
foreach($labels as $k => $l) {
if(is_numeric($l['dataset'])) {
$labels[$k]['w'] = $max_w;
$labels[$k]['h'] = $max_h;
}
}
return;
}
// per-dataset maxima (20 datasets should be enough for anybody)
$max_w = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
$max_h = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
foreach($labels as $l) {
$d = $l['dataset'];
if(is_numeric($d)) {
if($l['w'] > $max_w[$d])
$max_w[$d] = $l['w'];
if($l['h'] > $max_h[$d])
$max_h[$d] = $l['h'];
}
}
foreach($labels as $k => $l) {
$d = $l['dataset'];
if(is_numeric($d) &&
$this->graph->getOption(['data_label_same_size', $d])) {
$labels[$k]['w'] = $max_w[$d];
$labels[$k]['h'] = $max_h[$d];
}
}
}
/**
* Returns the text for a label
*/
protected function getLabelText($dataset, &$gobject)
{
if($gobject['item'] === null && empty($gobject['content']))
return '';
if($gobject['item'] === null)
return (string)$gobject['content'];
if(is_callable($this->callback)) {
$content = call_user_func($this->callback, $dataset,
$gobject['item']->key, $gobject['item']->value);
return $content === null ? '' : $content;
}
$content = $gobject['item']->label;
if($content !== null)
return $content;
if($gobject['content'] !== null)
return $gobject['content'];
$n = new Number($gobject['item']->value, $this->units, $this->units_before);
return $n->format();
}
/**
* Returns the style details for a label
*/
protected function getStyle($dataset, $index, &$gobject)
{
// global styles filled in by graph class
$style = $this->graph->dataLabelStyle($dataset, $index, $gobject['item']);
// structured styles and user defined label styles
if($gobject['item'] !== null)
$this->itemStyles($style, $gobject['item'], $index, $dataset);
elseif($dataset === '_user')
$this->userStyles($style, $gobject);
// deal with fill/fillColour
foreach($this->colour_options as $s) {
if(isset($style[$s]) && is_string($style[$s]) &&
strpos($style[$s], 'fill') !== false) {
$cg = new ColourGroup($this->graph, $gobject['item'], $index, $dataset,
$style[$s], null, null, true);
$style[$s] = $cg->stroke();
}
}
return $style;
}
/**
* Returns the font size and line spacing for a style as an array
*/
protected function getFontSize($style)
{
$font_size = $line_spacing = max(4, Number::units($style['font_size']));
if($style['line_spacing'] !== null)
$line_spacing = max(1, Number::units($style['line_spacing']));
return [$font_size, $line_spacing];
}
/**
* Returns size of a label as array (w, h)
*/
protected function measureLabel($content, $dataset, $index, &$gobject)
{
$style = $this->getStyle($dataset, $index, $gobject);
// get size of text
list($font_size, $line_spacing) = $this->getFontSize($style);
$svg_text = new Text($this->graph, $style['font'], $style['font_adjust']);
list($w, $h) = $svg_text->measure($content, $font_size, $style['angle'],
$line_spacing);
// if this label type uses padding, add it in
if($this->getTypeInfo($style['type'], 'pad')) {
$w += $style['pad_x'] * 2;
$h += $style['pad_y'] * 2;
}
return [$w, $h];
}
/**
* Returns some or all info about a type
*/
protected function getTypeInfo($type, $field = null)
{
if(!isset($this->types_info[$type]))
$type = 'plain';
$type_info = $this->types_info[$type];
return $field === null ? $type_info : $type_info[$field];
}
/**
* Returns the foreground and background colours, dependent on relative
* position
*/
protected function getColours($hpos, $vpos, $style)
{
// if the position is outside, use the alternative colours
$colour = new Colour($this->graph, $style['colour']);
$back_colour = new Colour($this->graph, $style['back_colour']);
if(strpos($hpos . $vpos, 'o') !== false) {
$alt = new Colour($this->graph, $style['altcolour']);
$back_alt = new Colour($this->graph, $style['back_altcolour']);
if(!$alt->isNone())
$colour = $alt;
if(!$back_alt->isNone())
$back_colour = $back_alt;
}
return [$colour, $back_colour];
}
/**
* Draws a label
*/
protected function drawLabel($content, $label_w, $label_h, $dataset, $index,
&$gobject)
{
$style = $this->getStyle($dataset, $index, $gobject);
$style['target'] = [$gobject['x'], $gobject['y']];
$space = (float)$style['space'];
$pos = null;
if($dataset === '_user') {
// user label, so convert coordinates
$pos = isset($gobject['position']) ? $gobject['position'] : 'above';
$xy = $this->coords->transformCoords($gobject['x'], $gobject['y']);
$gobject['x'] = $xy[0];
$gobject['y'] = $xy[1];
$style['target'] = [$gobject['x'], $gobject['y']];
} else {
// try to get position from item
if($gobject['item'] !== null)
$pos = $gobject['item']->data_label_position;
// find out from graph class where this label should go
if($pos === null) {
$label_wp = $label_w + $space * 2;
$label_hp = $label_h + $space * 2;
// get the label position and the target for tail
list($pos, $target) = $this->graph->dataLabelPosition($dataset,
$index, $gobject['item'], $gobject['x'], $gobject['y'],
$gobject['width'], $gobject['height'], $label_wp, $label_hp);
$style['target'] = $target;
}
}
// convert position string to an actual location
list($x, $y, $anchor, $hpos, $vpos) = Graph::relativePosition($pos,
$gobject['y'], $gobject['x'],
$gobject['y'] + $gobject['height'], $gobject['x'] + $gobject['width'],
$label_w, $label_h, $space, true);
list($colour, $back_colour) = $this->getColours($hpos, $vpos, $style);
list($font_size, $line_spacing) = $this->getFontSize($style);
$text = [
'font-family' => $style['font'],
'font-size' => $font_size,
'fill' => $colour,
];
$label_markup = '';
$label_pad_x = $label_pad_y = 0;
$type_info = $this->getTypeInfo($style['type']);
if($type_info['pad']) {
$label_pad_x = $style['pad_x'];
$label_pad_y = $style['pad_y'];
}
// need text size without padding, rotation, etc.
$svg_text = new Text($this->graph, $style['font'], $style['font_adjust']);
list($tbw, $tbh) = $svg_text->measure($content, $font_size, 0, $line_spacing);
$text_baseline = $svg_text->baseline($font_size);
// allow overriding text alignment
$align_map = ['left' => 'start', 'centre' => 'middle', 'right' => 'end'];
if($style['align'] !== null && isset($align_map[$style['align']])) {
$anchor_new = $align_map[$style['align']];
if($anchor_new != $anchor) {
// adjust label position for new anchor
$pos_remap = [
'middle' => ['start' => -0.5, 'end' => 0.5],
'start' => ['middle' => 0.5, 'end' => 1.0],
'end' => ['start' => -1.0, 'middle' => -0.5],
];
$x += ($label_w * $pos_remap[$anchor][$anchor_new]);
$anchor = $anchor_new;
}
}
$text['y'] = $y + ($label_h - $tbh) / 2 + $text_baseline;
if($style['angle'] != 0) {
if($anchor == 'middle') {
$text['x'] = $x;
} elseif($anchor == 'start') {
$text['x'] = $x + ($label_w - $tbw) / 2;
} else {
$text['x'] = $x - ($label_w - $tbw) / 2;
}
} else {
if($anchor == 'start') {
$text['x'] = $x + $label_pad_x;
} elseif($anchor == 'end') {
$text['x'] = $x - $label_pad_x;
} else {
$text['x'] = $x;
}
}
// make x right for bounding box
if($anchor == 'middle') {
$x -= $label_w / 2;
} elseif($anchor == 'end') {
$x -= $label_w;
}
if($style['angle'] != 0) {
// rotate text around centre of box
$rx = $x + $label_w / 2;
$ry = $y + $label_h / 2;
$xform = new Transform;
$xform->rotate($style['angle'], $rx, $ry);
$text['transform'] = $xform;
}
if($anchor != 'start')
$text['text-anchor'] = $anchor;
if(!empty($style['font_weight']) && $style['font_weight'] != 'normal')
$text['font-weight'] = $style['font_weight'];
$surround = [];
$element = null;
$shape_func = null;
$need_tail = false;
if($type_info['shape']) {
if($type_info['tail']) {
$style['tail_direction'] = $this->graph->dataLabelTailDirection($dataset,
$index, $hpos, $vpos);
}
// make the shape
$element = $this->{$type_info['shape']}($x, $y, $label_w, $label_h,
$style, $surround);
if($element) {
$surround['stroke'] = new Colour($this->graph, $style['stroke']);
if($style['stroke_width'] != 1)
$surround['stroke-width'] = (float)$style['stroke_width'];
// add shadow if not completely transparent
if($style['shadow_opacity'] > 0) {
$shadow = $surround;
$offset = 2 + floor($style['stroke_width'] / 2);
$xform = new Transform;
$xform->translate($offset, $offset);
$shadow['transform'] = $xform;
$shadow['fill'] = $shadow['stroke'] = '#000';
$shadow['opacity'] = $style['shadow_opacity'];
$label_markup .= $this->graph->element($element, $shadow);
}
$label_markup .= $this->graph->element($element, $surround);
}
}
//$back_colour = new Colour($this, $back_colour);
if(!$back_colour->isNone()) {
$outline = [
'stroke-width' => '3px',
'stroke' => $back_colour,
'stroke-linejoin' => 'round',
];
$t1 = array_merge($outline, $text);
$label_markup .= $svg_text->text($content, $line_spacing, $t1);
}
$label_markup .= $svg_text->text($content, $line_spacing, $text);
$group = [];
if(isset($gobject['id']) && $gobject['id'] !== null)
$group['id'] = $gobject['id'];
// opacity is required when set or using click-show-hide
$opacity = max(0, min(1, $style['opacity']));
if($opacity < 1 || $gobject['click'] == 'show')
$group['opacity'] = $opacity;
elseif($gobject['click'] == 'hide' || $gobject['fade'])
$group['opacity'] = 0;
$label_markup = $this->graph->element('g', $group, null, $label_markup);
return $label_markup;
}
/**
* Returns the mapping between style members and option names
*/
public function getStyleMap()
{
return $this->style_map;
}
/**
* Individual label styles from the structured data item
*/
protected function itemStyles(&$style, &$item, $index, $dataset)
{
// overwrite any style options that the item has set
$v = $item->data_label_padding;
if($v !== null)
$style['pad_x'] = $style['pad_y'] = $v;
foreach($this->style_map as $s => $k) {
$v = $item->data($k);
if($v !== null)
$style[$s] = $v;
}
}
/**
* Styles from the label option
*/
protected function userStyles(&$style, &$label_array)
{
// pad_x and pad_y will override single padding option
if(isset($label_array['padding']))
$style['pad_x'] = $style['pad_y'] = $label_array['padding'];
foreach($this->style_map as $s => $k) {
// remove the 'data_label_' part
$o = substr($k, 11);
if(isset($label_array[$o]))
$style[$s] = $label_array[$o];
}
}
/**
* Returns TRUE if the label should be shown
*/
protected function filter($filter, $dataset, &$label, $index)
{
// non-numeric datasets are for additional labels,
// empty filter does nothing
if(!is_numeric($dataset) || empty($filter))
return true;
$item =& $label['item'];
// if the item has a show_label member, use it
$struct_show = $item->show_label;
if($struct_show !== null)
return $struct_show;
// if 'all' is in the list, others don't matter
$filters = explode(' ', $filter);
if(in_array('all', $filters, true))
return true;
// an array of closures for filter tests
$tests = [
'start' => function() use ($index, $dataset) {
return $index == $this->start_indices[$dataset]; },
'end' => function() use ($index, $dataset) {
return $index == $this->end_indices[$dataset]; },
'max' => function() use (&$item, $dataset) {
return $item->value == $this->max_values[$dataset]; },
'min' => function() use (&$item, $dataset) {
return $item->value == $this->min_values[$dataset]; },
'peaks' => function() use ($index, $dataset) {
return in_array($index, $this->peak_indices[$dataset], true); },
'troughs' => function() use ($index, $dataset) {
return in_array($index, $this->trough_indices[$dataset], true); },
'nonzero' => function() use (&$item) { return $item->value != 0; },
'none' => function() use (&$item) {
return $item->label !== null && $item->label !== ''; },
];
foreach($filters as $f) {
if(isset($tests[$f]) && $tests[$f]())
return true;
// integer step
if(is_numeric($f) && $index % (int)$f == 0) {
return true;
} else {
// step with offset
$parts = explode('+', $f);
if(count($parts) == 2 &&
is_numeric($parts[0]) && is_numeric($parts[1]) &&
$parts[0] > 1 && $parts[1] < $parts[0] &&
$index % (int)$parts[0] == $parts[1])
return true;
}
}
// default is to show nothing
return false;
}
/**
* Straight line label style
*/
protected function lineLabel($x, $y, $w, $h, &$style, &$surround)
{
$w2 = $w / 2;
$h2 = $h / 2;
$a = $style['tail_direction'] * M_PI / 180;
if($style['round']) {
$bbradius = sqrt($w2 * $w2 + $h2 * $h2);
$w2a = $bbradius * cos($a);
$h2a = $bbradius * sin($a);
} else {
// start at edge of text bounding box
$w2a = $w2;
$h2a = $w2 * tan($a);
if(abs($h2a) > $h2) {
$h2a = $h2;
$w2a = $h2 / tan($a);
}
}
if(($a < M_PI && $h2a < 0) || ($a > M_PI && $h2a > 0)) {
$h2a = -$h2a;
$w2a = -$w2a;
}
$x1 = $x + $w2 + $w2a;
$y1 = $y + $h2 + $h2a;
if($style['tail_length'] == 'auto') {
list($x2, $y2) = $style['target'];
// check line is outside bbox
if($style['round']) {
$llen = sqrt(pow($x2 - $x - $w2, 2) + pow($y2 - $y - $h2, 2));
if($llen < $bbradius)
return '';
} else {
if($x2 > $x && $x2 < $x + $w && $y2 > $y && $y2 < $y + $h)
return '';
}
} else {
// make sure line is long enough to not look like part of text
list($font_size) = $this->getFontSize($style);
$llen = max($font_size, $style['tail_length']);
$x2 = $x1 + ($llen * cos($a));
$y2 = $y1 + ($llen * sin($a));
}
$surround['d'] = new PathData('M', $x1, $y1, 'L', $x2, $y2);
return 'path';
}
/**
* Simple box label style
*/
protected function boxLabel($x, $y, $w, $h, &$style, &$surround)
{
$surround['x'] = $x;
$surround['y'] = $y;
$surround['width'] = $w;
$surround['height'] = $h;
if($style['round'])
$surround['rx'] = $surround['ry'] = min((float)$style['round'],
$h / 2, $w / 2);
$surround['fill'] = new Colour($this->graph, $style['fill']);
return 'rect';
}
/**
* Speech bubble label style
*/
protected function bubbleLabel($x, $y, $w, $h, &$style, &$surround)
{
// can't be more round than this!
$round = min((float)$style['round'], $h / 3, $w / 3);
$drop = max(2, (float)$style['tail_length']);
$spread = min(max(2, (float)$style['tail_width']), $w - $round * 2);
$vert = $h - $round * 2;
$horz = $w - $round * 2;
$start = new PathData('M', ($x + $w - $round), $y);
$t = new PathData('z');
$r = new PathData('v', $vert);
$b = new PathData('h', -$horz);
$l = new PathData('v', -$vert);
$tr = new PathData;
$br = new PathData;
$bl = new PathData;
$tl = new PathData;
if($round) {
$tr->add('a', $round, $round, 90, 0, 1, $round, $round);
$br->add('a', $round, $round, 90, 0, 1, -$round, $round);
$bl->add('a', $round, $round, 90, 0, 1, -$round, -$round);
$tl->add('a', $round, $round, 90, 0, 1, $round, -$round);
}
$direction = floor(($style['tail_direction'] + 22.5) * 8 / 360) % 8;
$ddrop = 0.707 * $drop; // cos 45
$p1 = $ddrop + $spread * 0.707;
$s2 = $spread / 2;
$vcropped = $vert - $spread * 0.707 + $round;
$hcropped = $horz - $spread * 0.707 + $round;
switch($direction) {
case 0 :
$bside = $h / 2 - $s2 - $round;
$r = new PathData('v', $bside, 'l', $drop, $s2, 'l', -$drop, $s2, 'v', $bside);
break;
case 1 :
$r = new PathData('v', $vcropped);
$br = new PathData('l', $ddrop, $p1, 'l', -$p1, -$ddrop);
$b = new PathData('h', -$hcropped);
break;
case 2 :
$bside = $w / 2 - $s2 - $round;
$b = new PathData('h', -$bside, 'l', -$s2, $drop, 'l', -$s2, -$drop, 'h', -$bside);
break;
case 3 :
$l = new PathData('v', -$vcropped);
$bl = new PathData('l', -$p1, $ddrop, 'l', $ddrop, -$p1);
$b = new PathData('h', -$hcropped);
break;
case 4 :
$bside = $h / 2 - $s2 - $round;
$l = new PathData('v', -$bside, 'l', -$drop, -$s2, 'l', $drop, -$s2, 'v', -$bside);
break;
case 5 :
$l = new PathData('v', -$vcropped);
$tl = new PathData('l', -$ddrop, -$p1, 'l', $p1, $ddrop);
break;
case 6 :
$bside = $w / 2 - $s2 - $round;
$t = new PathData('h', $bside, 'l', $s2, -$drop, 'l', $s2, $drop, 'z');
break;
case 7 :
$start = new PathData('M', ($x + $hcropped + $round), $y);
$r = new PathData('v', $vcropped);
$tr = new PathData('l', $p1, -$ddrop, 'l', -$ddrop, $p1);
break;
}
$start->add($tr);
$start->add($r);
$start->add($br);
$start->add($b);
$start->add($bl);
$start->add($l);
$start->add($tl);
$start->add($t);
$surround['d'] = $start;
$surround['fill'] = new Colour($this->graph, $style['fill']);
return 'path';
}
/**
* Returns the cx, cy and radius for a round label
*/
protected function calcRoundLabel($x, $y, $w, $h)
{
$w2 = $w / 2;
$h2 = $h / 2;
$r = sqrt($w2 * $w2 + $h2 * $h2);
return [$x + $w2, $y + $h2, $r];
}
/**
* Circular label style
*/
protected function circleLabel($x, $y, $w, $h, &$style, &$surround)
{
$params = $this->calcRoundLabel($x, $y, $w, $h);
$surround['cx'] = $params[0];
$surround['cy'] = $params[1];
$surround['r'] = $params[2];
$surround['fill'] = new Colour($this->graph, $style['fill']);
return 'circle';
}
/**
* Returns the tail target coordinates, angle and length for a box label,
* or NULL if the tail would end inside the label.
* Return value is array(array($x, $y), $angle, $length)
*/
protected function getBoxTailTarget(&$style, $x1, $y1, $x2, $y2)
{
$target = null;
$length = $style['tail_length'];
$cx = ($x1 + $x2) / 2;
$cy = ($y1 + $y2) / 2;
$w2 = $cx - $x1;
$h2 = $cy - $y1;
if($length == 'auto') {
// just use the defined target
$target = $style['target'];
// check that target is outside the label
$tx = $target[0]; $ty = $target[1];
if($tx >= $x1 && $tx <= $x2 && $ty >= $y1 && $ty <= $y2)
return null;
if($tx > $x2) {
$dx = $tx - $x2;
} elseif($tx < $x1) {
$dx = $x1 - $tx;
} else {
$dx = abs($tx - $cx);
}
if($ty > $y2) {
$dy = $ty - $y2;
} elseif($ty < $y1) {
$dy = $y1 - $ty;
} else {
$dy = abs($ty - $cy);
}
$length = sqrt($dx * $dx + $dy * $dy);
$angle = atan2($ty - $cy, $tx - $cx);
} else {
// target is radius + tail length away
if($length <= 0)
return null;
$angle = $style['tail_direction'] * M_PI / 180;
$lx = $length * cos($angle);
$ly = $length * sin($angle);
// compare tangent with box ratio
if(abs(tan($angle)) > $h2 / $w2) {
// out top or bottom
$target = [
$cx + $lx,
$ly > 0 ? $y2 + $ly : $y1 + $ly
];
} else {
// out left or right
$target = [
$lx > 0 ? $x2 + $lx : $x1 + $lx,
$cy + $ly
];
}
}
return [$target, $angle, $length];
}
/**
* Returns the tail target coordinates, angle and length for a round label,
* or NULL if the tail would end inside the label.
* Return value is array(array($x, $y), $angle, $length)
*/