1 package Imager::Graph::Vertical;
5 Imager::Graph::Vertical- A super class for line/bar/column charts
12 @ISA = qw(Imager::Graph);
14 use constant STARTING_MIN_VALUE => 99999;
17 =item add_data_series(\@data, $series_name)
19 Add a data series to the graph, of the default type.
26 my $series_name = shift;
28 my $series_type = $self->_get_default_series_type();
29 $self->_add_data_series($series_type, $data_ref, $series_name);
34 =item add_column_data_series(\@data, $series_name)
36 Add a column data series to the graph.
40 sub add_column_data_series {
43 my $series_name = shift;
45 $self->_add_data_series('column', $data_ref, $series_name);
50 =item add_stacked_column_data_series(\@data, $series_name)
52 Add a stacked column data series to the graph.
56 sub add_stacked_column_data_series {
59 my $series_name = shift;
61 $self->_add_data_series('stacked_column', $data_ref, $series_name);
66 =item add_line_data_series(\@data, $series_name)
68 Add a line data series to the graph.
72 sub add_line_data_series {
75 my $series_name = shift;
77 $self->_add_data_series('line', $data_ref, $series_name);
82 =item set_y_max($value)
84 Sets the maximum y value to be displayed. This will be ignored if the y_max is lower than the highest value.
89 $_[0]->{'custom_style'}->{'y_max'} = $_[1];
92 =item set_y_min($value)
94 Sets the minimum y value to be displayed. This will be ignored if the y_min is higher than the lowest value.
99 $_[0]->{'custom_style'}->{'y_min'} = $_[1];
102 =item set_column_padding($int)
104 Sets the padding between columns. This is a percentage of the column width. Defaults to 0.
108 sub set_column_padding {
109 $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
112 =item set_range_padding($percentage)
114 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.
116 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.
120 sub set_range_padding {
121 $_[0]->{'custom_style'}->{'range_padding'} = $_[1];
124 =item set_negative_background($color)
126 Sets the background color used below the x axis.
130 sub set_negative_background {
131 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
141 my ($self, %opts) = @_;
143 if (!$self->_valid_input()) {
147 $self->_style_setup(\%opts);
149 my $style = $self->{_style};
151 my $img = $self->_get_image()
154 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
155 $self->_set_image_box(\@image_box);
157 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
158 $self->_draw_legend(\@chart_box);
159 if ($style->{title}{text}) {
160 $self->_draw_title($img, \@chart_box)
164 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
165 return unless $self->_get_data_range();
166 $self->_remove_tics_from_chart_box(\@chart_box);
167 my $column_count = $self->_get_column_count();
169 my $width = $self->_get_number('width');
170 my $height = $self->_get_number('height');
172 my $graph_width = $chart_box[2] - $chart_box[0];
173 my $graph_height = $chart_box[3] - $chart_box[1];
175 my $col_width = int(($graph_width - 1) / $column_count) -1;
176 $graph_width = $col_width * $column_count + 1;
178 my $tic_count = $self->_get_y_tics();
179 my $tic_distance = int(($graph_height-1) / ($tic_count - 1));
180 $graph_height = $tic_distance * ($tic_count - 1);
182 my $bottom = $chart_box[1];
183 my $left = $chart_box[0];
185 $self->{'_style'}{'graph_width'} = $graph_width;
186 $self->{'_style'}{'graph_height'} = $graph_height;
188 my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
189 $self->_set_graph_box(\@graph_box);
192 color => $self->_get_color('outline.line'),
194 xmax => $left+$graph_width,
196 ymax => $bottom+$graph_height,
200 color => $self->_get_color('bg'),
202 xmax => $left+$graph_width - 1,
204 ymax => $bottom+$graph_height-1 ,
208 my $min_value = $self->_get_min_value();
209 my $max_value = $self->_get_max_value();
210 my $value_range = $max_value - $min_value;
214 $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
217 if ($min_value < 0) {
219 color => $self->_get_color('negative_bg'),
221 xmax => $left+$graph_width- 1,
222 ymin => $zero_position,
223 ymax => $bottom+$graph_height - 1,
228 y1 => $zero_position,
229 x2 => $left + $graph_width,
230 y2 => $zero_position,
231 color => $self->_get_color('outline.line'),
235 if ($self->_get_data_series()->{'stacked_column'}) {
236 return unless $self->_draw_stacked_columns();
238 if ($self->_get_data_series()->{'column'}) {
239 return unless $self->_draw_columns();
241 if ($self->_get_data_series()->{'line'}) {
242 return unless $self->_draw_lines();
245 if ($self->_get_y_tics()) {
246 $self->_draw_y_tics();
248 if ($self->_get_labels()) {
249 $self->_draw_x_tics();
252 return $self->_get_image();
255 sub _get_data_range {
260 my $column_count = 0;
262 my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
263 my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
264 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
266 # These are side by side...
269 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min);
270 $max_value = $self->_max(0, $sc_max, $c_max, $l_max);
272 my $config_min = $self->_get_number('y_min');
273 my $config_max = $self->_get_number('y_max');
275 if (defined $config_max && $config_max < $max_value) {
278 if (defined $config_min && $config_min > $min_value) {
282 my $range_padding = $self->_get_number('range_padding');
283 if (defined $config_min) {
284 $min_value = $config_min;
287 if ($min_value > 0) {
290 if ($range_padding && $min_value < 0) {
291 my $difference = $min_value * $range_padding / 100;
292 if ($min_value < -1 && $difference > -1) {
295 $min_value += $difference;
298 if (defined $config_max) {
299 $max_value = $config_max;
302 if ($range_padding && $max_value > 0) {
303 my $difference = $max_value * $range_padding / 100;
304 if ($max_value > 1 && $difference < 1) {
307 $max_value += $difference;
310 $column_count = $self->_max(0, $sc_cols, $l_cols);
312 if ($self->_get_number('automatic_axis')) {
313 # In case this was set via a style, and not by the api method
314 eval { require Chart::Math::Axis; };
316 return $self->_error("Can't use automatic_axis - $@");
319 my $axis = Chart::Math::Axis->new();
320 $axis->add_data($min_value, $max_value);
321 $max_value = $axis->top;
322 $min_value = $axis->bottom;
323 my $ticks = $axis->ticks;
324 # The +1 is there because we have the bottom tick as well
325 $self->set_y_tics($ticks+1);
328 $self->_set_max_value($max_value);
329 $self->_set_min_value($min_value);
330 $self->_set_column_count($column_count);
339 foreach my $value (@_) {
340 next unless defined $value;
341 if ($value < $min) { $min = $value; }
350 foreach my $value (@_) {
351 next unless defined $value;
352 if ($value > $min) { $min = $value; }
357 sub _get_line_range {
359 my $series = $self->_get_data_series()->{'line'};
360 return (undef, undef, 0) unless $series;
363 my $min_value = STARTING_MIN_VALUE;
364 my $column_count = 0;
366 my @series = @{$series};
367 foreach my $series (@series) {
368 my @data = @{$series->{'data'}};
370 if (scalar @data > $column_count) {
371 $column_count = scalar @data;
374 foreach my $value (@data) {
375 if ($value > $max_value) { $max_value = $value; }
376 if ($value < $min_value) { $min_value = $value; }
380 return ($min_value, $max_value, $column_count);
383 sub _get_column_range {
386 my $series = $self->_get_data_series()->{'column'};
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 foreach my $value (@data) {
399 if ($value > $max_value) { $max_value = $value; }
400 if ($value < $min_value) { $min_value = $value; }
404 return ($min_value, $max_value, $column_count);
407 sub _get_stacked_column_range {
411 my $min_value = STARTING_MIN_VALUE;
412 my $column_count = 0;
414 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
415 my @series = @{$self->_get_data_series()->{'stacked_column'}};
419 for (my $i = scalar @series - 1; $i >= 0; $i--) {
420 my $series = $series[$i];
421 my $data = $series->{'data'};
423 for (my $i = 0; $i < scalar @$data; $i++) {
425 if ($data->[$i] > 0) {
426 $value = $data->[$i] + ($max_entries[$i] || 0);
427 $data->[$i] = $value;
428 $max_entries[$i] = $value;
430 elsif ($data->[$i] < 0) {
431 $value = $data->[$i] + ($min_entries[$i] || 0);
432 $data->[$i] = $value;
433 $min_entries[$i] = $value;
435 if ($value > $max_value) { $max_value = $value; }
436 if ($value < $min_value) { $min_value = $value; }
438 if (scalar @$data > $column_count) {
439 $column_count = scalar @$data;
443 return ($min_value, $max_value, $column_count);
448 my $chart_box = shift;
449 my $style = $self->{'_style'};
452 my $img = $self->_get_image();
453 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
454 push @labels, map { $_->{'series_name'} } @$series;
456 if (my $series = $self->_get_data_series()->{'column'}) {
457 push @labels, map { $_->{'series_name'} } @$series;
459 if (my $series = $self->_get_data_series()->{'line'}) {
460 push @labels, map { $_->{'series_name'} } @$series;
463 if ($style->{features}{legend} && (scalar @labels)) {
464 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
470 sub _draw_flat_legend {
476 my $style = $self->{'_style'};
478 my $img = $self->_get_image();
480 my $max_value = $self->_get_max_value();
481 my $min_value = $self->_get_min_value();
482 my $column_count = $self->_get_column_count();
484 my $value_range = $max_value - $min_value;
486 my $width = $self->_get_number('width');
487 my $height = $self->_get_number('height');
489 my $graph_width = $self->_get_number('graph_width');
490 my $graph_height = $self->_get_number('graph_height');
492 my $line_series = $self->_get_data_series()->{'line'};
493 my $series_counter = $self->_get_series_counter() || 0;
495 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
497 my $col_width = int($graph_width / $column_count) -1;
499 my $graph_box = $self->_get_graph_box();
500 my $left = $graph_box->[0] + 1;
501 my $bottom = $graph_box->[1];
503 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
506 my $line_aa = $self->_get_number("lineaa");
507 foreach my $series (@$line_series) {
508 my @data = @{$series->{'data'}};
509 my $data_size = scalar @data;
513 $interval = $graph_width / ($data_size);
516 $interval = $graph_width / ($data_size - 1);
518 my $color = $self->_data_color($series_counter);
520 # We need to add these last, otherwise the next line segment will overwrite half of the marker
521 my @marker_positions;
522 for (my $i = 0; $i < $data_size - 1; $i++) {
523 my $x1 = $left + $i * $interval;
524 my $x2 = $left + ($i + 1) * $interval;
526 $x1 += $has_columns * $interval / 2;
527 $x2 += $has_columns * $interval / 2;
529 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
530 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
532 push @marker_positions, [$x1, $y1];
533 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
536 my $x2 = $left + ($data_size - 1) * $interval;
537 $x2 += $has_columns * $interval / 2;
539 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
541 push @marker_positions, [$x2, $y2];
542 foreach my $position (@marker_positions) {
543 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
548 $self->_set_series_counter($series_counter);
553 my ($self, $index) = @_;
555 my $markers = $self->{'_style'}{'line_markers'};
559 my $marker = $markers->[$index % @$markers];
564 sub _draw_line_marker {
566 my ($x1, $y1, $series_counter) = @_;
568 my $img = $self->_get_image();
570 my $style = $self->_line_marker($series_counter);
571 return unless $style;
573 my $type = $style->{'shape'};
574 my $radius = $style->{'radius'};
576 my $line_aa = $self->_get_number("lineaa");
577 my $fill_aa = $self->_get_number("fill.aa");
578 if ($type eq 'circle') {
579 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
580 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
582 elsif ($type eq 'square') {
583 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
584 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
586 elsif ($type eq 'diamond') {
587 # The gradient really doesn't work for diamond
588 my $color = $self->_data_color($series_counter);
591 [$x1 - $radius, $y1],
592 [$x1, $y1 + $radius],
593 [$x1 + $radius, $y1],
594 [$x1, $y1 - $radius],
596 filled => 1, color => $color, aa => $fill_aa);
598 elsif ($type eq 'triangle') {
599 # The gradient really doesn't work for triangle
600 my $color = $self->_data_color($series_counter);
603 [$x1 - $radius, $y1 + $radius],
604 [$x1 + $radius, $y1 + $radius],
605 [$x1, $y1 - $radius],
607 filled => 1, color => $color, aa => $fill_aa);
610 elsif ($type eq 'x') {
611 my $color = $self->_data_color($series_counter);
612 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
613 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
615 elsif ($type eq 'plus') {
616 my $color = $self->_data_color($series_counter);
617 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
618 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
624 my $style = $self->{'_style'};
626 my $img = $self->_get_image();
628 my $max_value = $self->_get_max_value();
629 my $min_value = $self->_get_min_value();
630 my $column_count = $self->_get_column_count();
632 my $value_range = $max_value - $min_value;
634 my $width = $self->_get_number('width');
635 my $height = $self->_get_number('height');
637 my $graph_width = $self->_get_number('graph_width');
638 my $graph_height = $self->_get_number('graph_height');
641 my $graph_box = $self->_get_graph_box();
642 my $left = $graph_box->[0] + 1;
643 my $bottom = $graph_box->[1];
644 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
646 my $bar_width = int($graph_width / $column_count - 1);
649 if ($style->{'features'}{'outline'}) {
650 $outline_color = $self->_get_color('outline.line');
653 my $series_counter = $self->_get_series_counter() || 0;
654 my $col_series = $self->_get_data_series()->{'column'};
655 my $column_padding_percent = $self->_get_number('column_padding') || 0;
656 my $column_padding = int($column_padding_percent * $bar_width / 100);
658 # 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.
659 my $column_series = 0;
661 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
662 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
664 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
665 my $series = $col_series->[$series_pos];
666 my @data = @{$series->{'data'}};
667 my $data_size = scalar @data;
668 my $color = $self->_data_color($series_counter);
669 for (my $i = 0; $i < $data_size; $i++) {
670 my $x1 = int($left + $bar_width * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos + ($column_padding / 2);
671 if ($has_stacked_columns) {
672 $x1 += ($i + 1) * $bar_width + $i + 1;
674 my $x2 = $x1 + $bar_width - $column_padding;
676 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
678 my $color = $self->_data_color($series_counter);
680 # my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position]);
682 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1);
683 if ($style->{'features'}{'outline'}) {
684 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
688 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1);
689 if ($style->{'features'}{'outline'}) {
690 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
698 $self->_set_series_counter($series_counter);
702 sub _draw_stacked_columns {
704 my $style = $self->{'_style'};
706 my $img = $self->_get_image();
708 my $max_value = $self->_get_max_value();
709 my $min_value = $self->_get_min_value();
710 my $column_count = $self->_get_column_count();
711 my $value_range = $max_value - $min_value;
713 my $graph_box = $self->_get_graph_box();
714 my $left = $graph_box->[0] + 1;
715 my $bottom = $graph_box->[1];
717 my $graph_width = $self->_get_number('graph_width');
718 my $graph_height = $self->_get_number('graph_height');
720 my $bar_width = int($graph_width / $column_count -1);
721 my $column_series = 0;
722 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
723 $column_series = (scalar @$column_series_data);
727 my $column_padding_percent = $self->_get_number('column_padding') || 0;
728 if ($column_padding_percent < 0) {
729 return $self->_error("Column padding less than 0");
731 if ($column_padding_percent > 100) {
732 return $self->_error("Column padding greater than 0");
734 my $column_padding = int($column_padding_percent * $bar_width / 100);
737 if ($style->{'features'}{'outline'}) {
738 $outline_color = $self->_get_color('outline.line');
741 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
742 my $col_series = $self->_get_data_series()->{'stacked_column'};
743 my $series_counter = $self->_get_series_counter() || 0;
745 foreach my $series (@$col_series) {
746 my @data = @{$series->{'data'}};
747 my $data_size = scalar @data;
748 my $color = $self->_data_color($series_counter);
749 for (my $i = 0; $i < $data_size; $i++) {
750 my $x1 = int($left + $bar_width * ($column_series * $i)) + $column_series * $i + ($column_padding / 2);
751 my $x2 = $x1 + $bar_width - $column_padding;
753 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
756 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, color => $color, filled => 1);
757 if ($style->{'features'}{'outline'}) {
758 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
762 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, color => $color, filled => 1);
763 if ($style->{'features'}{'outline'}) {
764 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
771 $self->_set_series_counter($series_counter);
775 sub _add_data_series {
777 my $series_type = shift;
778 my $data_ref = shift;
779 my $series_name = shift;
781 my $graph_data = $self->{'graph_data'} || {};
783 my $series = $graph_data->{$series_type} || [];
785 push @$series, { data => $data_ref, series_name => $series_name };
787 $graph_data->{$series_type} = $series;
789 $self->{'graph_data'} = $graph_data;
795 =item show_horizontal_gridlines()
797 Shows horizontal gridlines at the y-tics.
801 sub show_horizontal_gridlines {
802 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
805 =item use_automatic_axis()
807 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.
811 sub use_automatic_axis {
812 eval { require Chart::Math::Axis; };
814 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
816 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
821 =item set_y_tics($count)
823 Set the number of Y tics to use. Their value and position will be determined by the data range.
828 $_[0]->{'y_tics'} = $_[1];
832 return $_[0]->{'y_tics'} || 0;
835 sub _remove_tics_from_chart_box {
837 my $chart_box = shift;
840 my $tic_width = $self->_get_y_tic_width() || 10;
841 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
844 my $tic_height = $self->_get_x_tic_height() || 10;
845 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
847 $self->_remove_box($chart_box, \@y_tic_box);
848 $self->_remove_box($chart_box, \@x_tic_box);
850 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
851 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
852 $self->_remove_box($chart_box, \@y_tic_tops);
856 sub _get_y_tic_width{
858 my $min = $self->_get_min_value();
859 my $max = $self->_get_max_value();
860 my $tic_count = $self->_get_y_tics();
862 my $img = $self->_get_image();
863 my $graph_box = $self->_get_graph_box();
864 my $image_box = $self->_get_image_box();
866 my $interval = ($max - $min) / ($tic_count - 1);
868 my %text_info = $self->_text_style('legend')
872 for my $count (0 .. $tic_count - 1) {
873 my $value = sprintf("%.2f", ($count*$interval)+$min);
875 my @box = $self->_text_bbox($value, 'legend');
876 my $width = $box[2] - $box[0];
880 if ($width > $max_width) {
888 sub _get_x_tic_height {
891 my $labels = $self->_get_labels();
897 my $tic_count = (scalar @$labels) - 1;
899 my %text_info = $self->_text_style('legend')
903 for my $count (0 .. $tic_count) {
904 my $label = $labels->[$count];
906 my @box = $self->_text_bbox($label, 'legend');
908 my $height = $box[3] - $box[1];
912 if ($height > $max_height) {
913 $max_height = $height;
922 my $min = $self->_get_min_value();
923 my $max = $self->_get_max_value();
924 my $tic_count = $self->_get_y_tics();
926 my $img = $self->_get_image();
927 my $graph_box = $self->_get_graph_box();
928 my $image_box = $self->_get_image_box();
930 my $interval = ($max - $min) / ($tic_count - 1);
932 my %text_info = $self->_text_style('legend')
935 my $show_gridlines = $self->_get_number('horizontal_gridlines');
936 my $tic_distance = int(($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1));
937 for my $count (0 .. $tic_count - 1) {
938 my $x1 = $graph_box->[0] - 5;
939 my $x2 = $graph_box->[0] + 5;
940 my $y1 = $graph_box->[3] - ($count * $tic_distance);
942 my $value = ($count*$interval)+$min;
943 if ($interval < 1 || ($value != int($value))) {
944 $value = sprintf("%.2f", $value);
947 my @box = $self->_text_bbox($value, 'legend')
950 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
953 my $height = $box[3];
955 $img->string(%text_info,
956 x => ($x1 - $width - 3),
957 y => ($y1 + ($height / 2)),
961 if ($show_gridlines) {
963 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
966 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
967 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
977 my $img = $self->_get_image();
978 my $graph_box = $self->_get_graph_box();
979 my $image_box = $self->_get_image_box();
981 my $labels = $self->_get_labels();
983 my $tic_count = (scalar @$labels) - 1;
985 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
987 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
988 my $denominator = $tic_count;
992 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
993 my %text_info = $self->_text_style('legend')
996 for my $count (0 .. $tic_count) {
997 my $label = $labels->[$count];
998 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1001 $x1 += $tic_distance / 2;
1003 my $y1 = $graph_box->[3] + 5;
1004 my $y2 = $graph_box->[3] - 5;
1006 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
1008 my @box = $self->_text_bbox($label, 'legend')
1011 my $width = $box[2];
1012 my $height = $box[3];
1014 $img->string(%text_info,
1015 x => ($x1 - ($width / 2)),
1016 y => ($y1 + ($height + 5)),
1026 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1027 return $self->_error("No data supplied");
1030 my $data = $self->_get_data_series();
1031 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1032 return $self->_error("No values in data series");
1034 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1035 return $self->_error("No values in data series");
1037 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1038 return $self->_error("No values in data series");
1044 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1045 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1046 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1047 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1048 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1049 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1050 sub _get_column_count { return $_[0]->{'column_count'} }
1051 sub _get_min_value { return $_[0]->{'min_value'} }
1052 sub _get_max_value { return $_[0]->{'max_value'} }
1053 sub _get_image_box { return $_[0]->{'image_box'} }
1054 sub _get_graph_box { return $_[0]->{'graph_box'} }
1055 sub _get_series_counter { return $_[0]->{'series_counter'} }