1 package Imager::Graph::Vertical;
5 Imager::Graph::Vertical- A super class for line/bar/column charts
12 @ISA = qw(Imager::Graph);
14 use constant STARTING_MIN_VALUE => 99999;
17 =item add_data_series(\@data, $series_name)
19 Add a data series to the graph, of the default type.
26 my $series_name = shift;
28 my $series_type = $self->_get_default_series_type();
29 $self->_add_data_series($series_type, $data_ref, $series_name);
34 =item add_column_data_series(\@data, $series_name)
36 Add a column data series to the graph.
40 sub add_column_data_series {
43 my $series_name = shift;
45 $self->_add_data_series('column', $data_ref, $series_name);
50 =item add_stacked_column_data_series(\@data, $series_name)
52 Add a stacked column data series to the graph.
56 sub add_stacked_column_data_series {
59 my $series_name = shift;
61 $self->_add_data_series('stacked_column', $data_ref, $series_name);
66 =item add_line_data_series(\@data, $series_name)
68 Add a line data series to the graph.
72 sub add_line_data_series {
75 my $series_name = shift;
77 $self->_add_data_series('line', $data_ref, $series_name);
82 =item set_y_max($value)
84 Sets the maximum y value to be displayed. This will be ignored if the y_max is lower than the highest value.
89 $_[0]->{'custom_style'}->{'y_max'} = $_[1];
92 =item set_y_min($value)
94 Sets the minimum y value to be displayed. This will be ignored if the y_min is higher than the lowest value.
99 $_[0]->{'custom_style'}->{'y_min'} = $_[1];
102 =item set_range_padding($percentage)
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.
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.
110 sub set_range_padding {
111 $_[0]->{'custom_style'}->{'range_padding'} = $_[1];
114 =item set_negative_background($color)
116 Sets the background color used below the x axis.
120 sub set_negative_background {
121 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
131 my ($self, %opts) = @_;
133 if (!$self->_valid_input()) {
137 $self->_style_setup(\%opts);
139 my $style = $self->{_style};
141 my $img = $self->_get_image()
144 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
145 $self->_set_image_box(\@image_box);
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();
151 my $width = $self->_get_number('width');
152 my $height = $self->_get_number('height');
153 my $size = $self->_get_number('size');
155 my $bottom = ($height - $size) / 2;
156 my $left = ($width - $size) / 2;
158 my $col_width = int($size / $column_count) -1;
159 my $graph_width = $col_width * $column_count + 1;
161 my @graph_box = ( $left, $bottom, $left + $graph_width - 1, $bottom + $size - 1 );
162 $self->_set_graph_box(\@graph_box);
164 $self->_draw_legend();
167 color => $self->_get_color('outline.line'),
169 xmax => $left+$graph_width,
171 ymax => $bottom+$size,
175 color => $self->_get_color('bg'),
177 xmax => $left+$graph_width - 1,
179 ymax => $bottom+$size -1 ,
183 my $min_value = $self->_get_min_value();
184 my $max_value = $self->_get_max_value();
185 my $value_range = $max_value - $min_value;
189 $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1);
192 if ($min_value < 0) {
194 color => $self->_get_color('negative_bg'),
196 xmax => $left+$graph_width- 1,
197 ymin => $zero_position,
198 ymax => $bottom+$size -1,
203 y1 => $zero_position,
204 x2 => $left + $graph_width,
205 y2 => $zero_position,
206 color => $self->_get_color('outline.line'),
210 if ($self->_get_data_series()->{'stacked_column'}) {
211 $self->_draw_stacked_columns();
213 if ($self->_get_data_series()->{'column'}) {
214 $self->_draw_columns();
216 if ($self->_get_data_series()->{'line'}) {
217 $self->_draw_lines();
219 return $self->_get_image();
222 sub _get_data_range {
227 my $column_count = 0;
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();
233 # These are side by side...
236 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min);
237 $max_value = $self->_max(0, $sc_max, $c_max, $l_max);
239 my $config_min = $self->_get_number('y_min');
240 my $config_max = $self->_get_number('y_max');
242 if (defined $config_max && $config_max < $max_value) {
245 if (defined $config_min && $config_min > $min_value) {
249 my $range_padding = $self->_get_number('range_padding');
250 if (!defined $range_padding) {
253 if (defined $config_min) {
254 $min_value = $config_min;
257 if ($min_value > 0) {
260 if ($range_padding && $min_value < 0) {
261 my $difference = $min_value * $range_padding / 100;
262 if ($min_value < -1 && $difference > -1) {
265 $min_value += $difference;
268 if (defined $config_max) {
269 $max_value = $config_max;
272 if ($range_padding && $max_value > 0) {
273 my $difference = $max_value * $range_padding / 100;
274 if ($max_value > 1 && $difference < 1) {
277 $max_value += $difference;
280 $column_count = $self->_max(0, $sc_cols, $l_cols);
282 $self->_set_max_value($max_value);
283 $self->_set_min_value($min_value);
284 $self->_set_column_count($column_count);
291 foreach my $value (@_) {
292 next unless defined $value;
293 if ($value < $min) { $min = $value; }
302 foreach my $value (@_) {
303 next unless defined $value;
304 if ($value > $min) { $min = $value; }
309 sub _get_line_range {
311 my $series = $self->_get_data_series()->{'line'};
312 return (undef, undef, 0) unless $series;
315 my $min_value = STARTING_MIN_VALUE;
316 my $column_count = 0;
318 my @series = @{$series};
319 foreach my $series (@series) {
320 my @data = @{$series->{'data'}};
322 if (scalar @data > $column_count) {
323 $column_count = scalar @data;
326 foreach my $value (@data) {
327 if ($value > $max_value) { $max_value = $value; }
328 if ($value < $min_value) { $min_value = $value; }
332 return ($min_value, $max_value, $column_count);
335 sub _get_column_range {
338 my $series = $self->_get_data_series()->{'column'};
339 return (undef, undef, 0) unless $series;
342 my $min_value = STARTING_MIN_VALUE;
343 my $column_count = 0;
345 my @series = @{$series};
346 foreach my $series (@series) {
347 my @data = @{$series->{'data'}};
349 foreach my $value (@data) {
351 if ($value > $max_value) { $max_value = $value; }
352 if ($value < $min_value) { $min_value = $value; }
356 return ($min_value, $max_value, $column_count);
359 sub _get_stacked_column_range {
363 my $min_value = STARTING_MIN_VALUE;
364 my $column_count = 0;
366 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
367 my @series = @{$self->_get_data_series()->{'stacked_column'}};
371 for (my $i = scalar @series - 1; $i >= 0; $i--) {
372 my $series = $series[$i];
373 my $data = $series->{'data'};
375 for (my $i = 0; $i < scalar @$data; $i++) {
377 if ($data->[$i] > 0) {
378 $value = $data->[$i] + ($max_entries[$i] || 0);
379 $data->[$i] = $value;
380 $max_entries[$i] = $value;
382 elsif ($data->[$i] < 0) {
383 $value = $data->[$i] + ($min_entries[$i] || 0);
384 $data->[$i] = $value;
385 $min_entries[$i] = $value;
387 if ($value > $max_value) { $max_value = $value; }
388 if ($value < $min_value) { $min_value = $value; }
390 if (scalar @$data > $column_count) {
391 $column_count = scalar @$data;
395 return ($min_value, $max_value, $column_count);
400 my $style = $self->{'_style'};
403 my $img = $self->_get_image();
404 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
405 push @labels, map { $_->{'series_name'} } @$series;
407 if (my $series = $self->_get_data_series()->{'column'}) {
408 push @labels, map { $_->{'series_name'} } @$series;
410 if (my $series = $self->_get_data_series()->{'line'}) {
411 push @labels, map { $_->{'series_name'} } @$series;
414 if ($style->{features}{legend} && (scalar @labels)) {
415 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $self->_get_image_box())
421 sub _draw_flat_legend {
427 my $style = $self->{'_style'};
429 my $img = $self->_get_image();
431 my $max_value = $self->_get_max_value();
432 my $min_value = $self->_get_min_value();
433 my $column_count = $self->_get_column_count();
435 my $value_range = $max_value - $min_value;
437 my $width = $self->_get_number('width');
438 my $height = $self->_get_number('height');
439 my $size = $self->_get_number('size');
441 my $bottom = ($height - $size) / 2;
442 my $left = ($width - $size) / 2;
444 my $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1);
446 if ($self->_get_y_tics()) {
447 $self->_draw_y_tics();
449 if ($self->_get_labels()) {
450 $self->_draw_x_tics();
453 my $line_series = $self->_get_data_series()->{'line'};
454 my $series_counter = $self->_get_series_counter() || 0;
456 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
458 my $col_width = int($size / $column_count) -1;
459 my $graph_width = $col_width * $column_count + 1;
461 foreach my $series (@$line_series) {
462 my @data = @{$series->{'data'}};
463 my $data_size = scalar @data;
467 $interval = $graph_width / ($data_size);
470 $interval = $graph_width / ($data_size - 1);
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;
478 $x1 += $has_columns * $interval / 2;
479 $x2 += $has_columns * $interval / 2;
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;
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);
488 my $x2 = $left + ($data_size - 1) * $interval;
489 $x2 += $has_columns * $interval / 2;
491 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $size;
493 $img->circle(x => $x2, y => $y2, r => 3, aa => 1, filled => 1, @fill);
497 $self->_set_series_counter($series_counter);
503 my $style = $self->{'_style'};
505 my $img = $self->_get_image();
507 my $max_value = $self->_get_max_value();
508 my $min_value = $self->_get_min_value();
509 my $column_count = $self->_get_column_count();
511 my $value_range = $max_value - $min_value;
513 my $width = $self->_get_number('width');
514 my $height = $self->_get_number('height');
515 my $size = $self->_get_number('size');
517 my $bottom = ($height - $size) / 2;
518 my $left = ($width - $size) / 2 + 1;
520 my $zero_position = int($bottom + $size - (-1*$min_value / $value_range) * ($size -1));
522 if ($self->_get_y_tics()) {
523 $self->_draw_y_tics();
525 if ($self->_get_labels()) {
526 $self->_draw_x_tics();
529 my $bar_width = int(($size)/ $column_count - 2);
532 if ($style->{'features'}{'outline'}) {
533 $outline_color = $self->_get_color('outline.line');
536 my $series_counter = $self->_get_series_counter() || 0;
537 my $col_series = $self->_get_data_series()->{'column'};
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;
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);
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;
555 my $x2 = $x1 + $bar_width;
557 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size);
559 my $color = $self->_data_color($series_counter);
561 # my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position]);
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);
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);
579 $self->_set_series_counter($series_counter);
583 sub _draw_stacked_columns {
585 my $style = $self->{'_style'};
587 my $img = $self->_get_image();
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;
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');
599 if ($self->_get_y_tics()) {
600 $self->_draw_y_tics();
602 if ($self->_get_labels()) {
603 $self->_draw_x_tics();
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);
614 if ($style->{'features'}{'outline'}) {
615 $outline_color = $self->_get_color('outline.line');
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;
630 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size;
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);
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);
648 $self->_set_series_counter($series_counter);
652 sub _add_data_series {
654 my $series_type = shift;
655 my $data_ref = shift;
656 my $series_name = shift;
658 my $graph_data = $self->{'graph_data'} || {};
660 my $series = $graph_data->{$series_type} || [];
662 push @$series, { data => $data_ref, series_name => $series_name };
664 $graph_data->{$series_type} = $series;
666 $self->{'graph_data'} = $graph_data;
672 =item set_y_tics($count)
674 Set the number of Y tics to use. Their value and position will be determined by the data range.
679 $_[0]->{'y_tics'} = $_[1];
683 return $_[0]->{'y_tics'};
688 my $min = $self->_get_min_value();
689 my $max = $self->_get_max_value();
690 my $tic_count = $self->_get_y_tics();
692 my $img = $self->_get_image();
693 my $graph_box = $self->_get_graph_box();
694 my $image_box = $self->_get_image_box();
696 my $interval = ($max - $min) / ($tic_count - 1);
698 my %text_info = $self->_text_style('legend')
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;
705 my $y1 = $graph_box->[3] - ($count * $tic_distance) + 1;
707 my $value = sprintf("%.2f", ($count*$interval)+$min);
709 my @box = $self->_text_bbox($value, 'legend')
712 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
715 my $height = $box[3];
717 $img->string(%text_info,
718 x => ($x1 - $width - 3),
719 y => ($y1 + ($height / 2)),
729 my $img = $self->_get_image();
730 my $graph_box = $self->_get_graph_box();
731 my $image_box = $self->_get_image_box();
733 my $labels = $self->_get_labels();
735 my $tic_count = (scalar @$labels) - 1;
737 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
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;
744 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
745 my %text_info = $self->_text_style('legend')
748 for my $count (0 .. $tic_count) {
749 my $label = $labels->[$count];
750 my $x1 = $graph_box->[0] + ($tic_distance * $count);
753 $x1 += $tic_distance / 2;
755 my $y1 = $graph_box->[3] + 5;
756 my $y2 = $graph_box->[3] - 5;
758 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
760 my @box = $self->_text_bbox($label, 'legend')
764 my $height = $box[3];
766 $img->string(%text_info,
767 x => ($x1 - ($width / 2)),
768 y => ($y1 + ($height + 5)),
778 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
779 return $self->_error("No data supplied");
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");
786 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
787 return $self->_error("No values in data series");
789 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
790 return $self->_error("No values in data series");
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'} }