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_bar_data_series(\@data, $series_name)
37 Add a bar data series to the graph.
41 sub add_bar_data_series {
44 my $series_name = shift;
46 $self->_add_data_series('bar', $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};
107 my $img = $self->_get_image()
110 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
111 $self->_set_image_box(\@image_box);
113 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
114 $self->_draw_legend(\@chart_box);
115 if ($style->{title}{text}) {
116 $self->_draw_title($img, \@chart_box)
120 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
121 return unless $self->_get_data_range();
122 $self->_remove_tics_from_chart_box(\@chart_box, \%opts);
123 my $column_count = $self->_get_column_count();
125 my $width = $self->_get_number('width');
126 my $height = $self->_get_number('height');
128 my $graph_width = $chart_box[2] - $chart_box[0];
129 my $graph_height = $chart_box[3] - $chart_box[1];
131 my $col_height = ($graph_height - 1) / $column_count;
132 if ($col_height > 1) {
133 $graph_height = int($col_height) * $column_count + 1;
136 $graph_height = $col_height * $column_count + 1;
139 my $tic_count = $self->_get_x_tics();
140 my $tic_distance = int(($graph_width -1) / ($tic_count - 1));
141 $graph_width = $tic_distance * ($tic_count - 1);
143 my $top = $chart_box[1];
144 my $left = $chart_box[0];
146 $self->{'_style'}{'graph_width'} = $graph_width;
147 $self->{'_style'}{'graph_height'} = $graph_height;
149 my @graph_box = ($left, $top, $left + $graph_width, $top + $graph_height);
151 $self->_set_graph_box(\@graph_box);
153 my @fill_box = @graph_box;
155 if ($self->_feature_enabled("graph_outline")) {
156 my @line = $self->_get_line("graph.outline")
171 $self->_get_fill("graph.fill"),
175 my $min_value = $self->_get_min_value();
176 my $max_value = $self->_get_max_value();
177 my $value_range = $max_value - $min_value;
181 $zero_position = $left + $graph_width + (-1*$min_value / $value_range) * ($graph_width-1);
184 if ($min_value < 0) {
186 color => $self->_get_color('negative_bg'),
188 xmax => $zero_position,
190 ymax => $top+$graph_height - 1,
194 x1 => $zero_position,
196 x2 => $zero_position,
197 y2 => $top + $graph_height,
198 color => $self->_get_color('outline.line'),
202 $self->_reset_series_counter();
204 if ($self->_get_data_series()->{'bar'}) {
207 if ($self->_get_data_series()->{'line'}) {
208 $self->_draw_lines();
211 if ($self->_get_x_tics()) {
212 $self->_draw_x_tics();
214 if ($self->_get_labels(\%opts)) {
215 $self->_draw_y_tics(\%opts);
218 return $self->_get_image();
221 sub _get_data_range {
226 my $column_count = 0;
228 my ($b_min, $b_max, $b_cols) = $self->_get_bar_range();
229 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
231 $min_value = $self->_min(STARTING_MIN_VALUE, $b_min, $l_min);
232 $max_value = $self->_max(0, $b_max, $l_max);
233 $column_count = $self->_max(0, $b_cols, $l_cols);
235 my $config_min = $self->_get_number('x_min');
236 my $config_max = $self->_get_number('x_max');
238 if (defined $config_max && $config_max < $max_value) {
241 if (defined $config_min && $config_min > $min_value) {
245 my $range_padding = $self->_get_number('range_padding');
246 if (defined $config_min) {
247 $min_value = $config_min;
250 if ($min_value > 0) {
253 if ($range_padding && $min_value < 0) {
254 my $difference = $min_value * $range_padding / 100;
255 if ($min_value < -1 && $difference > -1) {
258 $min_value += $difference;
261 if (defined $config_max) {
262 $max_value = $config_max;
265 if ($range_padding && $max_value > 0) {
266 my $difference = $max_value * $range_padding / 100;
267 if ($max_value > 1 && $difference < 1) {
270 $max_value += $difference;
274 if ($self->_get_number('automatic_axis')) {
275 # In case this was set via a style, and not by the api method
276 eval { require Chart::Math::Axis; };
278 return $self->_error("Can't use automatic_axis - $@");
281 my $axis = Chart::Math::Axis->new();
282 $axis->include_zero();
283 $axis->add_data($min_value, $max_value);
284 $max_value = $axis->top;
285 $min_value = $axis->bottom;
286 my $ticks = $axis->ticks;
287 # The +1 is there because we have the bottom tick as well
288 $self->set_x_tics($ticks+1);
291 $self->_set_max_value($max_value);
292 $self->_set_min_value($min_value);
293 $self->_set_column_count($column_count);
302 foreach my $value (@_) {
303 next unless defined $value;
304 if ($value < $min) { $min = $value; }
313 foreach my $value (@_) {
314 next unless defined $value;
315 if ($value > $min) { $min = $value; }
320 sub _get_line_range {
322 my $series = $self->_get_data_series()->{'line'};
323 return (undef, undef, 0) unless $series;
326 my $min_value = STARTING_MIN_VALUE;
327 my $column_count = 0;
329 my @series = @{$series};
330 foreach my $series (@series) {
331 my @data = @{$series->{'data'}};
333 if (scalar @data > $column_count) {
334 $column_count = scalar @data;
337 foreach my $value (@data) {
338 if ($value > $max_value) { $max_value = $value; }
339 if ($value < $min_value) { $min_value = $value; }
343 return ($min_value, $max_value, $column_count);
351 my $series = $self->_get_data_series()->{'bar'};
352 return (undef, undef, 0) unless $series;
355 my $min_value = STARTING_MIN_VALUE;
356 my $column_count = 0;
358 my @series = @{$series};
359 foreach my $series (@series) {
360 my @data = @{$series->{'data'}};
362 foreach my $value (@data) {
364 if ($value > $max_value) { $max_value = $value; }
365 if ($value < $min_value) { $min_value = $value; }
369 return ($min_value, $max_value, $column_count);
375 my $chart_box = shift;
376 my $style = $self->{'_style'};
379 my $img = $self->_get_image();
380 if (my $series = $self->_get_data_series()->{'bar'}) {
381 push @labels, map { $_->{'series_name'} } @$series;
384 if ($style->{features}{legend} && (scalar @labels)) {
385 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
391 sub _draw_flat_legend {
397 my $style = $self->{'_style'};
399 my $img = $self->_get_image();
401 my $max_value = $self->_get_max_value();
402 my $min_value = $self->_get_min_value();
403 my $column_count = $self->_get_column_count();
405 my $value_range = $max_value - $min_value;
407 my $width = $self->_get_number('width');
408 my $height = $self->_get_number('height');
410 my $graph_width = $self->_get_number('graph_width');
411 my $graph_height = $self->_get_number('graph_height');
413 my $line_series = $self->_get_data_series()->{'line'};
414 my $series_counter = $self->_get_series_counter() || 0;
416 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
418 my $col_height = int($graph_height / $column_count) -1;
420 my $graph_box = $self->_get_graph_box();
421 my $left = $graph_box->[0] + 1;
422 my $bottom = $graph_box->[1];
424 my $zero_position = $left + $graph_width - (-1*$min_value / $value_range) * ($graph_width - 1);
426 my $line_aa = $self->_get_number("lineaa");
427 foreach my $series (@$line_series) {
428 my @data = @{$series->{'data'}};
429 my $data_size = scalar @data;
433 $interval = $graph_height / ($data_size);
436 $interval = $graph_height / ($data_size - 1);
438 my $color = $self->_data_color($series_counter);
440 # We need to add these last, otherwise the next line segment will overwrite half of the marker
441 my @marker_positions;
442 for (my $i = 0; $i < $data_size - 1; $i++) {
443 my $y1 = $bottom + $i * $interval;
444 my $y2 = $bottom + ($i + 1) * $interval;
446 $y1 += $has_columns * $interval / 2;
447 $y2 += $has_columns * $interval / 2;
449 my $x1 = $left + ($value_range - $data[$i] + $min_value)/$value_range * $graph_width;
450 my $x2 = $left + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_width;
452 push @marker_positions, [$x1, $y1];
453 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
457 my $y2 = $bottom + ($data_size - 1) * $interval;
458 $y2 += $has_columns * $interval / 2;
460 my $x2 = $left + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_width;
462 if ($self->_feature_enabled("linemarkers")) {
463 push @marker_positions, [$x2, $y2];
464 foreach my $position (@marker_positions) {
465 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
471 $self->_set_series_counter($series_counter);
477 my $style = $self->{'_style'};
479 my $img = $self->_get_image();
481 my $max_value = $self->_get_max_value();
482 my $min_value = $self->_get_min_value();
483 my $column_count = $self->_get_column_count();
485 my $value_range = $max_value - $min_value;
487 my $width = $self->_get_number('width');
488 my $height = $self->_get_number('height');
490 my $graph_width = $self->_get_number('graph_width');
491 my $graph_height = $self->_get_number('graph_height');
494 my $graph_box = $self->_get_graph_box();
495 my $bottom = $graph_box->[1] + 1;
496 my $left = $graph_box->[0];
498 my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1));
500 my $bar_height = $graph_height / $column_count;
503 if ($style->{'features'}{'outline'}) {
504 $outline_color = $self->_get_color('outline.line');
507 my $series_counter = $self->_get_series_counter() || 0;
508 my $col_series = $self->_get_data_series()->{'bar'};
509 my $column_padding = $self->_get_number('column_padding') || 0;
511 # 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.
512 my $column_series = 0;
514 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
515 my $series = $col_series->[$series_pos];
516 my @data = @{$series->{'data'}};
517 my $data_size = scalar @data;
518 for (my $i = 0; $i < $data_size; $i++) {
520 my $part1 = $bar_height * (scalar @$col_series * $i);
521 my $part2 = ($series_pos) * $bar_height;
522 my $y1 = int($bottom + $part1 + $part2);
524 my $y2 = int($y1 + $bar_height - $column_padding)-1;
525 # Special case for when bar_height is less than 1.
530 my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
532 my $color = $self->_data_color($series_counter);
535 my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
536 $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
537 if ($style->{'features'}{'outline'}) {
538 $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
541 elsif ($data[$i] == 0) {
544 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
545 $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
546 if ($style->{'features'}{'outline'}) {
547 $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
555 $self->_set_series_counter($series_counter);
559 sub _add_data_series {
561 my $series_type = shift;
562 my $data_ref = shift;
563 my $series_name = shift;
565 my $graph_data = $self->{'graph_data'} || {};
567 my $series = $graph_data->{$series_type} || [];
569 push @$series, { data => $data_ref, series_name => $series_name };
571 $graph_data->{$series_type} = $series;
573 $self->{'graph_data'} = $graph_data;
579 =item show_vertical_gridlines()
581 Shows vertical gridlines at the y-tics.
583 Feature: vertical_gridlines
587 sub show_vertical_gridlines {
588 $_[0]->{'custom_style'}{features}{'vertical_gridlines'} = 1;
591 =item set_vertical_gridline_style(color => ..., style => ...)
593 Set the color and style of the lines drawn for gridlines.
595 Style equivalent: vgrid
599 sub set_vertical_gridline_style {
600 my ($self, %opts) = @_;
602 $self->{custom_style}{vgrid} ||= {};
603 @{$self->{custom_style}{vgrid}}{keys %opts} = values %opts;
608 =item show_line_markers()
610 =item show_line_markers($value)
612 Feature: linemarkers.
614 If $value is missing or true, draw markers on a line data series.
616 Note: line markers are drawn by default.
620 sub show_line_markers {
621 my ($self, $value) = @_;
623 @_ > 1 or $value = 1;
625 $self->{custom_style}{features}{linemarkers} = $value;
630 =item use_automatic_axis()
632 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.
636 sub use_automatic_axis {
637 eval { require Chart::Math::Axis; };
639 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
641 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
646 =item set_x_tics($count)
648 Set the number of X tics to use. Their value and position will be determined by the data range.
653 $_[0]->{'x_tics'} = $_[1];
657 return $_[0]->{'x_tics'} || 0;
660 sub _remove_tics_from_chart_box {
661 my ($self, $chart_box, $opts) = @_;
664 my $tic_width = $self->_get_y_tic_width($opts) || 10;
665 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
668 my $tic_height = $self->_get_x_tic_height() || 10;
669 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
671 $self->_remove_box($chart_box, \@y_tic_box);
672 $self->_remove_box($chart_box, \@x_tic_box);
674 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
675 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
676 $self->_remove_box($chart_box, \@y_tic_tops);
678 if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) {
679 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
685 $self->_remove_box($chart_box, \@remove_box);
691 sub _get_y_tic_width {
692 my ($self, $opts) = @_;
694 my $labels = $self->_get_labels($opts);
700 my %text_info = $self->_text_style('legend')
704 foreach my $label (@$labels) {
705 my @box = $self->_text_bbox($label, 'legend');
706 my $width = $box[2] + 5;
707 # For the tic itself...
709 if ($width > $max_width) {
716 sub _get_x_tic_height {
719 my $min = $self->_get_min_value();
720 my $max = $self->_get_max_value();
721 my $tic_count = $self->_get_x_tics();
723 my $interval = ($max - $min) / ($tic_count - 1);
725 my %text_info = $self->_text_style('legend')
729 for my $count (0 .. $tic_count - 1) {
730 my $value = sprintf("%.2f", ($count*$interval)+$min);
732 my @box = $self->_text_bbox($value, 'legend');
733 my $height = $box[3] - $box[1];
737 if ($height > $max_height) {
738 $max_height = $height;
747 my ($self, $opts) = @_;
749 my $img = $self->_get_image();
750 my $graph_box = $self->_get_graph_box();
751 my $image_box = $self->_get_image_box();
753 my $labels = $self->_get_labels($opts);
755 my $tic_count = (scalar @$labels) - 1;
757 my $has_columns = defined $self->_get_data_series()->{'bar'};
759 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
760 my $denominator = $tic_count;
764 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
765 my %text_info = $self->_text_style('legend')
768 for my $count (0 .. $tic_count) {
769 my $label = $labels->[$count];
771 my $x1 = $graph_box->[0] - 5;
772 my $x2 = $graph_box->[0] + 5;
774 my $y1 = $graph_box->[1] + ($tic_distance * $count);
777 $y1 += $tic_distance / 2;
780 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
782 my @box = $self->_text_bbox($label, 'legend')
786 my $height = $box[3];
788 $img->string(%text_info,
789 x => ($x1 - ($width + 5)),
790 y => ($y1 + ($height / 2)),
801 my $img = $self->_get_image();
802 my $graph_box = $self->_get_graph_box();
803 my $image_box = $self->_get_image_box();
805 my $tic_count = $self->_get_x_tics();
806 my $min = $self->_get_min_value();
807 my $max = $self->_get_max_value();
808 my $interval = ($max - $min) / ($tic_count - 1);
810 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
811 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
813 my %text_info = $self->_text_style('legend')
816 my $show_gridlines = $self->{_style}{features}{'vertical_gridlines'};
817 my @grid_line = $self->_get_line("vgrid");
818 for my $count (0 .. $tic_count-1) {
819 my $x1 = $graph_box->[0] + ($tic_distance * $count);
821 my $y1 = $graph_box->[3] + 5;
822 my $y2 = $graph_box->[3] - 5;
824 my $value = ($count*$interval)+$min;
826 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
828 my @box = $self->_text_bbox($value, 'legend')
832 my $height = $box[3];
834 $img->string(%text_info,
835 x => ($x1 - ($width / 2)),
836 y => ($y1 + $height + 5),
840 if ($show_gridlines && $x1 != $graph_box->[0] && $x1 != $graph_box->[2]) {
841 $self->_line(x1 => $x1, x2 => $x1,
842 y1 => $graph_box->[1], y2 => $graph_box->[3],
852 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
853 return $self->_error("No data supplied");
856 my $data = $self->_get_data_series();
857 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
858 return $self->_error("No values in data series");
860 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
861 return $self->_error("No values in data series");
863 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
864 return $self->_error("No values in data series");
870 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
871 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
872 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
873 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
874 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
875 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
876 sub _get_column_count { return $_[0]->{'column_count'} }
877 sub _get_min_value { return $_[0]->{'min_value'} }
878 sub _get_max_value { return $_[0]->{'max_value'} }
879 sub _get_image_box { return $_[0]->{'image_box'} }
880 sub _get_graph_box { return $_[0]->{'graph_box'} }
881 sub _reset_series_counter { $_[0]->{series_counter} = 0 }
882 sub _get_series_counter { return $_[0]->{'series_counter'} }
887 my %work = %{$self->SUPER::_style_defs()};
888 push @{$work{features}}, qw/graph_outline graph_fill linemarkers/;
891 color => "lookup(fg)",
900 return ( $self->SUPER::_composite(), "graph", "vgrid" );