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 add_area_data_series(\@data, $series_name)
84 Add a area data series to the graph.
88 sub add_area_data_series {
91 my $series_name = shift;
93 $self->_add_data_series('area', $data_ref, $series_name);
99 =item set_y_max($value)
101 Sets the maximum y value to be displayed. This will be ignored if the y_max is lower than the highest value.
106 $_[0]->{'custom_style'}->{'y_max'} = $_[1];
109 =item set_y_min($value)
111 Sets the minimum y value to be displayed. This will be ignored if the y_min is higher than the lowest value.
116 $_[0]->{'custom_style'}->{'y_min'} = $_[1];
119 =item set_column_padding($int)
121 Sets the padding between columns. This is a percentage of the column width. Defaults to 0.
125 sub set_column_padding {
126 $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
129 =item set_range_padding($percentage)
131 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.
133 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.
137 sub set_range_padding {
138 $_[0]->{'custom_style'}->{'range_padding'} = $_[1];
141 =item set_negative_background($color)
143 Sets the background color used below the x axis.
147 sub set_negative_background {
148 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
158 my ($self, %opts) = @_;
160 if (!$self->_valid_input()) {
164 $self->_style_setup(\%opts);
166 my $style = $self->{_style};
168 my $img = $self->_get_image()
171 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
172 $self->_set_image_box(\@image_box);
174 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
175 $self->_draw_legend(\@chart_box);
176 if ($style->{title}{text}) {
177 $self->_draw_title($img, \@chart_box)
181 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
182 return unless $self->_get_data_range();
183 $self->_remove_tics_from_chart_box(\@chart_box);
184 my $column_count = $self->_get_column_count();
186 my $width = $self->_get_number('width');
187 my $height = $self->_get_number('height');
189 my $graph_width = $chart_box[2] - $chart_box[0];
190 my $graph_height = $chart_box[3] - $chart_box[1];
192 my $col_width = ($graph_width - 1) / $column_count;
193 if ($col_width > 1) {
194 $graph_width = int($col_width) * $column_count + 1;
197 $graph_width = $col_width * $column_count + 1;
200 my $tic_count = $self->_get_y_tics();
201 my $tic_distance = ($graph_height-1) / ($tic_count - 1);
202 $graph_height = int($tic_distance * ($tic_count - 1));
204 my $bottom = $chart_box[1];
205 my $left = $chart_box[0];
207 $self->{'_style'}{'graph_width'} = $graph_width;
208 $self->{'_style'}{'graph_height'} = $graph_height;
210 my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
211 $self->_set_graph_box(\@graph_box);
214 color => $self->_get_color('outline.line'),
216 xmax => $left+$graph_width,
218 ymax => $bottom+$graph_height,
222 color => $self->_get_color('bg'),
224 xmax => $left+$graph_width - 1,
226 ymax => $bottom+$graph_height-1 ,
230 my $min_value = $self->_get_min_value();
231 my $max_value = $self->_get_max_value();
232 my $value_range = $max_value - $min_value;
236 $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
239 if ($min_value < 0) {
241 color => $self->_get_color('negative_bg'),
243 xmax => $left+$graph_width- 1,
244 ymin => $zero_position,
245 ymax => $bottom+$graph_height - 1,
250 y1 => $zero_position,
251 x2 => $left + $graph_width,
252 y2 => $zero_position,
253 color => $self->_get_color('outline.line'),
257 if ($self->_get_data_series()->{'stacked_column'}) {
258 return unless $self->_draw_stacked_columns();
260 if ($self->_get_data_series()->{'column'}) {
261 return unless $self->_draw_columns();
263 if ($self->_get_data_series()->{'line'}) {
264 return unless $self->_draw_lines();
266 if ($self->_get_data_series()->{'area'}) {
267 return unless $self->_draw_area();
270 if ($self->_get_y_tics()) {
271 $self->_draw_y_tics();
273 if ($self->_get_labels()) {
274 $self->_draw_x_tics();
277 return $self->_get_image();
280 sub _get_data_range {
285 my $column_count = 0;
287 my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
288 my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
289 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
290 my ($a_min, $a_max, $a_cols) = $self->_get_area_range();
292 # These are side by side...
295 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min, $a_min);
296 $max_value = $self->_max(0, $sc_max, $c_max, $l_max, $a_max);
298 my $config_min = $self->_get_number('y_min');
299 my $config_max = $self->_get_number('y_max');
301 if (defined $config_max && $config_max < $max_value) {
304 if (defined $config_min && $config_min > $min_value) {
308 my $range_padding = $self->_get_number('range_padding');
309 if (defined $config_min) {
310 $min_value = $config_min;
313 if ($min_value > 0) {
316 if ($range_padding && $min_value < 0) {
317 my $difference = $min_value * $range_padding / 100;
318 if ($min_value < -1 && $difference > -1) {
321 $min_value += $difference;
324 if (defined $config_max) {
325 $max_value = $config_max;
328 if ($range_padding && $max_value > 0) {
329 my $difference = $max_value * $range_padding / 100;
330 if ($max_value > 1 && $difference < 1) {
333 $max_value += $difference;
336 $column_count = $self->_max(0, $sc_cols, $l_cols, $a_cols);
338 if ($self->_get_number('automatic_axis')) {
339 # In case this was set via a style, and not by the api method
340 eval { require Chart::Math::Axis; };
342 return $self->_error("Can't use automatic_axis - $@");
345 my $axis = Chart::Math::Axis->new();
346 $axis->include_zero();
347 $axis->add_data($min_value, $max_value);
348 $max_value = $axis->top;
349 $min_value = $axis->bottom;
350 my $ticks = $axis->ticks;
351 # The +1 is there because we have the bottom tick as well
352 $self->set_y_tics($ticks+1);
355 $self->_set_max_value($max_value);
356 $self->_set_min_value($min_value);
357 $self->_set_column_count($column_count);
366 foreach my $value (@_) {
367 next unless defined $value;
368 if ($value < $min) { $min = $value; }
377 foreach my $value (@_) {
378 next unless defined $value;
379 if ($value > $min) { $min = $value; }
384 sub _get_line_range {
386 my $series = $self->_get_data_series()->{'line'};
387 return (undef, undef, 0) unless $series;
390 my $min_value = STARTING_MIN_VALUE;
391 my $column_count = 0;
393 my @series = @{$series};
394 foreach my $series (@series) {
395 my @data = @{$series->{'data'}};
397 if (scalar @data > $column_count) {
398 $column_count = scalar @data;
401 foreach my $value (@data) {
402 if ($value > $max_value) { $max_value = $value; }
403 if ($value < $min_value) { $min_value = $value; }
407 return ($min_value, $max_value, $column_count);
410 sub _get_area_range {
412 my $series = $self->_get_data_series()->{'area'};
413 return (undef, undef, 0) unless $series;
416 my $min_value = STARTING_MIN_VALUE;
417 my $column_count = 0;
419 my @series = @{$series};
420 foreach my $series (@series) {
421 my @data = @{$series->{'data'}};
423 if (scalar @data > $column_count) {
424 $column_count = scalar @data;
427 foreach my $value (@data) {
428 if ($value > $max_value) { $max_value = $value; }
429 if ($value < $min_value) { $min_value = $value; }
433 return ($min_value, $max_value, $column_count);
437 sub _get_column_range {
440 my $series = $self->_get_data_series()->{'column'};
441 return (undef, undef, 0) unless $series;
444 my $min_value = STARTING_MIN_VALUE;
445 my $column_count = 0;
447 my @series = @{$series};
448 foreach my $series (@series) {
449 my @data = @{$series->{'data'}};
451 foreach my $value (@data) {
453 if ($value > $max_value) { $max_value = $value; }
454 if ($value < $min_value) { $min_value = $value; }
458 return ($min_value, $max_value, $column_count);
461 sub _get_stacked_column_range {
465 my $min_value = STARTING_MIN_VALUE;
466 my $column_count = 0;
468 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
469 my @series = @{$self->_get_data_series()->{'stacked_column'}};
473 for (my $i = scalar @series - 1; $i >= 0; $i--) {
474 my $series = $series[$i];
475 my $data = $series->{'data'};
477 for (my $i = 0; $i < scalar @$data; $i++) {
479 if ($data->[$i] > 0) {
480 $value = $data->[$i] + ($max_entries[$i] || 0);
481 $data->[$i] = $value;
482 $max_entries[$i] = $value;
484 elsif ($data->[$i] < 0) {
485 $value = $data->[$i] + ($min_entries[$i] || 0);
486 $data->[$i] = $value;
487 $min_entries[$i] = $value;
489 if ($value > $max_value) { $max_value = $value; }
490 if ($value < $min_value) { $min_value = $value; }
492 if (scalar @$data > $column_count) {
493 $column_count = scalar @$data;
497 return ($min_value, $max_value, $column_count);
502 my $chart_box = shift;
503 my $style = $self->{'_style'};
506 my $img = $self->_get_image();
507 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
508 push @labels, map { $_->{'series_name'} } @$series;
510 if (my $series = $self->_get_data_series()->{'column'}) {
511 push @labels, map { $_->{'series_name'} } @$series;
513 if (my $series = $self->_get_data_series()->{'line'}) {
514 push @labels, map { $_->{'series_name'} } @$series;
517 if ($style->{features}{legend} && (scalar @labels)) {
518 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
524 sub _draw_flat_legend {
530 my $style = $self->{'_style'};
532 my $img = $self->_get_image();
534 my $max_value = $self->_get_max_value();
535 my $min_value = $self->_get_min_value();
536 my $column_count = $self->_get_column_count();
538 my $value_range = $max_value - $min_value;
540 my $width = $self->_get_number('width');
541 my $height = $self->_get_number('height');
543 my $graph_width = $self->_get_number('graph_width');
544 my $graph_height = $self->_get_number('graph_height');
546 my $line_series = $self->_get_data_series()->{'line'};
547 my $series_counter = $self->_get_series_counter() || 0;
549 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
551 my $col_width = int($graph_width / $column_count) -1;
553 my $graph_box = $self->_get_graph_box();
554 my $left = $graph_box->[0] + 1;
555 my $bottom = $graph_box->[1];
557 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
559 my $line_aa = $self->_get_number("lineaa");
560 foreach my $series (@$line_series) {
561 my @data = @{$series->{'data'}};
562 my $data_size = scalar @data;
566 $interval = $graph_width / ($data_size);
569 $interval = $graph_width / ($data_size - 1);
571 my $color = $self->_data_color($series_counter);
573 # We need to add these last, otherwise the next line segment will overwrite half of the marker
574 my @marker_positions;
575 for (my $i = 0; $i < $data_size - 1; $i++) {
576 my $x1 = $left + $i * $interval;
577 my $x2 = $left + ($i + 1) * $interval;
579 $x1 += $has_columns * $interval / 2;
580 $x2 += $has_columns * $interval / 2;
582 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
583 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
585 push @marker_positions, [$x1, $y1];
586 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
589 my $x2 = $left + ($data_size - 1) * $interval;
590 $x2 += $has_columns * $interval / 2;
592 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
594 push @marker_positions, [$x2, $y2];
595 foreach my $position (@marker_positions) {
596 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
601 $self->_set_series_counter($series_counter);
607 my $style = $self->{'_style'};
609 my $img = $self->_get_image();
611 my $max_value = $self->_get_max_value();
612 my $min_value = $self->_get_min_value();
613 my $column_count = $self->_get_column_count();
615 my $value_range = $max_value - $min_value;
617 my $width = $self->_get_number('width');
618 my $height = $self->_get_number('height');
620 my $graph_width = $self->_get_number('graph_width');
621 my $graph_height = $self->_get_number('graph_height');
623 my $area_series = $self->_get_data_series()->{'area'};
624 my $series_counter = $self->_get_series_counter() || 0;
626 my $col_width = int($graph_width / $column_count) -1;
628 my $graph_box = $self->_get_graph_box();
629 my $left = $graph_box->[0] + 1;
630 my $bottom = $graph_box->[1];
631 my $right = $graph_box->[2];
632 my $top = $graph_box->[3];
634 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
636 my $line_aa = $self->_get_number("lineaa");
637 foreach my $series (@$area_series) {
638 my @data = @{$series->{'data'}};
639 my $data_size = scalar @data;
641 my $interval = $graph_width / ($data_size - 1);
643 my $color = $self->_data_color($series_counter);
645 # We need to add these last, otherwise the next line segment will overwrite half of the marker
646 my @marker_positions;
648 for (my $i = 0; $i < $data_size - 1; $i++) {
649 my $x1 = $left + $i * $interval;
651 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
654 push @polygon_points, [$x1, $top];
656 push @polygon_points, [$x1, $y1];
658 push @marker_positions, [$x1, $y1];
661 my $x2 = $left + ($data_size - 1) * $interval;
663 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
664 push @polygon_points, [$x2, $y2];
665 push @polygon_points, [$x2, $top];
666 push @polygon_points, $polygon_points[0];
668 my @fill = $self->_data_fill($series_counter, [$left, $bottom, $right, $top]);
669 $img->polygon(points => [@polygon_points], @fill);
671 push @marker_positions, [$x2, $y2];
672 foreach my $position (@marker_positions) {
673 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
678 $self->_set_series_counter($series_counter);
685 my ($self, $index) = @_;
687 my $markers = $self->{'_style'}{'line_markers'};
691 my $marker = $markers->[$index % @$markers];
696 sub _draw_line_marker {
698 my ($x1, $y1, $series_counter) = @_;
700 my $img = $self->_get_image();
702 my $style = $self->_line_marker($series_counter);
703 return unless $style;
705 my $type = $style->{'shape'};
706 my $radius = $style->{'radius'};
708 my $line_aa = $self->_get_number("lineaa");
709 my $fill_aa = $self->_get_number("fill.aa");
710 if ($type eq 'circle') {
711 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
712 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
714 elsif ($type eq 'square') {
715 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
716 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
718 elsif ($type eq 'diamond') {
719 # The gradient really doesn't work for diamond
720 my $color = $self->_data_color($series_counter);
723 [$x1 - $radius, $y1],
724 [$x1, $y1 + $radius],
725 [$x1 + $radius, $y1],
726 [$x1, $y1 - $radius],
728 filled => 1, color => $color, aa => $fill_aa);
730 elsif ($type eq 'triangle') {
731 # The gradient really doesn't work for triangle
732 my $color = $self->_data_color($series_counter);
735 [$x1 - $radius, $y1 + $radius],
736 [$x1 + $radius, $y1 + $radius],
737 [$x1, $y1 - $radius],
739 filled => 1, color => $color, aa => $fill_aa);
742 elsif ($type eq 'x') {
743 my $color = $self->_data_color($series_counter);
744 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
745 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
747 elsif ($type eq 'plus') {
748 my $color = $self->_data_color($series_counter);
749 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
750 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
756 my $style = $self->{'_style'};
758 my $img = $self->_get_image();
760 my $max_value = $self->_get_max_value();
761 my $min_value = $self->_get_min_value();
762 my $column_count = $self->_get_column_count();
764 my $value_range = $max_value - $min_value;
766 my $width = $self->_get_number('width');
767 my $height = $self->_get_number('height');
769 my $graph_width = $self->_get_number('graph_width');
770 my $graph_height = $self->_get_number('graph_height');
773 my $graph_box = $self->_get_graph_box();
774 my $left = $graph_box->[0] + 1;
775 my $bottom = $graph_box->[1];
776 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
778 my $bar_width = $graph_width / $column_count;
781 if ($style->{'features'}{'outline'}) {
782 $outline_color = $self->_get_color('outline.line');
785 my $series_counter = $self->_get_series_counter() || 0;
786 my $col_series = $self->_get_data_series()->{'column'};
787 my $column_padding_percent = $self->_get_number('column_padding') || 0;
788 my $column_padding = int($column_padding_percent * $bar_width / 100);
790 # 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.
791 my $column_series = 0;
793 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
794 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
796 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
797 my $series = $col_series->[$series_pos];
798 my @data = @{$series->{'data'}};
799 my $data_size = scalar @data;
800 for (my $i = 0; $i < $data_size; $i++) {
801 my $part1 = $bar_width * (scalar @$col_series * $i);
802 my $part2 = ($series_pos) * $bar_width;
803 my $x1 = $left + $part1 + $part2;
804 if ($has_stacked_columns) {
805 $x1 += ($bar_width * ($i+1));
809 my $x2 = int($x1 + $bar_width - $column_padding)-1;
810 # Special case for when bar_width is less than 1.
815 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
817 my $color = $self->_data_color($series_counter);
820 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
821 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
822 if ($style->{'features'}{'outline'}) {
823 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
827 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
828 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
829 if ($style->{'features'}{'outline'}) {
830 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
838 $self->_set_series_counter($series_counter);
842 sub _draw_stacked_columns {
844 my $style = $self->{'_style'};
846 my $img = $self->_get_image();
848 my $max_value = $self->_get_max_value();
849 my $min_value = $self->_get_min_value();
850 my $column_count = $self->_get_column_count();
851 my $value_range = $max_value - $min_value;
853 my $graph_box = $self->_get_graph_box();
854 my $left = $graph_box->[0] + 1;
855 my $bottom = $graph_box->[1];
857 my $graph_width = $self->_get_number('graph_width');
858 my $graph_height = $self->_get_number('graph_height');
860 my $bar_width = $graph_width / $column_count;
861 my $column_series = 0;
862 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
863 $column_series = (scalar @$column_series_data);
867 my $column_padding_percent = $self->_get_number('column_padding') || 0;
868 if ($column_padding_percent < 0) {
869 return $self->_error("Column padding less than 0");
871 if ($column_padding_percent > 100) {
872 return $self->_error("Column padding greater than 0");
874 my $column_padding = int($column_padding_percent * $bar_width / 100);
877 if ($style->{'features'}{'outline'}) {
878 $outline_color = $self->_get_color('outline.line');
881 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
882 my $col_series = $self->_get_data_series()->{'stacked_column'};
883 my $series_counter = $self->_get_series_counter() || 0;
885 foreach my $series (@$col_series) {
886 my @data = @{$series->{'data'}};
887 my $data_size = scalar @data;
888 for (my $i = 0; $i < $data_size; $i++) {
889 my $part1 = $bar_width * $i * $column_series;
891 my $x1 = int($left + $part1 + $part2);
892 my $x2 = int($x1 + $bar_width - $column_padding) - 1;
893 # Special case for when bar_width is less than 1.
898 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
901 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
902 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
903 if ($style->{'features'}{'outline'}) {
904 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
908 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
909 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
910 if ($style->{'features'}{'outline'}) {
911 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
918 $self->_set_series_counter($series_counter);
922 sub _add_data_series {
924 my $series_type = shift;
925 my $data_ref = shift;
926 my $series_name = shift;
928 my $graph_data = $self->{'graph_data'} || {};
930 my $series = $graph_data->{$series_type} || [];
932 push @$series, { data => $data_ref, series_name => $series_name };
934 $graph_data->{$series_type} = $series;
936 $self->{'graph_data'} = $graph_data;
942 =item show_horizontal_gridlines()
944 Shows horizontal gridlines at the y-tics.
948 sub show_horizontal_gridlines {
949 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
952 =item use_automatic_axis()
954 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.
958 sub use_automatic_axis {
959 eval { require Chart::Math::Axis; };
961 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
963 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
968 =item set_y_tics($count)
970 Set the number of Y tics to use. Their value and position will be determined by the data range.
975 $_[0]->{'y_tics'} = $_[1];
979 return $_[0]->{'y_tics'} || 0;
982 sub _remove_tics_from_chart_box {
984 my $chart_box = shift;
987 my $tic_width = $self->_get_y_tic_width() || 10;
988 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
991 my $tic_height = $self->_get_x_tic_height() || 10;
992 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
994 $self->_remove_box($chart_box, \@y_tic_box);
995 $self->_remove_box($chart_box, \@x_tic_box);
997 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
998 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
999 $self->_remove_box($chart_box, \@y_tic_tops);
1001 # Make sure that the first and last label fit
1002 if (my $labels = $self->_get_labels()) {
1003 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1004 my @remove_box = ($chart_box->[0],
1006 $chart_box->[0] + int($box[2] / 2) + 1,
1010 $self->_remove_box($chart_box, \@remove_box);
1012 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1013 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1019 $self->_remove_box($chart_box, \@remove_box);
1024 sub _get_y_tic_width{
1026 my $min = $self->_get_min_value();
1027 my $max = $self->_get_max_value();
1028 my $tic_count = $self->_get_y_tics();
1030 my $interval = ($max - $min) / ($tic_count - 1);
1032 my %text_info = $self->_text_style('legend')
1036 for my $count (0 .. $tic_count - 1) {
1037 my $value = sprintf("%.2f", ($count*$interval)+$min);
1039 my @box = $self->_text_bbox($value, 'legend');
1040 my $width = $box[2] - $box[0];
1044 if ($width > $max_width) {
1045 $max_width = $width;
1052 sub _get_x_tic_height {
1055 my $labels = $self->_get_labels();
1061 my $tic_count = (scalar @$labels) - 1;
1063 my %text_info = $self->_text_style('legend')
1067 for my $count (0 .. $tic_count) {
1068 my $label = $labels->[$count];
1070 my @box = $self->_text_bbox($label, 'legend');
1072 my $height = $box[3] - $box[1];
1076 if ($height > $max_height) {
1077 $max_height = $height;
1086 my $min = $self->_get_min_value();
1087 my $max = $self->_get_max_value();
1088 my $tic_count = $self->_get_y_tics();
1090 my $img = $self->_get_image();
1091 my $graph_box = $self->_get_graph_box();
1092 my $image_box = $self->_get_image_box();
1094 my $interval = ($max - $min) / ($tic_count - 1);
1096 my %text_info = $self->_text_style('legend')
1099 my $line_style = $self->_get_color('outline.line');
1100 my $show_gridlines = $self->_get_number('horizontal_gridlines');
1101 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
1102 for my $count (0 .. $tic_count - 1) {
1103 my $x1 = $graph_box->[0] - 5;
1104 my $x2 = $graph_box->[0] + 5;
1105 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
1107 my $value = ($count*$interval)+$min;
1108 if ($interval < 1 || ($value != int($value))) {
1109 $value = sprintf("%.2f", $value);
1112 my @box = $self->_text_bbox($value, 'legend')
1115 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1117 my $width = $box[2];
1118 my $height = $box[3];
1120 $img->string(%text_info,
1121 x => ($x1 - $width - 3),
1122 y => ($y1 + ($height / 2)),
1126 if ($show_gridlines) {
1127 # XXX - line styles!
1128 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1131 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1132 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1142 my $img = $self->_get_image();
1143 my $graph_box = $self->_get_graph_box();
1144 my $image_box = $self->_get_image_box();
1146 my $labels = $self->_get_labels();
1148 my $tic_count = (scalar @$labels) - 1;
1150 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1152 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1153 my $denominator = $tic_count;
1157 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1158 my %text_info = $self->_text_style('legend')
1161 # If automatic axis is turned on, let's be selective about what labels we draw.
1164 if ($self->_get_number('automatic_axis')) {
1165 foreach my $label (@$labels) {
1166 my @box = $self->_text_bbox($label, 'legend');
1167 if ($box[2] > $max_size) {
1168 $max_size = $box[2];
1172 # Give the max_size some padding...
1175 $tic_skip = int($max_size / $tic_distance) + 1;
1178 my $line_style = $self->_get_color('outline.line');
1180 for my $count (0 .. $tic_count) {
1181 next if ($count % ($tic_skip + 1));
1182 my $label = $labels->[$count];
1183 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1186 $x1 += $tic_distance / 2;
1191 my $y1 = $graph_box->[3] + 5;
1192 my $y2 = $graph_box->[3] - 5;
1194 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1196 my @box = $self->_text_bbox($label, 'legend')
1199 my $width = $box[2];
1200 my $height = $box[3];
1202 $img->string(%text_info,
1203 x => ($x1 - ($width / 2)),
1204 y => ($y1 + ($height + 5)),
1214 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1215 return $self->_error("No data supplied");
1218 my $data = $self->_get_data_series();
1219 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1220 return $self->_error("No values in data series");
1222 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1223 return $self->_error("No values in data series");
1225 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1226 return $self->_error("No values in data series");
1232 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1233 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1234 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1235 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1236 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1237 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1238 sub _get_column_count { return $_[0]->{'column_count'} }
1239 sub _get_min_value { return $_[0]->{'min_value'} }
1240 sub _get_max_value { return $_[0]->{'max_value'} }
1241 sub _get_image_box { return $_[0]->{'image_box'} }
1242 sub _get_graph_box { return $_[0]->{'graph_box'} }
1243 sub _get_series_counter { return $_[0]->{'series_counter'} }