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 = $graph_width / $column_count;
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 $part1 = $bar_width * (scalar @$col_series * $i);
675 my $part2 = ($series_pos) * $bar_width;
676 my $x1 = $left + $part1 + $part2;
677 if ($has_stacked_columns) {
678 $x1 += ($bar_width * ($i+1));
682 my $x2 = int($x1 + $bar_width - $column_padding)-1;
683 # Special case for when bar_width is less than 1.
688 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
690 my $color = $self->_data_color($series_counter);
693 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
694 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
695 if ($style->{'features'}{'outline'}) {
696 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
700 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
701 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
702 if ($style->{'features'}{'outline'}) {
703 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
711 $self->_set_series_counter($series_counter);
715 sub _draw_stacked_columns {
717 my $style = $self->{'_style'};
719 my $img = $self->_get_image();
721 my $max_value = $self->_get_max_value();
722 my $min_value = $self->_get_min_value();
723 my $column_count = $self->_get_column_count();
724 my $value_range = $max_value - $min_value;
726 my $graph_box = $self->_get_graph_box();
727 my $left = $graph_box->[0] + 1;
728 my $bottom = $graph_box->[1];
730 my $graph_width = $self->_get_number('graph_width');
731 my $graph_height = $self->_get_number('graph_height');
733 my $bar_width = $graph_width / $column_count;
734 my $column_series = 0;
735 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
736 $column_series = (scalar @$column_series_data);
740 my $column_padding_percent = $self->_get_number('column_padding') || 0;
741 if ($column_padding_percent < 0) {
742 return $self->_error("Column padding less than 0");
744 if ($column_padding_percent > 100) {
745 return $self->_error("Column padding greater than 0");
747 my $column_padding = int($column_padding_percent * $bar_width / 100);
750 if ($style->{'features'}{'outline'}) {
751 $outline_color = $self->_get_color('outline.line');
754 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
755 my $col_series = $self->_get_data_series()->{'stacked_column'};
756 my $series_counter = $self->_get_series_counter() || 0;
758 foreach my $series (@$col_series) {
759 my @data = @{$series->{'data'}};
760 my $data_size = scalar @data;
761 for (my $i = 0; $i < $data_size; $i++) {
762 my $part1 = $bar_width * $i * $column_series;
764 my $x1 = int($left + $part1 + $part2);
765 my $x2 = int($x1 + $bar_width - $column_padding) - 1;
766 # Special case for when bar_width is less than 1.
771 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
774 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
775 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
776 if ($style->{'features'}{'outline'}) {
777 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
781 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
782 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
783 if ($style->{'features'}{'outline'}) {
784 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
791 $self->_set_series_counter($series_counter);
795 sub _add_data_series {
797 my $series_type = shift;
798 my $data_ref = shift;
799 my $series_name = shift;
801 my $graph_data = $self->{'graph_data'} || {};
803 my $series = $graph_data->{$series_type} || [];
805 push @$series, { data => $data_ref, series_name => $series_name };
807 $graph_data->{$series_type} = $series;
809 $self->{'graph_data'} = $graph_data;
815 =item show_horizontal_gridlines()
817 Shows horizontal gridlines at the y-tics.
821 sub show_horizontal_gridlines {
822 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
825 =item use_automatic_axis()
827 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.
831 sub use_automatic_axis {
832 eval { require Chart::Math::Axis; };
834 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
836 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
841 =item set_y_tics($count)
843 Set the number of Y tics to use. Their value and position will be determined by the data range.
848 $_[0]->{'y_tics'} = $_[1];
852 return $_[0]->{'y_tics'} || 0;
855 sub _remove_tics_from_chart_box {
857 my $chart_box = shift;
860 my $tic_width = $self->_get_y_tic_width() || 10;
861 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
864 my $tic_height = $self->_get_x_tic_height() || 10;
865 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
867 $self->_remove_box($chart_box, \@y_tic_box);
868 $self->_remove_box($chart_box, \@x_tic_box);
870 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
871 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
872 $self->_remove_box($chart_box, \@y_tic_tops);
874 # Make sure that the first and last label fit
875 if (my $labels = $self->_get_labels()) {
876 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
877 my @remove_box = ($chart_box->[0],
879 $chart_box->[0] + int($box[2] / 2) + 1,
883 $self->_remove_box($chart_box, \@remove_box);
885 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
886 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
892 $self->_remove_box($chart_box, \@remove_box);
897 sub _get_y_tic_width{
899 my $min = $self->_get_min_value();
900 my $max = $self->_get_max_value();
901 my $tic_count = $self->_get_y_tics();
903 my $interval = ($max - $min) / ($tic_count - 1);
905 my %text_info = $self->_text_style('legend')
909 for my $count (0 .. $tic_count - 1) {
910 my $value = sprintf("%.2f", ($count*$interval)+$min);
912 my @box = $self->_text_bbox($value, 'legend');
913 my $width = $box[2] - $box[0];
917 if ($width > $max_width) {
925 sub _get_x_tic_height {
928 my $labels = $self->_get_labels();
934 my $tic_count = (scalar @$labels) - 1;
936 my %text_info = $self->_text_style('legend')
940 for my $count (0 .. $tic_count) {
941 my $label = $labels->[$count];
943 my @box = $self->_text_bbox($label, 'legend');
945 my $height = $box[3] - $box[1];
949 if ($height > $max_height) {
950 $max_height = $height;
959 my $min = $self->_get_min_value();
960 my $max = $self->_get_max_value();
961 my $tic_count = $self->_get_y_tics();
963 my $img = $self->_get_image();
964 my $graph_box = $self->_get_graph_box();
965 my $image_box = $self->_get_image_box();
967 my $interval = ($max - $min) / ($tic_count - 1);
969 my %text_info = $self->_text_style('legend')
972 my $line_style = $self->_get_color('outline.line');
973 my $show_gridlines = $self->_get_number('horizontal_gridlines');
974 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
975 for my $count (0 .. $tic_count - 1) {
976 my $x1 = $graph_box->[0] - 5;
977 my $x2 = $graph_box->[0] + 5;
978 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
980 my $value = ($count*$interval)+$min;
981 if ($interval < 1 || ($value != int($value))) {
982 $value = sprintf("%.2f", $value);
985 my @box = $self->_text_bbox($value, 'legend')
988 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
991 my $height = $box[3];
993 $img->string(%text_info,
994 x => ($x1 - $width - 3),
995 y => ($y1 + ($height / 2)),
999 if ($show_gridlines) {
1000 # XXX - line styles!
1001 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1004 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1005 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1015 my $img = $self->_get_image();
1016 my $graph_box = $self->_get_graph_box();
1017 my $image_box = $self->_get_image_box();
1019 my $labels = $self->_get_labels();
1021 my $tic_count = (scalar @$labels) - 1;
1023 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1025 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1026 my $denominator = $tic_count;
1030 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1031 my %text_info = $self->_text_style('legend')
1034 # If automatic axis is turned on, let's be selective about what labels we draw.
1037 if ($self->_get_number('automatic_axis')) {
1038 foreach my $label (@$labels) {
1039 my @box = $self->_text_bbox($label, 'legend');
1040 if ($box[2] > $max_size) {
1041 $max_size = $box[2];
1045 # Give the max_size some padding...
1048 $tic_skip = int($max_size / $tic_distance) + 1;
1051 my $line_style = $self->_get_color('outline.line');
1053 for my $count (0 .. $tic_count) {
1054 next if ($count % ($tic_skip + 1));
1055 my $label = $labels->[$count];
1056 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1059 $x1 += $tic_distance / 2;
1064 my $y1 = $graph_box->[3] + 5;
1065 my $y2 = $graph_box->[3] - 5;
1067 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1069 my @box = $self->_text_bbox($label, 'legend')
1072 my $width = $box[2];
1073 my $height = $box[3];
1075 $img->string(%text_info,
1076 x => ($x1 - ($width / 2)),
1077 y => ($y1 + ($height + 5)),
1087 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1088 return $self->_error("No data supplied");
1091 my $data = $self->_get_data_series();
1092 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1093 return $self->_error("No values in data series");
1095 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1096 return $self->_error("No values in data series");
1098 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1099 return $self->_error("No values in data series");
1105 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1106 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1107 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1108 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1109 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1110 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1111 sub _get_column_count { return $_[0]->{'column_count'} }
1112 sub _get_min_value { return $_[0]->{'min_value'} }
1113 sub _get_max_value { return $_[0]->{'max_value'} }
1114 sub _get_image_box { return $_[0]->{'image_box'} }
1115 sub _get_graph_box { return $_[0]->{'graph_box'} }
1116 sub _get_series_counter { return $_[0]->{'series_counter'} }