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 $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
628 my $col_width = int($graph_width / $column_count) -1;
630 my $graph_box = $self->_get_graph_box();
631 my $left = $graph_box->[0] + 1;
632 my $bottom = $graph_box->[1];
633 my $right = $graph_box->[2];
634 my $top = $graph_box->[3];
636 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
638 my $line_aa = $self->_get_number("lineaa");
639 foreach my $series (@$area_series) {
640 my @data = @{$series->{'data'}};
641 my $data_size = scalar @data;
645 $interval = $graph_width / ($data_size);
648 $interval = $graph_width / ($data_size - 1);
650 my $color = $self->_data_color($series_counter);
652 # We need to add these last, otherwise the next line segment will overwrite half of the marker
653 my @marker_positions;
655 for (my $i = 0; $i < $data_size - 1; $i++) {
656 my $x1 = $left + $i * $interval;
658 $x1 += $has_columns * $interval / 2;
660 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
663 push @polygon_points, [$x1, $top];
665 push @polygon_points, [$x1, $y1];
667 push @marker_positions, [$x1, $y1];
670 my $x2 = $left + ($data_size - 1) * $interval;
671 $x2 += $has_columns * $interval / 2;
673 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
674 push @polygon_points, [$x2, $y2];
675 push @polygon_points, [$x2, $top];
676 push @polygon_points, $polygon_points[0];
678 my @fill = $self->_data_fill($series_counter, [$left, $bottom, $right, $top]);
679 $img->polygon(points => [@polygon_points], @fill);
681 push @marker_positions, [$x2, $y2];
682 foreach my $position (@marker_positions) {
683 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
688 $self->_set_series_counter($series_counter);
695 my ($self, $index) = @_;
697 my $markers = $self->{'_style'}{'line_markers'};
701 my $marker = $markers->[$index % @$markers];
706 sub _draw_line_marker {
708 my ($x1, $y1, $series_counter) = @_;
710 my $img = $self->_get_image();
712 my $style = $self->_line_marker($series_counter);
713 return unless $style;
715 my $type = $style->{'shape'};
716 my $radius = $style->{'radius'};
718 my $line_aa = $self->_get_number("lineaa");
719 my $fill_aa = $self->_get_number("fill.aa");
720 if ($type eq 'circle') {
721 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
722 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
724 elsif ($type eq 'square') {
725 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
726 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
728 elsif ($type eq 'diamond') {
729 # The gradient really doesn't work for diamond
730 my $color = $self->_data_color($series_counter);
733 [$x1 - $radius, $y1],
734 [$x1, $y1 + $radius],
735 [$x1 + $radius, $y1],
736 [$x1, $y1 - $radius],
738 filled => 1, color => $color, aa => $fill_aa);
740 elsif ($type eq 'triangle') {
741 # The gradient really doesn't work for triangle
742 my $color = $self->_data_color($series_counter);
745 [$x1 - $radius, $y1 + $radius],
746 [$x1 + $radius, $y1 + $radius],
747 [$x1, $y1 - $radius],
749 filled => 1, color => $color, aa => $fill_aa);
752 elsif ($type eq 'x') {
753 my $color = $self->_data_color($series_counter);
754 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
755 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
757 elsif ($type eq 'plus') {
758 my $color = $self->_data_color($series_counter);
759 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
760 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
766 my $style = $self->{'_style'};
768 my $img = $self->_get_image();
770 my $max_value = $self->_get_max_value();
771 my $min_value = $self->_get_min_value();
772 my $column_count = $self->_get_column_count();
774 my $value_range = $max_value - $min_value;
776 my $width = $self->_get_number('width');
777 my $height = $self->_get_number('height');
779 my $graph_width = $self->_get_number('graph_width');
780 my $graph_height = $self->_get_number('graph_height');
783 my $graph_box = $self->_get_graph_box();
784 my $left = $graph_box->[0] + 1;
785 my $bottom = $graph_box->[1];
786 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
788 my $bar_width = $graph_width / $column_count;
791 if ($style->{'features'}{'outline'}) {
792 $outline_color = $self->_get_color('outline.line');
795 my $series_counter = $self->_get_series_counter() || 0;
796 my $col_series = $self->_get_data_series()->{'column'};
797 my $column_padding_percent = $self->_get_number('column_padding') || 0;
798 my $column_padding = int($column_padding_percent * $bar_width / 100);
800 # 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.
801 my $column_series = 0;
803 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
804 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
806 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
807 my $series = $col_series->[$series_pos];
808 my @data = @{$series->{'data'}};
809 my $data_size = scalar @data;
810 for (my $i = 0; $i < $data_size; $i++) {
811 my $part1 = $bar_width * (scalar @$col_series * $i);
812 my $part2 = ($series_pos) * $bar_width;
813 my $x1 = $left + $part1 + $part2;
814 if ($has_stacked_columns) {
815 $x1 += ($bar_width * ($i+1));
819 my $x2 = int($x1 + $bar_width - $column_padding)-1;
820 # Special case for when bar_width is less than 1.
825 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
827 my $color = $self->_data_color($series_counter);
830 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
831 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
832 if ($style->{'features'}{'outline'}) {
833 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
837 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
838 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
839 if ($style->{'features'}{'outline'}) {
840 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
848 $self->_set_series_counter($series_counter);
852 sub _draw_stacked_columns {
854 my $style = $self->{'_style'};
856 my $img = $self->_get_image();
858 my $max_value = $self->_get_max_value();
859 my $min_value = $self->_get_min_value();
860 my $column_count = $self->_get_column_count();
861 my $value_range = $max_value - $min_value;
863 my $graph_box = $self->_get_graph_box();
864 my $left = $graph_box->[0] + 1;
865 my $bottom = $graph_box->[1];
867 my $graph_width = $self->_get_number('graph_width');
868 my $graph_height = $self->_get_number('graph_height');
870 my $bar_width = $graph_width / $column_count;
871 my $column_series = 0;
872 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
873 $column_series = (scalar @$column_series_data);
877 my $column_padding_percent = $self->_get_number('column_padding') || 0;
878 if ($column_padding_percent < 0) {
879 return $self->_error("Column padding less than 0");
881 if ($column_padding_percent > 100) {
882 return $self->_error("Column padding greater than 0");
884 my $column_padding = int($column_padding_percent * $bar_width / 100);
887 if ($style->{'features'}{'outline'}) {
888 $outline_color = $self->_get_color('outline.line');
891 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
892 my $col_series = $self->_get_data_series()->{'stacked_column'};
893 my $series_counter = $self->_get_series_counter() || 0;
895 foreach my $series (@$col_series) {
896 my @data = @{$series->{'data'}};
897 my $data_size = scalar @data;
898 for (my $i = 0; $i < $data_size; $i++) {
899 my $part1 = $bar_width * $i * $column_series;
901 my $x1 = int($left + $part1 + $part2);
902 my $x2 = int($x1 + $bar_width - $column_padding) - 1;
903 # Special case for when bar_width is less than 1.
908 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
911 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
912 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
913 if ($style->{'features'}{'outline'}) {
914 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
918 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
919 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
920 if ($style->{'features'}{'outline'}) {
921 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
928 $self->_set_series_counter($series_counter);
932 sub _add_data_series {
934 my $series_type = shift;
935 my $data_ref = shift;
936 my $series_name = shift;
938 my $graph_data = $self->{'graph_data'} || {};
940 my $series = $graph_data->{$series_type} || [];
942 push @$series, { data => $data_ref, series_name => $series_name };
944 $graph_data->{$series_type} = $series;
946 $self->{'graph_data'} = $graph_data;
952 =item show_horizontal_gridlines()
954 Shows horizontal gridlines at the y-tics.
958 sub show_horizontal_gridlines {
959 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
962 =item use_automatic_axis()
964 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.
968 sub use_automatic_axis {
969 eval { require Chart::Math::Axis; };
971 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
973 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
978 =item set_y_tics($count)
980 Set the number of Y tics to use. Their value and position will be determined by the data range.
985 $_[0]->{'y_tics'} = $_[1];
989 return $_[0]->{'y_tics'} || 0;
992 sub _remove_tics_from_chart_box {
994 my $chart_box = shift;
997 my $tic_width = $self->_get_y_tic_width() || 10;
998 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
1001 my $tic_height = $self->_get_x_tic_height() || 10;
1002 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
1004 $self->_remove_box($chart_box, \@y_tic_box);
1005 $self->_remove_box($chart_box, \@x_tic_box);
1007 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
1008 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
1009 $self->_remove_box($chart_box, \@y_tic_tops);
1011 # Make sure that the first and last label fit
1012 if (my $labels = $self->_get_labels()) {
1013 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1014 my @remove_box = ($chart_box->[0],
1016 $chart_box->[0] + int($box[2] / 2) + 1,
1020 $self->_remove_box($chart_box, \@remove_box);
1022 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1023 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1029 $self->_remove_box($chart_box, \@remove_box);
1034 sub _get_y_tic_width{
1036 my $min = $self->_get_min_value();
1037 my $max = $self->_get_max_value();
1038 my $tic_count = $self->_get_y_tics();
1040 my $interval = ($max - $min) / ($tic_count - 1);
1042 my %text_info = $self->_text_style('legend')
1046 for my $count (0 .. $tic_count - 1) {
1047 my $value = sprintf("%.2f", ($count*$interval)+$min);
1049 my @box = $self->_text_bbox($value, 'legend');
1050 my $width = $box[2] - $box[0];
1054 if ($width > $max_width) {
1055 $max_width = $width;
1062 sub _get_x_tic_height {
1065 my $labels = $self->_get_labels();
1071 my $tic_count = (scalar @$labels) - 1;
1073 my %text_info = $self->_text_style('legend')
1077 for my $count (0 .. $tic_count) {
1078 my $label = $labels->[$count];
1080 my @box = $self->_text_bbox($label, 'legend');
1082 my $height = $box[3] - $box[1];
1086 if ($height > $max_height) {
1087 $max_height = $height;
1096 my $min = $self->_get_min_value();
1097 my $max = $self->_get_max_value();
1098 my $tic_count = $self->_get_y_tics();
1100 my $img = $self->_get_image();
1101 my $graph_box = $self->_get_graph_box();
1102 my $image_box = $self->_get_image_box();
1104 my $interval = ($max - $min) / ($tic_count - 1);
1106 my %text_info = $self->_text_style('legend')
1109 my $line_style = $self->_get_color('outline.line');
1110 my $show_gridlines = $self->_get_number('horizontal_gridlines');
1111 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
1112 for my $count (0 .. $tic_count - 1) {
1113 my $x1 = $graph_box->[0] - 5;
1114 my $x2 = $graph_box->[0] + 5;
1115 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
1117 my $value = ($count*$interval)+$min;
1118 if ($interval < 1 || ($value != int($value))) {
1119 $value = sprintf("%.2f", $value);
1122 my @box = $self->_text_bbox($value, 'legend')
1125 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1127 my $width = $box[2];
1128 my $height = $box[3];
1130 $img->string(%text_info,
1131 x => ($x1 - $width - 3),
1132 y => ($y1 + ($height / 2)),
1136 if ($show_gridlines) {
1137 # XXX - line styles!
1138 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1141 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1142 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1152 my $img = $self->_get_image();
1153 my $graph_box = $self->_get_graph_box();
1154 my $image_box = $self->_get_image_box();
1156 my $labels = $self->_get_labels();
1158 my $tic_count = (scalar @$labels) - 1;
1160 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1162 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1163 my $denominator = $tic_count;
1167 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1168 my %text_info = $self->_text_style('legend')
1171 # If automatic axis is turned on, let's be selective about what labels we draw.
1174 if ($self->_get_number('automatic_axis')) {
1175 foreach my $label (@$labels) {
1176 my @box = $self->_text_bbox($label, 'legend');
1177 if ($box[2] > $max_size) {
1178 $max_size = $box[2];
1182 # Give the max_size some padding...
1185 $tic_skip = int($max_size / $tic_distance) + 1;
1188 my $line_style = $self->_get_color('outline.line');
1190 for my $count (0 .. $tic_count) {
1191 next if ($count % ($tic_skip + 1));
1192 my $label = $labels->[$count];
1193 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1196 $x1 += $tic_distance / 2;
1201 my $y1 = $graph_box->[3] + 5;
1202 my $y2 = $graph_box->[3] - 5;
1204 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1206 my @box = $self->_text_bbox($label, 'legend')
1209 my $width = $box[2];
1210 my $height = $box[3];
1212 $img->string(%text_info,
1213 x => ($x1 - ($width / 2)),
1214 y => ($y1 + ($height + 5)),
1224 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1225 return $self->_error("No data supplied");
1228 my $data = $self->_get_data_series();
1229 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1230 return $self->_error("No values in data series");
1232 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1233 return $self->_error("No values in data series");
1235 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1236 return $self->_error("No values in data series");
1242 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1243 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1244 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1245 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1246 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1247 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1248 sub _get_column_count { return $_[0]->{'column_count'} }
1249 sub _get_min_value { return $_[0]->{'min_value'} }
1250 sub _get_max_value { return $_[0]->{'max_value'} }
1251 sub _get_image_box { return $_[0]->{'image_box'} }
1252 sub _get_graph_box { return $_[0]->{'graph_box'} }
1253 sub _get_series_counter { return $_[0]->{'series_counter'} }