1 package Imager::Graph::Horizontal;
5 Imager::Graph::Horizontal - A super class for line/bar charts
12 @ISA = qw(Imager::Graph);
14 use constant STARTING_MIN_VALUE => 99999;
18 =item add_data_series(\@data, $series_name)
20 Add a data series to the graph, of the default type.
27 my $series_name = shift;
29 my $series_type = $self->_get_default_series_type();
30 $self->_add_data_series($series_type, $data_ref, $series_name);
35 =item add_column_data_series(\@data, $series_name)
37 Add a column data series to the graph.
41 sub add_column_data_series {
44 my $series_name = shift;
46 $self->_add_data_series('column', $data_ref, $series_name);
51 =item add_line_data_series(\@data, $series_name)
53 Add a line data series to the graph.
57 sub add_line_data_series {
60 my $series_name = shift;
62 $self->_add_data_series('line', $data_ref, $series_name);
67 =item set_column_padding($int)
69 Sets the number of pixels that should go between columns of data.
73 sub set_column_padding {
74 $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
77 =item set_negative_background($color)
79 Sets the background color used below the x axis.
83 sub set_negative_background {
84 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
94 my ($self, %opts) = @_;
96 if (!$self->_valid_input()) {
100 $self->_style_setup(\%opts);
102 my $style = $self->{_style};
104 my $img = $self->_get_image()
107 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
108 $self->_set_image_box(\@image_box);
110 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
111 $self->_draw_legend(\@chart_box);
112 if ($style->{title}{text}) {
113 $self->_draw_title($img, \@chart_box)
117 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
118 return unless $self->_get_data_range();
119 $self->_remove_tics_from_chart_box(\@chart_box);
120 my $column_count = $self->_get_column_count();
122 my $width = $self->_get_number('width');
123 my $height = $self->_get_number('height');
125 my $graph_width = $chart_box[2] - $chart_box[0];
126 my $graph_height = $chart_box[3] - $chart_box[1];
128 my $col_height = int(($graph_height - 1) / $column_count) -1;
129 $graph_height = $col_height * $column_count + 1;
131 my $tic_count = $self->_get_x_tics();
132 my $tic_distance = int(($graph_width -1) / ($tic_count - 1));
133 $graph_width = $tic_distance * ($tic_count - 1);
135 my $bottom = $chart_box[1];
136 my $left = $chart_box[0];
138 $self->{'_style'}{'graph_width'} = $graph_width;
139 $self->{'_style'}{'graph_height'} = $graph_height;
141 my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
142 $self->_set_graph_box(\@graph_box);
145 color => $self->_get_color('outline.line'),
147 xmax => $left+$graph_width,
149 ymax => $bottom+$graph_height,
153 color => $self->_get_color('bg'),
155 xmax => $left+$graph_width - 1,
157 ymax => $bottom+$graph_height-1 ,
161 my $min_value = $self->_get_min_value();
162 my $max_value = $self->_get_max_value();
163 my $value_range = $max_value - $min_value;
167 $zero_position = $left + $graph_width + (-1*$min_value / $value_range) * ($graph_width-1);
170 if ($min_value < 0) {
172 color => $self->_get_color('negative_bg'),
174 xmax => $zero_position,
176 ymax => $bottom+$graph_height - 1,
180 x1 => $zero_position,
182 x2 => $zero_position,
183 y2 => $bottom + $graph_height,
184 color => $self->_get_color('outline.line'),
188 if ($self->_get_data_series()->{'bar'}) {
192 if ($self->_get_x_tics()) {
193 $self->_draw_x_tics();
195 if ($self->_get_labels()) {
196 $self->_draw_y_tics();
199 return $self->_get_image();
202 sub _get_data_range {
207 my $column_count = 0;
209 my ($b_min, $b_max, $b_cols) = $self->_get_bar_range();
211 $min_value = $self->_min(STARTING_MIN_VALUE, $b_min);
212 $max_value = $self->_max(0, $b_max);
213 $column_count = $b_cols;
215 my $config_min = $self->_get_number('x_min');
216 my $config_max = $self->_get_number('x_max');
218 if (defined $config_max && $config_max < $max_value) {
221 if (defined $config_min && $config_min > $min_value) {
225 my $range_padding = $self->_get_number('range_padding');
226 if (defined $config_min) {
227 $min_value = $config_min;
230 if ($min_value > 0) {
233 if ($range_padding && $min_value < 0) {
234 my $difference = $min_value * $range_padding / 100;
235 if ($min_value < -1 && $difference > -1) {
238 $min_value += $difference;
241 if (defined $config_max) {
242 $max_value = $config_max;
245 if ($range_padding && $max_value > 0) {
246 my $difference = $max_value * $range_padding / 100;
247 if ($max_value > 1 && $difference < 1) {
250 $max_value += $difference;
254 if ($self->_get_number('automatic_axis')) {
255 # In case this was set via a style, and not by the api method
256 eval { require Chart::Math::Axis; };
258 return $self->_error("Can't use automatic_axis - $@");
261 my $axis = Chart::Math::Axis->new();
262 $axis->include_zero();
263 $axis->add_data($min_value, $max_value);
264 $max_value = $axis->top;
265 $min_value = $axis->bottom;
266 my $ticks = $axis->ticks;
267 # The +1 is there because we have the bottom tick as well
268 $self->set_x_tics($ticks+1);
271 $self->_set_max_value($max_value);
272 $self->_set_min_value($min_value);
273 $self->_set_column_count($column_count);
282 foreach my $value (@_) {
283 next unless defined $value;
284 if ($value < $min) { $min = $value; }
293 foreach my $value (@_) {
294 next unless defined $value;
295 if ($value > $min) { $min = $value; }
303 my $series = $self->_get_data_series()->{'bar'};
304 return (undef, undef, 0) unless $series;
307 my $min_value = STARTING_MIN_VALUE;
308 my $column_count = 0;
310 my @series = @{$series};
311 foreach my $series (@series) {
312 my @data = @{$series->{'data'}};
314 foreach my $value (@data) {
316 if ($value > $max_value) { $max_value = $value; }
317 if ($value < $min_value) { $min_value = $value; }
321 return ($min_value, $max_value, $column_count);
327 my $chart_box = shift;
328 my $style = $self->{'_style'};
331 my $img = $self->_get_image();
332 if (my $series = $self->_get_data_series()->{'bar'}) {
333 push @labels, map { $_->{'series_name'} } @$series;
336 if ($style->{features}{legend} && (scalar @labels)) {
337 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
343 sub _draw_flat_legend {
349 my $style = $self->{'_style'};
351 my $img = $self->_get_image();
353 my $max_value = $self->_get_max_value();
354 my $min_value = $self->_get_min_value();
355 my $column_count = $self->_get_column_count();
357 my $value_range = $max_value - $min_value;
359 my $width = $self->_get_number('width');
360 my $height = $self->_get_number('height');
362 my $graph_width = $self->_get_number('graph_width');
363 my $graph_height = $self->_get_number('graph_height');
365 my $line_series = $self->_get_data_series()->{'line'};
366 my $series_counter = $self->_get_series_counter() || 0;
368 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
370 my $col_width = int($graph_width / $column_count) -1;
372 my $graph_box = $self->_get_graph_box();
373 my $left = $graph_box->[0] + 1;
374 my $bottom = $graph_box->[1];
376 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
379 my $line_aa = $self->_get_number("lineaa");
380 foreach my $series (@$line_series) {
381 my @data = @{$series->{'data'}};
382 my $data_size = scalar @data;
386 $interval = $graph_width / ($data_size);
389 $interval = $graph_width / ($data_size - 1);
391 my $color = $self->_data_color($series_counter);
393 # We need to add these last, otherwise the next line segment will overwrite half of the marker
394 my @marker_positions;
395 for (my $i = 0; $i < $data_size - 1; $i++) {
396 my $x1 = $left + $i * $interval;
397 my $x2 = $left + ($i + 1) * $interval;
399 $x1 += $has_columns * $interval / 2;
400 $x2 += $has_columns * $interval / 2;
402 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
403 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
405 push @marker_positions, [$x1, $y1];
406 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
409 my $x2 = $left + ($data_size - 1) * $interval;
410 $x2 += $has_columns * $interval / 2;
412 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
414 push @marker_positions, [$x2, $y2];
415 foreach my $position (@marker_positions) {
416 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
421 $self->_set_series_counter($series_counter);
426 my ($self, $index) = @_;
428 my $markers = $self->{'_style'}{'line_markers'};
432 my $marker = $markers->[$index % @$markers];
437 sub _draw_line_marker {
439 my ($x1, $y1, $series_counter) = @_;
441 my $img = $self->_get_image();
443 my $style = $self->_line_marker($series_counter);
444 return unless $style;
446 my $type = $style->{'shape'};
447 my $radius = $style->{'radius'};
449 my $line_aa = $self->_get_number("lineaa");
450 my $fill_aa = $self->_get_number("fill.aa");
451 if ($type eq 'circle') {
452 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
453 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
455 elsif ($type eq 'square') {
456 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
457 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
459 elsif ($type eq 'diamond') {
460 # The gradient really doesn't work for diamond
461 my $color = $self->_data_color($series_counter);
464 [$x1 - $radius, $y1],
465 [$x1, $y1 + $radius],
466 [$x1 + $radius, $y1],
467 [$x1, $y1 - $radius],
469 filled => 1, color => $color, aa => $fill_aa);
471 elsif ($type eq 'triangle') {
472 # The gradient really doesn't work for triangle
473 my $color = $self->_data_color($series_counter);
476 [$x1 - $radius, $y1 + $radius],
477 [$x1 + $radius, $y1 + $radius],
478 [$x1, $y1 - $radius],
480 filled => 1, color => $color, aa => $fill_aa);
483 elsif ($type eq 'x') {
484 my $color = $self->_data_color($series_counter);
485 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
486 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
488 elsif ($type eq 'plus') {
489 my $color = $self->_data_color($series_counter);
490 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
491 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
497 my $style = $self->{'_style'};
499 my $img = $self->_get_image();
501 my $max_value = $self->_get_max_value();
502 my $min_value = $self->_get_min_value();
503 my $column_count = $self->_get_column_count();
505 my $value_range = $max_value - $min_value;
507 my $width = $self->_get_number('width');
508 my $height = $self->_get_number('height');
510 my $graph_width = $self->_get_number('graph_width');
511 my $graph_height = $self->_get_number('graph_height');
514 my $graph_box = $self->_get_graph_box();
515 my $bottom = $graph_box->[1] + 1;
516 my $left = $graph_box->[0];
518 my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1));
520 my $bar_height = int($graph_height / $column_count - 1);
523 if ($style->{'features'}{'outline'}) {
524 $outline_color = $self->_get_color('outline.line');
527 my $series_counter = $self->_get_series_counter() || 0;
528 my $col_series = $self->_get_data_series()->{'bar'};
529 my $column_padding = $self->_get_number('column_padding') || 0;
531 # 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.
532 my $column_series = 0;
534 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
535 my $series = $col_series->[$series_pos];
536 my @data = @{$series->{'data'}};
537 my $data_size = scalar @data;
538 for (my $i = 0; $i < $data_size; $i++) {
540 my $y1 = int($bottom + $bar_height * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos + ($column_padding / 2);
542 my $y2 = $y1 + $bar_height - $column_padding;
544 my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
546 my $color = $self->_data_color($series_counter);
549 my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
550 $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
551 if ($style->{'features'}{'outline'}) {
552 $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
555 elsif ($data[$i] == 0) {
558 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
559 $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
560 if ($style->{'features'}{'outline'}) {
561 $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
569 $self->_set_series_counter($series_counter);
573 sub _add_data_series {
575 my $series_type = shift;
576 my $data_ref = shift;
577 my $series_name = shift;
579 my $graph_data = $self->{'graph_data'} || {};
581 my $series = $graph_data->{$series_type} || [];
583 push @$series, { data => $data_ref, series_name => $series_name };
585 $graph_data->{$series_type} = $series;
587 $self->{'graph_data'} = $graph_data;
593 =item show_vertical_gridlines()
595 Shows vertical gridlines at the y-tics.
599 sub show_vertical_gridlines {
600 $_[0]->{'custom_style'}->{'vertical_gridlines'} = 1;
603 =item use_automatic_axis()
605 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.
609 sub use_automatic_axis {
610 eval { require Chart::Math::Axis; };
612 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
614 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
619 =item set_x_tics($count)
621 Set the number of X tics to use. Their value and position will be determined by the data range.
626 $_[0]->{'x_tics'} = $_[1];
630 return $_[0]->{'x_tics'} || 0;
633 sub _remove_tics_from_chart_box {
635 my $chart_box = shift;
638 my $tic_width = $self->_get_y_tic_width() || 10;
639 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
642 my $tic_height = $self->_get_x_tic_height() || 10;
643 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
645 $self->_remove_box($chart_box, \@y_tic_box);
646 $self->_remove_box($chart_box, \@x_tic_box);
648 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
649 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
650 $self->_remove_box($chart_box, \@y_tic_tops);
652 if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) {
653 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
659 $self->_remove_box($chart_box, \@remove_box);
665 sub _get_y_tic_width {
668 my $labels = $self->_get_labels();
670 foreach my $label (@$labels) {
671 my @box = $self->_text_bbox($label, 'legend');
672 my $width = $box[2] + 5;
673 # For the tic itself...
675 if ($width > $max_width) {
682 sub _get_x_tic_height {
685 my $min = $self->_get_min_value();
686 my $max = $self->_get_max_value();
687 my $tic_count = $self->_get_x_tics();
689 my $interval = ($max - $min) / ($tic_count - 1);
691 my %text_info = $self->_text_style('legend')
695 for my $count (0 .. $tic_count - 1) {
696 my $value = sprintf("%.2f", ($count*$interval)+$min);
698 my @box = $self->_text_bbox($value, 'legend');
699 my $height = $box[3] - $box[1];
703 if ($height > $max_height) {
704 $max_height = $height;
715 my $img = $self->_get_image();
716 my $graph_box = $self->_get_graph_box();
717 my $image_box = $self->_get_image_box();
719 my $labels = $self->_get_labels();
721 my $tic_count = (scalar @$labels) - 1;
723 my $has_columns = defined $self->_get_data_series()->{'bar'};
725 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
726 my $denominator = $tic_count;
730 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
731 my %text_info = $self->_text_style('legend')
734 for my $count (0 .. $tic_count) {
735 my $label = $labels->[$count];
737 my $x1 = $graph_box->[0] - 5;
738 my $x2 = $graph_box->[0] + 5;
740 my $y1 = $graph_box->[1] + ($tic_distance * $count);
743 $y1 += $tic_distance / 2;
746 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
748 my @box = $self->_text_bbox($label, 'legend')
752 my $height = $box[3];
754 $img->string(%text_info,
755 x => ($x1 - ($width + 5)),
756 y => ($y1 + ($height / 2)),
767 my $img = $self->_get_image();
768 my $graph_box = $self->_get_graph_box();
769 my $image_box = $self->_get_image_box();
771 my $tic_count = $self->_get_x_tics();
772 my $min = $self->_get_min_value();
773 my $max = $self->_get_max_value();
774 my $interval = ($max - $min) / ($tic_count - 1);
776 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
777 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
779 my %text_info = $self->_text_style('legend')
782 my $show_gridlines = $self->_get_number('vertical_gridlines');
783 for my $count (0 .. $tic_count-1) {
784 my $x1 = $graph_box->[0] + ($tic_distance * $count);
786 my $y1 = $graph_box->[3] + 5;
787 my $y2 = $graph_box->[3] - 5;
789 my $value = ($count*$interval)+$min;
791 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
793 my @box = $self->_text_bbox($value, 'legend')
797 my $height = $box[3];
799 $img->string(%text_info,
800 x => ($x1 - ($width / 2)),
801 y => ($y1 + $height + 5),
805 if ($show_gridlines) {
807 for (my $i = $graph_box->[1]; $i < $graph_box->[3]; $i += 6) {
810 if ($y2 > $graph_box->[3]) { $y2 = $graph_box->[3]; }
811 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
820 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
821 return $self->_error("No data supplied");
824 my $data = $self->_get_data_series();
825 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
826 return $self->_error("No values in data series");
828 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
829 return $self->_error("No values in data series");
831 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
832 return $self->_error("No values in data series");
838 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
839 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
840 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
841 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
842 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
843 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
844 sub _get_column_count { return $_[0]->{'column_count'} }
845 sub _get_min_value { return $_[0]->{'min_value'} }
846 sub _get_max_value { return $_[0]->{'max_value'} }
847 sub _get_image_box { return $_[0]->{'image_box'} }
848 sub _get_graph_box { return $_[0]->{'graph_box'} }
849 sub _get_series_counter { return $_[0]->{'series_counter'} }