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 | } | |
472 | my @fill = $self->_data_fill($series_counter, $self->_get_graph_box()); | |
473 | my $color = $self->_data_color($series_counter); | |
474 | for (my $i = 0; $i < $data_size - 1; $i++) { | |
475 | my $x1 = $left + $i * $interval; | |
476 | my $x2 = $left + ($i + 1) * $interval; | |
477 | ||
478 | $x1 += $has_columns * $interval / 2; | |
479 | $x2 += $has_columns * $interval / 2; | |
480 | ||
481 | my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size; | |
482 | my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $size; | |
483 | ||
484 | $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => 1, color => $color) || die $img->errstr; | |
485 | $img->circle(x => $x1, y => $y1, r => 3, aa => 1, filled => 1, @fill); | |
486 | } | |
487 | ||
488 | my $x2 = $left + ($data_size - 1) * $interval; | |
489 | $x2 += $has_columns * $interval / 2; | |
490 | ||
491 | my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $size; | |
492 | ||
493 | $img->circle(x => $x2, y => $y2, r => 3, aa => 1, filled => 1, @fill); | |
494 | $series_counter++; | |
495 | } | |
496 | ||
497 | $self->_set_series_counter($series_counter); | |
498 | return; | |
499 | } | |
500 | ||
501 | sub _draw_columns { | |
502 | my $self = shift; | |
503 | my $style = $self->{'_style'}; | |
504 | ||
505 | my $img = $self->_get_image(); | |
506 | ||
507 | my $max_value = $self->_get_max_value(); | |
508 | my $min_value = $self->_get_min_value(); | |
509 | my $column_count = $self->_get_column_count(); | |
510 | ||
511 | my $value_range = $max_value - $min_value; | |
512 | ||
513 | my $width = $self->_get_number('width'); | |
514 | my $height = $self->_get_number('height'); | |
515 | my $size = $self->_get_number('size'); | |
516 | ||
517 | my $bottom = ($height - $size) / 2; | |
518 | my $left = ($width - $size) / 2 + 1; | |
519 | ||
520 | my $zero_position = int($bottom + $size - (-1*$min_value / $value_range) * ($size -1)); | |
521 | ||
522 | if ($self->_get_y_tics()) { | |
523 | $self->_draw_y_tics(); | |
524 | } | |
525 | if ($self->_get_labels()) { | |
526 | $self->_draw_x_tics(); | |
527 | } | |
528 | ||
529 | my $bar_width = int(($size)/ $column_count - 2); | |
530 | ||
531 | my $outline_color; | |
532 | if ($style->{'features'}{'outline'}) { | |
533 | $outline_color = $self->_get_color('outline.line'); | |
534 | } | |
535 | ||
536 | my $series_counter = $self->_get_series_counter() || 0; | |
537 | my $col_series = $self->_get_data_series()->{'column'}; | |
538 | ||
539 | # 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. | |
540 | my $column_series = 0; | |
541 | ||
542 | # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise | |
543 | my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0); | |
544 | ||
545 | for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) { | |
546 | my $series = $col_series->[$series_pos]; | |
547 | my @data = @{$series->{'data'}}; | |
548 | my $data_size = scalar @data; | |
549 | my $color = $self->_data_color($series_counter); | |
550 | for (my $i = 0; $i < $data_size; $i++) { | |
551 | my $x1 = int($left + $bar_width * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos; | |
552 | if ($has_stacked_columns) { | |
553 | $x1 += ($i + 1) * $bar_width + $i + 1; | |
554 | } | |
555 | my $x2 = $x1 + $bar_width; | |
556 | ||
557 | my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size); | |
558 | ||
559 | my $color = $self->_data_color($series_counter); | |
560 | ||
561 | # my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position]); | |
562 | if ($data[$i] > 0) { | |
563 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1); | |
564 | if ($style->{'features'}{'outline'}) { | |
565 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color); | |
566 | } | |
567 | } | |
568 | else { | |
569 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1); | |
570 | if ($style->{'features'}{'outline'}) { | |
571 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color); | |
572 | } | |
573 | } | |
574 | } | |
575 | ||
576 | $series_counter++; | |
577 | $column_series++; | |
578 | } | |
579 | $self->_set_series_counter($series_counter); | |
580 | return; | |
581 | } | |
582 | ||
583 | sub _draw_stacked_columns { | |
584 | my $self = shift; | |
585 | my $style = $self->{'_style'}; | |
586 | ||
587 | my $img = $self->_get_image(); | |
588 | ||
589 | my $max_value = $self->_get_max_value(); | |
590 | my $min_value = $self->_get_min_value(); | |
591 | my $column_count = $self->_get_column_count(); | |
592 | my $value_range = $max_value - $min_value; | |
593 | ||
594 | my $graph_box = $self->_get_graph_box(); | |
595 | my $left = $graph_box->[0] + 1; | |
596 | my $bottom = $graph_box->[1]; | |
597 | my $size = $self->_get_number('size'); | |
598 | ||
599 | if ($self->_get_y_tics()) { | |
600 | $self->_draw_y_tics(); | |
601 | } | |
602 | if ($self->_get_labels()) { | |
603 | $self->_draw_x_tics(); | |
604 | } | |
605 | ||
606 | my $bar_width = int($size / $column_count -2); | |
607 | my $column_series = 0; | |
608 | if (my $column_series_data = $self->_get_data_series()->{'column'}) { | |
609 | $column_series = (scalar @$column_series_data); | |
610 | } | |
611 | $column_series++; | |
612 | ||
613 | my $outline_color; | |
614 | if ($style->{'features'}{'outline'}) { | |
615 | $outline_color = $self->_get_color('outline.line'); | |
616 | } | |
617 | ||
618 | my $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1); | |
619 | my $col_series = $self->_get_data_series()->{'stacked_column'}; | |
620 | my $series_counter = $self->_get_series_counter() || 0; | |
621 | foreach my $series (@$col_series) { | |
622 | my @data = @{$series->{'data'}}; | |
623 | my $data_size = scalar @data; | |
624 | my $color = $self->_data_color($series_counter); | |
625 | for (my $i = 0; $i < $data_size; $i++) { | |
626 | my $x1 = int($left + $bar_width * ($column_series * $i)) + $column_series * $i; | |
627 | # my $x1 = $left + $i * $size / ($data_size); | |
628 | my $x2 = $x1 + $bar_width; | |
629 | ||
630 | my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size; | |
631 | ||
632 | if ($data[$i] > 0) { | |
633 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1); | |
634 | if ($style->{'features'}{'outline'}) { | |
635 | $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color); | |
636 | } | |
637 | } | |
638 | else { | |
639 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1); | |
640 | if ($style->{'features'}{'outline'}) { | |
641 | $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color); | |
642 | } | |
643 | } | |
644 | } | |
645 | ||
646 | $series_counter++; | |
647 | } | |
648 | $self->_set_series_counter($series_counter); | |
649 | return; | |
650 | } | |
651 | ||
652 | sub _add_data_series { | |
653 | my $self = shift; | |
654 | my $series_type = shift; | |
655 | my $data_ref = shift; | |
656 | my $series_name = shift; | |
657 | ||
658 | my $graph_data = $self->{'graph_data'} || {}; | |
659 | ||
660 | my $series = $graph_data->{$series_type} || []; | |
661 | ||
662 | push @$series, { data => $data_ref, series_name => $series_name }; | |
663 | ||
664 | $graph_data->{$series_type} = $series; | |
665 | ||
666 | $self->{'graph_data'} = $graph_data; | |
667 | return; | |
668 | } | |
669 | ||
670 | =over | |
671 | ||
672 | =item set_y_tics($count) | |
673 | ||
674 | Set the number of Y tics to use. Their value and position will be determined by the data range. | |
675 | ||
676 | =cut | |
677 | ||
678 | sub set_y_tics { | |
679 | $_[0]->{'y_tics'} = $_[1]; | |
680 | } | |
681 | ||
682 | sub _get_y_tics { | |
683 | return $_[0]->{'y_tics'}; | |
684 | } | |
685 | ||
686 | sub _draw_y_tics { | |
687 | my $self = shift; | |
688 | my $min = $self->_get_min_value(); | |
689 | my $max = $self->_get_max_value(); | |
690 | my $tic_count = $self->_get_y_tics(); | |
691 | ||
692 | my $img = $self->_get_image(); | |
693 | my $graph_box = $self->_get_graph_box(); | |
694 | my $image_box = $self->_get_image_box(); | |
695 | ||
696 | my $interval = ($max - $min) / ($tic_count - 1); | |
697 | ||
698 | my %text_info = $self->_text_style('legend') | |
699 | or return; | |
700 | ||
701 | my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1); | |
702 | for my $count (0 .. $tic_count - 1) { | |
703 | my $x1 = $graph_box->[0] - 5; | |
704 | my $x2 = $graph_box->[0] + 5; | |
23a3585a | 705 | my $y1 = $graph_box->[3] - ($count * $tic_distance) + 1; |
2eac77fc | 706 | |
707 | my $value = sprintf("%.2f", ($count*$interval)+$min); | |
23a3585a | 708 | |
2eac77fc | 709 | my @box = $self->_text_bbox($value, 'legend') |
710 | or return; | |
711 | ||
712 | $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000'); | |
713 | ||
714 | my $width = $box[2]; | |
715 | my $height = $box[3]; | |
716 | ||
717 | $img->string(%text_info, | |
718 | x => ($x1 - $width - 3), | |
719 | y => ($y1 + ($height / 2)), | |
720 | text => $value | |
721 | ); | |
722 | } | |
723 | ||
724 | } | |
725 | ||
726 | sub _draw_x_tics { | |
727 | my $self = shift; | |
728 | ||
729 | my $img = $self->_get_image(); | |
730 | my $graph_box = $self->_get_graph_box(); | |
731 | my $image_box = $self->_get_image_box(); | |
732 | ||
733 | my $labels = $self->_get_labels(); | |
734 | ||
735 | my $tic_count = (scalar @$labels) - 1; | |
736 | ||
737 | my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'}); | |
738 | ||
739 | # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge | |
740 | my $denominator = $tic_count; | |
741 | if ($has_columns) { | |
742 | $denominator ++; | |
743 | } | |
744 | my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator); | |
745 | my %text_info = $self->_text_style('legend') | |
746 | or return; | |
747 | ||
748 | for my $count (0 .. $tic_count) { | |
749 | my $label = $labels->[$count]; | |
750 | my $x1 = $graph_box->[0] + ($tic_distance * $count); | |
751 | ||
752 | if ($has_columns) { | |
753 | $x1 += $tic_distance / 2; | |
754 | } | |
755 | my $y1 = $graph_box->[3] + 5; | |
756 | my $y2 = $graph_box->[3] - 5; | |
757 | ||
758 | $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000'); | |
759 | ||
760 | my @box = $self->_text_bbox($label, 'legend') | |
761 | or return; | |
762 | ||
763 | my $width = $box[2]; | |
764 | my $height = $box[3]; | |
765 | ||
766 | $img->string(%text_info, | |
767 | x => ($x1 - ($width / 2)), | |
768 | y => ($y1 + ($height + 5)), | |
769 | text => $label | |
770 | ); | |
771 | ||
772 | } | |
773 | } | |
774 | ||
775 | sub _valid_input { | |
776 | my $self = shift; | |
777 | ||
778 | if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) { | |
779 | return $self->_error("No data supplied"); | |
780 | } | |
781 | ||
782 | my $data = $self->_get_data_series(); | |
783 | if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) { | |
784 | return $self->_error("No values in data series"); | |
785 | } | |
786 | if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) { | |
787 | return $self->_error("No values in data series"); | |
788 | } | |
789 | if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) { | |
790 | return $self->_error("No values in data series"); | |
791 | } | |
792 | ||
793 | return 1; | |
794 | } | |
795 | ||
796 | sub _set_column_count { $_[0]->{'column_count'} = $_[1]; } | |
797 | sub _set_min_value { $_[0]->{'min_value'} = $_[1]; } | |
798 | sub _set_max_value { $_[0]->{'max_value'} = $_[1]; } | |
799 | sub _set_image_box { $_[0]->{'image_box'} = $_[1]; } | |
800 | sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; } | |
801 | sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; } | |
802 | sub _get_column_count { return $_[0]->{'column_count'} } | |
803 | sub _get_min_value { return $_[0]->{'min_value'} } | |
804 | sub _get_max_value { return $_[0]->{'max_value'} } | |
805 | sub _get_image_box { return $_[0]->{'image_box'} } | |
806 | sub _get_graph_box { return $_[0]->{'graph_box'} } | |
807 | sub _get_series_counter { return $_[0]->{'series_counter'} } | |
808 | ||
2eac77fc | 809 | 1; |