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 = ($graph_height - 1) / $column_count;
129 if ($col_height > 1) {
130 $graph_height = int($col_height) * $column_count + 1;
133 $graph_height = $col_height * $column_count + 1;
136 my $tic_count = $self->_get_x_tics();
137 my $tic_distance = int(($graph_width -1) / ($tic_count - 1));
138 $graph_width = $tic_distance * ($tic_count - 1);
140 my $bottom = $chart_box[1];
141 my $left = $chart_box[0];
143 $self->{'_style'}{'graph_width'} = $graph_width;
144 $self->{'_style'}{'graph_height'} = $graph_height;
146 my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
147 $self->_set_graph_box(\@graph_box);
150 color => $self->_get_color('outline.line'),
152 xmax => $left+$graph_width,
154 ymax => $bottom+$graph_height,
158 color => $self->_get_color('bg'),
160 xmax => $left+$graph_width - 1,
162 ymax => $bottom+$graph_height-1 ,
166 my $min_value = $self->_get_min_value();
167 my $max_value = $self->_get_max_value();
168 my $value_range = $max_value - $min_value;
172 $zero_position = $left + $graph_width + (-1*$min_value / $value_range) * ($graph_width-1);
175 if ($min_value < 0) {
177 color => $self->_get_color('negative_bg'),
179 xmax => $zero_position,
181 ymax => $bottom+$graph_height - 1,
185 x1 => $zero_position,
187 x2 => $zero_position,
188 y2 => $bottom + $graph_height,
189 color => $self->_get_color('outline.line'),
193 if ($self->_get_data_series()->{'bar'}) {
196 if ($self->_get_data_series()->{'line'}) {
197 $self->_draw_lines();
200 if ($self->_get_x_tics()) {
201 $self->_draw_x_tics();
203 if ($self->_get_labels()) {
204 $self->_draw_y_tics();
207 return $self->_get_image();
210 sub _get_data_range {
215 my $column_count = 0;
217 my ($b_min, $b_max, $b_cols) = $self->_get_bar_range();
218 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
220 $min_value = $self->_min(STARTING_MIN_VALUE, $b_min, $l_min);
221 $max_value = $self->_max(0, $b_max, $l_max);
222 $column_count = $self->_max(0, $b_cols, $l_cols);
224 my $config_min = $self->_get_number('x_min');
225 my $config_max = $self->_get_number('x_max');
227 if (defined $config_max && $config_max < $max_value) {
230 if (defined $config_min && $config_min > $min_value) {
234 my $range_padding = $self->_get_number('range_padding');
235 if (defined $config_min) {
236 $min_value = $config_min;
239 if ($min_value > 0) {
242 if ($range_padding && $min_value < 0) {
243 my $difference = $min_value * $range_padding / 100;
244 if ($min_value < -1 && $difference > -1) {
247 $min_value += $difference;
250 if (defined $config_max) {
251 $max_value = $config_max;
254 if ($range_padding && $max_value > 0) {
255 my $difference = $max_value * $range_padding / 100;
256 if ($max_value > 1 && $difference < 1) {
259 $max_value += $difference;
263 if ($self->_get_number('automatic_axis')) {
264 # In case this was set via a style, and not by the api method
265 eval { require Chart::Math::Axis; };
267 return $self->_error("Can't use automatic_axis - $@");
270 my $axis = Chart::Math::Axis->new();
271 $axis->include_zero();
272 $axis->add_data($min_value, $max_value);
273 $max_value = $axis->top;
274 $min_value = $axis->bottom;
275 my $ticks = $axis->ticks;
276 # The +1 is there because we have the bottom tick as well
277 $self->set_x_tics($ticks+1);
280 $self->_set_max_value($max_value);
281 $self->_set_min_value($min_value);
282 $self->_set_column_count($column_count);
291 foreach my $value (@_) {
292 next unless defined $value;
293 if ($value < $min) { $min = $value; }
302 foreach my $value (@_) {
303 next unless defined $value;
304 if ($value > $min) { $min = $value; }
309 sub _get_line_range {
311 my $series = $self->_get_data_series()->{'line'};
312 return (undef, undef, 0) unless $series;
315 my $min_value = STARTING_MIN_VALUE;
316 my $column_count = 0;
318 my @series = @{$series};
319 foreach my $series (@series) {
320 my @data = @{$series->{'data'}};
322 if (scalar @data > $column_count) {
323 $column_count = scalar @data;
326 foreach my $value (@data) {
327 if ($value > $max_value) { $max_value = $value; }
328 if ($value < $min_value) { $min_value = $value; }
332 return ($min_value, $max_value, $column_count);
340 my $series = $self->_get_data_series()->{'bar'};
341 return (undef, undef, 0) unless $series;
344 my $min_value = STARTING_MIN_VALUE;
345 my $column_count = 0;
347 my @series = @{$series};
348 foreach my $series (@series) {
349 my @data = @{$series->{'data'}};
351 foreach my $value (@data) {
353 if ($value > $max_value) { $max_value = $value; }
354 if ($value < $min_value) { $min_value = $value; }
358 return ($min_value, $max_value, $column_count);
364 my $chart_box = shift;
365 my $style = $self->{'_style'};
368 my $img = $self->_get_image();
369 if (my $series = $self->_get_data_series()->{'bar'}) {
370 push @labels, map { $_->{'series_name'} } @$series;
373 if ($style->{features}{legend} && (scalar @labels)) {
374 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
380 sub _draw_flat_legend {
386 my $style = $self->{'_style'};
388 my $img = $self->_get_image();
390 my $max_value = $self->_get_max_value();
391 my $min_value = $self->_get_min_value();
392 my $column_count = $self->_get_column_count();
394 my $value_range = $max_value - $min_value;
396 my $width = $self->_get_number('width');
397 my $height = $self->_get_number('height');
399 my $graph_width = $self->_get_number('graph_width');
400 my $graph_height = $self->_get_number('graph_height');
402 my $line_series = $self->_get_data_series()->{'line'};
403 my $series_counter = $self->_get_series_counter() || 0;
405 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
407 my $col_height = int($graph_height / $column_count) -1;
409 my $graph_box = $self->_get_graph_box();
410 my $left = $graph_box->[0] + 1;
411 my $bottom = $graph_box->[1];
413 my $zero_position = $left + $graph_width - (-1*$min_value / $value_range) * ($graph_width - 1);
415 my $line_aa = $self->_get_number("lineaa");
416 foreach my $series (@$line_series) {
417 my @data = @{$series->{'data'}};
418 my $data_size = scalar @data;
422 $interval = $graph_height / ($data_size);
425 $interval = $graph_height / ($data_size - 1);
427 my $color = $self->_data_color($series_counter);
429 # We need to add these last, otherwise the next line segment will overwrite half of the marker
430 my @marker_positions;
431 for (my $i = 0; $i < $data_size - 1; $i++) {
432 my $y1 = $bottom + $i * $interval;
433 my $y2 = $bottom + ($i + 1) * $interval;
435 $y1 += $has_columns * $interval / 2;
436 $y2 += $has_columns * $interval / 2;
438 my $x1 = $left + ($value_range - $data[$i] + $min_value)/$value_range * $graph_width;
439 my $x2 = $left + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_width;
441 push @marker_positions, [$x1, $y1];
442 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
446 my $y2 = $bottom + ($data_size - 1) * $interval;
447 $y2 += $has_columns * $interval / 2;
449 my $x2 = $left + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_width;
451 push @marker_positions, [$x2, $y2];
452 foreach my $position (@marker_positions) {
453 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
458 $self->_set_series_counter($series_counter);
463 my ($self, $index) = @_;
465 my $markers = $self->{'_style'}{'line_markers'};
469 my $marker = $markers->[$index % @$markers];
474 sub _draw_line_marker {
476 my ($x1, $y1, $series_counter) = @_;
478 my $img = $self->_get_image();
480 my $style = $self->_line_marker($series_counter);
481 return unless $style;
483 my $type = $style->{'shape'};
484 my $radius = $style->{'radius'};
486 my $line_aa = $self->_get_number("lineaa");
487 my $fill_aa = $self->_get_number("fill.aa");
488 if ($type eq 'circle') {
489 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
490 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
492 elsif ($type eq 'square') {
493 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
494 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
496 elsif ($type eq 'diamond') {
497 # The gradient really doesn't work for diamond
498 my $color = $self->_data_color($series_counter);
501 [$x1 - $radius, $y1],
502 [$x1, $y1 + $radius],
503 [$x1 + $radius, $y1],
504 [$x1, $y1 - $radius],
506 filled => 1, color => $color, aa => $fill_aa);
508 elsif ($type eq 'triangle') {
509 # The gradient really doesn't work for triangle
510 my $color = $self->_data_color($series_counter);
513 [$x1 - $radius, $y1 + $radius],
514 [$x1 + $radius, $y1 + $radius],
515 [$x1, $y1 - $radius],
517 filled => 1, color => $color, aa => $fill_aa);
520 elsif ($type eq 'x') {
521 my $color = $self->_data_color($series_counter);
522 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
523 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
525 elsif ($type eq 'plus') {
526 my $color = $self->_data_color($series_counter);
527 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
528 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
534 my $style = $self->{'_style'};
536 my $img = $self->_get_image();
538 my $max_value = $self->_get_max_value();
539 my $min_value = $self->_get_min_value();
540 my $column_count = $self->_get_column_count();
542 my $value_range = $max_value - $min_value;
544 my $width = $self->_get_number('width');
545 my $height = $self->_get_number('height');
547 my $graph_width = $self->_get_number('graph_width');
548 my $graph_height = $self->_get_number('graph_height');
551 my $graph_box = $self->_get_graph_box();
552 my $bottom = $graph_box->[1] + 1;
553 my $left = $graph_box->[0];
555 my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1));
557 my $bar_height = $graph_height / $column_count;
560 if ($style->{'features'}{'outline'}) {
561 $outline_color = $self->_get_color('outline.line');
564 my $series_counter = $self->_get_series_counter() || 0;
565 my $col_series = $self->_get_data_series()->{'bar'};
566 my $column_padding = $self->_get_number('column_padding') || 0;
568 # 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.
569 my $column_series = 0;
571 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
572 my $series = $col_series->[$series_pos];
573 my @data = @{$series->{'data'}};
574 my $data_size = scalar @data;
575 for (my $i = 0; $i < $data_size; $i++) {
577 my $part1 = $bar_height * (scalar @$col_series * $i);
578 my $part2 = ($series_pos) * $bar_height;
579 my $y1 = int($bottom + $part1 + $part2);
581 my $y2 = int($y1 + $bar_height - $column_padding)-1;
582 # Special case for when bar_height is less than 1.
587 my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
589 my $color = $self->_data_color($series_counter);
592 my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
593 $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
594 if ($style->{'features'}{'outline'}) {
595 $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
598 elsif ($data[$i] == 0) {
601 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
602 $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
603 if ($style->{'features'}{'outline'}) {
604 $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
612 $self->_set_series_counter($series_counter);
616 sub _add_data_series {
618 my $series_type = shift;
619 my $data_ref = shift;
620 my $series_name = shift;
622 my $graph_data = $self->{'graph_data'} || {};
624 my $series = $graph_data->{$series_type} || [];
626 push @$series, { data => $data_ref, series_name => $series_name };
628 $graph_data->{$series_type} = $series;
630 $self->{'graph_data'} = $graph_data;
636 =item show_vertical_gridlines()
638 Shows vertical gridlines at the y-tics.
642 sub show_vertical_gridlines {
643 $_[0]->{'custom_style'}->{'vertical_gridlines'} = 1;
646 =item use_automatic_axis()
648 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.
652 sub use_automatic_axis {
653 eval { require Chart::Math::Axis; };
655 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
657 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
662 =item set_x_tics($count)
664 Set the number of X tics to use. Their value and position will be determined by the data range.
669 $_[0]->{'x_tics'} = $_[1];
673 return $_[0]->{'x_tics'} || 0;
676 sub _remove_tics_from_chart_box {
678 my $chart_box = shift;
681 my $tic_width = $self->_get_y_tic_width() || 10;
682 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
685 my $tic_height = $self->_get_x_tic_height() || 10;
686 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
688 $self->_remove_box($chart_box, \@y_tic_box);
689 $self->_remove_box($chart_box, \@x_tic_box);
691 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
692 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
693 $self->_remove_box($chart_box, \@y_tic_tops);
695 if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) {
696 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
702 $self->_remove_box($chart_box, \@remove_box);
708 sub _get_y_tic_width {
711 my $labels = $self->_get_labels();
717 my %text_info = $self->_text_style('legend')
721 foreach my $label (@$labels) {
722 my @box = $self->_text_bbox($label, 'legend');
723 my $width = $box[2] + 5;
724 # For the tic itself...
726 if ($width > $max_width) {
733 sub _get_x_tic_height {
736 my $min = $self->_get_min_value();
737 my $max = $self->_get_max_value();
738 my $tic_count = $self->_get_x_tics();
740 my $interval = ($max - $min) / ($tic_count - 1);
742 my %text_info = $self->_text_style('legend')
746 for my $count (0 .. $tic_count - 1) {
747 my $value = sprintf("%.2f", ($count*$interval)+$min);
749 my @box = $self->_text_bbox($value, 'legend');
750 my $height = $box[3] - $box[1];
754 if ($height > $max_height) {
755 $max_height = $height;
766 my $img = $self->_get_image();
767 my $graph_box = $self->_get_graph_box();
768 my $image_box = $self->_get_image_box();
770 my $labels = $self->_get_labels();
772 my $tic_count = (scalar @$labels) - 1;
774 my $has_columns = defined $self->_get_data_series()->{'bar'};
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 $denominator = $tic_count;
781 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
782 my %text_info = $self->_text_style('legend')
785 for my $count (0 .. $tic_count) {
786 my $label = $labels->[$count];
788 my $x1 = $graph_box->[0] - 5;
789 my $x2 = $graph_box->[0] + 5;
791 my $y1 = $graph_box->[1] + ($tic_distance * $count);
794 $y1 += $tic_distance / 2;
797 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
799 my @box = $self->_text_bbox($label, 'legend')
803 my $height = $box[3];
805 $img->string(%text_info,
806 x => ($x1 - ($width + 5)),
807 y => ($y1 + ($height / 2)),
818 my $img = $self->_get_image();
819 my $graph_box = $self->_get_graph_box();
820 my $image_box = $self->_get_image_box();
822 my $tic_count = $self->_get_x_tics();
823 my $min = $self->_get_min_value();
824 my $max = $self->_get_max_value();
825 my $interval = ($max - $min) / ($tic_count - 1);
827 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
828 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
830 my %text_info = $self->_text_style('legend')
833 my $show_gridlines = $self->_get_number('vertical_gridlines');
834 for my $count (0 .. $tic_count-1) {
835 my $x1 = $graph_box->[0] + ($tic_distance * $count);
837 my $y1 = $graph_box->[3] + 5;
838 my $y2 = $graph_box->[3] - 5;
840 my $value = ($count*$interval)+$min;
842 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
844 my @box = $self->_text_bbox($value, 'legend')
848 my $height = $box[3];
850 $img->string(%text_info,
851 x => ($x1 - ($width / 2)),
852 y => ($y1 + $height + 5),
856 if ($show_gridlines) {
858 for (my $i = $graph_box->[1]; $i < $graph_box->[3]; $i += 6) {
861 if ($y2 > $graph_box->[3]) { $y2 = $graph_box->[3]; }
862 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
871 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
872 return $self->_error("No data supplied");
875 my $data = $self->_get_data_series();
876 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
877 return $self->_error("No values in data series");
879 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
880 return $self->_error("No values in data series");
882 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
883 return $self->_error("No values in data series");
889 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
890 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
891 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
892 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
893 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
894 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
895 sub _get_column_count { return $_[0]->{'column_count'} }
896 sub _get_min_value { return $_[0]->{'min_value'} }
897 sub _get_max_value { return $_[0]->{'max_value'} }
898 sub _get_image_box { return $_[0]->{'image_box'} }
899 sub _get_graph_box { return $_[0]->{'graph_box'} }
900 sub _get_series_counter { return $_[0]->{'series_counter'} }