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 + (-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();
213 $column_count = $b_cols;
214 # $min_value = $self->_min(STARTING_MIN_VALUE, $c_min, $l_min);
215 # $max_value = $self->_max(0, $sc_max, $c_max, $l_max);
217 if ($self->_get_number('automatic_axis')) {
218 # In case this was set via a style, and not by the api method
219 eval { require Chart::Math::Axis; };
221 return $self->_error("Can't use automatic_axis - $@");
224 my $axis = Chart::Math::Axis->new();
225 $axis->include_zero();
226 $axis->add_data($min_value, $max_value);
227 $max_value = $axis->top;
228 $min_value = $axis->bottom;
229 my $ticks = $axis->ticks;
230 # The +1 is there because we have the bottom tick as well
231 $self->set_x_tics($ticks+1);
234 $self->_set_max_value($max_value);
235 $self->_set_min_value($min_value);
236 $self->_set_column_count($column_count);
245 foreach my $value (@_) {
246 next unless defined $value;
247 if ($value < $min) { $min = $value; }
256 foreach my $value (@_) {
257 next unless defined $value;
258 if ($value > $min) { $min = $value; }
266 my $series = $self->_get_data_series()->{'bar'};
267 return (undef, undef, 0) unless $series;
270 my $min_value = STARTING_MIN_VALUE;
271 my $column_count = 0;
273 my @series = @{$series};
274 foreach my $series (@series) {
275 my @data = @{$series->{'data'}};
277 foreach my $value (@data) {
279 if ($value > $max_value) { $max_value = $value; }
280 if ($value < $min_value) { $min_value = $value; }
284 return ($min_value, $max_value, $column_count);
290 my $chart_box = shift;
291 my $style = $self->{'_style'};
294 my $img = $self->_get_image();
295 if (my $series = $self->_get_data_series()->{'bar'}) {
296 push @labels, map { $_->{'series_name'} } @$series;
299 if ($style->{features}{legend} && (scalar @labels)) {
300 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
306 sub _draw_flat_legend {
312 my $style = $self->{'_style'};
314 my $img = $self->_get_image();
316 my $max_value = $self->_get_max_value();
317 my $min_value = $self->_get_min_value();
318 my $column_count = $self->_get_column_count();
320 my $value_range = $max_value - $min_value;
322 my $width = $self->_get_number('width');
323 my $height = $self->_get_number('height');
325 my $graph_width = $self->_get_number('graph_width');
326 my $graph_height = $self->_get_number('graph_height');
328 my $line_series = $self->_get_data_series()->{'line'};
329 my $series_counter = $self->_get_series_counter() || 0;
331 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
333 my $col_width = int($graph_width / $column_count) -1;
335 my $graph_box = $self->_get_graph_box();
336 my $left = $graph_box->[0] + 1;
337 my $bottom = $graph_box->[1];
339 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
342 my $line_aa = $self->_get_number("lineaa");
343 foreach my $series (@$line_series) {
344 my @data = @{$series->{'data'}};
345 my $data_size = scalar @data;
349 $interval = $graph_width / ($data_size);
352 $interval = $graph_width / ($data_size - 1);
354 my $color = $self->_data_color($series_counter);
356 # We need to add these last, otherwise the next line segment will overwrite half of the marker
357 my @marker_positions;
358 for (my $i = 0; $i < $data_size - 1; $i++) {
359 my $x1 = $left + $i * $interval;
360 my $x2 = $left + ($i + 1) * $interval;
362 $x1 += $has_columns * $interval / 2;
363 $x2 += $has_columns * $interval / 2;
365 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
366 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
368 push @marker_positions, [$x1, $y1];
369 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
372 my $x2 = $left + ($data_size - 1) * $interval;
373 $x2 += $has_columns * $interval / 2;
375 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
377 push @marker_positions, [$x2, $y2];
378 foreach my $position (@marker_positions) {
379 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
384 $self->_set_series_counter($series_counter);
389 my ($self, $index) = @_;
391 my $markers = $self->{'_style'}{'line_markers'};
395 my $marker = $markers->[$index % @$markers];
400 sub _draw_line_marker {
402 my ($x1, $y1, $series_counter) = @_;
404 my $img = $self->_get_image();
406 my $style = $self->_line_marker($series_counter);
407 return unless $style;
409 my $type = $style->{'shape'};
410 my $radius = $style->{'radius'};
412 my $line_aa = $self->_get_number("lineaa");
413 my $fill_aa = $self->_get_number("fill.aa");
414 if ($type eq 'circle') {
415 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
416 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
418 elsif ($type eq 'square') {
419 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
420 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
422 elsif ($type eq 'diamond') {
423 # The gradient really doesn't work for diamond
424 my $color = $self->_data_color($series_counter);
427 [$x1 - $radius, $y1],
428 [$x1, $y1 + $radius],
429 [$x1 + $radius, $y1],
430 [$x1, $y1 - $radius],
432 filled => 1, color => $color, aa => $fill_aa);
434 elsif ($type eq 'triangle') {
435 # The gradient really doesn't work for triangle
436 my $color = $self->_data_color($series_counter);
439 [$x1 - $radius, $y1 + $radius],
440 [$x1 + $radius, $y1 + $radius],
441 [$x1, $y1 - $radius],
443 filled => 1, color => $color, aa => $fill_aa);
446 elsif ($type eq 'x') {
447 my $color = $self->_data_color($series_counter);
448 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
449 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
451 elsif ($type eq 'plus') {
452 my $color = $self->_data_color($series_counter);
453 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
454 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
460 my $style = $self->{'_style'};
462 my $img = $self->_get_image();
464 my $max_value = $self->_get_max_value();
465 my $min_value = $self->_get_min_value();
466 my $column_count = $self->_get_column_count();
468 my $value_range = $max_value - $min_value;
470 my $width = $self->_get_number('width');
471 my $height = $self->_get_number('height');
473 my $graph_width = $self->_get_number('graph_width');
474 my $graph_height = $self->_get_number('graph_height');
477 my $graph_box = $self->_get_graph_box();
478 my $bottom = $graph_box->[1] + 1;
479 my $left = $graph_box->[0];
481 my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1));
483 my $bar_height = int($graph_height / $column_count - 1);
486 if ($style->{'features'}{'outline'}) {
487 $outline_color = $self->_get_color('outline.line');
490 my $series_counter = $self->_get_series_counter() || 0;
491 my $col_series = $self->_get_data_series()->{'bar'};
492 my $column_padding = $self->_get_number('column_padding') || 0;
494 # 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.
495 my $column_series = 0;
497 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
498 my $series = $col_series->[$series_pos];
499 my @data = @{$series->{'data'}};
500 my $data_size = scalar @data;
501 for (my $i = 0; $i < $data_size; $i++) {
503 my $y1 = int($bottom + $bar_height * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos + ($column_padding / 2);
505 my $y2 = $y1 + $bar_height - $column_padding;
507 my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
509 my $color = $self->_data_color($series_counter);
512 my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
513 $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
514 if ($style->{'features'}{'outline'}) {
515 $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
518 elsif ($data[$i] == 0) {
521 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
522 $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
523 if ($style->{'features'}{'outline'}) {
524 $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
532 $self->_set_series_counter($series_counter);
536 sub _add_data_series {
538 my $series_type = shift;
539 my $data_ref = shift;
540 my $series_name = shift;
542 my $graph_data = $self->{'graph_data'} || {};
544 my $series = $graph_data->{$series_type} || [];
546 push @$series, { data => $data_ref, series_name => $series_name };
548 $graph_data->{$series_type} = $series;
550 $self->{'graph_data'} = $graph_data;
556 =item show_vertical_gridlines()
558 Shows vertical gridlines at the y-tics.
562 sub show_vertical_gridlines {
563 $_[0]->{'custom_style'}->{'vertical_gridlines'} = 1;
566 =item use_automatic_axis()
568 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.
572 sub use_automatic_axis {
573 eval { require Chart::Math::Axis; };
575 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
577 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
582 =item set_x_tics($count)
584 Set the number of X tics to use. Their value and position will be determined by the data range.
589 $_[0]->{'x_tics'} = $_[1];
593 return $_[0]->{'x_tics'} || 0;
596 sub _remove_tics_from_chart_box {
598 my $chart_box = shift;
601 my $tic_width = $self->_get_y_tic_width() || 10;
602 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
605 my $tic_height = $self->_get_x_tic_height() || 10;
606 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
608 $self->_remove_box($chart_box, \@y_tic_box);
609 $self->_remove_box($chart_box, \@x_tic_box);
611 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
612 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
613 $self->_remove_box($chart_box, \@y_tic_tops);
617 sub _get_y_tic_width {
620 my $labels = $self->_get_labels();
622 foreach my $label (@$labels) {
623 my @box = $self->_text_bbox($label, 'legend');
624 my $width = $box[2] + 5;
625 # For the tic itself...
627 if ($width > $max_width) {
634 sub _get_x_tic_height {
637 my $min = $self->_get_min_value();
638 my $max = $self->_get_max_value();
639 my $tic_count = $self->_get_x_tics();
641 my $interval = ($max - $min) / ($tic_count - 1);
643 my %text_info = $self->_text_style('legend')
647 for my $count (0 .. $tic_count - 1) {
648 my $value = sprintf("%.2f", ($count*$interval)+$min);
650 my @box = $self->_text_bbox($value, 'legend');
651 my $height = $box[3] - $box[1];
655 if ($height > $max_height) {
656 $max_height = $height;
667 my $img = $self->_get_image();
668 my $graph_box = $self->_get_graph_box();
669 my $image_box = $self->_get_image_box();
671 my $labels = $self->_get_labels();
673 my $tic_count = (scalar @$labels) - 1;
675 my $has_columns = defined $self->_get_data_series()->{'bar'};
677 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
678 my $denominator = $tic_count;
682 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
683 my %text_info = $self->_text_style('legend')
686 for my $count (0 .. $tic_count) {
687 my $label = $labels->[$count];
689 my $x1 = $graph_box->[0] - 5;
690 my $x2 = $graph_box->[0] + 5;
692 my $y1 = $graph_box->[1] + ($tic_distance * $count);
695 $y1 += $tic_distance / 2;
698 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
700 my @box = $self->_text_bbox($label, 'legend')
704 my $height = $box[3];
706 $img->string(%text_info,
707 x => ($x1 - ($width + 5)),
708 y => ($y1 + ($height / 2)),
719 my $img = $self->_get_image();
720 my $graph_box = $self->_get_graph_box();
721 my $image_box = $self->_get_image_box();
723 my $tic_count = $self->_get_x_tics();
724 my $min = $self->_get_min_value();
725 my $max = $self->_get_max_value();
726 my $interval = ($max - $min) / ($tic_count - 1);
728 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
729 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
731 my %text_info = $self->_text_style('legend')
734 my $show_gridlines = $self->_get_number('vertical_gridlines');
735 for my $count (0 .. $tic_count-1) {
736 my $x1 = $graph_box->[0] + ($tic_distance * $count);
738 my $y1 = $graph_box->[3] + 5;
739 my $y2 = $graph_box->[3] - 5;
741 my $value = ($count*$interval)+$min;
743 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
745 my @box = $self->_text_bbox($value, 'legend')
749 my $height = $box[3];
751 $img->string(%text_info,
752 x => ($x1 - ($width / 2)),
753 y => ($y1 + $height + 5),
757 if ($show_gridlines) {
759 for (my $i = $graph_box->[1]; $i < $graph_box->[3]; $i += 6) {
762 if ($y2 > $graph_box->[3]) { $y2 = $graph_box->[3]; }
763 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
772 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
773 return $self->_error("No data supplied");
776 my $data = $self->_get_data_series();
777 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
778 return $self->_error("No values in data series");
780 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
781 return $self->_error("No values in data series");
783 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
784 return $self->_error("No values in data series");
790 sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
791 sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
792 sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
793 sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
794 sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
795 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
796 sub _get_column_count { return $_[0]->{'column_count'} }
797 sub _get_min_value { return $_[0]->{'min_value'} }
798 sub _get_max_value { return $_[0]->{'max_value'} }
799 sub _get_image_box { return $_[0]->{'image_box'} }
800 sub _get_graph_box { return $_[0]->{'graph_box'} }
801 sub _get_series_counter { return $_[0]->{'series_counter'} }