]> git.imager.perl.org - imager-graph.git/blob - lib/Imager/Graph/Vertical.pm
Changes the area drawing, so it still uses the full width if there are columns. ...
[imager-graph.git] / lib / Imager / Graph / Vertical.pm
1 package Imager::Graph::Vertical;
2
3 =head1 NAME
4
5   Imager::Graph::Vertical- A super class for line/bar/column charts
6
7 =cut
8
9 use strict;
10 use vars qw(@ISA);
11 use Imager::Graph;
12 @ISA = qw(Imager::Graph);
13
14 use constant STARTING_MIN_VALUE => 99999;
15 =over 4
16
17 =item add_data_series(\@data, $series_name)
18
19 Add a data series to the graph, of the default type.
20
21 =cut
22
23 sub add_data_series {
24   my $self = shift;
25   my $data_ref = shift;
26   my $series_name = shift;
27
28   my $series_type = $self->_get_default_series_type();
29   $self->_add_data_series($series_type, $data_ref, $series_name);
30
31   return;
32 }
33
34 =item add_column_data_series(\@data, $series_name)
35
36 Add a column data series to the graph.
37
38 =cut
39
40 sub add_column_data_series {
41   my $self = shift;
42   my $data_ref = shift;
43   my $series_name = shift;
44
45   $self->_add_data_series('column', $data_ref, $series_name);
46
47   return;
48 }
49
50 =item add_stacked_column_data_series(\@data, $series_name)
51
52 Add a stacked column data series to the graph.
53
54 =cut
55
56 sub add_stacked_column_data_series {
57   my $self = shift;
58   my $data_ref = shift;
59   my $series_name = shift;
60
61   $self->_add_data_series('stacked_column', $data_ref, $series_name);
62
63   return;
64 }
65
66 =item add_line_data_series(\@data, $series_name)
67
68 Add a line data series to the graph.
69
70 =cut
71
72 sub add_line_data_series {
73   my $self = shift;
74   my $data_ref = shift;
75   my $series_name = shift;
76
77   $self->_add_data_series('line', $data_ref, $series_name);
78
79   return;
80 }
81
82 =item add_area_data_series(\@data, $series_name)
83
84 Add a area data series to the graph.
85
86 =cut
87
88 sub add_area_data_series {
89   my $self = shift;
90   my $data_ref = shift;
91   my $series_name = shift;
92
93   $self->_add_data_series('area', $data_ref, $series_name);
94
95   return;
96 }
97
98
99 =item set_y_max($value)
100
101 Sets the maximum y value to be displayed.  This will be ignored if the y_max is lower than the highest value.
102
103 =cut
104
105 sub set_y_max {
106   $_[0]->{'custom_style'}->{'y_max'} = $_[1];
107 }
108
109 =item set_y_min($value)
110
111 Sets the minimum y value to be displayed.  This will be ignored if the y_min is higher than the lowest value.
112
113 =cut
114
115 sub set_y_min {
116   $_[0]->{'custom_style'}->{'y_min'} = $_[1];
117 }
118
119 =item set_column_padding($int)
120
121 Sets the padding between columns.  This is a percentage of the column width.  Defaults to 0.
122
123 =cut
124
125 sub set_column_padding {
126   $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
127 }
128
129 =item set_range_padding($percentage)
130
131 Sets the padding to be used, as a percentage.  For example, if your data ranges from 0 to 10, and you have a 20 percent padding, the y axis will go to 12.
132
133 Defaults to 10.  This attribute is ignored for positive numbers if set_y_max() has been called, and ignored for negative numbers if set_y_min() has been called.
134
135 =cut
136
137 sub set_range_padding {
138   $_[0]->{'custom_style'}->{'range_padding'} = $_[1];
139 }
140
141 =item set_negative_background($color)
142
143 Sets the background color used below the x axis.
144
145 =cut
146
147 sub set_negative_background {
148   $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
149 }
150
151 =item draw()
152
153 Draw the graph
154
155 =cut
156
157 sub draw {
158   my ($self, %opts) = @_;
159
160   if (!$self->_valid_input()) {
161     return;
162   }
163
164   $self->_style_setup(\%opts);
165
166   my $style = $self->{_style};
167
168   my $img = $self->_get_image()
169     or return;
170
171   my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
172   $self->_set_image_box(\@image_box);
173
174   my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
175   $self->_draw_legend(\@chart_box);
176   if ($style->{title}{text}) {
177     $self->_draw_title($img, \@chart_box)
178       or return;
179   }
180
181   # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
182   return unless $self->_get_data_range();
183   $self->_remove_tics_from_chart_box(\@chart_box);
184   my $column_count = $self->_get_column_count();
185
186   my $width = $self->_get_number('width');
187   my $height = $self->_get_number('height');
188
189   my $graph_width = $chart_box[2] - $chart_box[0];
190   my $graph_height = $chart_box[3] - $chart_box[1];
191
192   my $col_width = ($graph_width - 1) / $column_count;
193   if ($col_width > 1) {
194     $graph_width = int($col_width) * $column_count + 1;
195   }
196   else {
197     $graph_width = $col_width * $column_count + 1;
198   }
199
200   my $tic_count = $self->_get_y_tics();
201   my $tic_distance = ($graph_height-1) / ($tic_count - 1);
202   $graph_height = int($tic_distance * ($tic_count - 1));
203
204   my $bottom = $chart_box[1];
205   my $left   = $chart_box[0];
206
207   $self->{'_style'}{'graph_width'} = $graph_width;
208   $self->{'_style'}{'graph_height'} = $graph_height;
209
210   my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height);
211   $self->_set_graph_box(\@graph_box);
212
213   $img->box(
214             color   => $self->_get_color('outline.line'),
215             xmin    => $left,
216             xmax    => $left+$graph_width,
217             ymin    => $bottom,
218             ymax    => $bottom+$graph_height,
219             );
220
221   $img->box(
222             color   => $self->_get_color('bg'),
223             xmin    => $left + 1,
224             xmax    => $left+$graph_width - 1,
225             ymin    => $bottom + 1,
226             ymax    => $bottom+$graph_height-1 ,
227             filled  => 1,
228             );
229
230   my $min_value = $self->_get_min_value();
231   my $max_value = $self->_get_max_value();
232   my $value_range = $max_value - $min_value;
233
234   my $zero_position;
235   if ($value_range) {
236     $zero_position =  $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
237   }
238
239   if ($min_value < 0) {
240     $img->box(
241             color   => $self->_get_color('negative_bg'),
242             xmin    => $left + 1,
243             xmax    => $left+$graph_width- 1,
244             ymin    => $zero_position,
245             ymax    => $bottom+$graph_height - 1,
246             filled  => 1,
247     );
248     $img->line(
249             x1 => $left+1,
250             y1 => $zero_position,
251             x2 => $left + $graph_width,
252             y2 => $zero_position,
253             color => $self->_get_color('outline.line'),
254     );
255   }
256
257   if ($self->_get_data_series()->{'stacked_column'}) {
258     return unless $self->_draw_stacked_columns();
259   }
260   if ($self->_get_data_series()->{'column'}) {
261     return unless $self->_draw_columns();
262   }
263   if ($self->_get_data_series()->{'line'}) {
264     return unless $self->_draw_lines();
265   }
266   if ($self->_get_data_series()->{'area'}) {
267     return unless $self->_draw_area();
268   }
269
270   if ($self->_get_y_tics()) {
271     $self->_draw_y_tics();
272   }
273   if ($self->_get_labels()) {
274     $self->_draw_x_tics();
275   }
276
277   return $self->_get_image();
278 }
279
280 sub _get_data_range {
281   my $self = shift;
282
283   my $max_value = 0;
284   my $min_value = 0;
285   my $column_count = 0;
286
287   my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
288   my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
289   my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
290   my ($a_min, $a_max, $a_cols) = $self->_get_area_range();
291
292   # These are side by side...
293   $sc_cols += $c_cols;
294
295   $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min, $a_min);
296   $max_value = $self->_max(0, $sc_max, $c_max, $l_max, $a_max);
297
298   my $config_min = $self->_get_number('y_min');
299   my $config_max = $self->_get_number('y_max');
300
301   if (defined $config_max && $config_max < $max_value) {
302     $config_max = undef;
303   }
304   if (defined $config_min && $config_min > $min_value) {
305     $config_min = undef;
306   }
307
308   my $range_padding = $self->_get_number('range_padding');
309   if (defined $config_min) {
310     $min_value = $config_min;
311   }
312   else {
313     if ($min_value > 0) {
314       $min_value = 0;
315     }
316     if ($range_padding && $min_value < 0) {
317       my $difference = $min_value * $range_padding / 100;
318       if ($min_value < -1 && $difference > -1) {
319         $difference = -1;
320       }
321       $min_value += $difference;
322     }
323   }
324   if (defined $config_max) {
325     $max_value = $config_max;
326   }
327   else {
328     if ($range_padding && $max_value > 0) {
329       my $difference = $max_value * $range_padding / 100;
330       if ($max_value > 1 && $difference < 1) {
331         $difference = 1;
332       }
333       $max_value += $difference;
334     }
335   }
336   $column_count = $self->_max(0, $sc_cols, $l_cols, $a_cols);
337
338   if ($self->_get_number('automatic_axis')) {
339     # In case this was set via a style, and not by the api method
340     eval { require Chart::Math::Axis; };
341     if ($@) {
342       return $self->_error("Can't use automatic_axis - $@");
343     }
344
345     my $axis = Chart::Math::Axis->new();
346     $axis->include_zero();
347     $axis->add_data($min_value, $max_value);
348     $max_value = $axis->top;
349     $min_value = $axis->bottom;
350     my $ticks     = $axis->ticks;
351     # The +1 is there because we have the bottom tick as well
352     $self->set_y_tics($ticks+1);
353   }
354
355   $self->_set_max_value($max_value);
356   $self->_set_min_value($min_value);
357   $self->_set_column_count($column_count);
358
359   return 1;
360 }
361
362 sub _min {
363   my $self = shift;
364   my $min = shift;
365
366   foreach my $value (@_) {
367     next unless defined $value;
368     if ($value < $min) { $min = $value; }
369   }
370   return $min;
371 }
372
373 sub _max {
374   my $self = shift;
375   my $min = shift;
376
377   foreach my $value (@_) {
378     next unless defined $value;
379     if ($value > $min) { $min = $value; }
380   }
381   return $min;
382 }
383
384 sub _get_line_range {
385   my $self = shift;
386   my $series = $self->_get_data_series()->{'line'};
387   return (undef, undef, 0) unless $series;
388
389   my $max_value = 0;
390   my $min_value = STARTING_MIN_VALUE;
391   my $column_count = 0;
392
393   my @series = @{$series};
394   foreach my $series (@series) {
395     my @data = @{$series->{'data'}};
396
397     if (scalar @data > $column_count) {
398       $column_count = scalar @data;
399     }
400
401     foreach my $value (@data) {
402       if ($value > $max_value) { $max_value = $value; }
403       if ($value < $min_value) { $min_value = $value; }
404     }
405   }
406
407   return ($min_value, $max_value, $column_count);
408 }
409
410 sub _get_area_range {
411   my $self = shift;
412   my $series = $self->_get_data_series()->{'area'};
413   return (undef, undef, 0) unless $series;
414
415   my $max_value = 0;
416   my $min_value = STARTING_MIN_VALUE;
417   my $column_count = 0;
418
419   my @series = @{$series};
420   foreach my $series (@series) {
421     my @data = @{$series->{'data'}};
422
423     if (scalar @data > $column_count) {
424       $column_count = scalar @data;
425     }
426
427     foreach my $value (@data) {
428       if ($value > $max_value) { $max_value = $value; }
429       if ($value < $min_value) { $min_value = $value; }
430     }
431   }
432
433   return ($min_value, $max_value, $column_count);
434 }
435
436
437 sub _get_column_range {
438   my $self = shift;
439
440   my $series = $self->_get_data_series()->{'column'};
441   return (undef, undef, 0) unless $series;
442
443   my $max_value = 0;
444   my $min_value = STARTING_MIN_VALUE;
445   my $column_count = 0;
446
447   my @series = @{$series};
448   foreach my $series (@series) {
449     my @data = @{$series->{'data'}};
450
451     foreach my $value (@data) {
452       $column_count++;
453       if ($value > $max_value) { $max_value = $value; }
454       if ($value < $min_value) { $min_value = $value; }
455     }
456   }
457
458   return ($min_value, $max_value, $column_count);
459 }
460
461 sub _get_stacked_column_range {
462   my $self = shift;
463
464   my $max_value = 0;
465   my $min_value = STARTING_MIN_VALUE;
466   my $column_count = 0;
467
468   return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
469   my @series = @{$self->_get_data_series()->{'stacked_column'}};
470
471   my @max_entries;
472   my @min_entries;
473   for (my $i = scalar @series - 1; $i >= 0; $i--) {
474     my $series = $series[$i];
475     my $data = $series->{'data'};
476
477     for (my $i = 0; $i < scalar @$data; $i++) {
478       my $value = 0;
479       if ($data->[$i] > 0) {
480         $value = $data->[$i] + ($max_entries[$i] || 0);
481         $data->[$i] = $value;
482         $max_entries[$i] = $value;
483       }
484       elsif ($data->[$i] < 0) {
485         $value = $data->[$i] + ($min_entries[$i] || 0);
486         $data->[$i] = $value;
487         $min_entries[$i] = $value;
488       }
489       if ($value > $max_value) { $max_value = $value; }
490       if ($value < $min_value) { $min_value = $value; }
491     }
492     if (scalar @$data > $column_count) {
493       $column_count = scalar @$data;
494     }
495   }
496
497   return ($min_value, $max_value, $column_count);
498 }
499
500 sub _draw_legend {
501   my $self = shift;
502   my $chart_box = shift;
503   my $style = $self->{'_style'};
504
505   my @labels;
506   my $img = $self->_get_image();
507   if (my $series = $self->_get_data_series()->{'stacked_column'}) {
508     push @labels, map { $_->{'series_name'} } @$series;
509   }
510   if (my $series = $self->_get_data_series()->{'column'}) {
511     push @labels, map { $_->{'series_name'} } @$series;
512   }
513   if (my $series = $self->_get_data_series()->{'line'}) {
514     push @labels, map { $_->{'series_name'} } @$series;
515   }
516
517   if ($style->{features}{legend} && (scalar @labels)) {
518     $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
519       or return;
520   }
521   return;
522 }
523
524 sub _draw_flat_legend {
525   return 1;
526 }
527
528 sub _draw_lines {
529   my $self = shift;
530   my $style = $self->{'_style'};
531
532   my $img = $self->_get_image();
533
534   my $max_value = $self->_get_max_value();
535   my $min_value = $self->_get_min_value();
536   my $column_count = $self->_get_column_count();
537
538   my $value_range = $max_value - $min_value;
539
540   my $width = $self->_get_number('width');
541   my $height = $self->_get_number('height');
542
543   my $graph_width = $self->_get_number('graph_width');
544   my $graph_height = $self->_get_number('graph_height');
545
546   my $line_series = $self->_get_data_series()->{'line'};
547   my $series_counter = $self->_get_series_counter() || 0;
548
549   my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
550
551   my $col_width = int($graph_width / $column_count) -1;
552
553   my $graph_box = $self->_get_graph_box();
554   my $left = $graph_box->[0] + 1;
555   my $bottom = $graph_box->[1];
556
557   my $zero_position =  $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
558
559   my $line_aa = $self->_get_number("lineaa");
560   foreach my $series (@$line_series) {
561     my @data = @{$series->{'data'}};
562     my $data_size = scalar @data;
563
564     my $interval;
565     if ($has_columns) {
566       $interval = $graph_width / ($data_size);
567     }
568     else {
569       $interval = $graph_width / ($data_size - 1);
570     }
571     my $color = $self->_data_color($series_counter);
572
573     # We need to add these last, otherwise the next line segment will overwrite half of the marker
574     my @marker_positions;
575     for (my $i = 0; $i < $data_size - 1; $i++) {
576       my $x1 = $left + $i * $interval;
577       my $x2 = $left + ($i + 1) * $interval;
578
579       $x1 += $has_columns * $interval / 2;
580       $x2 += $has_columns * $interval / 2;
581
582       my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
583       my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
584
585       push @marker_positions, [$x1, $y1];
586       $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
587     }
588
589     my $x2 = $left + ($data_size - 1) * $interval;
590     $x2 += $has_columns * $interval / 2;
591
592     my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
593
594     push @marker_positions, [$x2, $y2];
595     foreach my $position (@marker_positions) {
596       $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
597     }
598     $series_counter++;
599   }
600
601   $self->_set_series_counter($series_counter);
602   return 1;
603 }
604
605 sub _draw_area {
606   my $self = shift;
607   my $style = $self->{'_style'};
608
609   my $img = $self->_get_image();
610
611   my $max_value = $self->_get_max_value();
612   my $min_value = $self->_get_min_value();
613   my $column_count = $self->_get_column_count();
614
615   my $value_range = $max_value - $min_value;
616
617   my $width = $self->_get_number('width');
618   my $height = $self->_get_number('height');
619
620   my $graph_width = $self->_get_number('graph_width');
621   my $graph_height = $self->_get_number('graph_height');
622
623   my $area_series = $self->_get_data_series()->{'area'};
624   my $series_counter = $self->_get_series_counter() || 0;
625
626   my $col_width = int($graph_width / $column_count) -1;
627
628   my $graph_box = $self->_get_graph_box();
629   my $left = $graph_box->[0] + 1;
630   my $bottom = $graph_box->[1];
631   my $right = $graph_box->[2];
632   my $top = $graph_box->[3];
633
634   my $zero_position =  $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
635
636   my $line_aa = $self->_get_number("lineaa");
637   foreach my $series (@$area_series) {
638     my @data = @{$series->{'data'}};
639     my $data_size = scalar @data;
640
641     my $interval = $graph_width / ($data_size - 1);
642
643     my $color = $self->_data_color($series_counter);
644
645     # We need to add these last, otherwise the next line segment will overwrite half of the marker
646     my @marker_positions;
647     my @polygon_points;
648     for (my $i = 0; $i < $data_size - 1; $i++) {
649       my $x1 = $left + $i * $interval;
650
651       my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
652
653       if ($i == 0) {
654         push @polygon_points, [$x1, $top];
655       }
656       push @polygon_points, [$x1, $y1];
657
658       push @marker_positions, [$x1, $y1];
659     }
660
661     my $x2 = $left + ($data_size - 1) * $interval;
662
663     my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
664     push @polygon_points, [$x2, $y2];
665     push @polygon_points, [$x2, $top];
666     push @polygon_points, $polygon_points[0];
667
668     my @fill = $self->_data_fill($series_counter, [$left, $bottom, $right, $top]);
669     $img->polygon(points => [@polygon_points], @fill);
670
671     push @marker_positions, [$x2, $y2];
672     foreach my $position (@marker_positions) {
673       $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
674     }
675     $series_counter++;
676   }
677
678   $self->_set_series_counter($series_counter);
679   return 1;
680 }
681
682
683
684 sub _line_marker {
685   my ($self, $index) = @_;
686
687   my $markers = $self->{'_style'}{'line_markers'};
688   if (!$markers) {
689     return;
690   }
691   my $marker = $markers->[$index % @$markers];
692
693   return $marker;
694 }
695
696 sub _draw_line_marker {
697   my $self = shift;
698   my ($x1, $y1, $series_counter) = @_;
699
700   my $img = $self->_get_image();
701
702   my $style = $self->_line_marker($series_counter);
703   return unless $style;
704
705   my $type = $style->{'shape'};
706   my $radius = $style->{'radius'};
707
708   my $line_aa = $self->_get_number("lineaa");
709   my $fill_aa = $self->_get_number("fill.aa");
710   if ($type eq 'circle') {
711     my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
712     $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
713   }
714   elsif ($type eq 'square') {
715     my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
716     $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
717   }
718   elsif ($type eq 'diamond') {
719     # The gradient really doesn't work for diamond
720     my $color = $self->_data_color($series_counter);
721     $img->polygon(
722         points => [
723                     [$x1 - $radius, $y1],
724                     [$x1, $y1 + $radius],
725                     [$x1 + $radius, $y1],
726                     [$x1, $y1 - $radius],
727                   ],
728         filled => 1, color => $color, aa => $fill_aa);
729   }
730   elsif ($type eq 'triangle') {
731     # The gradient really doesn't work for triangle
732     my $color = $self->_data_color($series_counter);
733     $img->polygon(
734         points => [
735                     [$x1 - $radius, $y1 + $radius],
736                     [$x1 + $radius, $y1 + $radius],
737                     [$x1, $y1 - $radius],
738                   ],
739         filled => 1, color => $color, aa => $fill_aa);
740
741   }
742   elsif ($type eq 'x') {
743     my $color = $self->_data_color($series_counter);
744     $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
745     $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
746   }
747   elsif ($type eq 'plus') {
748     my $color = $self->_data_color($series_counter);
749     $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
750     $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
751   }
752 }
753
754 sub _draw_columns {
755   my $self = shift;
756   my $style = $self->{'_style'};
757
758   my $img = $self->_get_image();
759
760   my $max_value = $self->_get_max_value();
761   my $min_value = $self->_get_min_value();
762   my $column_count = $self->_get_column_count();
763
764   my $value_range = $max_value - $min_value;
765
766   my $width = $self->_get_number('width');
767   my $height = $self->_get_number('height');
768
769   my $graph_width = $self->_get_number('graph_width');
770   my $graph_height = $self->_get_number('graph_height');
771
772
773   my $graph_box = $self->_get_graph_box();
774   my $left = $graph_box->[0] + 1;
775   my $bottom = $graph_box->[1];
776   my $zero_position =  int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
777
778   my $bar_width = $graph_width / $column_count;
779
780   my $outline_color;
781   if ($style->{'features'}{'outline'}) {
782     $outline_color = $self->_get_color('outline.line');
783   }
784
785   my $series_counter = $self->_get_series_counter() || 0;
786   my $col_series = $self->_get_data_series()->{'column'};
787   my $column_padding_percent = $self->_get_number('column_padding') || 0;
788   my $column_padding = int($column_padding_percent * $bar_width / 100);
789
790   # 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.
791   my $column_series = 0;
792
793   # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
794   my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
795
796   for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
797     my $series = $col_series->[$series_pos];
798     my @data = @{$series->{'data'}};
799     my $data_size = scalar @data;
800     for (my $i = 0; $i < $data_size; $i++) {
801       my $part1 = $bar_width * (scalar @$col_series * $i);
802       my $part2 = ($series_pos) * $bar_width;
803       my $x1 = $left + $part1 + $part2;
804       if ($has_stacked_columns) {
805         $x1 += ($bar_width * ($i+1));
806       }
807       $x1 = int($x1);
808
809       my $x2 = int($x1 + $bar_width - $column_padding)-1;
810       # Special case for when bar_width is less than 1.
811       if ($x2 < $x1) {
812         $x2 = $x1;
813       }
814
815       my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
816
817       my $color = $self->_data_color($series_counter);
818
819       if ($data[$i] > 0) {
820         my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
821         $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
822         if ($style->{'features'}{'outline'}) {
823           $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
824         }
825       }
826       else {
827         my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
828         $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
829         if ($style->{'features'}{'outline'}) {
830           $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
831         }
832       }
833     }
834
835     $series_counter++;
836     $column_series++;
837   }
838   $self->_set_series_counter($series_counter);
839   return 1;
840 }
841
842 sub _draw_stacked_columns {
843   my $self = shift;
844   my $style = $self->{'_style'};
845
846   my $img = $self->_get_image();
847
848   my $max_value = $self->_get_max_value();
849   my $min_value = $self->_get_min_value();
850   my $column_count = $self->_get_column_count();
851   my $value_range = $max_value - $min_value;
852
853   my $graph_box = $self->_get_graph_box();
854   my $left = $graph_box->[0] + 1;
855   my $bottom = $graph_box->[1];
856
857   my $graph_width = $self->_get_number('graph_width');
858   my $graph_height = $self->_get_number('graph_height');
859
860   my $bar_width = $graph_width / $column_count;
861   my $column_series = 0;
862   if (my $column_series_data = $self->_get_data_series()->{'column'}) {
863     $column_series = (scalar @$column_series_data);
864   }
865   $column_series++;
866
867   my $column_padding_percent = $self->_get_number('column_padding') || 0;
868   if ($column_padding_percent < 0) {
869     return $self->_error("Column padding less than 0");
870   }
871   if ($column_padding_percent > 100) {
872     return $self->_error("Column padding greater than 0");
873   }
874   my $column_padding = int($column_padding_percent * $bar_width / 100);
875
876   my $outline_color;
877   if ($style->{'features'}{'outline'}) {
878     $outline_color = $self->_get_color('outline.line');
879   }
880
881   my $zero_position =  $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
882   my $col_series = $self->_get_data_series()->{'stacked_column'};
883   my $series_counter = $self->_get_series_counter() || 0;
884
885   foreach my $series (@$col_series) {
886     my @data = @{$series->{'data'}};
887     my $data_size = scalar @data;
888     for (my $i = 0; $i < $data_size; $i++) {
889       my $part1 = $bar_width * $i * $column_series;
890       my $part2 = 0;
891       my $x1 = int($left + $part1 + $part2);
892       my $x2 = int($x1 + $bar_width - $column_padding) - 1;
893       # Special case for when bar_width is less than 1.
894       if ($x2 < $x1) {
895         $x2 = $x1;
896       }
897
898       my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
899
900       if ($data[$i] > 0) {
901         my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
902         $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
903         if ($style->{'features'}{'outline'}) {
904           $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
905         }
906       }
907       else {
908         my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
909         $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
910         if ($style->{'features'}{'outline'}) {
911           $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
912         }
913       }
914     }
915
916     $series_counter++;
917   }
918   $self->_set_series_counter($series_counter);
919   return 1;
920 }
921
922 sub _add_data_series {
923   my $self = shift;
924   my $series_type = shift;
925   my $data_ref = shift;
926   my $series_name = shift;
927
928   my $graph_data = $self->{'graph_data'} || {};
929
930   my $series = $graph_data->{$series_type} || [];
931
932   push @$series, { data => $data_ref, series_name => $series_name };
933
934   $graph_data->{$series_type} = $series;
935
936   $self->{'graph_data'} = $graph_data;
937   return;
938 }
939
940 =over
941
942 =item show_horizontal_gridlines()
943
944 Shows horizontal gridlines at the y-tics.
945
946 =cut
947
948 sub show_horizontal_gridlines {
949     $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
950 }
951
952 =item use_automatic_axis()
953
954 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.
955
956 =cut
957
958 sub use_automatic_axis {
959   eval { require Chart::Math::Axis; };
960   if ($@) {
961     return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
962   }
963   $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
964   return 1;
965 }
966
967
968 =item set_y_tics($count)
969
970 Set the number of Y tics to use.  Their value and position will be determined by the data range.
971
972 =cut
973
974 sub set_y_tics {
975   $_[0]->{'y_tics'} = $_[1];
976 }
977
978 sub _get_y_tics {
979   return $_[0]->{'y_tics'} || 0;
980 }
981
982 sub _remove_tics_from_chart_box {
983   my $self = shift;
984   my $chart_box = shift;
985
986   # XXX - bad default
987   my $tic_width = $self->_get_y_tic_width() || 10;
988   my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
989
990   # XXX - bad default
991   my $tic_height = $self->_get_x_tic_height() || 10;
992   my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
993
994   $self->_remove_box($chart_box, \@y_tic_box);
995   $self->_remove_box($chart_box, \@x_tic_box);
996
997   # If there's no title, the y-tics will be part off-screen.  Half of the x-tic height should be more than sufficient.
998   my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
999   $self->_remove_box($chart_box, \@y_tic_tops);
1000
1001   # Make sure that the first and last label fit
1002   if (my $labels = $self->_get_labels()) {
1003     if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1004       my @remove_box = ($chart_box->[0],
1005                         $chart_box->[1],
1006                         $chart_box->[0] + int($box[2] / 2) + 1,
1007                         $chart_box->[3]
1008                         );
1009
1010       $self->_remove_box($chart_box, \@remove_box);
1011     }
1012     if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1013       my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1014                         $chart_box->[1],
1015                         $chart_box->[2],
1016                         $chart_box->[3]
1017                         );
1018
1019       $self->_remove_box($chart_box, \@remove_box);
1020     }
1021   }
1022 }
1023
1024 sub _get_y_tic_width{
1025   my $self = shift;
1026   my $min = $self->_get_min_value();
1027   my $max = $self->_get_max_value();
1028   my $tic_count = $self->_get_y_tics();
1029
1030   my $interval = ($max - $min) / ($tic_count - 1);
1031
1032   my %text_info = $self->_text_style('legend')
1033     or return;
1034
1035   my $max_width = 0;
1036   for my $count (0 .. $tic_count - 1) {
1037     my $value = sprintf("%.2f", ($count*$interval)+$min);
1038
1039     my @box = $self->_text_bbox($value, 'legend');
1040     my $width = $box[2] - $box[0];
1041
1042     # For the tic width
1043     $width += 10;
1044     if ($width > $max_width) {
1045       $max_width = $width;
1046     }
1047   }
1048
1049   return $max_width;
1050 }
1051
1052 sub _get_x_tic_height {
1053   my $self = shift;
1054
1055   my $labels = $self->_get_labels();
1056
1057   if (!$labels) {
1058         return;
1059   }
1060
1061   my $tic_count = (scalar @$labels) - 1;
1062
1063   my %text_info = $self->_text_style('legend')
1064     or return;
1065
1066   my $max_height = 0;
1067   for my $count (0 .. $tic_count) {
1068     my $label = $labels->[$count];
1069
1070     my @box = $self->_text_bbox($label, 'legend');
1071
1072     my $height = $box[3] - $box[1];
1073
1074     # Padding + the tic
1075     $height += 10;
1076     if ($height > $max_height) {
1077       $max_height = $height;
1078     }
1079   }
1080
1081   return $max_height;
1082 }
1083
1084 sub _draw_y_tics {
1085   my $self = shift;
1086   my $min = $self->_get_min_value();
1087   my $max = $self->_get_max_value();
1088   my $tic_count = $self->_get_y_tics();
1089
1090   my $img = $self->_get_image();
1091   my $graph_box = $self->_get_graph_box();
1092   my $image_box = $self->_get_image_box();
1093
1094   my $interval = ($max - $min) / ($tic_count - 1);
1095
1096   my %text_info = $self->_text_style('legend')
1097     or return;
1098
1099   my $line_style = $self->_get_color('outline.line');
1100   my $show_gridlines = $self->_get_number('horizontal_gridlines');
1101   my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
1102   for my $count (0 .. $tic_count - 1) {
1103     my $x1 = $graph_box->[0] - 5;
1104     my $x2 = $graph_box->[0] + 5;
1105     my $y1 = int($graph_box->[3] - ($count * $tic_distance));
1106
1107     my $value = ($count*$interval)+$min;
1108     if ($interval < 1 || ($value != int($value))) {
1109         $value = sprintf("%.2f", $value);
1110     }
1111
1112     my @box = $self->_text_bbox($value, 'legend')
1113       or return;
1114
1115     $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1116
1117     my $width = $box[2];
1118     my $height = $box[3];
1119
1120     $img->string(%text_info,
1121                  x    => ($x1 - $width - 3),
1122                  y    => ($y1 + ($height / 2)),
1123                  text => $value
1124                 );
1125
1126     if ($show_gridlines) {
1127       # XXX - line styles!
1128       for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1129         my $x1 = $i;
1130         my $x2 = $i + 2;
1131         if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1132         $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1133       }
1134     }
1135   }
1136
1137 }
1138
1139 sub _draw_x_tics {
1140   my $self = shift;
1141
1142   my $img = $self->_get_image();
1143   my $graph_box = $self->_get_graph_box();
1144   my $image_box = $self->_get_image_box();
1145
1146   my $labels = $self->_get_labels();
1147
1148   my $tic_count = (scalar @$labels) - 1;
1149
1150   my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1151
1152   # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1153   my $denominator = $tic_count;
1154   if ($has_columns) {
1155     $denominator ++;
1156   }
1157   my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1158   my %text_info = $self->_text_style('legend')
1159     or return;
1160
1161   # If automatic axis is turned on, let's be selective about what labels we draw.
1162   my $max_size = 0;
1163   my $tic_skip = 0;
1164   if ($self->_get_number('automatic_axis')) {
1165     foreach my $label (@$labels) {
1166       my @box = $self->_text_bbox($label, 'legend');
1167       if ($box[2] > $max_size) {
1168         $max_size = $box[2];
1169       }
1170     }
1171
1172     # Give the max_size some padding...
1173     $max_size *= 1.2;
1174
1175     $tic_skip = int($max_size / $tic_distance) + 1;
1176   }
1177
1178   my $line_style = $self->_get_color('outline.line');
1179
1180   for my $count (0 .. $tic_count) {
1181     next if ($count % ($tic_skip + 1));
1182     my $label = $labels->[$count];
1183     my $x1 = $graph_box->[0] + ($tic_distance * $count);
1184
1185     if ($has_columns) {
1186       $x1 += $tic_distance / 2;
1187     }
1188
1189     $x1 = int($x1);
1190
1191     my $y1 = $graph_box->[3] + 5;
1192     my $y2 = $graph_box->[3] - 5;
1193
1194     $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1195
1196     my @box = $self->_text_bbox($label, 'legend')
1197       or return;
1198
1199     my $width = $box[2];
1200     my $height = $box[3];
1201
1202     $img->string(%text_info,
1203                  x    => ($x1 - ($width / 2)),
1204                  y    => ($y1 + ($height + 5)),
1205                  text => $label
1206                 );
1207
1208   }
1209 }
1210
1211 sub _valid_input {
1212   my $self = shift;
1213
1214   if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1215     return $self->_error("No data supplied");
1216   }
1217
1218   my $data = $self->_get_data_series();
1219   if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1220     return $self->_error("No values in data series");
1221   }
1222   if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1223     return $self->_error("No values in data series");
1224   }
1225   if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1226     return $self->_error("No values in data series");
1227   }
1228
1229   return 1;
1230 }
1231
1232 sub _set_column_count   { $_[0]->{'column_count'} = $_[1]; }
1233 sub _set_min_value      { $_[0]->{'min_value'} = $_[1]; }
1234 sub _set_max_value      { $_[0]->{'max_value'} = $_[1]; }
1235 sub _set_image_box      { $_[0]->{'image_box'} = $_[1]; }
1236 sub _set_graph_box      { $_[0]->{'graph_box'} = $_[1]; }
1237 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1238 sub _get_column_count   { return $_[0]->{'column_count'} }
1239 sub _get_min_value      { return $_[0]->{'min_value'} }
1240 sub _get_max_value      { return $_[0]->{'max_value'} }
1241 sub _get_image_box      { return $_[0]->{'image_box'} }
1242 sub _get_graph_box      { return $_[0]->{'graph_box'} }
1243 sub _get_series_counter { return $_[0]->{'series_counter'} }
1244
1245 1;