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 = ($graph_width - 1) / $column_count;
176 if ($col_width > 1) {
177 $graph_width = int($col_width) * $column_count + 1;
180 $graph_width = $col_width * $column_count + 1;
183 my $tic_count = $self->_get_y_tics();
184 my $tic_distance = ($graph_height-1) / ($tic_count - 1);
185 $graph_height = int($tic_distance * ($tic_count - 1));
187 my $bottom = $chart_box[1];
188 my $left = $chart_box[0];
190 $self->{'_style'}{'graph_width'} = $graph_width;
191 $self->{'_style'}{'graph_height'} = $graph_height;
193 my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
194 $self->_set_graph_box(\@graph_box);
197 color => $self->_get_color('outline.line'),
199 xmax => $left+$graph_width,
201 ymax => $bottom+$graph_height,
205 color => $self->_get_color('bg'),
207 xmax => $left+$graph_width - 1,
209 ymax => $bottom+$graph_height-1 ,
213 my $min_value = $self->_get_min_value();
214 my $max_value = $self->_get_max_value();
215 my $value_range = $max_value - $min_value;
219 $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
222 if ($min_value < 0) {
224 color => $self->_get_color('negative_bg'),
226 xmax => $left+$graph_width- 1,
227 ymin => $zero_position,
228 ymax => $bottom+$graph_height - 1,
233 y1 => $zero_position,
234 x2 => $left + $graph_width,
235 y2 => $zero_position,
236 color => $self->_get_color('outline.line'),
240 if ($self->_get_data_series()->{'stacked_column'}) {
241 return unless $self->_draw_stacked_columns();
243 if ($self->_get_data_series()->{'column'}) {
244 return unless $self->_draw_columns();
246 if ($self->_get_data_series()->{'line'}) {
247 return unless $self->_draw_lines();
250 if ($self->_get_y_tics()) {
251 $self->_draw_y_tics();
253 if ($self->_get_labels()) {
254 $self->_draw_x_tics();
257 return $self->_get_image();
260 sub _get_data_range {
265 my $column_count = 0;
267 my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
268 my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
269 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
271 # These are side by side...
274 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min);
275 $max_value = $self->_max(0, $sc_max, $c_max, $l_max);
277 my $config_min = $self->_get_number('y_min');
278 my $config_max = $self->_get_number('y_max');
280 if (defined $config_max && $config_max < $max_value) {
283 if (defined $config_min && $config_min > $min_value) {
287 my $range_padding = $self->_get_number('range_padding');
288 if (defined $config_min) {
289 $min_value = $config_min;
292 if ($min_value > 0) {
295 if ($range_padding && $min_value < 0) {
296 my $difference = $min_value * $range_padding / 100;
297 if ($min_value < -1 && $difference > -1) {
300 $min_value += $difference;
303 if (defined $config_max) {
304 $max_value = $config_max;
307 if ($range_padding && $max_value > 0) {
308 my $difference = $max_value * $range_padding / 100;
309 if ($max_value > 1 && $difference < 1) {
312 $max_value += $difference;
315 $column_count = $self->_max(0, $sc_cols, $l_cols);
317 if ($self->_get_number('automatic_axis')) {
318 # In case this was set via a style, and not by the api method
319 eval { require Chart::Math::Axis; };
321 return $self->_error("Can't use automatic_axis - $@");
324 my $axis = Chart::Math::Axis->new();
325 $axis->include_zero();
326 $axis->add_data($min_value, $max_value);
327 $max_value = $axis->top;
328 $min_value = $axis->bottom;
329 my $ticks = $axis->ticks;
330 # The +1 is there because we have the bottom tick as well
331 $self->set_y_tics($ticks+1);
334 $self->_set_max_value($max_value);
335 $self->_set_min_value($min_value);
336 $self->_set_column_count($column_count);
345 foreach my $value (@_) {
346 next unless defined $value;
347 if ($value < $min) { $min = $value; }
356 foreach my $value (@_) {
357 next unless defined $value;
358 if ($value > $min) { $min = $value; }
363 sub _get_line_range {
365 my $series = $self->_get_data_series()->{'line'};
366 return (undef, undef, 0) unless $series;
369 my $min_value = STARTING_MIN_VALUE;
370 my $column_count = 0;
372 my @series = @{$series};
373 foreach my $series (@series) {
374 my @data = @{$series->{'data'}};
376 if (scalar @data > $column_count) {
377 $column_count = scalar @data;
380 foreach my $value (@data) {
381 if ($value > $max_value) { $max_value = $value; }
382 if ($value < $min_value) { $min_value = $value; }
386 return ($min_value, $max_value, $column_count);
389 sub _get_column_range {
392 my $series = $self->_get_data_series()->{'column'};
393 return (undef, undef, 0) unless $series;
396 my $min_value = STARTING_MIN_VALUE;
397 my $column_count = 0;
399 my @series = @{$series};
400 foreach my $series (@series) {
401 my @data = @{$series->{'data'}};
403 foreach my $value (@data) {
405 if ($value > $max_value) { $max_value = $value; }
406 if ($value < $min_value) { $min_value = $value; }
410 return ($min_value, $max_value, $column_count);
413 sub _get_stacked_column_range {
417 my $min_value = STARTING_MIN_VALUE;
418 my $column_count = 0;
420 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
421 my @series = @{$self->_get_data_series()->{'stacked_column'}};
425 for (my $i = scalar @series - 1; $i >= 0; $i--) {
426 my $series = $series[$i];
427 my $data = $series->{'data'};
429 for (my $i = 0; $i < scalar @$data; $i++) {
431 if ($data->[$i] > 0) {
432 $value = $data->[$i] + ($max_entries[$i] || 0);
433 $data->[$i] = $value;
434 $max_entries[$i] = $value;
436 elsif ($data->[$i] < 0) {
437 $value = $data->[$i] + ($min_entries[$i] || 0);
438 $data->[$i] = $value;
439 $min_entries[$i] = $value;
441 if ($value > $max_value) { $max_value = $value; }
442 if ($value < $min_value) { $min_value = $value; }
444 if (scalar @$data > $column_count) {
445 $column_count = scalar @$data;
449 return ($min_value, $max_value, $column_count);
454 my $chart_box = shift;
455 my $style = $self->{'_style'};
458 my $img = $self->_get_image();
459 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
460 push @labels, map { $_->{'series_name'} } @$series;
462 if (my $series = $self->_get_data_series()->{'column'}) {
463 push @labels, map { $_->{'series_name'} } @$series;
465 if (my $series = $self->_get_data_series()->{'line'}) {
466 push @labels, map { $_->{'series_name'} } @$series;
469 if ($style->{features}{legend} && (scalar @labels)) {
470 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
476 sub _draw_flat_legend {
482 my $style = $self->{'_style'};
484 my $img = $self->_get_image();
486 my $max_value = $self->_get_max_value();
487 my $min_value = $self->_get_min_value();
488 my $column_count = $self->_get_column_count();
490 my $value_range = $max_value - $min_value;
492 my $width = $self->_get_number('width');
493 my $height = $self->_get_number('height');
495 my $graph_width = $self->_get_number('graph_width');
496 my $graph_height = $self->_get_number('graph_height');
498 my $line_series = $self->_get_data_series()->{'line'};
499 my $series_counter = $self->_get_series_counter() || 0;
501 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
503 my $col_width = int($graph_width / $column_count) -1;
505 my $graph_box = $self->_get_graph_box();
506 my $left = $graph_box->[0] + 1;
507 my $bottom = $graph_box->[1];
509 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
511 my $line_aa = $self->_get_number("lineaa");
512 foreach my $series (@$line_series) {
513 my @data = @{$series->{'data'}};
514 my $data_size = scalar @data;
518 $interval = $graph_width / ($data_size);
521 $interval = $graph_width / ($data_size - 1);
523 my $color = $self->_data_color($series_counter);
525 # We need to add these last, otherwise the next line segment will overwrite half of the marker
526 my @marker_positions;
527 for (my $i = 0; $i < $data_size - 1; $i++) {
528 my $x1 = $left + $i * $interval;
529 my $x2 = $left + ($i + 1) * $interval;
531 $x1 += $has_columns * $interval / 2;
532 $x2 += $has_columns * $interval / 2;
534 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
535 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
537 push @marker_positions, [$x1, $y1];
538 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
541 my $x2 = $left + ($data_size - 1) * $interval;
542 $x2 += $has_columns * $interval / 2;
544 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
546 push @marker_positions, [$x2, $y2];
547 foreach my $position (@marker_positions) {
548 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
553 $self->_set_series_counter($series_counter);
558 my ($self, $index) = @_;
560 my $markers = $self->{'_style'}{'line_markers'};
564 my $marker = $markers->[$index % @$markers];
569 sub _draw_line_marker {
571 my ($x1, $y1, $series_counter) = @_;
573 my $img = $self->_get_image();
575 my $style = $self->_line_marker($series_counter);
576 return unless $style;
578 my $type = $style->{'shape'};
579 my $radius = $style->{'radius'};
581 my $line_aa = $self->_get_number("lineaa");
582 my $fill_aa = $self->_get_number("fill.aa");
583 if ($type eq 'circle') {
584 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
585 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
587 elsif ($type eq 'square') {
588 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
589 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
591 elsif ($type eq 'diamond') {
592 # The gradient really doesn't work for diamond
593 my $color = $self->_data_color($series_counter);
596 [$x1 - $radius, $y1],
597 [$x1, $y1 + $radius],
598 [$x1 + $radius, $y1],
599 [$x1, $y1 - $radius],
601 filled => 1, color => $color, aa => $fill_aa);
603 elsif ($type eq 'triangle') {
604 # The gradient really doesn't work for triangle
605 my $color = $self->_data_color($series_counter);
608 [$x1 - $radius, $y1 + $radius],
609 [$x1 + $radius, $y1 + $radius],
610 [$x1, $y1 - $radius],
612 filled => 1, color => $color, aa => $fill_aa);
615 elsif ($type eq 'x') {
616 my $color = $self->_data_color($series_counter);
617 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
618 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
620 elsif ($type eq 'plus') {
621 my $color = $self->_data_color($series_counter);
622 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
623 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
629 my $style = $self->{'_style'};
631 my $img = $self->_get_image();
633 my $max_value = $self->_get_max_value();
634 my $min_value = $self->_get_min_value();
635 my $column_count = $self->_get_column_count();
637 my $value_range = $max_value - $min_value;
639 my $width = $self->_get_number('width');
640 my $height = $self->_get_number('height');
642 my $graph_width = $self->_get_number('graph_width');
643 my $graph_height = $self->_get_number('graph_height');
646 my $graph_box = $self->_get_graph_box();
647 my $left = $graph_box->[0] + 1;
648 my $bottom = $graph_box->[1];
649 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
651 my $bar_width = int($graph_width / $column_count - 1);
654 if ($style->{'features'}{'outline'}) {
655 $outline_color = $self->_get_color('outline.line');
658 my $series_counter = $self->_get_series_counter() || 0;
659 my $col_series = $self->_get_data_series()->{'column'};
660 my $column_padding_percent = $self->_get_number('column_padding') || 0;
661 my $column_padding = int($column_padding_percent * $bar_width / 100);
663 # 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.
664 my $column_series = 0;
666 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
667 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
669 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
670 my $series = $col_series->[$series_pos];
671 my @data = @{$series->{'data'}};
672 my $data_size = scalar @data;
673 for (my $i = 0; $i < $data_size; $i++) {
674 my $x1 = int($left + $bar_width * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos + ($column_padding / 2);
675 if ($has_stacked_columns) {
676 $x1 += ($i + 1) * $bar_width + $i + 1;
678 my $x2 = $x1 + $bar_width - $column_padding;
680 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
682 my $color = $self->_data_color($series_counter);
685 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
686 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
687 if ($style->{'features'}{'outline'}) {
688 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
692 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
693 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
694 if ($style->{'features'}{'outline'}) {
695 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
703 $self->_set_series_counter($series_counter);
707 sub _draw_stacked_columns {
709 my $style = $self->{'_style'};
711 my $img = $self->_get_image();
713 my $max_value = $self->_get_max_value();
714 my $min_value = $self->_get_min_value();
715 my $column_count = $self->_get_column_count();
716 my $value_range = $max_value - $min_value;
718 my $graph_box = $self->_get_graph_box();
719 my $left = $graph_box->[0] + 1;
720 my $bottom = $graph_box->[1];
722 my $graph_width = $self->_get_number('graph_width');
723 my $graph_height = $self->_get_number('graph_height');
725 my $bar_width = int($graph_width / $column_count -1);
726 my $column_series = 0;
727 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
728 $column_series = (scalar @$column_series_data);
732 my $column_padding_percent = $self->_get_number('column_padding') || 0;
733 if ($column_padding_percent < 0) {
734 return $self->_error("Column padding less than 0");
736 if ($column_padding_percent > 100) {
737 return $self->_error("Column padding greater than 0");
739 my $column_padding = int($column_padding_percent * $bar_width / 100);
742 if ($style->{'features'}{'outline'}) {
743 $outline_color = $self->_get_color('outline.line');
746 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
747 my $col_series = $self->_get_data_series()->{'stacked_column'};
748 my $series_counter = $self->_get_series_counter() || 0;
750 foreach my $series (@$col_series) {
751 my @data = @{$series->{'data'}};
752 my $data_size = scalar @data;
753 for (my $i = 0; $i < $data_size; $i++) {
754 my $x1 = int($left + $bar_width * ($column_series * $i)) + $column_series * $i + ($column_padding / 2);
755 my $x2 = $x1 + $bar_width - $column_padding;
757 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
760 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
761 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
762 if ($style->{'features'}{'outline'}) {
763 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
767 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
768 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
769 if ($style->{'features'}{'outline'}) {
770 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
777 $self->_set_series_counter($series_counter);
781 sub _add_data_series {
783 my $series_type = shift;
784 my $data_ref = shift;
785 my $series_name = shift;
787 my $graph_data = $self->{'graph_data'} || {};
789 my $series = $graph_data->{$series_type} || [];
791 push @$series, { data => $data_ref, series_name => $series_name };
793 $graph_data->{$series_type} = $series;
795 $self->{'graph_data'} = $graph_data;
801 =item show_horizontal_gridlines()
803 Shows horizontal gridlines at the y-tics.
807 sub show_horizontal_gridlines {
808 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
811 =item use_automatic_axis()
813 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.
817 sub use_automatic_axis {
818 eval { require Chart::Math::Axis; };
820 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
822 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
827 =item set_y_tics($count)
829 Set the number of Y tics to use. Their value and position will be determined by the data range.
834 $_[0]->{'y_tics'} = $_[1];
838 return $_[0]->{'y_tics'} || 0;
841 sub _remove_tics_from_chart_box {
843 my $chart_box = shift;
846 my $tic_width = $self->_get_y_tic_width() || 10;
847 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
850 my $tic_height = $self->_get_x_tic_height() || 10;
851 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
853 $self->_remove_box($chart_box, \@y_tic_box);
854 $self->_remove_box($chart_box, \@x_tic_box);
856 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
857 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
858 $self->_remove_box($chart_box, \@y_tic_tops);
860 # Make sure that the first and last label fit
861 if (my $labels = $self->_get_labels()) {
862 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
863 my @remove_box = ($chart_box->[0],
865 $chart_box->[0] + int($box[2] / 2) + 1,
869 $self->_remove_box($chart_box, \@remove_box);
871 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
872 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
878 $self->_remove_box($chart_box, \@remove_box);
883 sub _get_y_tic_width{
885 my $min = $self->_get_min_value();
886 my $max = $self->_get_max_value();
887 my $tic_count = $self->_get_y_tics();
889 my $interval = ($max - $min) / ($tic_count - 1);
891 my %text_info = $self->_text_style('legend')
895 for my $count (0 .. $tic_count - 1) {
896 my $value = sprintf("%.2f", ($count*$interval)+$min);
898 my @box = $self->_text_bbox($value, 'legend');
899 my $width = $box[2] - $box[0];
903 if ($width > $max_width) {
911 sub _get_x_tic_height {
914 my $labels = $self->_get_labels();
920 my $tic_count = (scalar @$labels) - 1;
922 my %text_info = $self->_text_style('legend')
926 for my $count (0 .. $tic_count) {
927 my $label = $labels->[$count];
929 my @box = $self->_text_bbox($label, 'legend');
931 my $height = $box[3] - $box[1];
935 if ($height > $max_height) {
936 $max_height = $height;
945 my $min = $self->_get_min_value();
946 my $max = $self->_get_max_value();
947 my $tic_count = $self->_get_y_tics();
949 my $img = $self->_get_image();
950 my $graph_box = $self->_get_graph_box();
951 my $image_box = $self->_get_image_box();
953 my $interval = ($max - $min) / ($tic_count - 1);
955 my %text_info = $self->_text_style('legend')
958 my $line_style = $self->_get_color('outline.line');
959 my $show_gridlines = $self->_get_number('horizontal_gridlines');
960 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
961 for my $count (0 .. $tic_count - 1) {
962 my $x1 = $graph_box->[0] - 5;
963 my $x2 = $graph_box->[0] + 5;
964 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
966 my $value = ($count*$interval)+$min;
967 if ($interval < 1 || ($value != int($value))) {
968 $value = sprintf("%.2f", $value);
971 my @box = $self->_text_bbox($value, 'legend')
974 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
977 my $height = $box[3];
979 $img->string(%text_info,
980 x => ($x1 - $width - 3),
981 y => ($y1 + ($height / 2)),
985 if ($show_gridlines) {
987 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
990 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
991 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1001 my $img = $self->_get_image();
1002 my $graph_box = $self->_get_graph_box();
1003 my $image_box = $self->_get_image_box();
1005 my $labels = $self->_get_labels();
1007 my $tic_count = (scalar @$labels) - 1;
1009 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1011 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1012 my $denominator = $tic_count;
1016 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1017 my %text_info = $self->_text_style('legend')
1020 # If automatic axis is turned on, let's be selective about what labels we draw.
1023 if ($self->_get_number('automatic_axis')) {
1024 foreach my $label (@$labels) {
1025 my @box = $self->_text_bbox($label, 'legend');
1026 if ($box[2] > $max_size) {
1027 $max_size = $box[2];
1031 # Give the max_size some padding...
1034 $tic_skip = int($max_size / $tic_distance) + 1;
1037 my $line_style = $self->_get_color('outline.line');
1039 for my $count (0 .. $tic_count) {
1040 next if ($count % ($tic_skip + 1));
1041 my $label = $labels->[$count];
1042 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1045 $x1 += $tic_distance / 2;
1050 my $y1 = $graph_box->[3] + 5;
1051 my $y2 = $graph_box->[3] - 5;
1053 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1055 my @box = $self->_text_bbox($label, 'legend')
1058 my $width = $box[2];
1059 my $height = $box[3];
1061 $img->string(%text_info,
1062 x => ($x1 - ($width / 2)),
1063 y => ($y1 + ($height + 5)),
1073 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1074 return $self->_error("No data supplied");
1077 my $data = $self->_get_data_series();
1078 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1079 return $self->_error("No values in data series");
1081 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1082 return $self->_error("No values in data series");
1084 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1085 return $self->_error("No values in data series");
1091 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1092 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1093 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1094 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1095 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1096 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1097 sub _get_column_count { return $_[0]->{'column_count'} }
1098 sub _get_min_value { return $_[0]->{'min_value'} }
1099 sub _get_max_value { return $_[0]->{'max_value'} }
1100 sub _get_image_box { return $_[0]->{'image_box'} }
1101 sub _get_graph_box { return $_[0]->{'graph_box'} }
1102 sub _get_series_counter { return $_[0]->{'series_counter'} }