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 my $line_aa = $self->_get_number("lineaa");
462 foreach my $series (@$line_series) {
463 my @data = @{$series->{'data'}};
464 my $data_size = scalar @data;
468 $interval = $graph_width / ($data_size);
471 $interval = $graph_width / ($data_size - 1);
473 my $color = $self->_data_color($series_counter);
475 # We need to add these last, otherwise the next line segment will overwrite half of the marker
476 my @marker_positions;
477 for (my $i = 0; $i < $data_size - 1; $i++) {
478 my $x1 = $left + $i * $interval;
479 my $x2 = $left + ($i + 1) * $interval;
481 $x1 += $has_columns * $interval / 2;
482 $x2 += $has_columns * $interval / 2;
484 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size;
485 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $size;
487 push @marker_positions, [$x1, $y1];
488 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
491 my $x2 = $left + ($data_size - 1) * $interval;
492 $x2 += $has_columns * $interval / 2;
494 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $size;
496 push @marker_positions, [$x2, $y2];
497 foreach my $position (@marker_positions) {
498 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
503 $self->_set_series_counter($series_counter);
508 my ($self, $index) = @_;
510 my $markers = $self->{'_style'}{'line_markers'};
514 my $marker = $markers->[$index % @$markers];
519 sub _draw_line_marker {
521 my ($x1, $y1, $series_counter) = @_;
523 my $img = $self->_get_image();
525 my $style = $self->_line_marker($series_counter);
526 return unless $style;
528 my $type = $style->{'shape'};
529 my $radius = $style->{'radius'};
531 my $line_aa = $self->_get_number("lineaa");
532 my $fill_aa = $self->_get_number("fill.aa");
533 if ($type eq 'circle') {
534 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
535 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
537 elsif ($type eq 'square') {
538 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
539 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
541 elsif ($type eq 'diamond') {
542 # The gradient really doesn't work for diamond
543 my $color = $self->_data_color($series_counter);
546 [$x1 - $radius, $y1],
547 [$x1, $y1 + $radius],
548 [$x1 + $radius, $y1],
549 [$x1, $y1 - $radius],
551 filled => 1, color => $color, aa => $fill_aa);
553 elsif ($type eq 'triangle') {
554 # The gradient really doesn't work for triangle
555 my $color = $self->_data_color($series_counter);
558 [$x1 - $radius, $y1 + $radius],
559 [$x1 + $radius, $y1 + $radius],
560 [$x1, $y1 - $radius],
562 filled => 1, color => $color, aa => $fill_aa);
565 elsif ($type eq 'x') {
566 my $color = $self->_data_color($series_counter);
567 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
568 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
570 elsif ($type eq 'plus') {
571 my $color = $self->_data_color($series_counter);
572 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
573 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
579 my $style = $self->{'_style'};
581 my $img = $self->_get_image();
583 my $max_value = $self->_get_max_value();
584 my $min_value = $self->_get_min_value();
585 my $column_count = $self->_get_column_count();
587 my $value_range = $max_value - $min_value;
589 my $width = $self->_get_number('width');
590 my $height = $self->_get_number('height');
591 my $size = $self->_get_number('size');
593 my $bottom = ($height - $size) / 2;
594 my $left = ($width - $size) / 2 + 1;
596 my $zero_position = int($bottom + $size - (-1*$min_value / $value_range) * ($size -1));
598 if ($self->_get_y_tics()) {
599 $self->_draw_y_tics();
601 if ($self->_get_labels()) {
602 $self->_draw_x_tics();
605 my $bar_width = int(($size)/ $column_count - 2);
608 if ($style->{'features'}{'outline'}) {
609 $outline_color = $self->_get_color('outline.line');
612 my $series_counter = $self->_get_series_counter() || 0;
613 my $col_series = $self->_get_data_series()->{'column'};
615 # 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.
616 my $column_series = 0;
618 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
619 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
621 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
622 my $series = $col_series->[$series_pos];
623 my @data = @{$series->{'data'}};
624 my $data_size = scalar @data;
625 my $color = $self->_data_color($series_counter);
626 for (my $i = 0; $i < $data_size; $i++) {
627 my $x1 = int($left + $bar_width * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos;
628 if ($has_stacked_columns) {
629 $x1 += ($i + 1) * $bar_width + $i + 1;
631 my $x2 = $x1 + $bar_width;
633 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size);
635 my $color = $self->_data_color($series_counter);
637 # my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position]);
639 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1);
640 if ($style->{'features'}{'outline'}) {
641 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
645 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1);
646 if ($style->{'features'}{'outline'}) {
647 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
655 $self->_set_series_counter($series_counter);
659 sub _draw_stacked_columns {
661 my $style = $self->{'_style'};
663 my $img = $self->_get_image();
665 my $max_value = $self->_get_max_value();
666 my $min_value = $self->_get_min_value();
667 my $column_count = $self->_get_column_count();
668 my $value_range = $max_value - $min_value;
670 my $graph_box = $self->_get_graph_box();
671 my $left = $graph_box->[0] + 1;
672 my $bottom = $graph_box->[1];
673 my $size = $self->_get_number('size');
675 if ($self->_get_y_tics()) {
676 $self->_draw_y_tics();
678 if ($self->_get_labels()) {
679 $self->_draw_x_tics();
682 my $bar_width = int($size / $column_count -2);
683 my $column_series = 0;
684 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
685 $column_series = (scalar @$column_series_data);
690 if ($style->{'features'}{'outline'}) {
691 $outline_color = $self->_get_color('outline.line');
694 my $zero_position = $bottom + $size - (-1*$min_value / $value_range) * ($size -1);
695 my $col_series = $self->_get_data_series()->{'stacked_column'};
696 my $series_counter = $self->_get_series_counter() || 0;
697 foreach my $series (@$col_series) {
698 my @data = @{$series->{'data'}};
699 my $data_size = scalar @data;
700 my $color = $self->_data_color($series_counter);
701 for (my $i = 0; $i < $data_size; $i++) {
702 my $x1 = int($left + $bar_width * ($column_series * $i)) + $column_series * $i;
703 # my $x1 = $left + $i * $size / ($data_size);
704 my $x2 = $x1 + $bar_width;
706 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $size;
709 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1);
710 if ($style->{'features'}{'outline'}) {
711 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
715 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1);
716 if ($style->{'features'}{'outline'}) {
717 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
724 $self->_set_series_counter($series_counter);
728 sub _add_data_series {
730 my $series_type = shift;
731 my $data_ref = shift;
732 my $series_name = shift;
734 my $graph_data = $self->{'graph_data'} || {};
736 my $series = $graph_data->{$series_type} || [];
738 push @$series, { data => $data_ref, series_name => $series_name };
740 $graph_data->{$series_type} = $series;
742 $self->{'graph_data'} = $graph_data;
748 =item set_y_tics($count)
750 Set the number of Y tics to use. Their value and position will be determined by the data range.
755 $_[0]->{'y_tics'} = $_[1];
759 return $_[0]->{'y_tics'};
764 my $min = $self->_get_min_value();
765 my $max = $self->_get_max_value();
766 my $tic_count = $self->_get_y_tics();
768 my $img = $self->_get_image();
769 my $graph_box = $self->_get_graph_box();
770 my $image_box = $self->_get_image_box();
772 my $interval = ($max - $min) / ($tic_count - 1);
774 my %text_info = $self->_text_style('legend')
777 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
778 for my $count (0 .. $tic_count - 1) {
779 my $x1 = $graph_box->[0] - 5;
780 my $x2 = $graph_box->[0] + 5;
781 my $y1 = $graph_box->[3] - ($count * $tic_distance) + 1;
783 my $value = sprintf("%.2f", ($count*$interval)+$min);
785 my @box = $self->_text_bbox($value, 'legend')
788 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
791 my $height = $box[3];
793 $img->string(%text_info,
794 x => ($x1 - $width - 3),
795 y => ($y1 + ($height / 2)),
805 my $img = $self->_get_image();
806 my $graph_box = $self->_get_graph_box();
807 my $image_box = $self->_get_image_box();
809 my $labels = $self->_get_labels();
811 my $tic_count = (scalar @$labels) - 1;
813 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
815 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
816 my $denominator = $tic_count;
820 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
821 my %text_info = $self->_text_style('legend')
824 for my $count (0 .. $tic_count) {
825 my $label = $labels->[$count];
826 my $x1 = $graph_box->[0] + ($tic_distance * $count);
829 $x1 += $tic_distance / 2;
831 my $y1 = $graph_box->[3] + 5;
832 my $y2 = $graph_box->[3] - 5;
834 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
836 my @box = $self->_text_bbox($label, 'legend')
840 my $height = $box[3];
842 $img->string(%text_info,
843 x => ($x1 - ($width / 2)),
844 y => ($y1 + ($height + 5)),
854 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
855 return $self->_error("No data supplied");
858 my $data = $self->_get_data_series();
859 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
860 return $self->_error("No values in data series");
862 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
863 return $self->_error("No values in data series");
865 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
866 return $self->_error("No values in data series");
872 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
873 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
874 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
875 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
876 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
877 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
878 sub _get_column_count { return $_[0]->{'column_count'} }
879 sub _get_min_value { return $_[0]->{'min_value'} }
880 sub _get_max_value { return $_[0]->{'max_value'} }
881 sub _get_image_box { return $_[0]->{'image_box'} }
882 sub _get_graph_box { return $_[0]->{'graph_box'} }
883 sub _get_series_counter { return $_[0]->{'series_counter'} }