]> git.imager.perl.org - imager-graph.git/blob - lib/Imager/Graph/Vertical.pm
Start of area graphs
[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 $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
627
628   my $col_width = int($graph_width / $column_count) -1;
629
630   my $graph_box = $self->_get_graph_box();
631   my $left = $graph_box->[0] + 1;
632   my $bottom = $graph_box->[1];
633   my $right = $graph_box->[2];
634   my $top = $graph_box->[3];
635
636   my $zero_position =  $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
637
638   my $line_aa = $self->_get_number("lineaa");
639   foreach my $series (@$area_series) {
640     my @data = @{$series->{'data'}};
641     my $data_size = scalar @data;
642
643     my $interval;
644     if ($has_columns) {
645       $interval = $graph_width / ($data_size);
646     }
647     else {
648       $interval = $graph_width / ($data_size - 1);
649     }
650     my $color = $self->_data_color($series_counter);
651
652     # We need to add these last, otherwise the next line segment will overwrite half of the marker
653     my @marker_positions;
654     my @polygon_points;
655     for (my $i = 0; $i < $data_size - 1; $i++) {
656       my $x1 = $left + $i * $interval;
657
658       $x1 += $has_columns * $interval / 2;
659
660       my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
661
662       if ($i == 0) {
663         push @polygon_points, [$x1, $top];
664       }
665       push @polygon_points, [$x1, $y1];
666
667       push @marker_positions, [$x1, $y1];
668     }
669
670     my $x2 = $left + ($data_size - 1) * $interval;
671     $x2 += $has_columns * $interval / 2;
672
673     my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
674     push @polygon_points, [$x2, $y2];
675     push @polygon_points, [$x2, $top];
676     push @polygon_points, $polygon_points[0];
677
678     my @fill = $self->_data_fill($series_counter, [$left, $bottom, $right, $top]);
679     $img->polygon(points => [@polygon_points], @fill);
680
681     push @marker_positions, [$x2, $y2];
682     foreach my $position (@marker_positions) {
683       $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
684     }
685     $series_counter++;
686   }
687
688   $self->_set_series_counter($series_counter);
689   return 1;
690 }
691
692
693
694 sub _line_marker {
695   my ($self, $index) = @_;
696
697   my $markers = $self->{'_style'}{'line_markers'};
698   if (!$markers) {
699     return;
700   }
701   my $marker = $markers->[$index % @$markers];
702
703   return $marker;
704 }
705
706 sub _draw_line_marker {
707   my $self = shift;
708   my ($x1, $y1, $series_counter) = @_;
709
710   my $img = $self->_get_image();
711
712   my $style = $self->_line_marker($series_counter);
713   return unless $style;
714
715   my $type = $style->{'shape'};
716   my $radius = $style->{'radius'};
717
718   my $line_aa = $self->_get_number("lineaa");
719   my $fill_aa = $self->_get_number("fill.aa");
720   if ($type eq 'circle') {
721     my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
722     $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
723   }
724   elsif ($type eq 'square') {
725     my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
726     $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
727   }
728   elsif ($type eq 'diamond') {
729     # The gradient really doesn't work for diamond
730     my $color = $self->_data_color($series_counter);
731     $img->polygon(
732         points => [
733                     [$x1 - $radius, $y1],
734                     [$x1, $y1 + $radius],
735                     [$x1 + $radius, $y1],
736                     [$x1, $y1 - $radius],
737                   ],
738         filled => 1, color => $color, aa => $fill_aa);
739   }
740   elsif ($type eq 'triangle') {
741     # The gradient really doesn't work for triangle
742     my $color = $self->_data_color($series_counter);
743     $img->polygon(
744         points => [
745                     [$x1 - $radius, $y1 + $radius],
746                     [$x1 + $radius, $y1 + $radius],
747                     [$x1, $y1 - $radius],
748                   ],
749         filled => 1, color => $color, aa => $fill_aa);
750
751   }
752   elsif ($type eq 'x') {
753     my $color = $self->_data_color($series_counter);
754     $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
755     $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
756   }
757   elsif ($type eq 'plus') {
758     my $color = $self->_data_color($series_counter);
759     $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
760     $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
761   }
762 }
763
764 sub _draw_columns {
765   my $self = shift;
766   my $style = $self->{'_style'};
767
768   my $img = $self->_get_image();
769
770   my $max_value = $self->_get_max_value();
771   my $min_value = $self->_get_min_value();
772   my $column_count = $self->_get_column_count();
773
774   my $value_range = $max_value - $min_value;
775
776   my $width = $self->_get_number('width');
777   my $height = $self->_get_number('height');
778
779   my $graph_width = $self->_get_number('graph_width');
780   my $graph_height = $self->_get_number('graph_height');
781
782
783   my $graph_box = $self->_get_graph_box();
784   my $left = $graph_box->[0] + 1;
785   my $bottom = $graph_box->[1];
786   my $zero_position =  int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
787
788   my $bar_width = $graph_width / $column_count;
789
790   my $outline_color;
791   if ($style->{'features'}{'outline'}) {
792     $outline_color = $self->_get_color('outline.line');
793   }
794
795   my $series_counter = $self->_get_series_counter() || 0;
796   my $col_series = $self->_get_data_series()->{'column'};
797   my $column_padding_percent = $self->_get_number('column_padding') || 0;
798   my $column_padding = int($column_padding_percent * $bar_width / 100);
799
800   # 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.
801   my $column_series = 0;
802
803   # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
804   my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
805
806   for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
807     my $series = $col_series->[$series_pos];
808     my @data = @{$series->{'data'}};
809     my $data_size = scalar @data;
810     for (my $i = 0; $i < $data_size; $i++) {
811       my $part1 = $bar_width * (scalar @$col_series * $i);
812       my $part2 = ($series_pos) * $bar_width;
813       my $x1 = $left + $part1 + $part2;
814       if ($has_stacked_columns) {
815         $x1 += ($bar_width * ($i+1));
816       }
817       $x1 = int($x1);
818
819       my $x2 = int($x1 + $bar_width - $column_padding)-1;
820       # Special case for when bar_width is less than 1.
821       if ($x2 < $x1) {
822         $x2 = $x1;
823       }
824
825       my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
826
827       my $color = $self->_data_color($series_counter);
828
829       if ($data[$i] > 0) {
830         my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
831         $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
832         if ($style->{'features'}{'outline'}) {
833           $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
834         }
835       }
836       else {
837         my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
838         $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
839         if ($style->{'features'}{'outline'}) {
840           $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
841         }
842       }
843     }
844
845     $series_counter++;
846     $column_series++;
847   }
848   $self->_set_series_counter($series_counter);
849   return 1;
850 }
851
852 sub _draw_stacked_columns {
853   my $self = shift;
854   my $style = $self->{'_style'};
855
856   my $img = $self->_get_image();
857
858   my $max_value = $self->_get_max_value();
859   my $min_value = $self->_get_min_value();
860   my $column_count = $self->_get_column_count();
861   my $value_range = $max_value - $min_value;
862
863   my $graph_box = $self->_get_graph_box();
864   my $left = $graph_box->[0] + 1;
865   my $bottom = $graph_box->[1];
866
867   my $graph_width = $self->_get_number('graph_width');
868   my $graph_height = $self->_get_number('graph_height');
869
870   my $bar_width = $graph_width / $column_count;
871   my $column_series = 0;
872   if (my $column_series_data = $self->_get_data_series()->{'column'}) {
873     $column_series = (scalar @$column_series_data);
874   }
875   $column_series++;
876
877   my $column_padding_percent = $self->_get_number('column_padding') || 0;
878   if ($column_padding_percent < 0) {
879     return $self->_error("Column padding less than 0");
880   }
881   if ($column_padding_percent > 100) {
882     return $self->_error("Column padding greater than 0");
883   }
884   my $column_padding = int($column_padding_percent * $bar_width / 100);
885
886   my $outline_color;
887   if ($style->{'features'}{'outline'}) {
888     $outline_color = $self->_get_color('outline.line');
889   }
890
891   my $zero_position =  $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
892   my $col_series = $self->_get_data_series()->{'stacked_column'};
893   my $series_counter = $self->_get_series_counter() || 0;
894
895   foreach my $series (@$col_series) {
896     my @data = @{$series->{'data'}};
897     my $data_size = scalar @data;
898     for (my $i = 0; $i < $data_size; $i++) {
899       my $part1 = $bar_width * $i * $column_series;
900       my $part2 = 0;
901       my $x1 = int($left + $part1 + $part2);
902       my $x2 = int($x1 + $bar_width - $column_padding) - 1;
903       # Special case for when bar_width is less than 1.
904       if ($x2 < $x1) {
905         $x2 = $x1;
906       }
907
908       my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
909
910       if ($data[$i] > 0) {
911         my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
912         $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
913         if ($style->{'features'}{'outline'}) {
914           $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
915         }
916       }
917       else {
918         my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
919         $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
920         if ($style->{'features'}{'outline'}) {
921           $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
922         }
923       }
924     }
925
926     $series_counter++;
927   }
928   $self->_set_series_counter($series_counter);
929   return 1;
930 }
931
932 sub _add_data_series {
933   my $self = shift;
934   my $series_type = shift;
935   my $data_ref = shift;
936   my $series_name = shift;
937
938   my $graph_data = $self->{'graph_data'} || {};
939
940   my $series = $graph_data->{$series_type} || [];
941
942   push @$series, { data => $data_ref, series_name => $series_name };
943
944   $graph_data->{$series_type} = $series;
945
946   $self->{'graph_data'} = $graph_data;
947   return;
948 }
949
950 =over
951
952 =item show_horizontal_gridlines()
953
954 Shows horizontal gridlines at the y-tics.
955
956 =cut
957
958 sub show_horizontal_gridlines {
959     $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
960 }
961
962 =item use_automatic_axis()
963
964 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.
965
966 =cut
967
968 sub use_automatic_axis {
969   eval { require Chart::Math::Axis; };
970   if ($@) {
971     return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
972   }
973   $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
974   return 1;
975 }
976
977
978 =item set_y_tics($count)
979
980 Set the number of Y tics to use.  Their value and position will be determined by the data range.
981
982 =cut
983
984 sub set_y_tics {
985   $_[0]->{'y_tics'} = $_[1];
986 }
987
988 sub _get_y_tics {
989   return $_[0]->{'y_tics'} || 0;
990 }
991
992 sub _remove_tics_from_chart_box {
993   my $self = shift;
994   my $chart_box = shift;
995
996   # XXX - bad default
997   my $tic_width = $self->_get_y_tic_width() || 10;
998   my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
999
1000   # XXX - bad default
1001   my $tic_height = $self->_get_x_tic_height() || 10;
1002   my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
1003
1004   $self->_remove_box($chart_box, \@y_tic_box);
1005   $self->_remove_box($chart_box, \@x_tic_box);
1006
1007   # If there's no title, the y-tics will be part off-screen.  Half of the x-tic height should be more than sufficient.
1008   my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
1009   $self->_remove_box($chart_box, \@y_tic_tops);
1010
1011   # Make sure that the first and last label fit
1012   if (my $labels = $self->_get_labels()) {
1013     if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1014       my @remove_box = ($chart_box->[0],
1015                         $chart_box->[1],
1016                         $chart_box->[0] + int($box[2] / 2) + 1,
1017                         $chart_box->[3]
1018                         );
1019
1020       $self->_remove_box($chart_box, \@remove_box);
1021     }
1022     if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1023       my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1024                         $chart_box->[1],
1025                         $chart_box->[2],
1026                         $chart_box->[3]
1027                         );
1028
1029       $self->_remove_box($chart_box, \@remove_box);
1030     }
1031   }
1032 }
1033
1034 sub _get_y_tic_width{
1035   my $self = shift;
1036   my $min = $self->_get_min_value();
1037   my $max = $self->_get_max_value();
1038   my $tic_count = $self->_get_y_tics();
1039
1040   my $interval = ($max - $min) / ($tic_count - 1);
1041
1042   my %text_info = $self->_text_style('legend')
1043     or return;
1044
1045   my $max_width = 0;
1046   for my $count (0 .. $tic_count - 1) {
1047     my $value = sprintf("%.2f", ($count*$interval)+$min);
1048
1049     my @box = $self->_text_bbox($value, 'legend');
1050     my $width = $box[2] - $box[0];
1051
1052     # For the tic width
1053     $width += 10;
1054     if ($width > $max_width) {
1055       $max_width = $width;
1056     }
1057   }
1058
1059   return $max_width;
1060 }
1061
1062 sub _get_x_tic_height {
1063   my $self = shift;
1064
1065   my $labels = $self->_get_labels();
1066
1067   if (!$labels) {
1068         return;
1069   }
1070
1071   my $tic_count = (scalar @$labels) - 1;
1072
1073   my %text_info = $self->_text_style('legend')
1074     or return;
1075
1076   my $max_height = 0;
1077   for my $count (0 .. $tic_count) {
1078     my $label = $labels->[$count];
1079
1080     my @box = $self->_text_bbox($label, 'legend');
1081
1082     my $height = $box[3] - $box[1];
1083
1084     # Padding + the tic
1085     $height += 10;
1086     if ($height > $max_height) {
1087       $max_height = $height;
1088     }
1089   }
1090
1091   return $max_height;
1092 }
1093
1094 sub _draw_y_tics {
1095   my $self = shift;
1096   my $min = $self->_get_min_value();
1097   my $max = $self->_get_max_value();
1098   my $tic_count = $self->_get_y_tics();
1099
1100   my $img = $self->_get_image();
1101   my $graph_box = $self->_get_graph_box();
1102   my $image_box = $self->_get_image_box();
1103
1104   my $interval = ($max - $min) / ($tic_count - 1);
1105
1106   my %text_info = $self->_text_style('legend')
1107     or return;
1108
1109   my $line_style = $self->_get_color('outline.line');
1110   my $show_gridlines = $self->_get_number('horizontal_gridlines');
1111   my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
1112   for my $count (0 .. $tic_count - 1) {
1113     my $x1 = $graph_box->[0] - 5;
1114     my $x2 = $graph_box->[0] + 5;
1115     my $y1 = int($graph_box->[3] - ($count * $tic_distance));
1116
1117     my $value = ($count*$interval)+$min;
1118     if ($interval < 1 || ($value != int($value))) {
1119         $value = sprintf("%.2f", $value);
1120     }
1121
1122     my @box = $self->_text_bbox($value, 'legend')
1123       or return;
1124
1125     $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1126
1127     my $width = $box[2];
1128     my $height = $box[3];
1129
1130     $img->string(%text_info,
1131                  x    => ($x1 - $width - 3),
1132                  y    => ($y1 + ($height / 2)),
1133                  text => $value
1134                 );
1135
1136     if ($show_gridlines) {
1137       # XXX - line styles!
1138       for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1139         my $x1 = $i;
1140         my $x2 = $i + 2;
1141         if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1142         $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
1143       }
1144     }
1145   }
1146
1147 }
1148
1149 sub _draw_x_tics {
1150   my $self = shift;
1151
1152   my $img = $self->_get_image();
1153   my $graph_box = $self->_get_graph_box();
1154   my $image_box = $self->_get_image_box();
1155
1156   my $labels = $self->_get_labels();
1157
1158   my $tic_count = (scalar @$labels) - 1;
1159
1160   my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1161
1162   # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1163   my $denominator = $tic_count;
1164   if ($has_columns) {
1165     $denominator ++;
1166   }
1167   my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1168   my %text_info = $self->_text_style('legend')
1169     or return;
1170
1171   # If automatic axis is turned on, let's be selective about what labels we draw.
1172   my $max_size = 0;
1173   my $tic_skip = 0;
1174   if ($self->_get_number('automatic_axis')) {
1175     foreach my $label (@$labels) {
1176       my @box = $self->_text_bbox($label, 'legend');
1177       if ($box[2] > $max_size) {
1178         $max_size = $box[2];
1179       }
1180     }
1181
1182     # Give the max_size some padding...
1183     $max_size *= 1.2;
1184
1185     $tic_skip = int($max_size / $tic_distance) + 1;
1186   }
1187
1188   my $line_style = $self->_get_color('outline.line');
1189
1190   for my $count (0 .. $tic_count) {
1191     next if ($count % ($tic_skip + 1));
1192     my $label = $labels->[$count];
1193     my $x1 = $graph_box->[0] + ($tic_distance * $count);
1194
1195     if ($has_columns) {
1196       $x1 += $tic_distance / 2;
1197     }
1198
1199     $x1 = int($x1);
1200
1201     my $y1 = $graph_box->[3] + 5;
1202     my $y2 = $graph_box->[3] - 5;
1203
1204     $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
1205
1206     my @box = $self->_text_bbox($label, 'legend')
1207       or return;
1208
1209     my $width = $box[2];
1210     my $height = $box[3];
1211
1212     $img->string(%text_info,
1213                  x    => ($x1 - ($width / 2)),
1214                  y    => ($y1 + ($height + 5)),
1215                  text => $label
1216                 );
1217
1218   }
1219 }
1220
1221 sub _valid_input {
1222   my $self = shift;
1223
1224   if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1225     return $self->_error("No data supplied");
1226   }
1227
1228   my $data = $self->_get_data_series();
1229   if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1230     return $self->_error("No values in data series");
1231   }
1232   if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1233     return $self->_error("No values in data series");
1234   }
1235   if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1236     return $self->_error("No values in data series");
1237   }
1238
1239   return 1;
1240 }
1241
1242 sub _set_column_count   { $_[0]->{'column_count'} = $_[1]; }
1243 sub _set_min_value      { $_[0]->{'min_value'} = $_[1]; }
1244 sub _set_max_value      { $_[0]->{'max_value'} = $_[1]; }
1245 sub _set_image_box      { $_[0]->{'image_box'} = $_[1]; }
1246 sub _set_graph_box      { $_[0]->{'graph_box'} = $_[1]; }
1247 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1248 sub _get_column_count   { return $_[0]->{'column_count'} }
1249 sub _get_min_value      { return $_[0]->{'min_value'} }
1250 sub _get_max_value      { return $_[0]->{'max_value'} }
1251 sub _get_image_box      { return $_[0]->{'image_box'} }
1252 sub _get_graph_box      { return $_[0]->{'graph_box'} }
1253 sub _get_series_counter { return $_[0]->{'series_counter'} }
1254
1255 1;