This repository has been archived by the owner on Nov 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSVGGraphHorizontalStackedBarGraph.php
240 lines (217 loc) · 7.75 KB
/
SVGGraphHorizontalStackedBarGraph.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
<?php
/**
* Copyright (C) 2011-2015 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]>
*/
require_once 'SVGGraphMultiGraph.php';
require_once 'SVGGraphHorizontalBarGraph.php';
require_once 'SVGGraphData.php';
class HorizontalStackedBarGraph extends HorizontalBarGraph {
protected $legend_reverse = false;
protected $single_axis = true;
// used to determine where the total label should go
protected $last_position_pos = array();
protected $last_position_neg = array();
protected function Draw()
{
if($this->log_axis_y)
throw new Exception('log_axis_y not supported by HorizontalStackedBarGraph');
$body = $this->Grid() . $this->Guidelines(SVGG_GUIDELINE_BELOW);
$bar_height = $this->BarHeight();
$bspace = max(0, ($this->y_axes[$this->main_y_axis]->Unit() - $bar_height) / 2);
$bar_style = array();
$bar = array('height' => $bar_height);
$bnum = 0;
$b_start = $this->height - $this->pad_bottom - ($this->bar_space / 2);
$chunk_count = count($this->multi_graph);
$bars_shown = array_fill(0, $chunk_count, 0);
$this->ColourSetup($this->multi_graph->ItemsCount(-1), $chunk_count);
$bars = '';
foreach($this->multi_graph as $itemlist) {
$k = $itemlist[0]->key;
$bar_pos = $this->GridPosition($k, $bnum);
if(!is_null($bar_pos)) {
$bar['y'] = $bar_pos - $bspace - $bar_height;
$xpos = $xneg = 0;
// find greatest -/+ bar
$max_neg_bar = $max_pos_bar = -1;
for($j = 0; $j < $chunk_count; ++$j) {
if($itemlist[$j]->value > 0)
$max_pos_bar = $j;
else
$max_neg_bar = $j;
}
for($j = 0; $j < $chunk_count; ++$j) {
$item = $itemlist[$j];
$this->SetStroke($bar_style, $item, $j);
$bar_style['fill'] = $this->GetColour($item, $bnum, $j);
if(!is_null($item->value)) {
$this->Bar($item->value, $bar, $item->value >= 0 ? $xpos : $xneg);
if($item->value < 0)
$xneg += $item->value;
else
$xpos += $item->value;
if($bar['width'] > 0) {
++$bars_shown[$j];
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
$rect = $this->Element('rect', $bar, $bar_style);
$bars .= $this->GetLink($item, $k, $rect);
unset($bar['id']); // clear ID for next generated value
}
}
$this->bar_styles[$j] = $bar_style;
}
if($this->show_bar_totals) {
if($xpos) {
$this->Bar($xpos, $bar);
$this->AddContentLabel('totalpos', $bnum, $bar['x'], $bar['y'],
$bar['width'], $bar['height'], $xpos);
}
if($xneg) {
$this->Bar($xneg, $bar);
$this->AddContentLabel('totalneg', $bnum, $bar['x'], $bar['y'],
$bar['width'], $bar['height'], $xneg);
}
}
}
++$bnum;
}
if(!$this->legend_show_empty) {
foreach($bars_shown as $j => $bar) {
if(!$bar)
$this->bar_styles[$j] = NULL;
}
}
if($this->semantic_classes)
$bars = $this->Element('g', array('class' => 'series'), NULL, $bars);
$body .= $bars;
$body .= $this->Guidelines(SVGG_GUIDELINE_ABOVE) . $this->Axes();
return $body;
}
/**
* Overridden to prevent drawing on other bars
*/
public function DataLabelPosition($dataset, $index, &$item, $x, $y, $w, $h,
$label_w, $label_h)
{
if(!is_numeric($dataset)) {
if($dataset === 'totalpos') {
if(isset($this->last_position_pos[$index])) {
list($lpos, $l_w) = $this->last_position_pos[$index];
list($hpos, $vpos) = Graph::TranslatePosition($lpos);
if($hpos == 'or')
return "middle outside right {$l_w} 0";
}
return 'outside right';
}
if($dataset === 'totalneg') {
if(isset($this->last_position_neg[$index])) {
list($lpos, $l_w) = $this->last_position_neg[$index];
list($hpos, $vpos) = Graph::TranslatePosition($lpos);
if($hpos == 'ol')
return "middle outside left -{$l_w} 0";
}
return 'outside left';
}
}
if($dataset === 'totalpos')
return 'outside right';
if($dataset === 'totalneg')
return 'outside left';
$pos = parent::DataLabelPosition($dataset, $index, $item, $x, $y, $w, $h,
$label_w, $label_h);
if($label_w > $w && Graph::IsPositionInside($pos))
$pos = str_replace(array('outside left','outside right'), 'centre', $pos);
if($item->value > 0)
$this->last_position_pos[$index] = array($pos, $label_w);
else
$this->last_position_neg[$index] = array($pos, $label_w);
return $pos;
}
/**
* Returns the style options for labels
*/
public function DataLabelStyle($dataset, $index, &$item)
{
$style = parent::DataLabelStyle($dataset, $index, $item);
if($dataset === 'totalpos' || $dataset === 'totalneg') {
// total settings can override label settings
$opts = array(
'font' => 'bar_total_font',
'font_size' => 'bar_total_font_size',
'font_weight' => 'bar_total_font_weight',
'colour' => 'bar_total_colour',
'space' => 'bar_total_space',
'type' => 'bar_total_type',
'font_adjust' => 'bar_total_font_adjust',
'back_colour' => 'bar_total_back_colour',
'angle' => 'bar_total_angle',
'round' => 'bar_total_round',
'stroke' => 'bar_total_stroke',
'stroke_width' => 'bar_total_stroke_width',
'fill' => 'bar_total_fill',
'tail_width' => 'bar_total_tail_width',
'tail_length' => 'bar_total_tail_length',
'shadow_opacity' => 'bar_total_shadow_opacity',
'pad_x' => 'bar_total_padding_x',
'pad_y' => 'bar_total_padding_y',
);
// special case
$opt = 'bar_total_padding';
if(isset($this->settings[$opt]) && !empty($this->settings[$opt])) {
$style['pad_x'] = $this->settings[$opt];
$style['pad_y'] = $this->settings[$opt];
}
foreach($opts as $key => $opt) {
if(isset($this->settings[$opt]) && !empty($this->settings[$opt]))
$style[$key] = $this->settings[$opt];
}
}
return $style;
}
/**
* construct multigraph
*/
public function Values($values)
{
parent::Values($values);
if(!$this->values->error)
$this->multi_graph = new MultiGraph($this->values, $this->force_assoc,
$this->require_integer_keys);
}
/**
* Returns the maximum (stacked) value
*/
protected function GetMaxValue()
{
return $this->multi_graph->GetMaxSumValue();
}
/**
* Returns the minimum (stacked) value
*/
protected function GetMinValue()
{
return $this->multi_graph->GetMinSumValue();
}
}