Commit | Line | Data |
---|---|---|
2eac77fc | 1 | package Imager::Graph::Vertical; |
2 | ||
3 | =head1 NAME | |
4 | ||
5 | Imager::Graph::Vertical- A super class for line/bar/column charts | |
6 | ||
7 | =cut | |
8 | ||
9 | use strict; | |
10 | use vars qw(@ISA); | |
11 | use Imager::Graph; | |
12 | @ISA = qw(Imager::Graph); | |
13 | ||
23a3585a | 14 | use constant STARTING_MIN_VALUE => 99999; |
2eac77fc | 15 | =over 4 |
16 | ||
17 | =item add_data_series(\@data, $series_name) | |
18 | ||
19 | Add a data series to the graph, of the default type. | |
20 | ||
21 | =cut | |
22 | ||
23 | sub add_data_series { | |
24 | my $self = shift; | |
25 | my $data_ref = shift; | |
26 | my $series_name = shift; | |
27 | ||
28 | my $series_type = $self->_get_default_series_type(); | |
29 | $self->_add_data_series($series_type, $data_ref, $series_name); | |
30 | ||
31 | return; | |
32 | } | |
33 | ||
34 | =item add_column_data_series(\@data, $series_name) | |
35 | ||
36 | Add a column data series to the graph. | |
37 | ||
38 | =cut | |
39 | ||
40 | sub add_column_data_series { | |
41 | my $self = shift; | |
42 | my $data_ref = shift; | |
43 | my $series_name = shift; | |
44 | ||
45 | $self->_add_data_series('column', $data_ref, $series_name); | |
46 | ||
47 | return; | |
48 | } | |
49 | ||
50 | =item add_stacked_column_data_series(\@data, $series_name) | |
51 | ||
52 | Add a stacked column data series to the graph. | |
53 | ||
54 | =cut | |
55 | ||
56 | sub add_stacked_column_data_series { | |
57 | my $self = shift; | |
58 | my $data_ref = shift; | |
59 | my $series_name = shift; | |
60 | ||
61 | $self->_add_data_series('stacked_column', $data_ref, $series_name); | |
62 | ||
63 | return; | |
64 | } | |
65 | ||
66 | =item add_line_data_series(\@data, $series_name) | |
67 | ||
68 | Add a line data series to the graph. | |
69 | ||
70 | =cut | |
71 | ||
72 | sub add_line_data_series { | |
73 | my $self = shift; | |
74 | my $data_ref = shift; | |
75 | my $series_name = shift; | |
76 | ||
77 | $self->_add_data_series('line', $data_ref, $series_name); | |
78 | ||
79 | return; | |
80 | } | |
81 | ||
23a3585a TC |
82 | =item set_y_max($value) |
83 | ||
84 | Sets the maximum y value to be displayed. This will be ignored if the y_max is lower than the highest value. | |
85 | ||
86 | =cut | |
87 | ||
88 | sub set_y_max { | |
89 | $_[0]->{'custom_style'}->{'y_max'} = $_[1]; | |
90 | } | |
91 | ||
92 | =item set_y_min($value) | |
93 | ||
94 | Sets the minimum y value to be displayed. This will be ignored if the y_min is higher than the lowest value. | |
95 | ||
96 | =cut | |
97 | ||
98 | sub set_y_min { | |
99 | $_[0]->{'custom_style'}->{'y_min'} = $_[1]; | |
100 | } | |
101 | ||
2eac77fc | 102 | =item set_range_padding($percentage) |
103 | ||
104 | Sets the padding to be used, as a percentage. For example, if your data ranges from 0 to 10, and you have a 20 percent padding, the y axis will go to 12. | |
105 | ||
23a3585a | 106 | Defaults to 10. This attribute is ignored for positive numbers if set_y_max() has been called, and ignored for negative numbers if set_y_min() has been called. |
2eac77fc | 107 | |
108 | =cut | |
109 | ||
110 | sub set_range_padding { | |
111 | $_[0]->{'custom_style'}->{'range_padding'} = $_[1]; | |
112 | } | |
113 | ||
23a3585a TC |
114 | =item set_negative_background($color) |
115 | ||
116 | Sets the background color used below the x axis. | |
117 | ||
118 | =cut | |
119 | ||
120 | sub set_negative_background { | |
121 | $_[0]->{'custom_style'}->{'negative_bg'} = $_[1]; | |
122 | } | |
123 | ||
2eac77fc | 124 | =item draw() |
125 | ||
126 | Draw the graph | |
127 | ||
128 | =cut | |
129 | ||
130 | sub draw { | |
131 | my ($self, %opts) = @_; | |
132 | ||
133 | if (!$self->_valid_input()) { | |
134 | return; | |
135 | } | |
136 | ||
137 | $self->_style_setup(\%opts); | |
138 | ||
139 | my $style = $self->{_style}; | |
140 | ||
141 | my $img = $self->_get_image() | |
142 | or return; | |
143 | ||
144 | my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 ); | |
145 | $self->_set_image_box(\@image_box); | |
146 | ||
147 | # Scale the graph box down to the widest graph that can cleanly hold the # of columns. | |
148 | $self->_get_data_range(); | |
149 | my $column_count = $self->_get_column_count(); | |
150 | ||
151 | my $width = $self->_get_number('width'); | |
152 | my $height = $self->_get_number('height'); | |
153 | my $size = $self->_get_number('size'); | |
154 | ||
155 | my $bottom = ($height - $size) / 2; | |
156 | my $left = ($width - $size) / 2; | |
157 | ||
158 | my $col_width = int($size / $column_count) -1; | |
159 | my $graph_width = $col_width * $column_count + 1; | |
160 | ||
161 | my @graph_box = ( $left, $bottom, $left + $graph_width - 1, $bottom + $size - 1 ); | |
162 | $self->_set_graph_box(\@graph_box); | |
163 | ||
164 | $self->_draw_legend(); | |
165 | ||
166 | $img->box( | |
167 | color => $self->_get_color('outline.line'), | |
168 | xmin => $left, | |
169 | xmax => $left+$graph_width, | |
170 | ymin => $bottom, | |
171 | ymax => $bottom+$size, | |
172 | ); | |
173 | ||
174 | $img->box( | |
175 | color => $self->_get_color('bg'), | |
176 | xmin => $left + 1, | |
177 | xmax => $left+$graph_width - 1, | |
178 | ymin => $bottom + 1, | |
179 | ymax => $bottom+$size -1 , | |
180 | filled => 1, | |
181 | ); | |
182 | ||
183 | my $min_value = $self->_get_min_value(); | |
184 | my $max_value = $self->_get_max_value(); | |
185 | my $value_range = $max_value - $min_value; | |
186 | ||
187 | my $zero_position; | |
188 | if ($value_range) { | |
189 | $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1); | |
190 | } | |
191 | ||
192 | if ($min_value < 0) { | |
193 | $img->box( | |
194 | color => $self->_get_color('negative_bg'), | |
195 | xmin => $left + 1, | |
196 | xmax => $left+$graph_width- 1, | |
197 | ymin => $zero_position, | |
198 | ymax => $bottom+$size -1, | |
199 | filled => 1, | |
200 | ); | |
201 | $img->line( | |
202 | x1 => $left+1, | |
203 | y1 => $zero_position, | |
204 | x2 => $left + $graph_width, | |
205 | y2 => $zero_position, | |
206 | color => $self->_get_color('outline.line'), | |
207 | ); | |
208 | } | |
209 | ||
210 | if ($self->_get_data_series()->{'stacked_column'}) { | |
211 | $self->_draw_stacked_columns(); | |
212 | } | |
213 | if ($self->_get_data_series()->{'column'}) { | |
214 | $self->_draw_columns(); | |
215 | } | |
216 | if ($self->_get_data_series()->{'line'}) { | |
217 | $self->_draw_lines(); | |
218 | } | |
219 | return $self->_get_image(); | |
220 | } | |
221 | ||
222 | sub _get_data_range { | |
223 | my $self = shift; | |
224 | ||
225 | my $max_value = 0; | |
226 | my $min_value = 0; | |
227 | my $column_count = 0; | |
228 | ||
229 | my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range(); | |
230 | my ($c_min, $c_max, $c_cols) = $self->_get_column_range(); | |
231 | my ($l_min, $l_max, $l_cols) = $self->_get_line_range(); | |
232 | ||
233 | # These are side by side... | |
234 | $sc_cols += $c_cols; | |
235 | ||
23a3585a | 236 | $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min); |
2eac77fc | 237 | $max_value = $self->_max(0, $sc_max, $c_max, $l_max); |
238 | ||
23a3585a TC |
239 | my $config_min = $self->_get_number('y_min'); |
240 | my $config_max = $self->_get_number('y_max'); | |
241 | ||
242 | if (defined $config_max && $config_max < $max_value) { | |
243 | $config_max = undef; | |
244 | } | |
245 | if (defined $config_min && $config_min > $min_value) { | |
246 | $config_min = undef; | |
247 | } | |
248 | ||
2eac77fc | 249 | my $range_padding = $self->_get_number('range_padding'); |
250 | if (!defined $range_padding) { | |
251 | $range_padding = 10; | |
252 | } | |
23a3585a TC |
253 | if (defined $config_min) { |
254 | $min_value = $config_min; | |
255 | } | |
256 | else { | |
257 | if ($min_value > 0) { | |
258 | $min_value = 0; | |
259 | } | |
260 | if ($range_padding && $min_value < 0) { | |
261 | my $difference = $min_value * $range_padding / 100; | |
262 | if ($min_value < -1 && $difference > -1) { | |
263 | $difference = -1; | |
264 | } | |
265 | $min_value += $difference; | |
2eac77fc | 266 | } |
2eac77fc | 267 | } |
23a3585a TC |
268 | if (defined $config_max) { |
269 | $max_value = $config_max; | |
270 | } | |
271 | else { | |
272 | if ($range_padding && $max_value > 0) { | |
273 | my $difference = $max_value * $range_padding / 100; | |
274 | if ($max_value > 1 && $difference < 1) { | |
275 | $difference = 1; | |
276 | } | |
277 | $max_value += $difference; | |
2eac77fc | 278 | } |
2eac77fc | 279 | } |
2eac77fc | 280 | $column_count = $self->_max(0, $sc_cols, $l_cols); |
281 | ||
282 | $self->_set_max_value($max_value); | |
283 | $self->_set_min_value($min_value); | |
284 | $self->_set_column_count($column_count); | |
285 | } | |
286 | ||
287 | sub _min { | |
288 | my $self = shift; | |
289 | my $min = shift; | |
290 | ||
291 | foreach my $value (@_) { | |
23a3585a | 292 | next unless defined $value; |
2eac77fc | 293 | if ($value < $min) { $min = $value; } |
294 | } | |
295 | return $min; | |
296 | } | |
297 | ||
298 | sub _max { | |
299 | my $self = shift; | |
300 | my $min = shift; | |
301 | ||
302 | foreach my $value (@_) { | |
23a3585a | 303 | next unless defined $value; |
2eac77fc | 304 | if ($value > $min) { $min = $value; } |
305 | } | |
306 | return $min; | |
307 | } | |
308 | ||
309 | sub _get_line_range { | |
310 | my $self = shift; | |
311 | my $series = $self->_get_data_series()->{'line'}; | |
23a3585a | 312 | return (undef, undef, 0) unless $series; |
2eac77fc | 313 | |
314 | my $max_value = 0; | |
23a3585a | 315 | my $min_value = STARTING_MIN_VALUE; |
2eac77fc | 316 | my $column_count = 0; |
317 | ||
318 | my @series = @{$series}; | |
319 | foreach my $series (@series) { | |
320 | my @data = @{$series->{'data'}}; | |
321 | ||
322 | if (scalar @data > $column_count) { | |
323 | $column_count = scalar @data; | |
324 | } | |
325 | ||
326 | foreach my $value (@data) { | |
327 | if ($value > $max_value) { $max_value = $value; } | |
328 | if ($value < $min_value) { $min_value = $value; } | |
329 | } | |
330 | } | |
331 | ||
332 | return ($min_value, $max_value, $column_count); | |
333 | } | |
334 | ||
335 | sub _get_column_range { | |
336 | my $self = shift; | |
337 | ||
338 | my $series = $self->_get_data_series()->{'column'}; | |
23a3585a | 339 | return (undef, undef, 0) unless $series; |
2eac77fc | 340 | |
341 | my $max_value = 0; | |
23a3585a | 342 | my $min_value = STARTING_MIN_VALUE; |
2eac77fc | 343 | my $column_count = 0; |
344 | ||
345 | my @series = @{$series}; | |
346 | foreach my $series (@series) { | |
347 | my @data = @{$series->{'data'}}; | |
348 | ||
349 | foreach my $value (@data) { | |
350 | $column_count++; | |
351 | if ($value > $max_value) { $max_value = $value; } | |
352 | if ($value < $min_value) { $min_value = $value; } | |
353 | } | |
354 | } | |
355 | ||
356 | return ($min_value, $max_value, $column_count); | |
357 | } | |
358 | ||
359 | sub _get_stacked_column_range { | |
360 | my $self = shift; | |
361 | ||
362 | my $max_value = 0; | |
23a3585a | 363 | my $min_value = STARTING_MIN_VALUE; |
2eac77fc | 364 | my $column_count = 0; |
365 | ||
23a3585a | 366 | return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'}; |
2eac77fc | 367 | my @series = @{$self->_get_data_series()->{'stacked_column'}}; |
368 | ||
369 | my @max_entries; | |
370 | my @min_entries; | |
371 | for (my $i = scalar @series - 1; $i >= 0; $i--) { | |
372 | my $series = $series[$i]; | |
373 | my $data = $series->{'data'}; | |
374 | ||
375 | for (my $i = 0; $i < scalar @$data; $i++) { | |
376 | my $value = 0; | |
377 | if ($data->[$i] > 0) { | |
378 | $value = $data->[$i] + ($max_entries[$i] || 0); | |
379 | $data->[$i] = $value; | |
380 | $max_entries[$i] = $value; | |
381 | } | |
382 | elsif ($data->[$i] < 0) { | |
383 | $value = $data->[$i] + ($min_entries[$i] || 0); | |
384 | $data->[$i] = $value; | |
385 | $min_entries[$i] = $value; | |
386 | } | |
387 | if ($value > $max_value) { $max_value = $value; } | |
388 | if ($value < $min_value) { $min_value = $value; } | |
389 | } | |
390 | if (scalar @$data > $column_count) { | |
391 | $column_count = scalar @$data; | |
392 | } | |
393 | } | |
394 | ||
395 | return ($min_value, $max_value, $column_count); | |
396 | } | |
397 | ||
398 | sub _draw_legend { | |
399 | my $self = shift; | |
400 | my $style = $self->{'_style'}; | |
401 | ||
402 | my @labels; | |
403 | my $img = $self->_get_image(); | |
404 | if (my $series = $self->_get_data_series()->{'stacked_column'}) { | |
405 | push @labels, map { $_->{'series_name'} } @$series; | |
406 | } | |
407 | if (my $series = $self->_get_data_series()->{'column'}) { | |
408 | push @labels, map { $_->{'series_name'} } @$series; | |
409 | } | |
410 | if (my $series = $self->_get_data_series()->{'line'}) { | |
411 | push @labels, map { $_->{'series_name'} } @$series; | |
412 | } | |
413 | ||
414 | if ($style->{features}{legend} && (scalar @labels)) { | |
415 | $self->SUPER::_draw_legend($self->_get_image(), \@labels, $self->_get_image_box()) | |
416 | or return; | |
417 | } | |
418 | return; | |
419 | } | |
420 | ||
421 | sub _draw_flat_legend { | |
422 | return 1; | |
423 | } | |
424 | ||
425 | sub _draw_lines { | |
426 | my $self = shift; | |
427 | my $style = $self->{'_style'}; | |
428 | ||
429 | my $img = $self->_get_image(); | |
430 | ||
431 | my $max_value = $self->_get_max_value(); | |
432 | my $min_value = $self->_get_min_value(); | |
433 | my $column_count = $self->_get_column_count(); | |
434 | ||
435 | my $value_range = $max_value - $min_value; | |
436 | ||
437 | my $width = $self->_get_number('width'); | |
438 | my $height = $self->_get_number('height'); | |
439 | my $size = $self->_get_number('size'); | |
440 | ||
441 | my $bottom = ($height - $size) / 2; | |
442 | my $left = ($width - $size) / 2; | |
443 | ||
444 | my $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1); | |
445 | ||
446 | if ($self->_get_y_tics()) { | |
447 | $self->_draw_y_tics(); | |
448 | } | |
449 | if ($self->_get_labels()) { | |
450 | $self->_draw_x_tics(); | |
451 | } | |
452 | ||
453 | my $line_series = $self->_get_data_series()->{'line'}; | |
454 | my $series_counter = $self->_get_series_counter() || 0; | |
455 | ||
456 | my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0; | |
457 | ||
458 | my $col_width = int($size / $column_count) -1; | |
459 | my $graph_width = $col_width * $column_count + 1; | |
460 | ||
461 | foreach my $series (@$line_series) { | |
462 | my @data = @{$series->{'data'}}; | |
463 | my $data_size = scalar @data; | |
464 | ||
465 | my $interval; | |
466 | if ($has_columns) { | |
467 | $interval = $graph_width / ($data_size); | |
468 | } | |
469 | else { | |
470 | $interval = $graph_width / ($data_size - 1); | |
471 | } | |
2eac77fc | 472 | my $color = $self->_data_color($series_counter); |
8e140388 TC |
473 | |
474 | # We need to add these last, otherwise the next line segment will overwrite half of the marker | |
475 | my @marker_positions; | |
2eac77fc | 476 | for (my $i = 0; $i < $data_size - 1; $i++) { |
477 | my $x1 = $left + $i * $interval; | |
478 | my $x2 = $left + ($i + 1) * $interval; | |
479 | ||
480 | $x1 += $has_columns * $interval / 2; | |
481 | $x2 += $has_columns * $interval / 2; | |
482 | ||
483 | my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size; | |
484 | my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $size; | |
485 | ||
8e140388 | 486 | push @marker_positions, [$x1, $y1]; |
2eac77fc | 487 | $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => 1, color => $color) || die $img->errstr; |
2eac77fc | 488 | } |
489 | ||
490 | my $x2 = $left + ($data_size - 1) * $interval; | |
491 | $x2 += $has_columns * $interval / 2; | |
492 | ||
493 | my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $size; | |
494 | ||
8e140388 TC |
495 | push @marker_positions, [$x2, $y2]; |
496 | foreach my $position (@marker_positions) { | |
497 | $self->_draw_line_marker($position->[0], $position->[1], $series_counter); | |
498 | } | |
2eac77fc | 499 | $series_counter++; |
500 | } | |
501 | ||
502 | $self->_set_series_counter($series_counter); | |
503 | return; | |
504 | } | |
505 | ||
8e140388 TC |
506 | sub _line_marker { |
507 | my ($self, $index) = @_; | |
508 | ||
509 | my $markers = $self->{'_style'}{'line_markers'}; | |
510 | if (!$markers) { | |
511 | return; | |
512 | } | |
513 | my $marker = $markers->[$index % @$markers]; | |
514 | ||
515 | return $marker; | |
516 | } | |
517 | ||
518 | sub _draw_line_marker { | |
519 | my $self = shift; | |
520 | my ($x1, $y1, $series_counter) = @_; | |
521 | ||
522 | my $img = $self->_get_image(); | |
523 | ||
524 | my $style = $self->_line_marker($series_counter); | |
525 | return unless $style; | |
526 | ||
527 | my $type = $style->{'shape'}; | |
528 | my $radius = $style->{'radius'}; | |
529 | ||
530 | if ($type eq 'circle') { | |
531 | my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]); | |
532 | $img->circle(x => $x1, y => $y1, r => $radius, aa => 1, filled => 1, @fill); | |
533 | } | |
534 | elsif ($type eq 'square') { | |
535 | my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]); | |
536 | $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill); | |
537 | } | |
538 | elsif ($type eq 'diamond') { | |
539 | # The gradient really doesn't work for diamond | |
540 | my $color = $self->_data_color($series_counter); | |
541 | $img->polygon( | |
542 | points => [ | |
543 | [$x1 - $radius, $y1], | |
544 | [$x1, $y1 + $radius], | |
545 | [$x1 + $radius, $y1], | |
546 | [$x1, $y1 - $radius], | |
547 | ], | |
548 | filled => 1, color => $color, aa => 1); | |
549 | } | |
550 | elsif ($type eq 'triangle') { | |
551 | # The gradient really doesn't work for triangle | |
552 | my $color = $self->_data_color($series_counter); | |
553 | $img->polygon( | |
554 | points => [ | |
555 | [$x1 - $radius, $y1 + $radius], | |
556 | [$x1 + $radius, $y1 + $radius], | |
557 | [$x1, $y1 - $radius], | |
558 | ], | |
559 | filled => 1, color => $color, aa => 1); | |
560 | ||
561 | } | |
562 | elsif ($type eq 'x') { | |
563 | my $color = $self->_data_color($series_counter); | |
564 | $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => 1, color => $color) || die $img->errstr; | |
565 | $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => 1, color => $color) || die $img->errstr; | |
566 | } | |
567 | elsif ($type eq 'plus') { | |
568 | my $color = $self->_data_color($series_counter); | |
569 | $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => 1, color => $color) || die $img->errstr; | |
570 | $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => 1, color => $color) || die $img->errstr; | |
571 | } | |
572 | } | |
573 | ||
2eac77fc | 574 | sub _draw_columns { |
575 | my $self = shift; | |
576 | my $style = $self->{'_style'}; | |
577 | ||
578 | my $img = $self->_get_image(); | |
579 | ||
580 | my $max_value = $self->_get_max_value(); | |
581 | my $min_value = $self->_get_min_value(); | |
582 | my $column_count = $self->_get_column_count(); | |
583 | ||
584 | my $value_range = $max_value - $min_value; | |
585 | ||
586 | my $width = $self->_get_number('width'); | |
587 | my $height = $self->_get_number('height'); | |
588 | my $size = $self->_get_number('size'); | |
589 | ||
590 | my $bottom = ($height - $size) / 2; | |
591 | my $left = ($width - $size) / 2 + 1; | |
592 | ||
593 | my $zero_position = int($bottom + $size - (-1*$min_value / $value_range) * ($size -1)); | |
594 | ||
595 | if ($self->_get_y_tics()) { | |
596 | $self->_draw_y_tics(); | |
597 | } | |
598 | if ($self->_get_labels()) { | |
599 | $self->_draw_x_tics(); | |
600 | } | |
601 | ||
602 | my $bar_width = int(($size)/ $column_count - 2); | |
603 | ||
604 | my $outline_color; | |
605 | if ($style->{'features'}{'outline'}) { | |
606 | $outline_color = $self->_get_color('outline.line'); | |
607 | } | |
608 | ||
609 | my $series_counter = $self->_get_series_counter() || 0; | |
610 | my $col_series = $self->_get_data_series()->{'column'}; | |
611 | ||
612 | # This tracks the series we're in relative to the starting series - this way colors stay accurate, but the columns don't start out too far to the right. | |
613 | my $column_series = 0; | |
614 | ||
615 | # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise | |
616 | my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0); | |
617 | ||
618 | for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) { | |
619 | my $series = $col_series->[$series_pos]; | |
620 | my @data = @{$series->{'data'}}; | |
621 | my $data_size = scalar @data; | |
622 | my $color = $self->_data_color($series_counter); | |
623 | for (my $i = 0; $i < $data_size; $i++) { | |
624 | my $x1 = int($left + $bar_width * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos; | |
625 | if ($has_stacked_columns) { | |
626 | $x1 += ($i + 1) * $bar_width + $i + 1; | |
627 | } | |
628 | my $x2 = $x1 + $bar_width; | |
629 | ||
630 | my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size); | |
631 | ||
632 | my $color = $self->_data_color($series_counter); | |
633 | ||
634 | # my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position]); | |
635 | if ($data[$i] > 0) { | |
636 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1); | |
637 | if ($style->{'features'}{'outline'}) { | |
638 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color); | |
639 | } | |
640 | } | |
641 | else { | |
642 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1); | |
643 | if ($style->{'features'}{'outline'}) { | |
644 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color); | |
645 | } | |
646 | } | |
647 | } | |
648 | ||
649 | $series_counter++; | |
650 | $column_series++; | |
651 | } | |
652 | $self->_set_series_counter($series_counter); | |
653 | return; | |
654 | } | |
655 | ||
656 | sub _draw_stacked_columns { | |
657 | my $self = shift; | |
658 | my $style = $self->{'_style'}; | |
659 | ||
660 | my $img = $self->_get_image(); | |
661 | ||
662 | my $max_value = $self->_get_max_value(); | |
663 | my $min_value = $self->_get_min_value(); | |
664 | my $column_count = $self->_get_column_count(); | |
665 | my $value_range = $max_value - $min_value; | |
666 | ||
667 | my $graph_box = $self->_get_graph_box(); | |
668 | my $left = $graph_box->[0] + 1; | |
669 | my $bottom = $graph_box->[1]; | |
670 | my $size = $self->_get_number('size'); | |
671 | ||
672 | if ($self->_get_y_tics()) { | |
673 | $self->_draw_y_tics(); | |
674 | } | |
675 | if ($self->_get_labels()) { | |
676 | $self->_draw_x_tics(); | |
677 | } | |
678 | ||
679 | my $bar_width = int($size / $column_count -2); | |
680 | my $column_series = 0; | |
681 | if (my $column_series_data = $self->_get_data_series()->{'column'}) { | |
682 | $column_series = (scalar @$column_series_data); | |
683 | } | |
684 | $column_series++; | |
685 | ||
686 | my $outline_color; | |
687 | if ($style->{'features'}{'outline'}) { | |
688 | $outline_color = $self->_get_color('outline.line'); | |
689 | } | |
690 | ||
691 | my $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1); | |
692 | my $col_series = $self->_get_data_series()->{'stacked_column'}; | |
693 | my $series_counter = $self->_get_series_counter() || 0; | |
694 | foreach my $series (@$col_series) { | |
695 | my @data = @{$series->{'data'}}; | |
696 | my $data_size = scalar @data; | |
697 | my $color = $self->_data_color($series_counter); | |
698 | for (my $i = 0; $i < $data_size; $i++) { | |
699 | my $x1 = int($left + $bar_width * ($column_series * $i)) + $column_series * $i; | |
700 | # my $x1 = $left + $i * $size / ($data_size); | |
701 | my $x2 = $x1 + $bar_width; | |
702 | ||
703 | my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size; | |
704 | ||
705 | if ($data[$i] > 0) { | |
706 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1); | |
707 | if ($style->{'features'}{'outline'}) { | |
708 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color); | |
709 | } | |
710 | } | |
711 | else { | |
712 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1); | |
713 | if ($style->{'features'}{'outline'}) { | |
714 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color); | |
715 | } | |
716 | } | |
717 | } | |
718 | ||
719 | $series_counter++; | |
720 | } | |
721 | $self->_set_series_counter($series_counter); | |
722 | return; | |
723 | } | |
724 | ||
725 | sub _add_data_series { | |
726 | my $self = shift; | |
727 | my $series_type = shift; | |
728 | my $data_ref = shift; | |
729 | my $series_name = shift; | |
730 | ||
731 | my $graph_data = $self->{'graph_data'} || {}; | |
732 | ||
733 | my $series = $graph_data->{$series_type} || []; | |
734 | ||
735 | push @$series, { data => $data_ref, series_name => $series_name }; | |
736 | ||
737 | $graph_data->{$series_type} = $series; | |
738 | ||
739 | $self->{'graph_data'} = $graph_data; | |
740 | return; | |
741 | } | |
742 | ||
743 | =over | |
744 | ||
745 | =item set_y_tics($count) | |
746 | ||
747 | Set the number of Y tics to use. Their value and position will be determined by the data range. | |
748 | ||
749 | =cut | |
750 | ||
751 | sub set_y_tics { | |
752 | $_[0]->{'y_tics'} = $_[1]; | |
753 | } | |
754 | ||
755 | sub _get_y_tics { | |
756 | return $_[0]->{'y_tics'}; | |
757 | } | |
758 | ||
759 | sub _draw_y_tics { | |
760 | my $self = shift; | |
761 | my $min = $self->_get_min_value(); | |
762 | my $max = $self->_get_max_value(); | |
763 | my $tic_count = $self->_get_y_tics(); | |
764 | ||
765 | my $img = $self->_get_image(); | |
766 | my $graph_box = $self->_get_graph_box(); | |
767 | my $image_box = $self->_get_image_box(); | |
768 | ||
769 | my $interval = ($max - $min) / ($tic_count - 1); | |
770 | ||
771 | my %text_info = $self->_text_style('legend') | |
772 | or return; | |
773 | ||
774 | my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1); | |
775 | for my $count (0 .. $tic_count - 1) { | |
776 | my $x1 = $graph_box->[0] - 5; | |
777 | my $x2 = $graph_box->[0] + 5; | |
23a3585a | 778 | my $y1 = $graph_box->[3] - ($count * $tic_distance) + 1; |
2eac77fc | 779 | |
780 | my $value = sprintf("%.2f", ($count*$interval)+$min); | |
23a3585a | 781 | |
2eac77fc | 782 | my @box = $self->_text_bbox($value, 'legend') |
783 | or return; | |
784 | ||
785 | $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000'); | |
786 | ||
787 | my $width = $box[2]; | |
788 | my $height = $box[3]; | |
789 | ||
790 | $img->string(%text_info, | |
791 | x => ($x1 - $width - 3), | |
792 | y => ($y1 + ($height / 2)), | |
793 | text => $value | |
794 | ); | |
795 | } | |
796 | ||
797 | } | |
798 | ||
799 | sub _draw_x_tics { | |
800 | my $self = shift; | |
801 | ||
802 | my $img = $self->_get_image(); | |
803 | my $graph_box = $self->_get_graph_box(); | |
804 | my $image_box = $self->_get_image_box(); | |
805 | ||
806 | my $labels = $self->_get_labels(); | |
807 | ||
808 | my $tic_count = (scalar @$labels) - 1; | |
809 | ||
810 | my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'}); | |
811 | ||
812 | # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge | |
813 | my $denominator = $tic_count; | |
814 | if ($has_columns) { | |
815 | $denominator ++; | |
816 | } | |
817 | my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator); | |
818 | my %text_info = $self->_text_style('legend') | |
819 | or return; | |
820 | ||
821 | for my $count (0 .. $tic_count) { | |
822 | my $label = $labels->[$count]; | |
823 | my $x1 = $graph_box->[0] + ($tic_distance * $count); | |
824 | ||
825 | if ($has_columns) { | |
826 | $x1 += $tic_distance / 2; | |
827 | } | |
828 | my $y1 = $graph_box->[3] + 5; | |
829 | my $y2 = $graph_box->[3] - 5; | |
830 | ||
831 | $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000'); | |
832 | ||
833 | my @box = $self->_text_bbox($label, 'legend') | |
834 | or return; | |
835 | ||
836 | my $width = $box[2]; | |
837 | my $height = $box[3]; | |
838 | ||
839 | $img->string(%text_info, | |
840 | x => ($x1 - ($width / 2)), | |
841 | y => ($y1 + ($height + 5)), | |
842 | text => $label | |
843 | ); | |
844 | ||
845 | } | |
846 | } | |
847 | ||
848 | sub _valid_input { | |
849 | my $self = shift; | |
850 | ||
851 | if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) { | |
852 | return $self->_error("No data supplied"); | |
853 | } | |
854 | ||
855 | my $data = $self->_get_data_series(); | |
856 | if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) { | |
857 | return $self->_error("No values in data series"); | |
858 | } | |
859 | if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) { | |
860 | return $self->_error("No values in data series"); | |
861 | } | |
862 | if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) { | |
863 | return $self->_error("No values in data series"); | |
864 | } | |
865 | ||
866 | return 1; | |
867 | } | |
868 | ||
869 | sub _set_column_count { $_[0]->{'column_count'} = $_[1]; } | |
870 | sub _set_min_value { $_[0]->{'min_value'} = $_[1]; } | |
871 | sub _set_max_value { $_[0]->{'max_value'} = $_[1]; } | |
872 | sub _set_image_box { $_[0]->{'image_box'} = $_[1]; } | |
873 | sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; } | |
874 | sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; } | |
875 | sub _get_column_count { return $_[0]->{'column_count'} } | |
876 | sub _get_min_value { return $_[0]->{'min_value'} } | |
877 | sub _get_max_value { return $_[0]->{'max_value'} } | |
878 | sub _get_image_box { return $_[0]->{'image_box'} } | |
879 | sub _get_graph_box { return $_[0]->{'graph_box'} } | |
880 | sub _get_series_counter { return $_[0]->{'series_counter'} } | |
881 | ||
2eac77fc | 882 | 1; |