-
Notifications
You must be signed in to change notification settings - Fork 31
/
BarGraphTrait.php
301 lines (263 loc) · 8.32 KB
/
BarGraphTrait.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
<?php
/**
* Copyright (C) 2019-2022 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 <[email protected]>
*/
namespace Goat1000\SVGGraph;
trait BarGraphTrait {
// these are filled in by barSetup()
protected $calculated_bar_width;
protected $calculated_bar_space;
/**
* Draws the graph
*/
protected function draw()
{
$this->setup();
$body = $this->grid();
$bars = $this->drawBars();
$bar_group = $this->barGroup();
if(!empty($bar_group))
$bars = $this->element('g', $bar_group, null, $bars);
$body .= $this->underShapes();
$body .= $bars;
$body .= $this->overShapes();
$body .= $this->axes();
return $body;
}
/**
* Draws the bars
*/
protected function drawBars()
{
$dataset = $this->getOption(['dataset', 0], 0);
$this->barSetup();
$bars = '';
foreach($this->values[$dataset] as $bnum => $item) {
$this->setBarLegendEntry($dataset, $bnum, $item);
$bars .= $this->drawBar($item, $bnum, 0, null, $dataset);
}
return $bars;
}
/**
* Returns the width of a bar
*/
protected function barWidth()
{
$bw = $this->getOption('bar_width');
if(is_numeric($bw) && $bw >= 1)
return $bw;
$unit_w = $this->x_axes[$this->main_x_axis]->unit();
$bw = $unit_w - $this->getOption('bar_space');
return max(1, $bw, $this->getOption('bar_width_min'));
}
/**
* Initialize bars
*/
protected function barSetup()
{
$width = $this->barWidth();
$space = $this->barSpace($width);
$this->setBarWidth($width, $space);
}
/**
* Sets up the width of the bar and the spacing
*/
protected function setBarWidth($width, $space)
{
$this->calculated_bar_width = $width;
$this->calculated_bar_space = $this->getOption('datetime_keys') ? 0 : $space;
}
/**
* Returns an array of attributes for whole group of bars
*/
protected function barGroup()
{
$group = [];
if($this->getOption('semantic_classes'))
$group['class'] = 'series';
$shadow_id = $this->defs->getShadow();
if($shadow_id !== null)
$group['filter'] = 'url(#' . $shadow_id . ')';
return $group;
}
/**
* Sets the legend entry for a bar
*/
protected function setBarLegendEntry($dataset, $index, DataItem $item)
{
$bar = ['fill' => $this->getColour($item, $index, $dataset)];
$this->setStroke($bar, $item, $index, $dataset);
$this->setLegendEntry($dataset, $index, $item, $bar);
}
/**
* Returns the SVG code for a bar
*/
protected function drawBar(DataItem $item, $index, $start = 0, $axis = null,
$dataset = 0, $options = [])
{
if($item->value === null)
return '';
$bar = $this->barDimensions($item, $index, $start, $axis, $dataset);
if(empty($bar))
return '';
// if the bar is empty and no legend or labels to show give up now
if((string)$bar['height'] == '0' && !$this->getOption('legend_show_empty') &&
!$this->getOption('show_data_labels'))
return '';
$this->setStroke($bar, $item, $index, $dataset);
$bar['fill'] = $this->getColour($item, $index, $dataset);
if($this->getOption('semantic_classes'))
$bar['class'] = 'series' . $dataset;
$label_shown = $this->addDataLabel($dataset, $index, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->getOption('show_tooltips'))
$this->setTooltip($bar, $item, $dataset, $item->key, $item->value,
$label_shown);
if($this->getOption('show_context_menu'))
$this->setContextMenu($bar, $dataset, $item, $label_shown);
$round = max($this->getItemOption('bar_round', $dataset, $item), 0);
if($round > 0) {
// don't allow the round corner to be more than 1/2 bar width or height
$bar['rx'] = $bar['ry'] = min($round, $bar['width'] / 2, $bar['height'] / 2);
}
$bar_part = $this->element('rect', $bar);
return $this->getLink($item, $item->key, $bar_part);
}
/**
* Returns the space before a bar
*/
protected function barSpace($bar_width)
{
return max(0, ($this->x_axes[$this->main_x_axis]->unit() - $bar_width) / 2);
}
/**
* Returns an array with x, y, width and height set
*/
protected function barDimensions($item, $index, $start, $axis, $dataset)
{
$bar = [];
$bar_x = $this->barX($item, $index, $bar, $axis, $dataset);
if($bar_x === null)
return [];
$y_pos = $this->barY($item->value, $bar, $start, $axis);
if($y_pos === null)
return [];
return $bar;
}
/**
* Fills in the x and width of bar
*/
protected function barX($item, $index, &$bar, $axis, $dataset)
{
$bar_x = $this->gridPosition($item, $index);
if($bar_x === null)
return null;
$bar['x'] = $bar_x + $this->calculated_bar_space;
$bar['width'] = $this->calculated_bar_width;
return $bar_x;
}
/**
* Fills in the y and height of a bar
* @return number unclamped bar position
*/
protected function barY($value, &$bar, $start = null, $axis = null)
{
if($start)
$value += $start;
$startpos = $start === null ? $this->originY($axis) :
$this->gridY($start, $axis);
if($startpos === null)
$startpos = $this->originY($axis);
$pos = $this->gridY($value, $axis);
if($pos === null) {
$bar['height'] = 0;
} else {
$l1 = $this->clampVertical($startpos);
$l2 = $this->clampVertical($pos);
$bar['y'] = min($l1, $l2);
$bar['height'] = abs($l1-$l2);
}
return $pos;
}
/**
* Override to check minimum space requirement
*/
protected function addDataLabel($dataset, $index, &$element, &$item,
$x, $y, $w, $h, $content = null, $duplicate = true)
{
if($h < $this->getOption(['data_label_min_space', $dataset]))
return false;
return parent::addDataLabel($dataset, $index, $element, $item, $x, $y,
$w, $h, $content, $duplicate);
}
/**
* Returns the position for a data label
*/
public function dataLabelPosition($dataset, $index, &$item, $x, $y, $w, $h,
$label_w, $label_h)
{
list($pos,$end) = parent::dataLabelPosition($dataset, $index, $item,
$x, $y, $w, $h, $label_w, $label_h);
$bpos = $this->getOption('bar_label_position');
if(!empty($bpos))
$pos = $bpos;
if($label_h > $h && Graph::isPositionInside($pos))
$pos = str_replace(['top','middle','bottom'], 'outside top inside ', $pos);
// flip top/bottom for negative values
if($item !== null && $item->value < 0) {
if(strpos($pos, 'top') !== false)
$pos = str_replace('top','bottom', $pos);
elseif(strpos($pos, 'above') !== false)
$pos = str_replace('above','below', $pos);
elseif(strpos($pos, 'below') !== false)
$pos = str_replace('below','above', $pos);
elseif(strpos($pos, 'bottom') !== false)
$pos = str_replace('bottom','top', $pos);
}
return [$pos, $end];
}
/**
* Returns the style options for bar labels
*/
public function dataLabelStyle($dataset, $index, &$item)
{
$style = parent::dataLabelStyle($dataset, $index, $item);
// bar label settings can override global settings
$opts = [
'font' => 'bar_label_font',
'font_size' => 'bar_label_font_size',
'font_weight' => 'bar_label_font_weight',
'colour' => 'bar_label_colour',
'altcolour' => 'bar_label_colour_above',
'space' => 'bar_label_space',
];
foreach($opts as $key => $opt)
if(isset($this->settings[$opt]))
$style[$key] = $this->settings[$opt];
return $style;
}
/**
* Return box for legend
*/
public function drawLegendEntry($x, $y, $w, $h, $entry)
{
$bar = ['x' => $x, 'y' => $y, 'width' => $w, 'height' => $h];
return $this->element('rect', $bar, $entry->style);
}
}