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;
18 =item add_data_series(\@data, $series_name)
20 Add a data series to the graph, of the default type.
27 my $series_name = shift;
29 my $series_type = $self->_get_default_series_type();
30 $self->_add_data_series($series_type, $data_ref, $series_name);
35 =item add_column_data_series(\@data, $series_name)
37 Add a column data series to the graph.
41 sub add_column_data_series {
44 my $series_name = shift;
46 $self->_add_data_series('column', $data_ref, $series_name);
51 =item add_stacked_column_data_series(\@data, $series_name)
53 Add a stacked column data series to the graph.
57 sub add_stacked_column_data_series {
60 my $series_name = shift;
62 $self->_add_data_series('stacked_column', $data_ref, $series_name);
67 =item add_line_data_series(\@data, $series_name)
69 Add a line data series to the graph.
73 sub add_line_data_series {
76 my $series_name = shift;
78 $self->_add_data_series('line', $data_ref, $series_name);
83 =item add_area_data_series(\@data, $series_name)
85 Add a area data series to the graph.
89 sub add_area_data_series {
92 my $series_name = shift;
94 $self->_add_data_series('area', $data_ref, $series_name);
100 =item set_y_max($value)
102 Sets the maximum y value to be displayed. This will be ignored if the y_max is lower than the highest value.
107 $_[0]->{'custom_style'}->{'y_max'} = $_[1];
110 =item set_y_min($value)
112 Sets the minimum y value to be displayed. This will be ignored if the y_min is higher than the lowest value.
117 $_[0]->{'custom_style'}->{'y_min'} = $_[1];
120 =item set_column_padding($int)
122 Sets the padding between columns. This is a percentage of the column width. Defaults to 0.
126 sub set_column_padding {
127 $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
130 =item set_range_padding($percentage)
132 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.
134 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.
138 sub set_range_padding {
139 $_[0]->{'custom_style'}->{'range_padding'} = $_[1];
142 =item set_negative_background($color)
144 Sets the background color used below the x axis.
148 sub set_negative_background {
149 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
159 my ($self, %opts) = @_;
161 if (!$self->_valid_input()) {
165 $self->_style_setup(\%opts);
167 my $style = $self->{_style};
169 my $img = $self->_get_image()
172 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
173 $self->_set_image_box(\@image_box);
175 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
176 $self->_draw_legend(\@chart_box);
177 if ($style->{title}{text}) {
178 $self->_draw_title($img, \@chart_box)
182 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
183 return unless $self->_get_data_range();
184 $self->_remove_tics_from_chart_box(\@chart_box);
185 my $column_count = $self->_get_column_count();
187 my $width = $self->_get_number('width');
188 my $height = $self->_get_number('height');
190 my $graph_width = $chart_box[2] - $chart_box[0];
191 my $graph_height = $chart_box[3] - $chart_box[1];
193 my $col_width = ($graph_width - 1) / $column_count;
194 if ($col_width > 1) {
195 $graph_width = int($col_width) * $column_count + 1;
198 $graph_width = $col_width * $column_count + 1;
201 my $tic_count = $self->_get_y_tics();
202 my $tic_distance = ($graph_height-1) / ($tic_count - 1);
203 $graph_height = int($tic_distance * ($tic_count - 1));
205 my $bottom = $chart_box[1];
206 my $left = $chart_box[0];
208 $self->{'_style'}{'graph_width'} = $graph_width;
209 $self->{'_style'}{'graph_height'} = $graph_height;
211 my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
212 $self->_set_graph_box(\@graph_box);
215 color => $self->_get_color('outline.line'),
217 xmax => $left+$graph_width,
219 ymax => $bottom+$graph_height,
223 color => $self->_get_color('bg'),
225 xmax => $left+$graph_width - 1,
227 ymax => $bottom+$graph_height-1 ,
231 my $min_value = $self->_get_min_value();
232 my $max_value = $self->_get_max_value();
233 my $value_range = $max_value - $min_value;
237 $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
240 if ($min_value < 0) {
242 color => $self->_get_color('negative_bg'),
244 xmax => $left+$graph_width- 1,
245 ymin => $zero_position,
246 ymax => $bottom+$graph_height - 1,
251 y1 => $zero_position,
252 x2 => $left + $graph_width,
253 y2 => $zero_position,
254 color => $self->_get_color('outline.line'),
258 if ($self->_get_data_series()->{'stacked_column'}) {
259 return unless $self->_draw_stacked_columns();
261 if ($self->_get_data_series()->{'column'}) {
262 return unless $self->_draw_columns();
264 if ($self->_get_data_series()->{'line'}) {
265 return unless $self->_draw_lines();
267 if ($self->_get_data_series()->{'area'}) {
268 return unless $self->_draw_area();
271 if ($self->_get_y_tics()) {
272 $self->_draw_y_tics();
274 if ($self->_get_labels()) {
275 $self->_draw_x_tics();
278 return $self->_get_image();
281 sub _get_data_range {
286 my $column_count = 0;
288 my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
289 my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
290 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
291 my ($a_min, $a_max, $a_cols) = $self->_get_area_range();
293 # These are side by side...
296 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min, $a_min);
297 $max_value = $self->_max(0, $sc_max, $c_max, $l_max, $a_max);
299 my $config_min = $self->_get_number('y_min');
300 my $config_max = $self->_get_number('y_max');
302 if (defined $config_max && $config_max < $max_value) {
305 if (defined $config_min && $config_min > $min_value) {
309 my $range_padding = $self->_get_number('range_padding');
310 if (defined $config_min) {
311 $min_value = $config_min;
314 if ($min_value > 0) {
317 if ($range_padding && $min_value < 0) {
318 my $difference = $min_value * $range_padding / 100;
319 if ($min_value < -1 && $difference > -1) {
322 $min_value += $difference;
325 if (defined $config_max) {
326 $max_value = $config_max;
329 if ($range_padding && $max_value > 0) {
330 my $difference = $max_value * $range_padding / 100;
331 if ($max_value > 1 && $difference < 1) {
334 $max_value += $difference;
337 $column_count = $self->_max(0, $sc_cols, $l_cols, $a_cols);
339 if ($self->_get_number('automatic_axis')) {
340 # In case this was set via a style, and not by the api method
341 eval { require Chart::Math::Axis; };
343 return $self->_error("Can't use automatic_axis - $@");
346 my $axis = Chart::Math::Axis->new();
347 $axis->include_zero();
348 $axis->add_data($min_value, $max_value);
349 $max_value = $axis->top;
350 $min_value = $axis->bottom;
351 my $ticks = $axis->ticks;
352 # The +1 is there because we have the bottom tick as well
353 $self->set_y_tics($ticks+1);
356 $self->_set_max_value($max_value);
357 $self->_set_min_value($min_value);
358 $self->_set_column_count($column_count);
367 foreach my $value (@_) {
368 next unless defined $value;
369 if ($value < $min) { $min = $value; }
378 foreach my $value (@_) {
379 next unless defined $value;
380 if ($value > $min) { $min = $value; }
385 sub _get_line_range {
387 my $series = $self->_get_data_series()->{'line'};
388 return (undef, undef, 0) unless $series;
391 my $min_value = STARTING_MIN_VALUE;
392 my $column_count = 0;
394 my @series = @{$series};
395 foreach my $series (@series) {
396 my @data = @{$series->{'data'}};
398 if (scalar @data > $column_count) {
399 $column_count = scalar @data;
402 foreach my $value (@data) {
403 if ($value > $max_value) { $max_value = $value; }
404 if ($value < $min_value) { $min_value = $value; }
408 return ($min_value, $max_value, $column_count);
411 sub _get_area_range {
413 my $series = $self->_get_data_series()->{'area'};
414 return (undef, undef, 0) unless $series;
417 my $min_value = STARTING_MIN_VALUE;
418 my $column_count = 0;
420 my @series = @{$series};
421 foreach my $series (@series) {
422 my @data = @{$series->{'data'}};
424 if (scalar @data > $column_count) {
425 $column_count = scalar @data;
428 foreach my $value (@data) {
429 if ($value > $max_value) { $max_value = $value; }
430 if ($value < $min_value) { $min_value = $value; }
434 return ($min_value, $max_value, $column_count);
438 sub _get_column_range {
441 my $series = $self->_get_data_series()->{'column'};
442 return (undef, undef, 0) unless $series;
445 my $min_value = STARTING_MIN_VALUE;
446 my $column_count = 0;
448 my @series = @{$series};
449 foreach my $series (@series) {
450 my @data = @{$series->{'data'}};
452 foreach my $value (@data) {
454 if ($value > $max_value) { $max_value = $value; }
455 if ($value < $min_value) { $min_value = $value; }
459 return ($min_value, $max_value, $column_count);
462 sub _get_stacked_column_range {
466 my $min_value = STARTING_MIN_VALUE;
467 my $column_count = 0;
469 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
470 my @series = @{$self->_get_data_series()->{'stacked_column'}};
474 for (my $i = scalar @series - 1; $i >= 0; $i--) {
475 my $series = $series[$i];
476 my $data = $series->{'data'};
478 for (my $i = 0; $i < scalar @$data; $i++) {
480 if ($data->[$i] > 0) {
481 $value = $data->[$i] + ($max_entries[$i] || 0);
482 $data->[$i] = $value;
483 $max_entries[$i] = $value;
485 elsif ($data->[$i] < 0) {
486 $value = $data->[$i] + ($min_entries[$i] || 0);
487 $data->[$i] = $value;
488 $min_entries[$i] = $value;
490 if ($value > $max_value) { $max_value = $value; }
491 if ($value < $min_value) { $min_value = $value; }
493 if (scalar @$data > $column_count) {
494 $column_count = scalar @$data;
498 return ($min_value, $max_value, $column_count);
503 my $chart_box = shift;
504 my $style = $self->{'_style'};
507 my $img = $self->_get_image();
508 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
509 push @labels, map { $_->{'series_name'} } @$series;
511 if (my $series = $self->_get_data_series()->{'column'}) {
512 push @labels, map { $_->{'series_name'} } @$series;
514 if (my $series = $self->_get_data_series()->{'line'}) {
515 push @labels, map { $_->{'series_name'} } @$series;
517 if (my $series = $self->_get_data_series()->{'area'}) {
518 push @labels, map { $_->{'series_name'} } @$series;
521 if ($style->{features}{legend} && (scalar @labels)) {
522 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
528 sub _draw_flat_legend {
534 my $style = $self->{'_style'};
536 my $img = $self->_get_image();
538 my $max_value = $self->_get_max_value();
539 my $min_value = $self->_get_min_value();
540 my $column_count = $self->_get_column_count();
542 my $value_range = $max_value - $min_value;
544 my $width = $self->_get_number('width');
545 my $height = $self->_get_number('height');
547 my $graph_width = $self->_get_number('graph_width');
548 my $graph_height = $self->_get_number('graph_height');
550 my $line_series = $self->_get_data_series()->{'line'};
551 my $series_counter = $self->_get_series_counter() || 0;
553 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
555 my $col_width = int($graph_width / $column_count) -1;
557 my $graph_box = $self->_get_graph_box();
558 my $left = $graph_box->[0] + 1;
559 my $bottom = $graph_box->[1];
561 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
563 my $line_aa = $self->_get_number("lineaa");
564 foreach my $series (@$line_series) {
565 my @data = @{$series->{'data'}};
566 my $data_size = scalar @data;
570 $interval = $graph_width / ($data_size);
573 $interval = $graph_width / ($data_size - 1);
575 my $color = $self->_data_color($series_counter);
577 # We need to add these last, otherwise the next line segment will overwrite half of the marker
578 my @marker_positions;
579 for (my $i = 0; $i < $data_size - 1; $i++) {
580 my $x1 = $left + $i * $interval;
581 my $x2 = $left + ($i + 1) * $interval;
583 $x1 += $has_columns * $interval / 2;
584 $x2 += $has_columns * $interval / 2;
586 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
587 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
589 push @marker_positions, [$x1, $y1];
590 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
593 my $x2 = $left + ($data_size - 1) * $interval;
594 $x2 += $has_columns * $interval / 2;
596 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
598 push @marker_positions, [$x2, $y2];
599 foreach my $position (@marker_positions) {
600 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
605 $self->_set_series_counter($series_counter);
609 sub _area_data_fill {
610 my ($self, $index, $box) = @_;
612 my %fill = $self->_data_fill($index, $box);
614 my $opacity = $self->_get_number("area.opacity");
618 my $orig_fill = $fill{fill};
619 unless ($orig_fill) {
620 $orig_fill = Imager::Fill->new
622 solid => $fill{color},
628 fill => Imager::Fill->new
639 my $style = $self->{'_style'};
641 my $img = $self->_get_image();
643 my $max_value = $self->_get_max_value();
644 my $min_value = $self->_get_min_value();
645 my $column_count = $self->_get_column_count();
647 my $value_range = $max_value - $min_value;
649 my $width = $self->_get_number('width');
650 my $height = $self->_get_number('height');
652 my $graph_width = $self->_get_number('graph_width');
653 my $graph_height = $self->_get_number('graph_height');
655 my $area_series = $self->_get_data_series()->{'area'};
656 my $series_counter = $self->_get_series_counter() || 0;
658 my $col_width = int($graph_width / $column_count) -1;
660 my $graph_box = $self->_get_graph_box();
661 my $left = $graph_box->[0] + 1;
662 my $bottom = $graph_box->[1];
663 my $right = $graph_box->[2];
664 my $top = $graph_box->[3];
666 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
668 my $line_aa = $self->_get_number("lineaa");
669 foreach my $series (@$area_series) {
670 my @data = @{$series->{'data'}};
671 my $data_size = scalar @data;
673 my $interval = $graph_width / ($data_size - 1);
675 my $color = $self->_data_color($series_counter);
677 # We need to add these last, otherwise the next line segment will overwrite half of the marker
678 my @marker_positions;
680 for (my $i = 0; $i < $data_size - 1; $i++) {
681 my $x1 = $left + $i * $interval;
683 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
686 push @polygon_points, [$x1, $top];
688 push @polygon_points, [$x1, $y1];
690 push @marker_positions, [$x1, $y1];
693 my $x2 = $left + ($data_size - 1) * $interval;
695 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
696 push @polygon_points, [$x2, $y2];
697 push @polygon_points, [$x2, $top];
698 push @polygon_points, $polygon_points[0];
700 my @fill = $self->_area_data_fill($series_counter, [$left, $bottom, $right, $top]);
701 $img->polygon(points => [@polygon_points], @fill);
703 push @marker_positions, [$x2, $y2];
704 foreach my $position (@marker_positions) {
705 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
710 $self->_set_series_counter($series_counter);
717 my ($self, $index) = @_;
719 my $markers = $self->{'_style'}{'line_markers'};
723 my $marker = $markers->[$index % @$markers];
728 sub _draw_line_marker {
730 my ($x1, $y1, $series_counter) = @_;
732 my $img = $self->_get_image();
734 my $style = $self->_line_marker($series_counter);
735 return unless $style;
737 my $type = $style->{'shape'};
738 my $radius = $style->{'radius'};
740 my $line_aa = $self->_get_number("lineaa");
741 my $fill_aa = $self->_get_number("fill.aa");
742 if ($type eq 'circle') {
743 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
744 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
746 elsif ($type eq 'square') {
747 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
748 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
750 elsif ($type eq 'diamond') {
751 # The gradient really doesn't work for diamond
752 my $color = $self->_data_color($series_counter);
755 [$x1 - $radius, $y1],
756 [$x1, $y1 + $radius],
757 [$x1 + $radius, $y1],
758 [$x1, $y1 - $radius],
760 filled => 1, color => $color, aa => $fill_aa);
762 elsif ($type eq 'triangle') {
763 # The gradient really doesn't work for triangle
764 my $color = $self->_data_color($series_counter);
767 [$x1 - $radius, $y1 + $radius],
768 [$x1 + $radius, $y1 + $radius],
769 [$x1, $y1 - $radius],
771 filled => 1, color => $color, aa => $fill_aa);
774 elsif ($type eq 'x') {
775 my $color = $self->_data_color($series_counter);
776 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
777 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
779 elsif ($type eq 'plus') {
780 my $color = $self->_data_color($series_counter);
781 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
782 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
788 my $style = $self->{'_style'};
790 my $img = $self->_get_image();
792 my $max_value = $self->_get_max_value();
793 my $min_value = $self->_get_min_value();
794 my $column_count = $self->_get_column_count();
796 my $value_range = $max_value - $min_value;
798 my $width = $self->_get_number('width');
799 my $height = $self->_get_number('height');
801 my $graph_width = $self->_get_number('graph_width');
802 my $graph_height = $self->_get_number('graph_height');
805 my $graph_box = $self->_get_graph_box();
806 my $left = $graph_box->[0] + 1;
807 my $bottom = $graph_box->[1];
808 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
810 my $bar_width = $graph_width / $column_count;
813 if ($style->{'features'}{'outline'}) {
814 $outline_color = $self->_get_color('outline.line');
817 my $series_counter = $self->_get_series_counter() || 0;
818 my $col_series = $self->_get_data_series()->{'column'};
819 my $column_padding_percent = $self->_get_number('column_padding') || 0;
820 my $column_padding = int($column_padding_percent * $bar_width / 100);
822 # 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.
823 my $column_series = 0;
825 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
826 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
828 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
829 my $series = $col_series->[$series_pos];
830 my @data = @{$series->{'data'}};
831 my $data_size = scalar @data;
832 for (my $i = 0; $i < $data_size; $i++) {
833 my $part1 = $bar_width * (scalar @$col_series * $i);
834 my $part2 = ($series_pos) * $bar_width;
835 my $x1 = $left + $part1 + $part2;
836 if ($has_stacked_columns) {
837 $x1 += ($bar_width * ($i+1));
841 my $x2 = int($x1 + $bar_width - $column_padding)-1;
842 # Special case for when bar_width is less than 1.
847 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
849 my $color = $self->_data_color($series_counter);
852 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
853 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
854 if ($style->{'features'}{'outline'}) {
855 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
859 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
860 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
861 if ($style->{'features'}{'outline'}) {
862 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
870 $self->_set_series_counter($series_counter);
874 sub _draw_stacked_columns {
876 my $style = $self->{'_style'};
878 my $img = $self->_get_image();
880 my $max_value = $self->_get_max_value();
881 my $min_value = $self->_get_min_value();
882 my $column_count = $self->_get_column_count();
883 my $value_range = $max_value - $min_value;
885 my $graph_box = $self->_get_graph_box();
886 my $left = $graph_box->[0] + 1;
887 my $bottom = $graph_box->[1];
889 my $graph_width = $self->_get_number('graph_width');
890 my $graph_height = $self->_get_number('graph_height');
892 my $bar_width = $graph_width / $column_count;
893 my $column_series = 0;
894 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
895 $column_series = (scalar @$column_series_data);
899 my $column_padding_percent = $self->_get_number('column_padding') || 0;
900 if ($column_padding_percent < 0) {
901 return $self->_error("Column padding less than 0");
903 if ($column_padding_percent > 100) {
904 return $self->_error("Column padding greater than 0");
906 my $column_padding = int($column_padding_percent * $bar_width / 100);
909 if ($style->{'features'}{'outline'}) {
910 $outline_color = $self->_get_color('outline.line');
913 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
914 my $col_series = $self->_get_data_series()->{'stacked_column'};
915 my $series_counter = $self->_get_series_counter() || 0;
917 foreach my $series (@$col_series) {
918 my @data = @{$series->{'data'}};
919 my $data_size = scalar @data;
920 for (my $i = 0; $i < $data_size; $i++) {
921 my $part1 = $bar_width * $i * $column_series;
923 my $x1 = int($left + $part1 + $part2);
924 my $x2 = int($x1 + $bar_width - $column_padding) - 1;
925 # Special case for when bar_width is less than 1.
930 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
933 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
934 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
935 if ($style->{'features'}{'outline'}) {
936 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
940 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
941 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
942 if ($style->{'features'}{'outline'}) {
943 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
950 $self->_set_series_counter($series_counter);
954 sub _add_data_series {
956 my $series_type = shift;
957 my $data_ref = shift;
958 my $series_name = shift;
960 my $graph_data = $self->{'graph_data'} || {};
962 my $series = $graph_data->{$series_type} || [];
964 push @$series, { data => $data_ref, series_name => $series_name };
966 $graph_data->{$series_type} = $series;
968 $self->{'graph_data'} = $graph_data;
974 =item show_horizontal_gridlines()
976 Shows horizontal gridlines at the y-tics.
980 sub show_horizontal_gridlines {
981 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
984 =item use_automatic_axis()
986 Automatically scale the Y axis, based on L<Chart::Math::Axis>. If Chart::Math::Axis isn't installed, this sets an error and returns undef. Returns 1 if it is installed.
990 sub use_automatic_axis {
991 eval { require Chart::Math::Axis; };
993 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
995 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
1000 =item set_y_tics($count)
1002 Set the number of Y tics to use. Their value and position will be determined by the data range.
1007 $_[0]->{'y_tics'} = $_[1];
1011 return $_[0]->{'y_tics'} || 0;
1014 sub _remove_tics_from_chart_box {
1016 my $chart_box = shift;
1019 my $tic_width = $self->_get_y_tic_width() || 10;
1020 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
1023 my $tic_height = $self->_get_x_tic_height() || 10;
1024 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
1026 $self->_remove_box($chart_box, \@y_tic_box);
1027 $self->_remove_box($chart_box, \@x_tic_box);
1029 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
1030 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
1031 $self->_remove_box($chart_box, \@y_tic_tops);
1033 # Make sure that the first and last label fit
1034 if (my $labels = $self->_get_labels()) {
1035 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1036 my @remove_box = ($chart_box->[0],
1038 $chart_box->[0] + int($box[2] / 2) + 1,
1042 $self->_remove_box($chart_box, \@remove_box);
1044 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1045 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1051 $self->_remove_box($chart_box, \@remove_box);
1056 sub _get_y_tic_width{
1058 my $min = $self->_get_min_value();
1059 my $max = $self->_get_max_value();
1060 my $tic_count = $self->_get_y_tics();
1062 my $interval = ($max - $min) / ($tic_count - 1);
1064 my %text_info = $self->_text_style('legend')
1068 for my $count (0 .. $tic_count - 1) {
1069 my $value = sprintf("%.2f", ($count*$interval)+$min);
1071 my @box = $self->_text_bbox($value, 'legend');
1072 my $width = $box[2] - $box[0];
1076 if ($width > $max_width) {
1077 $max_width = $width;
1084 sub _get_x_tic_height {
1087 my $labels = $self->_get_labels();
1093 my $tic_count = (scalar @$labels) - 1;
1095 my %text_info = $self->_text_style('legend')
1099 for my $count (0 .. $tic_count) {
1100 my $label = $labels->[$count];
1102 my @box = $self->_text_bbox($label, 'legend');
1104 my $height = $box[3] - $box[1];
1108 if ($height > $max_height) {
1109 $max_height = $height;
1118 my $min = $self->_get_min_value();
1119 my $max = $self->_get_max_value();
1120 my $tic_count = $self->_get_y_tics();
1122 my $img = $self->_get_image();
1123 my $graph_box = $self->_get_graph_box();
1124 my $image_box = $self->_get_image_box();
1126 my $interval = ($max - $min) / ($tic_count - 1);
1128 my %text_info = $self->_text_style('legend')
1131 my $line_style = $self->_get_color('outline.line');
1132 my $show_gridlines = $self->_get_number('horizontal_gridlines');
1133 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
1134 for my $count (0 .. $tic_count - 1) {
1135 my $x1 = $graph_box->[0] - 5;
1136 my $x2 = $graph_box->[0] + 5;
1137 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
1139 my $value = ($count*$interval)+$min;
1140 if ($interval < 1 || ($value != int($value))) {
1141 $value = sprintf("%.2f", $value);
1144 my @box = $self->_text_bbox($value, 'legend')
1147 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1149 my $width = $box[2];
1150 my $height = $box[3];
1152 $img->string(%text_info,
1153 x => ($x1 - $width - 3),
1154 y => ($y1 + ($height / 2)),
1158 if ($show_gridlines) {
1159 # XXX - line styles!
1160 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1163 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1164 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1174 my $img = $self->_get_image();
1175 my $graph_box = $self->_get_graph_box();
1176 my $image_box = $self->_get_image_box();
1178 my $labels = $self->_get_labels();
1180 my $tic_count = (scalar @$labels) - 1;
1182 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1184 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1185 my $denominator = $tic_count;
1189 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1190 my %text_info = $self->_text_style('legend')
1193 # If automatic axis is turned on, let's be selective about what labels we draw.
1196 if ($self->_get_number('automatic_axis')) {
1197 foreach my $label (@$labels) {
1198 my @box = $self->_text_bbox($label, 'legend');
1199 if ($box[2] > $max_size) {
1200 $max_size = $box[2];
1204 # Give the max_size some padding...
1207 $tic_skip = int($max_size / $tic_distance) + 1;
1210 my $line_style = $self->_get_color('outline.line');
1212 for my $count (0 .. $tic_count) {
1213 next if ($count % ($tic_skip + 1));
1214 my $label = $labels->[$count];
1215 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1218 $x1 += $tic_distance / 2;
1223 my $y1 = $graph_box->[3] + 5;
1224 my $y2 = $graph_box->[3] - 5;
1226 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1228 my @box = $self->_text_bbox($label, 'legend')
1231 my $width = $box[2];
1232 my $height = $box[3];
1234 $img->string(%text_info,
1235 x => ($x1 - ($width / 2)),
1236 y => ($y1 + ($height + 5)),
1246 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1247 return $self->_error("No data supplied");
1250 my $data = $self->_get_data_series();
1251 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1252 return $self->_error("No values in data series");
1254 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1255 return $self->_error("No values in data series");
1257 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1258 return $self->_error("No values in data series");
1264 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1265 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1266 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1267 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1268 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1269 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1270 sub _get_column_count { return $_[0]->{'column_count'} }
1271 sub _get_min_value { return $_[0]->{'min_value'} }
1272 sub _get_max_value { return $_[0]->{'max_value'} }
1273 sub _get_image_box { return $_[0]->{'image_box'} }
1274 sub _get_graph_box { return $_[0]->{'graph_box'} }
1275 sub _get_series_counter { return $_[0]->{'series_counter'} }
1280 my %work = %{$self->SUPER::_style_defs()};