1 package Imager::Graph::Horizontal;
5 Imager::Graph::Horizontal - A super class for line/bar charts
9 This is a base class that implements base functionality for line and
12 The sub-classes, Imager::Graph::Bar and Imager::Graph::Line simply
13 provide default data series types.
20 @ISA = qw(Imager::Graph);
22 use constant STARTING_MIN_VALUE => 99999;
24 our $VERSION = "0.10";
30 =item add_data_series(\@data, $series_name)
32 Add a data series to the graph, of the default type.
39 my $series_name = shift;
41 my $series_type = $self->_get_default_series_type();
42 $self->_add_data_series($series_type, $data_ref, $series_name);
47 =item add_bar_data_series(\@data, $series_name)
49 Add a bar data series to the graph.
53 sub add_bar_data_series {
56 my $series_name = shift;
58 $self->_add_data_series('bar', $data_ref, $series_name);
63 =item add_line_data_series(\@data, $series_name)
65 Add a line data series to the graph.
69 sub add_line_data_series {
72 my $series_name = shift;
74 $self->_add_data_series('line', $data_ref, $series_name);
79 =item set_column_padding($int)
81 Sets the number of pixels that should go between columns of data.
85 sub set_column_padding {
86 $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
89 =item set_negative_background($color)
91 Sets the background color or fill used below the y axis.
95 sub set_negative_background {
96 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
106 my ($self, %opts) = @_;
108 if (!$self->_valid_input()) {
112 $self->_style_setup(\%opts);
114 my $style = $self->{_style};
119 my $img = $self->_get_image()
122 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
123 $self->_set_image_box(\@image_box);
125 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
126 $self->_draw_legend(\@chart_box);
127 if ($style->{title}{text}) {
128 $self->_draw_title($img, \@chart_box)
132 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
133 return unless $self->_get_data_range();
134 $self->_remove_tics_from_chart_box(\@chart_box, \%opts);
135 my $column_count = $self->_get_column_count();
137 my $width = $self->_get_number('width');
138 my $height = $self->_get_number('height');
140 my $graph_width = $chart_box[2] - $chart_box[0];
141 my $graph_height = $chart_box[3] - $chart_box[1];
143 my $col_height = ($graph_height - 1) / $column_count;
144 if ($col_height > 1) {
145 $graph_height = int($col_height) * $column_count + 1;
148 $graph_height = $col_height * $column_count + 1;
151 my $tic_count = $self->_get_x_tics();
152 my $tic_distance = int(($graph_width -1) / ($tic_count - 1));
153 $graph_width = $tic_distance * ($tic_count - 1);
155 my $top = $chart_box[1];
156 my $left = $chart_box[0];
158 $self->{'_style'}{'graph_width'} = $graph_width;
159 $self->{'_style'}{'graph_height'} = $graph_height;
161 my @graph_box = ($left, $top, $left + $graph_width, $top + $graph_height);
163 $self->_set_graph_box(\@graph_box);
165 my @fill_box = @graph_box;
167 if ($self->_feature_enabled("graph_outline")) {
168 my @line = $self->_get_line("graph.outline")
183 my @back_fill = $self->_get_fill("graph.fill", \@fill_box)
191 my $min_value = $self->_get_min_value();
192 my $max_value = $self->_get_max_value();
193 my $value_range = $max_value - $min_value;
197 $zero_position = $left + (-1*$min_value / $value_range) * ($graph_width-1);
200 if ($min_value < 0) {
201 my @neg_box = ( $left+1, $top+1, $zero_position, $top+$graph_height - 1 );
202 my @neg_fill = $self->_get_fill('negative_bg', \@neg_box)
210 x1 => $zero_position,
212 x2 => $zero_position,
213 y2 => $top + $graph_height,
214 color => $self->_get_color('outline.line'),
218 $self->_reset_series_counter();
220 if ($self->_get_data_series()->{'bar'}) {
223 if ($self->_get_data_series()->{'line'}) {
224 $self->_draw_lines();
227 if ($self->_get_x_tics()) {
228 $self->_draw_x_tics();
230 if ($self->_get_labels(\%opts)) {
231 $self->_draw_y_tics(\%opts);
234 return $self->_get_image();
237 sub _get_data_range {
242 my $column_count = 0;
244 my ($b_min, $b_max, $b_cols) = $self->_get_bar_range();
245 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
247 $min_value = $self->_min(STARTING_MIN_VALUE, $b_min, $l_min);
248 $max_value = $self->_max(0, $b_max, $l_max);
249 $column_count = $self->_max(0, $b_cols, $l_cols);
251 my $config_min = $self->_get_number('x_min');
252 my $config_max = $self->_get_number('x_max');
254 if (defined $config_max && $config_max < $max_value) {
257 if (defined $config_min && $config_min > $min_value) {
261 my $range_padding = $self->_get_number('range_padding');
262 if (defined $config_min) {
263 $min_value = $config_min;
266 if ($min_value > 0) {
269 if ($range_padding && $min_value < 0) {
270 my $difference = $min_value * $range_padding / 100;
271 if ($min_value < -1 && $difference > -1) {
274 $min_value += $difference;
277 if (defined $config_max) {
278 $max_value = $config_max;
281 if ($range_padding && $max_value > 0) {
282 my $difference = $max_value * $range_padding / 100;
283 if ($max_value > 1 && $difference < 1) {
286 $max_value += $difference;
290 if ($self->_get_number('automatic_axis')) {
291 # In case this was set via a style, and not by the api method
292 eval { require Chart::Math::Axis; };
294 return $self->_error("Can't use automatic_axis - $@");
297 my $axis = Chart::Math::Axis->new();
298 $axis->include_zero();
299 $axis->add_data($min_value, $max_value);
300 $max_value = $axis->top;
301 $min_value = $axis->bottom;
302 my $ticks = $axis->ticks;
303 # The +1 is there because we have the bottom tick as well
304 $self->set_x_tics($ticks+1);
307 $self->_set_max_value($max_value);
308 $self->_set_min_value($min_value);
309 $self->_set_column_count($column_count);
318 foreach my $value (@_) {
319 next unless defined $value;
320 if ($value < $min) { $min = $value; }
329 foreach my $value (@_) {
330 next unless defined $value;
331 if ($value > $min) { $min = $value; }
336 sub _get_line_range {
338 my $series = $self->_get_data_series()->{'line'};
339 return (undef, undef, 0) unless $series;
342 my $min_value = STARTING_MIN_VALUE;
343 my $column_count = 0;
345 my @series = @{$series};
346 foreach my $series (@series) {
347 my @data = @{$series->{'data'}};
349 if (scalar @data > $column_count) {
350 $column_count = scalar @data;
353 foreach my $value (@data) {
354 if ($value > $max_value) { $max_value = $value; }
355 if ($value < $min_value) { $min_value = $value; }
359 return ($min_value, $max_value, $column_count);
367 my $series = $self->_get_data_series()->{'bar'};
368 return (undef, undef, 0) unless $series;
371 my $min_value = STARTING_MIN_VALUE;
372 my $column_count = 0;
374 my @series = @{$series};
375 foreach my $series (@series) {
376 my @data = @{$series->{'data'}};
378 foreach my $value (@data) {
380 if ($value > $max_value) { $max_value = $value; }
381 if ($value < $min_value) { $min_value = $value; }
385 return ($min_value, $max_value, $column_count);
391 my $chart_box = shift;
392 my $style = $self->{'_style'};
395 my $img = $self->_get_image();
396 if (my $series = $self->_get_data_series()->{'bar'}) {
397 push @labels, map { $_->{'series_name'} } @$series;
400 if ($style->{features}{legend} && (scalar @labels)) {
401 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
407 sub _draw_flat_legend {
413 my $style = $self->{'_style'};
415 my $img = $self->_get_image();
417 my $max_value = $self->_get_max_value();
418 my $min_value = $self->_get_min_value();
419 my $column_count = $self->_get_column_count();
421 my $value_range = $max_value - $min_value;
423 my $width = $self->_get_number('width');
424 my $height = $self->_get_number('height');
426 my $graph_width = $self->_get_number('graph_width');
427 my $graph_height = $self->_get_number('graph_height');
429 my $line_series = $self->_get_data_series()->{'line'};
430 my $series_counter = $self->_get_series_counter() || 0;
432 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
434 my $col_height = int($graph_height / $column_count) -1;
436 my $graph_box = $self->_get_graph_box();
437 my $left = $graph_box->[0] + 1;
438 my $bottom = $graph_box->[1];
440 my $zero_position = $left + $graph_width - (-1*$min_value / $value_range) * ($graph_width - 1);
442 my $line_aa = $self->_get_number("lineaa");
443 foreach my $series (@$line_series) {
444 my @data = @{$series->{'data'}};
445 my $data_size = scalar @data;
449 $interval = $graph_height / ($data_size);
452 $interval = $graph_height / ($data_size - 1);
454 my $color = $self->_data_color($series_counter);
456 # We need to add these last, otherwise the next line segment will overwrite half of the marker
457 my @marker_positions;
458 for (my $i = 0; $i < $data_size - 1; $i++) {
459 my $y1 = $bottom + $i * $interval;
460 my $y2 = $bottom + ($i + 1) * $interval;
462 $y1 += $has_columns * $interval / 2;
463 $y2 += $has_columns * $interval / 2;
465 my $x1 = $left + ($value_range - $data[$i] + $min_value)/$value_range * $graph_width;
466 my $x2 = $left + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_width;
468 push @marker_positions, [$x1, $y1];
469 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
473 my $y2 = $bottom + ($data_size - 1) * $interval;
474 $y2 += $has_columns * $interval / 2;
476 my $x2 = $left + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_width;
478 if ($self->_feature_enabled("linemarkers")) {
479 push @marker_positions, [$x2, $y2];
480 foreach my $position (@marker_positions) {
481 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
487 $self->_set_series_counter($series_counter);
493 my $style = $self->{'_style'};
495 my $img = $self->_get_image();
497 my $max_value = $self->_get_max_value();
498 my $min_value = $self->_get_min_value();
499 my $column_count = $self->_get_column_count();
501 my $value_range = $max_value - $min_value;
503 my $width = $self->_get_number('width');
504 my $height = $self->_get_number('height');
506 my $graph_width = $self->_get_number('graph_width');
507 my $graph_height = $self->_get_number('graph_height');
510 my $graph_box = $self->_get_graph_box();
511 my $bottom = $graph_box->[1] + 1;
512 my $left = $graph_box->[0];
514 my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1));
516 my $bar_height = $graph_height / $column_count;
519 if ($style->{'features'}{'outline'}) {
520 $outline_color = $self->_get_color('outline.line');
523 my $series_counter = $self->_get_series_counter() || 0;
524 my $col_series = $self->_get_data_series()->{'bar'};
525 my $column_padding = $self->_get_number('column_padding') || 0;
527 # 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.
528 my $column_series = 0;
530 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
531 my $series = $col_series->[$series_pos];
532 my @data = @{$series->{'data'}};
533 my $data_size = scalar @data;
534 for (my $i = 0; $i < $data_size; $i++) {
536 my $part1 = $bar_height * (scalar @$col_series * $i);
537 my $part2 = ($series_pos) * $bar_height;
538 my $y1 = int($bottom + $part1 + $part2);
540 my $y2 = int($y1 + $bar_height - $column_padding)-1;
541 # Special case for when bar_height is less than 1.
546 my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
548 my $color = $self->_data_color($series_counter);
551 my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
552 $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
553 if ($style->{'features'}{'outline'}) {
554 $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
557 elsif ($data[$i] == 0) {
560 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
561 $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
562 if ($style->{'features'}{'outline'}) {
563 $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
571 $self->_set_series_counter($series_counter);
575 sub _add_data_series {
577 my $series_type = shift;
578 my $data_ref = shift;
579 my $series_name = shift;
581 my $graph_data = $self->{'graph_data'} || {};
583 my $series = $graph_data->{$series_type} || [];
585 push @$series, { data => $data_ref, series_name => $series_name };
587 $graph_data->{$series_type} = $series;
589 $self->{'graph_data'} = $graph_data;
593 =item show_vertical_gridlines()
595 Shows vertical gridlines at the y-tics.
597 Feature: vertical_gridlines
601 sub show_vertical_gridlines {
602 $_[0]->{'custom_style'}{features}{'vertical_gridlines'} = 1;
605 =item set_vertical_gridline_style(color => ..., style => ...)
607 Set the color and style of the lines drawn for gridlines.
609 Style equivalent: vgrid
613 sub set_vertical_gridline_style {
614 my ($self, %opts) = @_;
616 $self->{custom_style}{vgrid} ||= {};
617 @{$self->{custom_style}{vgrid}}{keys %opts} = values %opts;
622 =item show_line_markers()
624 =item show_line_markers($value)
626 Feature: linemarkers.
628 If $value is missing or true, draw markers on a line data series.
630 Note: line markers are drawn by default.
634 sub show_line_markers {
635 my ($self, $value) = @_;
637 @_ > 1 or $value = 1;
639 $self->{custom_style}{features}{linemarkers} = $value;
644 =item use_automatic_axis()
646 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.
650 sub use_automatic_axis {
651 eval { require Chart::Math::Axis; };
653 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
655 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
660 =item set_x_tics($count)
662 Set the number of X tics to use. Their value and position will be determined by the data range.
667 $_[0]->{'x_tics'} = $_[1];
671 return $_[0]->{'x_tics'} || 0;
674 sub _remove_tics_from_chart_box {
675 my ($self, $chart_box, $opts) = @_;
678 my $tic_width = $self->_get_y_tic_width($opts) || 10;
679 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
682 my $tic_height = $self->_get_x_tic_height() || 10;
683 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
685 $self->_remove_box($chart_box, \@y_tic_box);
686 $self->_remove_box($chart_box, \@x_tic_box);
688 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
689 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
690 $self->_remove_box($chart_box, \@y_tic_tops);
692 if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) {
693 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
699 $self->_remove_box($chart_box, \@remove_box);
705 sub _get_y_tic_width {
706 my ($self, $opts) = @_;
708 my $labels = $self->_get_labels($opts);
714 my %text_info = $self->_text_style('legend')
718 foreach my $label (@$labels) {
719 my @box = $self->_text_bbox($label, 'legend');
720 my $width = $box[2] + 5;
721 # For the tic itself...
723 if ($width > $max_width) {
730 sub _get_x_tic_height {
733 my $min = $self->_get_min_value();
734 my $max = $self->_get_max_value();
735 my $tic_count = $self->_get_x_tics();
737 my $interval = ($max - $min) / ($tic_count - 1);
739 my %text_info = $self->_text_style('legend')
743 for my $count (0 .. $tic_count - 1) {
744 my $value = sprintf("%.2f", ($count*$interval)+$min);
746 my @box = $self->_text_bbox($value, 'legend');
747 my $height = $box[3] - $box[1];
751 if ($height > $max_height) {
752 $max_height = $height;
761 my ($self, $opts) = @_;
763 my $img = $self->_get_image();
764 my $graph_box = $self->_get_graph_box();
765 my $image_box = $self->_get_image_box();
767 my $labels = $self->_get_labels($opts);
769 my $tic_count = (scalar @$labels) - 1;
771 my $has_columns = defined $self->_get_data_series()->{'bar'};
773 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
774 my $denominator = $tic_count;
778 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
779 my %text_info = $self->_text_style('legend')
782 for my $count (0 .. $tic_count) {
783 my $label = $labels->[$count];
785 my $x1 = $graph_box->[0] - 5;
786 my $x2 = $graph_box->[0] + 5;
788 my $y1 = $graph_box->[1] + ($tic_distance * $count);
791 $y1 += $tic_distance / 2;
794 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
796 my @box = $self->_text_bbox($label, 'legend')
800 my $height = $box[3];
802 $img->string(%text_info,
803 x => ($x1 - ($width + 5)),
804 y => ($y1 + ($height / 2)),
815 my $img = $self->_get_image();
816 my $graph_box = $self->_get_graph_box();
817 my $image_box = $self->_get_image_box();
819 my $tic_count = $self->_get_x_tics();
820 my $min = $self->_get_min_value();
821 my $max = $self->_get_max_value();
822 my $interval = ($max - $min) / ($tic_count - 1);
824 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
825 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
827 my %text_info = $self->_text_style('legend')
830 my $show_gridlines = $self->{_style}{features}{'vertical_gridlines'};
831 my @grid_line = $self->_get_line("vgrid");
832 for my $count (0 .. $tic_count-1) {
833 my $x1 = $graph_box->[0] + ($tic_distance * $count);
835 my $y1 = $graph_box->[3] + 5;
836 my $y2 = $graph_box->[3] - 5;
838 my $value = ($count*$interval)+$min;
840 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
842 my @box = $self->_text_bbox($value, 'legend')
846 my $height = $box[3];
848 $img->string(%text_info,
849 x => ($x1 - ($width / 2)),
850 y => ($y1 + $height + 5),
854 if ($show_gridlines && $x1 != $graph_box->[0] && $x1 != $graph_box->[2]) {
855 $self->_line(x1 => $x1, x2 => $x1,
856 y1 => $graph_box->[1], y2 => $graph_box->[3],
866 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
867 return $self->_error("No data supplied");
870 my $data = $self->_get_data_series();
871 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
872 return $self->_error("No values in data series");
874 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
875 return $self->_error("No values in data series");
877 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
878 return $self->_error("No values in data series");
884 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
885 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
886 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
887 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
888 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
889 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
890 sub _get_column_count { return $_[0]->{'column_count'} }
891 sub _get_min_value { return $_[0]->{'min_value'} }
892 sub _get_max_value { return $_[0]->{'max_value'} }
893 sub _get_image_box { return $_[0]->{'image_box'} }
894 sub _get_graph_box { return $_[0]->{'graph_box'} }
895 sub _reset_series_counter { $_[0]->{series_counter} = 0 }
896 sub _get_series_counter { return $_[0]->{'series_counter'} }
901 my %work = %{$self->SUPER::_style_defs()};
902 push @{$work{features}}, qw/graph_outline graph_fill linemarkers/;
905 color => "lookup(fg)",
914 return ( $self->SUPER::_composite(), "graph", "vgrid" );
923 Patrick Michaud, Tony Cook