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 $color = $self->_data_color($series_counter);
474 # We need to add these last, otherwise the next line segment will overwrite half of the marker
475 my @marker_positions;
476 for (my $i = 0; $i < $data_size - 1; $i++) {
477 my $x1 = $left + $i * $interval;
478 my $x2 = $left + ($i + 1) * $interval;
480 $x1 += $has_columns * $interval / 2;
481 $x2 += $has_columns * $interval / 2;
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;
486 push @marker_positions, [$x1, $y1];
487 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => 1, color => $color) || die $img->errstr;
490 my $x2 = $left + ($data_size - 1) * $interval;
491 $x2 += $has_columns * $interval / 2;
493 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $size;
495 push @marker_positions, [$x2, $y2];
496 foreach my $position (@marker_positions) {
497 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
502 $self->_set_series_counter($series_counter);
507 my ($self, $index) = @_;
509 my $markers = $self->{'_style'}{'line_markers'};
513 my $marker = $markers->[$index % @$markers];
518 sub _draw_line_marker {
520 my ($x1, $y1, $series_counter) = @_;
522 my $img = $self->_get_image();
524 my $style = $self->_line_marker($series_counter);
525 return unless $style;
527 my $type = $style->{'shape'};
528 my $radius = $style->{'radius'};
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);
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);
538 elsif ($type eq 'diamond') {
539 # The gradient really doesn't work for diamond
540 my $color = $self->_data_color($series_counter);
543 [$x1 - $radius, $y1],
544 [$x1, $y1 + $radius],
545 [$x1 + $radius, $y1],
546 [$x1, $y1 - $radius],
548 filled => 1, color => $color, aa => 1);
550 elsif ($type eq 'triangle') {
551 # The gradient really doesn't work for triangle
552 my $color = $self->_data_color($series_counter);
555 [$x1 - $radius, $y1 + $radius],
556 [$x1 + $radius, $y1 + $radius],
557 [$x1, $y1 - $radius],
559 filled => 1, color => $color, aa => 1);
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;
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;
576 my $style = $self->{'_style'};
578 my $img = $self->_get_image();
580 my $max_value = $self->_get_max_value();
581 my $min_value = $self->_get_min_value();
582 my $column_count = $self->_get_column_count();
584 my $value_range = $max_value - $min_value;
586 my $width = $self->_get_number('width');
587 my $height = $self->_get_number('height');
588 my $size = $self->_get_number('size');
590 my $bottom = ($height - $size) / 2;
591 my $left = ($width - $size) / 2 + 1;
593 my $zero_position = int($bottom + $size - (-1*$min_value / $value_range) * ($size -1));
595 if ($self->_get_y_tics()) {
596 $self->_draw_y_tics();
598 if ($self->_get_labels()) {
599 $self->_draw_x_tics();
602 my $bar_width = int(($size)/ $column_count - 2);
605 if ($style->{'features'}{'outline'}) {
606 $outline_color = $self->_get_color('outline.line');
609 my $series_counter = $self->_get_series_counter() || 0;
610 my $col_series = $self->_get_data_series()->{'column'};
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;
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);
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;
628 my $x2 = $x1 + $bar_width;
630 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size);
632 my $color = $self->_data_color($series_counter);
634 # my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position]);
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);
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);
652 $self->_set_series_counter($series_counter);
656 sub _draw_stacked_columns {
658 my $style = $self->{'_style'};
660 my $img = $self->_get_image();
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;
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');
672 if ($self->_get_y_tics()) {
673 $self->_draw_y_tics();
675 if ($self->_get_labels()) {
676 $self->_draw_x_tics();
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);
687 if ($style->{'features'}{'outline'}) {
688 $outline_color = $self->_get_color('outline.line');
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;
703 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size;
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);
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);
721 $self->_set_series_counter($series_counter);
725 sub _add_data_series {
727 my $series_type = shift;
728 my $data_ref = shift;
729 my $series_name = shift;
731 my $graph_data = $self->{'graph_data'} || {};
733 my $series = $graph_data->{$series_type} || [];
735 push @$series, { data => $data_ref, series_name => $series_name };
737 $graph_data->{$series_type} = $series;
739 $self->{'graph_data'} = $graph_data;
745 =item set_y_tics($count)
747 Set the number of Y tics to use. Their value and position will be determined by the data range.
752 $_[0]->{'y_tics'} = $_[1];
756 return $_[0]->{'y_tics'};
761 my $min = $self->_get_min_value();
762 my $max = $self->_get_max_value();
763 my $tic_count = $self->_get_y_tics();
765 my $img = $self->_get_image();
766 my $graph_box = $self->_get_graph_box();
767 my $image_box = $self->_get_image_box();
769 my $interval = ($max - $min) / ($tic_count - 1);
771 my %text_info = $self->_text_style('legend')
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;
778 my $y1 = $graph_box->[3] - ($count * $tic_distance) + 1;
780 my $value = sprintf("%.2f", ($count*$interval)+$min);
782 my @box = $self->_text_bbox($value, 'legend')
785 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
788 my $height = $box[3];
790 $img->string(%text_info,
791 x => ($x1 - $width - 3),
792 y => ($y1 + ($height / 2)),
802 my $img = $self->_get_image();
803 my $graph_box = $self->_get_graph_box();
804 my $image_box = $self->_get_image_box();
806 my $labels = $self->_get_labels();
808 my $tic_count = (scalar @$labels) - 1;
810 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
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;
817 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
818 my %text_info = $self->_text_style('legend')
821 for my $count (0 .. $tic_count) {
822 my $label = $labels->[$count];
823 my $x1 = $graph_box->[0] + ($tic_distance * $count);
826 $x1 += $tic_distance / 2;
828 my $y1 = $graph_box->[3] + 5;
829 my $y2 = $graph_box->[3] - 5;
831 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
833 my @box = $self->_text_bbox($label, 'legend')
837 my $height = $box[3];
839 $img->string(%text_info,
840 x => ($x1 - ($width / 2)),
841 y => ($y1 + ($height + 5)),
851 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
852 return $self->_error("No data supplied");
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");
859 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
860 return $self->_error("No values in data series");
862 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
863 return $self->_error("No values in data series");
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'} }