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 $top = $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, $top, $left + $graph_width, $top + $graph_height);
212 $self->_set_graph_box(\@graph_box);
214 my @fill_box = ( $left, $top, $left+$graph_width, $top+$graph_height );
215 if ($self->{_style}{features}{graph_outline}) {
217 color => $self->_get_color('graph.outline'),
219 xmax => $left+$graph_width,
221 ymax => $top+$graph_height,
230 $self->_get_fill('graph.fill'),
234 my $min_value = $self->_get_min_value();
235 my $max_value = $self->_get_max_value();
236 my $value_range = $max_value - $min_value;
240 $zero_position = $top + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
243 if ($min_value < 0) {
245 color => $self->_get_color('negative_bg'),
247 xmax => $left+$graph_width- 1,
248 ymin => $zero_position,
249 ymax => $top+$graph_height - 1,
254 y1 => $zero_position,
255 x2 => $left + $graph_width,
256 y2 => $zero_position,
257 color => $self->_get_color('outline.line'),
261 if ($self->_get_data_series()->{'stacked_column'}) {
262 return unless $self->_draw_stacked_columns();
264 if ($self->_get_data_series()->{'column'}) {
265 return unless $self->_draw_columns();
267 if ($self->_get_data_series()->{'line'}) {
268 return unless $self->_draw_lines();
270 if ($self->_get_data_series()->{'area'}) {
271 return unless $self->_draw_area();
274 if ($self->_get_y_tics()) {
275 $self->_draw_y_tics();
277 if ($self->_get_labels()) {
278 $self->_draw_x_tics();
281 return $self->_get_image();
284 sub _get_data_range {
289 my $column_count = 0;
291 my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
292 my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
293 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
294 my ($a_min, $a_max, $a_cols) = $self->_get_area_range();
296 # These are side by side...
299 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min, $a_min);
300 $max_value = $self->_max(0, $sc_max, $c_max, $l_max, $a_max);
302 my $config_min = $self->_get_number('y_min');
303 my $config_max = $self->_get_number('y_max');
305 if (defined $config_max && $config_max < $max_value) {
308 if (defined $config_min && $config_min > $min_value) {
312 my $range_padding = $self->_get_number('range_padding');
313 if (defined $config_min) {
314 $min_value = $config_min;
317 if ($min_value > 0) {
320 if ($range_padding && $min_value < 0) {
321 my $difference = $min_value * $range_padding / 100;
322 if ($min_value < -1 && $difference > -1) {
325 $min_value += $difference;
328 if (defined $config_max) {
329 $max_value = $config_max;
332 if ($range_padding && $max_value > 0) {
333 my $difference = $max_value * $range_padding / 100;
334 if ($max_value > 1 && $difference < 1) {
337 $max_value += $difference;
340 $column_count = $self->_max(0, $sc_cols, $l_cols, $a_cols);
342 if ($self->_get_number('automatic_axis')) {
343 # In case this was set via a style, and not by the api method
344 eval { require Chart::Math::Axis; };
346 return $self->_error("Can't use automatic_axis - $@");
349 my $axis = Chart::Math::Axis->new();
350 $axis->include_zero();
351 $axis->add_data($min_value, $max_value);
352 $max_value = $axis->top;
353 $min_value = $axis->bottom;
354 my $ticks = $axis->ticks;
355 # The +1 is there because we have the bottom tick as well
356 $self->set_y_tics($ticks+1);
359 $self->_set_max_value($max_value);
360 $self->_set_min_value($min_value);
361 $self->_set_column_count($column_count);
370 foreach my $value (@_) {
371 next unless defined $value;
372 if ($value < $min) { $min = $value; }
381 foreach my $value (@_) {
382 next unless defined $value;
383 if ($value > $min) { $min = $value; }
388 sub _get_line_range {
390 my $series = $self->_get_data_series()->{'line'};
391 return (undef, undef, 0) unless $series;
394 my $min_value = STARTING_MIN_VALUE;
395 my $column_count = 0;
397 my @series = @{$series};
398 foreach my $series (@series) {
399 my @data = @{$series->{'data'}};
401 if (scalar @data > $column_count) {
402 $column_count = scalar @data;
405 foreach my $value (@data) {
406 if ($value > $max_value) { $max_value = $value; }
407 if ($value < $min_value) { $min_value = $value; }
411 return ($min_value, $max_value, $column_count);
414 sub _get_area_range {
416 my $series = $self->_get_data_series()->{'area'};
417 return (undef, undef, 0) unless $series;
420 my $min_value = STARTING_MIN_VALUE;
421 my $column_count = 0;
423 my @series = @{$series};
424 foreach my $series (@series) {
425 my @data = @{$series->{'data'}};
427 if (scalar @data > $column_count) {
428 $column_count = scalar @data;
431 foreach my $value (@data) {
432 if ($value > $max_value) { $max_value = $value; }
433 if ($value < $min_value) { $min_value = $value; }
437 return ($min_value, $max_value, $column_count);
441 sub _get_column_range {
444 my $series = $self->_get_data_series()->{'column'};
445 return (undef, undef, 0) unless $series;
448 my $min_value = STARTING_MIN_VALUE;
449 my $column_count = 0;
451 my @series = @{$series};
452 foreach my $series (@series) {
453 my @data = @{$series->{'data'}};
455 foreach my $value (@data) {
457 if ($value > $max_value) { $max_value = $value; }
458 if ($value < $min_value) { $min_value = $value; }
462 return ($min_value, $max_value, $column_count);
465 sub _get_stacked_column_range {
469 my $min_value = STARTING_MIN_VALUE;
470 my $column_count = 0;
472 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
473 my @series = @{$self->_get_data_series()->{'stacked_column'}};
477 for (my $i = scalar @series - 1; $i >= 0; $i--) {
478 my $series = $series[$i];
479 my $data = $series->{'data'};
481 for (my $i = 0; $i < scalar @$data; $i++) {
483 if ($data->[$i] > 0) {
484 $value = $data->[$i] + ($max_entries[$i] || 0);
485 $data->[$i] = $value;
486 $max_entries[$i] = $value;
488 elsif ($data->[$i] < 0) {
489 $value = $data->[$i] + ($min_entries[$i] || 0);
490 $data->[$i] = $value;
491 $min_entries[$i] = $value;
493 if ($value > $max_value) { $max_value = $value; }
494 if ($value < $min_value) { $min_value = $value; }
496 if (scalar @$data > $column_count) {
497 $column_count = scalar @$data;
501 return ($min_value, $max_value, $column_count);
506 my $chart_box = shift;
507 my $style = $self->{'_style'};
510 my $img = $self->_get_image();
511 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
512 push @labels, map { $_->{'series_name'} } @$series;
514 if (my $series = $self->_get_data_series()->{'column'}) {
515 push @labels, map { $_->{'series_name'} } @$series;
517 if (my $series = $self->_get_data_series()->{'line'}) {
518 push @labels, map { $_->{'series_name'} } @$series;
520 if (my $series = $self->_get_data_series()->{'area'}) {
521 push @labels, map { $_->{'series_name'} } @$series;
524 if ($style->{features}{legend} && (scalar @labels)) {
525 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
531 sub _draw_flat_legend {
537 my $style = $self->{'_style'};
539 my $img = $self->_get_image();
541 my $max_value = $self->_get_max_value();
542 my $min_value = $self->_get_min_value();
543 my $column_count = $self->_get_column_count();
545 my $value_range = $max_value - $min_value;
547 my $width = $self->_get_number('width');
548 my $height = $self->_get_number('height');
550 my $graph_width = $self->_get_number('graph_width');
551 my $graph_height = $self->_get_number('graph_height');
553 my $line_series = $self->_get_data_series()->{'line'};
554 my $series_counter = $self->_get_series_counter() || 0;
556 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
558 my $col_width = int($graph_width / $column_count) -1;
560 my $graph_box = $self->_get_graph_box();
561 my $left = $graph_box->[0] + 1;
562 my $bottom = $graph_box->[1];
564 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
566 my $line_aa = $self->_get_number("lineaa");
567 foreach my $series (@$line_series) {
568 my @data = @{$series->{'data'}};
569 my $data_size = scalar @data;
573 $interval = $graph_width / ($data_size);
576 $interval = $graph_width / ($data_size - 1);
578 my $color = $self->_data_color($series_counter);
580 # We need to add these last, otherwise the next line segment will overwrite half of the marker
581 my @marker_positions;
582 for (my $i = 0; $i < $data_size - 1; $i++) {
583 my $x1 = $left + $i * $interval;
584 my $x2 = $left + ($i + 1) * $interval;
586 $x1 += $has_columns * $interval / 2;
587 $x2 += $has_columns * $interval / 2;
589 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
590 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
592 push @marker_positions, [$x1, $y1];
593 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
596 my $x2 = $left + ($data_size - 1) * $interval;
597 $x2 += $has_columns * $interval / 2;
599 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
601 push @marker_positions, [$x2, $y2];
602 foreach my $position (@marker_positions) {
603 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
608 $self->_set_series_counter($series_counter);
612 sub _area_data_fill {
613 my ($self, $index, $box) = @_;
615 my %fill = $self->_data_fill($index, $box);
617 my $opacity = $self->_get_number("area.opacity");
621 my $orig_fill = $fill{fill};
622 unless ($orig_fill) {
623 $orig_fill = Imager::Fill->new
625 solid => $fill{color},
631 fill => Imager::Fill->new
642 my $style = $self->{'_style'};
644 my $img = $self->_get_image();
646 my $max_value = $self->_get_max_value();
647 my $min_value = $self->_get_min_value();
648 my $column_count = $self->_get_column_count();
650 my $value_range = $max_value - $min_value;
652 my $width = $self->_get_number('width');
653 my $height = $self->_get_number('height');
655 my $graph_width = $self->_get_number('graph_width');
656 my $graph_height = $self->_get_number('graph_height');
658 my $area_series = $self->_get_data_series()->{'area'};
659 my $series_counter = $self->_get_series_counter() || 0;
661 my $col_width = int($graph_width / $column_count) -1;
663 my $graph_box = $self->_get_graph_box();
664 my $left = $graph_box->[0] + 1;
665 my $bottom = $graph_box->[1];
666 my $right = $graph_box->[2];
667 my $top = $graph_box->[3];
669 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
671 my $line_aa = $self->_get_number("lineaa");
672 foreach my $series (@$area_series) {
673 my @data = @{$series->{'data'}};
674 my $data_size = scalar @data;
676 my $interval = $graph_width / ($data_size - 1);
678 my $color = $self->_data_color($series_counter);
680 # We need to add these last, otherwise the next line segment will overwrite half of the marker
681 my @marker_positions;
683 for (my $i = 0; $i < $data_size - 1; $i++) {
684 my $x1 = $left + $i * $interval;
686 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
689 push @polygon_points, [$x1, $top];
691 push @polygon_points, [$x1, $y1];
693 push @marker_positions, [$x1, $y1];
696 my $x2 = $left + ($data_size - 1) * $interval;
698 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
699 push @polygon_points, [$x2, $y2];
700 push @polygon_points, [$x2, $top];
701 push @polygon_points, $polygon_points[0];
703 my @fill = $self->_area_data_fill($series_counter, [$left, $bottom, $right, $top]);
704 $img->polygon(points => [@polygon_points], @fill);
706 push @marker_positions, [$x2, $y2];
707 foreach my $position (@marker_positions) {
708 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
713 $self->_set_series_counter($series_counter);
720 my ($self, $index) = @_;
722 my $markers = $self->{'_style'}{'line_markers'};
726 my $marker = $markers->[$index % @$markers];
731 sub _draw_line_marker {
733 my ($x1, $y1, $series_counter) = @_;
735 my $img = $self->_get_image();
737 my $style = $self->_line_marker($series_counter);
738 return unless $style;
740 my $type = $style->{'shape'};
741 my $radius = $style->{'radius'};
743 my $line_aa = $self->_get_number("lineaa");
744 my $fill_aa = $self->_get_number("fill.aa");
745 if ($type eq 'circle') {
746 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
747 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
749 elsif ($type eq 'square') {
750 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
751 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
753 elsif ($type eq 'diamond') {
754 # The gradient really doesn't work for diamond
755 my $color = $self->_data_color($series_counter);
758 [$x1 - $radius, $y1],
759 [$x1, $y1 + $radius],
760 [$x1 + $radius, $y1],
761 [$x1, $y1 - $radius],
763 filled => 1, color => $color, aa => $fill_aa);
765 elsif ($type eq 'triangle') {
766 # The gradient really doesn't work for triangle
767 my $color = $self->_data_color($series_counter);
770 [$x1 - $radius, $y1 + $radius],
771 [$x1 + $radius, $y1 + $radius],
772 [$x1, $y1 - $radius],
774 filled => 1, color => $color, aa => $fill_aa);
777 elsif ($type eq 'x') {
778 my $color = $self->_data_color($series_counter);
779 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
780 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
782 elsif ($type eq 'plus') {
783 my $color = $self->_data_color($series_counter);
784 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
785 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
791 my $style = $self->{'_style'};
793 my $img = $self->_get_image();
795 my $max_value = $self->_get_max_value();
796 my $min_value = $self->_get_min_value();
797 my $column_count = $self->_get_column_count();
799 my $value_range = $max_value - $min_value;
801 my $width = $self->_get_number('width');
802 my $height = $self->_get_number('height');
804 my $graph_width = $self->_get_number('graph_width');
805 my $graph_height = $self->_get_number('graph_height');
808 my $graph_box = $self->_get_graph_box();
809 my $left = $graph_box->[0] + 1;
810 my $bottom = $graph_box->[1];
811 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
813 my $bar_width = $graph_width / $column_count;
816 if ($style->{'features'}{'outline'}) {
817 $outline_color = $self->_get_color('outline.line');
820 my $series_counter = $self->_get_series_counter() || 0;
821 my $col_series = $self->_get_data_series()->{'column'};
822 my $column_padding_percent = $self->_get_number('column_padding') || 0;
823 my $column_padding = int($column_padding_percent * $bar_width / 100);
825 # 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.
826 my $column_series = 0;
828 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
829 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
831 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
832 my $series = $col_series->[$series_pos];
833 my @data = @{$series->{'data'}};
834 my $data_size = scalar @data;
835 for (my $i = 0; $i < $data_size; $i++) {
836 my $part1 = $bar_width * (scalar @$col_series * $i);
837 my $part2 = ($series_pos) * $bar_width;
838 my $x1 = $left + $part1 + $part2;
839 if ($has_stacked_columns) {
840 $x1 += ($bar_width * ($i+1));
844 my $x2 = int($x1 + $bar_width - $column_padding)-1;
845 # Special case for when bar_width is less than 1.
850 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
852 my $color = $self->_data_color($series_counter);
855 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
856 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
857 if ($style->{'features'}{'outline'}) {
858 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
862 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
863 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
864 if ($style->{'features'}{'outline'}) {
865 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
873 $self->_set_series_counter($series_counter);
877 sub _draw_stacked_columns {
879 my $style = $self->{'_style'};
881 my $img = $self->_get_image();
883 my $max_value = $self->_get_max_value();
884 my $min_value = $self->_get_min_value();
885 my $column_count = $self->_get_column_count();
886 my $value_range = $max_value - $min_value;
888 my $graph_box = $self->_get_graph_box();
889 my $left = $graph_box->[0] + 1;
890 my $bottom = $graph_box->[1];
892 my $graph_width = $self->_get_number('graph_width');
893 my $graph_height = $self->_get_number('graph_height');
895 my $bar_width = $graph_width / $column_count;
896 my $column_series = 0;
897 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
898 $column_series = (scalar @$column_series_data);
902 my $column_padding_percent = $self->_get_number('column_padding') || 0;
903 if ($column_padding_percent < 0) {
904 return $self->_error("Column padding less than 0");
906 if ($column_padding_percent > 100) {
907 return $self->_error("Column padding greater than 0");
909 my $column_padding = int($column_padding_percent * $bar_width / 100);
912 if ($style->{'features'}{'outline'}) {
913 $outline_color = $self->_get_color('outline.line');
916 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
917 my $col_series = $self->_get_data_series()->{'stacked_column'};
918 my $series_counter = $self->_get_series_counter() || 0;
920 foreach my $series (@$col_series) {
921 my @data = @{$series->{'data'}};
922 my $data_size = scalar @data;
923 for (my $i = 0; $i < $data_size; $i++) {
924 my $part1 = $bar_width * $i * $column_series;
926 my $x1 = int($left + $part1 + $part2);
927 my $x2 = int($x1 + $bar_width - $column_padding) - 1;
928 # Special case for when bar_width is less than 1.
933 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
936 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
937 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
938 if ($style->{'features'}{'outline'}) {
939 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
943 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
944 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
945 if ($style->{'features'}{'outline'}) {
946 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
953 $self->_set_series_counter($series_counter);
957 sub _add_data_series {
959 my $series_type = shift;
960 my $data_ref = shift;
961 my $series_name = shift;
963 my $graph_data = $self->{'graph_data'} || {};
965 my $series = $graph_data->{$series_type} || [];
967 push @$series, { data => $data_ref, series_name => $series_name };
969 $graph_data->{$series_type} = $series;
971 $self->{'graph_data'} = $graph_data;
977 =item show_horizontal_gridlines()
979 Shows horizontal gridlines at the y-tics.
983 sub show_horizontal_gridlines {
984 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
987 =item use_automatic_axis()
989 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.
993 sub use_automatic_axis {
994 eval { require Chart::Math::Axis; };
996 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
998 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
1003 =item set_y_tics($count)
1005 Set the number of Y tics to use. Their value and position will be determined by the data range.
1010 $_[0]->{'y_tics'} = $_[1];
1014 return $_[0]->{'y_tics'} || 0;
1017 sub _remove_tics_from_chart_box {
1019 my $chart_box = shift;
1022 my $tic_width = $self->_get_y_tic_width() || 10;
1023 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
1026 my $tic_height = $self->_get_x_tic_height() || 10;
1027 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
1029 $self->_remove_box($chart_box, \@y_tic_box);
1030 $self->_remove_box($chart_box, \@x_tic_box);
1032 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
1033 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
1034 $self->_remove_box($chart_box, \@y_tic_tops);
1036 # Make sure that the first and last label fit
1037 if (my $labels = $self->_get_labels()) {
1038 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1039 my @remove_box = ($chart_box->[0],
1041 $chart_box->[0] + int($box[2] / 2) + 1,
1045 $self->_remove_box($chart_box, \@remove_box);
1047 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1048 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1054 $self->_remove_box($chart_box, \@remove_box);
1059 sub _get_y_tic_width{
1061 my $min = $self->_get_min_value();
1062 my $max = $self->_get_max_value();
1063 my $tic_count = $self->_get_y_tics();
1065 my $interval = ($max - $min) / ($tic_count - 1);
1067 my %text_info = $self->_text_style('legend')
1071 for my $count (0 .. $tic_count - 1) {
1072 my $value = sprintf("%.2f", ($count*$interval)+$min);
1074 my @box = $self->_text_bbox($value, 'legend');
1075 my $width = $box[2] - $box[0];
1079 if ($width > $max_width) {
1080 $max_width = $width;
1087 sub _get_x_tic_height {
1090 my $labels = $self->_get_labels();
1096 my $tic_count = (scalar @$labels) - 1;
1098 my %text_info = $self->_text_style('legend')
1102 for my $count (0 .. $tic_count) {
1103 my $label = $labels->[$count];
1105 my @box = $self->_text_bbox($label, 'legend');
1107 my $height = $box[3] - $box[1];
1111 if ($height > $max_height) {
1112 $max_height = $height;
1121 my $min = $self->_get_min_value();
1122 my $max = $self->_get_max_value();
1123 my $tic_count = $self->_get_y_tics();
1125 my $img = $self->_get_image();
1126 my $graph_box = $self->_get_graph_box();
1127 my $image_box = $self->_get_image_box();
1129 my $interval = ($max - $min) / ($tic_count - 1);
1131 my %text_info = $self->_text_style('legend')
1134 my $line_style = $self->_get_color('outline.line');
1135 my $show_gridlines = $self->_get_number('horizontal_gridlines');
1136 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
1137 for my $count (0 .. $tic_count - 1) {
1138 my $x1 = $graph_box->[0] - 5;
1139 my $x2 = $graph_box->[0] + 5;
1140 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
1142 my $value = ($count*$interval)+$min;
1143 if ($interval < 1 || ($value != int($value))) {
1144 $value = sprintf("%.2f", $value);
1147 my @box = $self->_text_bbox($value, 'legend')
1150 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1152 my $width = $box[2];
1153 my $height = $box[3];
1155 $img->string(%text_info,
1156 x => ($x1 - $width - 3),
1157 y => ($y1 + ($height / 2)),
1161 if ($show_gridlines) {
1162 # XXX - line styles!
1163 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1166 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1167 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1177 my $img = $self->_get_image();
1178 my $graph_box = $self->_get_graph_box();
1179 my $image_box = $self->_get_image_box();
1181 my $labels = $self->_get_labels();
1183 my $tic_count = (scalar @$labels) - 1;
1185 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1187 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1188 my $denominator = $tic_count;
1192 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1193 my %text_info = $self->_text_style('legend')
1196 # If automatic axis is turned on, let's be selective about what labels we draw.
1199 if ($self->_get_number('automatic_axis')) {
1200 foreach my $label (@$labels) {
1201 my @box = $self->_text_bbox($label, 'legend');
1202 if ($box[2] > $max_size) {
1203 $max_size = $box[2];
1207 # Give the max_size some padding...
1210 $tic_skip = int($max_size / $tic_distance) + 1;
1213 my $line_style = $self->_get_color('outline.line');
1215 for my $count (0 .. $tic_count) {
1216 next if ($count % ($tic_skip + 1));
1217 my $label = $labels->[$count];
1218 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1221 $x1 += $tic_distance / 2;
1226 my $y1 = $graph_box->[3] + 5;
1227 my $y2 = $graph_box->[3] - 5;
1229 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1231 my @box = $self->_text_bbox($label, 'legend')
1234 my $width = $box[2];
1235 my $height = $box[3];
1237 $img->string(%text_info,
1238 x => ($x1 - ($width / 2)),
1239 y => ($y1 + ($height + 5)),
1249 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1250 return $self->_error("No data supplied");
1253 my $data = $self->_get_data_series();
1254 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1255 return $self->_error("No values in data series");
1257 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1258 return $self->_error("No values in data series");
1260 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1261 return $self->_error("No values in data series");
1267 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1268 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1269 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1270 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1271 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1272 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1273 sub _get_column_count { return $_[0]->{'column_count'} }
1274 sub _get_min_value { return $_[0]->{'min_value'} }
1275 sub _get_max_value { return $_[0]->{'max_value'} }
1276 sub _get_image_box { return $_[0]->{'image_box'} }
1277 sub _get_graph_box { return $_[0]->{'graph_box'} }
1278 sub _get_series_counter { return $_[0]->{'series_counter'} }
1283 my %work = %{$self->SUPER::_style_defs()};
1288 push @{$work{features}}, qw/graph_outline graph_fill/;
1295 return ( $self->SUPER::_composite(), "graph" );