]> git.imager.perl.org - imager-graph.git/blob - lib/Imager/Graph/Horizontal.pm
f00c2365dd7eb552075f54d8d459a1ae77ca1518
[imager-graph.git] / lib / Imager / Graph / Horizontal.pm
1 package Imager::Graph::Horizontal;
2
3 =head1 NAME
4
5   Imager::Graph::Horizontal - A super class for line/bar charts
6
7 =head1 DESCRIPTION
8
9 This is a base class that implements base functionality for line and
10 bar charts.
11
12 The sub-classes, Imager::Graph::Bar and Imager::Graph::Line simply
13 provide default data series types.
14
15 =cut
16
17 use strict;
18 use vars qw(@ISA);
19 use Imager::Graph;
20 @ISA = qw(Imager::Graph);
21
22 use constant STARTING_MIN_VALUE => 99999;
23
24 our $VERSION = "0.10";
25
26 =head1 METHODS
27
28 =over
29
30 =item add_data_series(\@data, $series_name)
31
32 Add a data series to the graph, of the default type.
33
34 =cut
35
36 sub add_data_series {
37   my $self = shift;
38   my $data_ref = shift;
39   my $series_name = shift;
40
41   my $series_type = $self->_get_default_series_type();
42   $self->_add_data_series($series_type, $data_ref, $series_name);
43
44   return;
45 }
46
47 =item add_bar_data_series(\@data, $series_name)
48
49 Add a bar data series to the graph.
50
51 =cut
52
53 sub add_bar_data_series {
54   my $self = shift;
55   my $data_ref = shift;
56   my $series_name = shift;
57
58   $self->_add_data_series('bar', $data_ref, $series_name);
59
60   return;
61 }
62
63 =item add_line_data_series(\@data, $series_name)
64
65 Add a line data series to the graph.
66
67 =cut
68
69 sub add_line_data_series {
70   my $self = shift;
71   my $data_ref = shift;
72   my $series_name = shift;
73
74   $self->_add_data_series('line', $data_ref, $series_name);
75
76   return;
77 }
78
79 =item set_column_padding($int)
80
81 Sets the number of pixels that should go between columns of data.
82
83 =cut
84
85 sub set_column_padding {
86   $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
87 }
88
89 =item set_negative_background($color)
90
91 Sets the background color or fill used below the y axis.
92
93 =cut
94
95 sub set_negative_background {
96   $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
97 }
98
99 =item draw()
100
101 Draw the graph
102
103 =cut
104
105 sub draw {
106   my ($self, %opts) = @_;
107
108   if (!$self->_valid_input()) {
109     return;
110   }
111
112   $self->_style_setup(\%opts);
113
114   my $style = $self->{_style};
115
116   $self->_make_img
117     or return;
118
119   my $img = $self->_get_image()
120     or return;
121
122   my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
123   $self->_set_image_box(\@image_box);
124
125   my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
126   $self->_draw_legend(\@chart_box);
127   if ($style->{title}{text}) {
128     $self->_draw_title($img, \@chart_box)
129       or return;
130   }
131
132   # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
133   return unless $self->_get_data_range();
134   $self->_remove_tics_from_chart_box(\@chart_box, \%opts);
135   my $column_count = $self->_get_column_count();
136
137   my $width = $self->_get_number('width');
138   my $height = $self->_get_number('height');
139
140   my $graph_width = $chart_box[2] - $chart_box[0];
141   my $graph_height = $chart_box[3] - $chart_box[1];
142
143   my $col_height = ($graph_height - 1) / $column_count;
144   if ($col_height > 1) {
145     $graph_height = int($col_height) * $column_count + 1;
146   }
147   else {
148     $graph_height = $col_height * $column_count + 1;
149   }
150
151   my $tic_count = $self->_get_x_tics();
152   my $tic_distance = int(($graph_width -1) / ($tic_count - 1));
153   $graph_width = $tic_distance * ($tic_count - 1);
154
155   my $top = $chart_box[1];
156   my $left   = $chart_box[0];
157
158   $self->{'_style'}{'graph_width'} = $graph_width;
159   $self->{'_style'}{'graph_height'} = $graph_height;
160
161   my @graph_box = ($left, $top, $left + $graph_width, $top + $graph_height);
162
163   $self->_set_graph_box(\@graph_box);
164
165   my @fill_box = @graph_box;
166
167   if ($self->_feature_enabled("graph_outline")) {
168     my @line = $self->_get_line("graph.outline")
169       or return;
170
171     $self->_box(
172                 @line,
173                 box => \@fill_box,
174                 img => $img,
175                );
176     ++$fill_box[0];
177     ++$fill_box[1];
178     --$fill_box[2];
179     --$fill_box[3];
180   }
181
182   {
183     my @back_fill = $self->_get_fill("graph.fill", \@fill_box)
184       or return;
185     $img->box(
186               @back_fill,
187               box => \@fill_box,
188              );
189   }
190
191   my $min_value = $self->_get_min_value();
192   my $max_value = $self->_get_max_value();
193   my $value_range = $max_value - $min_value;
194
195   my $zero_position;
196   if ($value_range) {
197     $zero_position =  $left + (-1*$min_value / $value_range) * ($graph_width-1);
198   }
199
200   if ($min_value < 0) {
201     my @neg_box = ( $left+1, $top+1, $zero_position, $top+$graph_height - 1 );
202     my @neg_fill = $self->_get_fill('negative_bg', \@neg_box)
203       or return;
204
205     $img->box(
206               @neg_fill,
207               box => \@neg_box,
208     );
209     $img->line(
210             x1 => $zero_position,
211             y1 => $top,
212             x2 => $zero_position,
213             y2 => $top + $graph_height,
214             color => $self->_get_color('outline.line'),
215     );
216   }
217
218   $self->_reset_series_counter();
219
220   if ($self->_get_data_series()->{'bar'}) {
221     $self->_draw_bars();
222   }
223   if ($self->_get_data_series()->{'line'}) {
224     $self->_draw_lines();
225   }
226
227   if ($self->_get_x_tics()) {
228     $self->_draw_x_tics();
229   }
230   if ($self->_get_labels(\%opts)) {
231     $self->_draw_y_tics(\%opts);
232   }
233
234   return $self->_get_image();
235 }
236
237 sub _get_data_range {
238   my $self = shift;
239
240   my $max_value = 0;
241   my $min_value = 0;
242   my $column_count = 0;
243
244   my ($b_min, $b_max, $b_cols) = $self->_get_bar_range();
245   my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
246
247   $min_value = $self->_min(STARTING_MIN_VALUE, $b_min, $l_min);
248   $max_value = $self->_max(0, $b_max, $l_max);
249   $column_count = $self->_max(0, $b_cols, $l_cols);
250
251   my $config_min = $self->_get_number('x_min');
252   my $config_max = $self->_get_number('x_max');
253
254   if (defined $config_max && $config_max < $max_value) {
255     $config_max = undef;
256   }
257   if (defined $config_min && $config_min > $min_value) {
258     $config_min = undef;
259   }
260
261   my $range_padding = $self->_get_number('range_padding');
262   if (defined $config_min) {
263     $min_value = $config_min;
264   }
265   else {
266     if ($min_value > 0) {
267       $min_value = 0;
268     }
269     if ($range_padding && $min_value < 0) {
270       my $difference = $min_value * $range_padding / 100;
271       if ($min_value < -1 && $difference > -1) {
272         $difference = -1;
273       }
274       $min_value += $difference;
275     }
276   }
277   if (defined $config_max) {
278     $max_value = $config_max;
279   }
280   else {
281     if ($range_padding && $max_value > 0) {
282       my $difference = $max_value * $range_padding / 100;
283       if ($max_value > 1 && $difference < 1) {
284         $difference = 1;
285       }
286       $max_value += $difference;
287     }
288   }
289
290   if ($self->_get_number('automatic_axis')) {
291     # In case this was set via a style, and not by the api method
292     eval { require Chart::Math::Axis; };
293     if ($@) {
294       return $self->_error("Can't use automatic_axis - $@");
295     }
296
297     my $axis = Chart::Math::Axis->new();
298     $axis->include_zero();
299     $axis->add_data($min_value, $max_value);
300     $max_value = $axis->top;
301     $min_value = $axis->bottom;
302     my $ticks     = $axis->ticks;
303     # The +1 is there because we have the bottom tick as well
304     $self->set_x_tics($ticks+1);
305   }
306
307   $self->_set_max_value($max_value);
308   $self->_set_min_value($min_value);
309   $self->_set_column_count($column_count);
310
311   return 1;
312 }
313
314 sub _min {
315   my $self = shift;
316   my $min = shift;
317
318   foreach my $value (@_) {
319     next unless defined $value;
320     if ($value < $min) { $min = $value; }
321   }
322   return $min;
323 }
324
325 sub _max {
326   my $self = shift;
327   my $min = shift;
328
329   foreach my $value (@_) {
330     next unless defined $value;
331     if ($value > $min) { $min = $value; }
332   }
333   return $min;
334 }
335
336 sub _get_line_range {
337   my $self = shift;
338   my $series = $self->_get_data_series()->{'line'};
339   return (undef, undef, 0) unless $series;
340
341   my $max_value = 0;
342   my $min_value = STARTING_MIN_VALUE;
343   my $column_count = 0;
344
345   my @series = @{$series};
346   foreach my $series (@series) {
347     my @data = @{$series->{'data'}};
348
349     if (scalar @data > $column_count) {
350       $column_count = scalar @data;
351     }
352
353     foreach my $value (@data) {
354       if ($value > $max_value) { $max_value = $value; }
355       if ($value < $min_value) { $min_value = $value; }
356     }
357   }
358
359   return ($min_value, $max_value, $column_count);
360 }
361
362
363
364 sub _get_bar_range {
365   my $self = shift;
366
367   my $series = $self->_get_data_series()->{'bar'};
368   return (undef, undef, 0) unless $series;
369
370   my $max_value = 0;
371   my $min_value = STARTING_MIN_VALUE;
372   my $column_count = 0;
373
374   my @series = @{$series};
375   foreach my $series (@series) {
376     my @data = @{$series->{'data'}};
377
378     foreach my $value (@data) {
379       $column_count++;
380       if ($value > $max_value) { $max_value = $value; }
381       if ($value < $min_value) { $min_value = $value; }
382     }
383   }
384
385   return ($min_value, $max_value, $column_count);
386 }
387
388
389 sub _draw_legend {
390   my $self = shift;
391   my $chart_box = shift;
392   my $style = $self->{'_style'};
393
394   my @labels;
395   my $img = $self->_get_image();
396   if (my $series = $self->_get_data_series()->{'bar'}) {
397     push @labels, map { $_->{'series_name'} } @$series;
398   }
399
400   if ($style->{features}{legend} && (scalar @labels)) {
401     $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
402       or return;
403   }
404   return;
405 }
406
407 sub _draw_flat_legend {
408   return 1;
409 }
410
411 sub _draw_lines {
412   my $self = shift;
413   my $style = $self->{'_style'};
414
415   my $img = $self->_get_image();
416
417   my $max_value = $self->_get_max_value();
418   my $min_value = $self->_get_min_value();
419   my $column_count = $self->_get_column_count();
420
421   my $value_range = $max_value - $min_value;
422
423   my $width = $self->_get_number('width');
424   my $height = $self->_get_number('height');
425
426   my $graph_width = $self->_get_number('graph_width');
427   my $graph_height = $self->_get_number('graph_height');
428
429   my $line_series = $self->_get_data_series()->{'line'};
430   my $series_counter = $self->_get_series_counter() || 0;
431
432   my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
433
434   my $col_height = int($graph_height / $column_count) -1;
435
436   my $graph_box = $self->_get_graph_box();
437   my $left = $graph_box->[0] + 1;
438   my $bottom = $graph_box->[1];
439
440   my $zero_position =  $left + $graph_width - (-1*$min_value / $value_range) * ($graph_width - 1);
441
442   my $line_aa = $self->_get_number("lineaa");
443   foreach my $series (@$line_series) {
444     my @data = @{$series->{'data'}};
445     my $data_size = scalar @data;
446
447     my $interval;
448     if ($has_columns) {
449       $interval = $graph_height / ($data_size);
450     }
451     else {
452       $interval = $graph_height / ($data_size - 1);
453     }
454     my $color = $self->_data_color($series_counter);
455
456     # We need to add these last, otherwise the next line segment will overwrite half of the marker
457     my @marker_positions;
458     for (my $i = 0; $i < $data_size - 1; $i++) {
459       my $y1 = $bottom + $i * $interval;
460       my $y2 = $bottom + ($i + 1) * $interval;
461
462       $y1 += $has_columns * $interval / 2;
463       $y2 += $has_columns * $interval / 2;
464
465       my $x1 = $left + ($value_range - $data[$i] + $min_value)/$value_range * $graph_width;
466       my $x2 = $left + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_width;
467
468       push @marker_positions, [$x1, $y1];
469       $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
470     }
471
472
473     my $y2 = $bottom + ($data_size - 1) * $interval;
474     $y2 += $has_columns * $interval / 2;
475
476     my $x2 = $left + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_width;
477
478     if ($self->_feature_enabled("linemarkers")) {
479       push @marker_positions, [$x2, $y2];
480       foreach my $position (@marker_positions) {
481         $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
482       }
483     }
484     $series_counter++;
485   }
486
487   $self->_set_series_counter($series_counter);
488   return;
489 }
490
491 sub _draw_bars {
492   my $self = shift;
493   my $style = $self->{'_style'};
494
495   my $img = $self->_get_image();
496
497   my $max_value = $self->_get_max_value();
498   my $min_value = $self->_get_min_value();
499   my $column_count = $self->_get_column_count();
500
501   my $value_range = $max_value - $min_value;
502
503   my $width = $self->_get_number('width');
504   my $height = $self->_get_number('height');
505
506   my $graph_width = $self->_get_number('graph_width');
507   my $graph_height = $self->_get_number('graph_height');
508
509
510   my $graph_box = $self->_get_graph_box();
511   my $bottom = $graph_box->[1] + 1;
512   my $left  = $graph_box->[0];
513
514   my $zero_position =  int($left + (-1*$min_value / $value_range) * ($graph_width-1));
515
516   my $bar_height = $graph_height / $column_count;
517
518   my $outline_color;
519   if ($style->{'features'}{'outline'}) {
520     $outline_color = $self->_get_color('outline.line');
521   }
522
523   my $series_counter = $self->_get_series_counter() || 0;
524   my $col_series = $self->_get_data_series()->{'bar'};
525   my $column_padding = $self->_get_number('column_padding') || 0;
526
527   # This tracks the series we're in relative to the starting series - this way colors stay accurate, but the columns don't start out too far to the right.
528   my $column_series = 0;
529
530   for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
531     my $series = $col_series->[$series_pos];
532     my @data = @{$series->{'data'}};
533     my $data_size = scalar @data;
534     for (my $i = 0; $i < $data_size; $i++) {
535
536       my $part1 = $bar_height * (scalar @$col_series * $i);
537       my $part2 = ($series_pos) * $bar_height;
538       my $y1 = int($bottom + $part1 + $part2);
539
540       my $y2 = int($y1 + $bar_height - $column_padding)-1;
541       # Special case for when bar_height is less than 1.
542       if ($y2 < $y1) {
543         $y2 = $y1;
544       }
545
546       my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
547
548       my $color = $self->_data_color($series_counter);
549
550       if ($data[$i] > 0) {
551         my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
552         $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
553         if ($style->{'features'}{'outline'}) {
554           $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
555         }
556       }
557       elsif ($data[$i] == 0) {
558       }
559       else {
560         my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
561         $img->box(xmax  => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
562         if ($style->{'features'}{'outline'}) {
563           $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
564         }
565       }
566     }
567
568     $series_counter++;
569     $column_series++;
570   }
571   $self->_set_series_counter($series_counter);
572   return;
573 }
574
575 sub _add_data_series {
576   my $self = shift;
577   my $series_type = shift;
578   my $data_ref = shift;
579   my $series_name = shift;
580
581   my $graph_data = $self->{'graph_data'} || {};
582
583   my $series = $graph_data->{$series_type} || [];
584
585   push @$series, { data => $data_ref, series_name => $series_name };
586
587   $graph_data->{$series_type} = $series;
588
589   $self->{'graph_data'} = $graph_data;
590   return;
591 }
592
593 =item show_vertical_gridlines()
594
595 Shows vertical gridlines at the y-tics.
596
597 Feature: vertical_gridlines
598
599 =cut
600
601 sub show_vertical_gridlines {
602     $_[0]->{'custom_style'}{features}{'vertical_gridlines'} = 1;
603 }
604
605 =item set_vertical_gridline_style(color => ..., style => ...)
606
607 Set the color and style of the lines drawn for gridlines.
608
609 Style equivalent: vgrid
610
611 =cut
612
613 sub set_vertical_gridline_style {
614   my ($self, %opts) = @_;
615
616   $self->{custom_style}{vgrid} ||= {};
617   @{$self->{custom_style}{vgrid}}{keys %opts} = values %opts;
618
619   return 1;
620 }
621
622 =item show_line_markers()
623
624 =item show_line_markers($value)
625
626 Feature: linemarkers.
627
628 If $value is missing or true, draw markers on a line data series.
629
630 Note: line markers are drawn by default.
631
632 =cut
633
634 sub show_line_markers {
635   my ($self, $value) = @_;
636
637   @_ > 1 or $value = 1;
638
639   $self->{custom_style}{features}{linemarkers} = $value;
640
641   return 1;
642 }
643
644 =item use_automatic_axis()
645
646 Automatically scale the Y axis, based on L<Chart::Math::Axis>.  If Chart::Math::Axis isn't installed, this sets an error and returns undef.  Returns 1 if it is installed.
647
648 =cut
649
650 sub use_automatic_axis {
651   eval { require Chart::Math::Axis; };
652   if ($@) {
653     return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
654   }
655   $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
656   return 1;
657 }
658
659
660 =item set_x_tics($count)
661
662 Set the number of X tics to use.  Their value and position will be determined by the data range.
663
664 =cut
665
666 sub set_x_tics {
667   $_[0]->{'x_tics'} = $_[1];
668 }
669
670 sub _get_x_tics {
671   return $_[0]->{'x_tics'} || 0;
672 }
673
674 sub _remove_tics_from_chart_box {
675   my ($self, $chart_box, $opts) = @_;
676
677   # XXX - bad default
678   my $tic_width = $self->_get_y_tic_width($opts) || 10;
679   my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
680
681   # XXX - bad default
682   my $tic_height = $self->_get_x_tic_height() || 10;
683   my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
684
685   $self->_remove_box($chart_box, \@y_tic_box);
686   $self->_remove_box($chart_box, \@x_tic_box);
687
688   # If there's no title, the y-tics will be part off-screen.  Half of the x-tic height should be more than sufficient.
689   my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
690   $self->_remove_box($chart_box, \@y_tic_tops);
691
692     if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) {
693       my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
694                         $chart_box->[1],
695                         $chart_box->[2],
696                         $chart_box->[3]
697                         );
698
699       $self->_remove_box($chart_box, \@remove_box);
700     }
701
702
703 }
704
705 sub _get_y_tic_width {
706   my ($self, $opts) = @_;
707
708   my $labels = $self->_get_labels($opts);
709
710   if (!$labels) {
711     return;
712   }
713
714   my %text_info = $self->_text_style('legend')
715     or return;
716
717   my $max_width = 0;
718   foreach my $label (@$labels) {
719     my @box = $self->_text_bbox($label, 'legend');
720     my $width = $box[2] + 5;
721     # For the tic itself...
722     $width += 10;
723     if ($width > $max_width) {
724       $max_width = $width;
725     }
726   }
727   return $max_width;
728 }
729
730 sub _get_x_tic_height {
731   my $self = shift;
732
733   my $min = $self->_get_min_value();
734   my $max = $self->_get_max_value();
735   my $tic_count = $self->_get_x_tics();
736
737   my $interval = ($max - $min) / ($tic_count - 1);
738
739   my %text_info = $self->_text_style('legend')
740     or return;
741
742   my $max_height = 0;
743   for my $count (0 .. $tic_count - 1) {
744     my $value = sprintf("%.2f", ($count*$interval)+$min);
745
746     my @box = $self->_text_bbox($value, 'legend');
747     my $height = $box[3] - $box[1];
748
749     # For the tic width
750     $height += 10;
751     if ($height > $max_height) {
752       $max_height = $height;
753     }
754   }
755
756
757   return $max_height;
758 }
759
760 sub _draw_y_tics {
761   my ($self, $opts) = @_;
762
763   my $img = $self->_get_image();
764   my $graph_box = $self->_get_graph_box();
765   my $image_box = $self->_get_image_box();
766
767   my $labels = $self->_get_labels($opts);
768
769   my $tic_count = (scalar @$labels) - 1;
770
771   my $has_columns = defined $self->_get_data_series()->{'bar'};
772
773   # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
774   my $denominator = $tic_count;
775   if ($has_columns) {
776     $denominator ++;
777   }
778   my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
779   my %text_info = $self->_text_style('legend')
780     or return;
781
782   for my $count (0 .. $tic_count) {
783     my $label = $labels->[$count];
784
785     my $x1 = $graph_box->[0] - 5;
786     my $x2 = $graph_box->[0] + 5;
787
788     my $y1 = $graph_box->[1] + ($tic_distance * $count);
789
790     if ($has_columns) {
791       $y1 += $tic_distance / 2;
792     }
793
794     $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
795
796     my @box = $self->_text_bbox($label, 'legend')
797       or return;
798
799     my $width = $box[2];
800     my $height = $box[3];
801
802     $img->string(%text_info,
803                  x    => ($x1 - ($width + 5)),
804                  y    => ($y1 + ($height / 2)),
805                  text => $label
806                 );
807
808   }
809
810 }
811
812 sub _draw_x_tics {
813   my $self = shift;
814
815   my $img = $self->_get_image();
816   my $graph_box = $self->_get_graph_box();
817   my $image_box = $self->_get_image_box();
818
819   my $tic_count = $self->_get_x_tics();
820   my $min = $self->_get_min_value();
821   my $max = $self->_get_max_value();
822   my $interval = ($max - $min) / ($tic_count - 1);
823
824   # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
825   my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
826
827   my %text_info = $self->_text_style('legend')
828     or return;
829
830   my $show_gridlines = $self->{_style}{features}{'vertical_gridlines'};
831   my @grid_line = $self->_get_line("vgrid");
832   for my $count (0 .. $tic_count-1) {
833     my $x1 = $graph_box->[0] + ($tic_distance * $count);
834
835     my $y1 = $graph_box->[3] + 5;
836     my $y2 = $graph_box->[3] - 5;
837
838     my $value = ($count*$interval)+$min;
839
840     $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
841
842     my @box = $self->_text_bbox($value, 'legend')
843       or return;
844
845     my $width = $box[2];
846     my $height = $box[3];
847
848     $img->string(%text_info,
849                  x    => ($x1 - ($width / 2)),
850                  y    => ($y1 + $height + 5),
851                  text => $value
852                 );
853
854     if ($show_gridlines && $x1 != $graph_box->[0] && $x1 != $graph_box->[2]) {
855       $self->_line(x1 => $x1, x2 => $x1,
856                    y1 => $graph_box->[1], y2 => $graph_box->[3],
857                    img => $img,
858                    @grid_line);
859     }
860   }
861 }
862
863 sub _valid_input {
864   my $self = shift;
865
866   if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
867     return $self->_error("No data supplied");
868   }
869
870   my $data = $self->_get_data_series();
871   if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
872     return $self->_error("No values in data series");
873   }
874   if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
875     return $self->_error("No values in data series");
876   }
877   if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
878     return $self->_error("No values in data series");
879   }
880
881   return 1;
882 }
883
884 sub _set_column_count   { $_[0]->{'column_count'} = $_[1]; }
885 sub _set_min_value      { $_[0]->{'min_value'} = $_[1]; }
886 sub _set_max_value      { $_[0]->{'max_value'} = $_[1]; }
887 sub _set_image_box      { $_[0]->{'image_box'} = $_[1]; }
888 sub _set_graph_box      { $_[0]->{'graph_box'} = $_[1]; }
889 sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
890 sub _get_column_count   { return $_[0]->{'column_count'} }
891 sub _get_min_value      { return $_[0]->{'min_value'} }
892 sub _get_max_value      { return $_[0]->{'max_value'} }
893 sub _get_image_box      { return $_[0]->{'image_box'} }
894 sub _get_graph_box      { return $_[0]->{'graph_box'} }
895 sub _reset_series_counter { $_[0]->{series_counter} = 0 }
896 sub _get_series_counter { return $_[0]->{'series_counter'} }
897
898 sub _style_defs {
899   my ($self) = @_;
900
901   my %work = %{$self->SUPER::_style_defs()};
902   push @{$work{features}}, qw/graph_outline graph_fill linemarkers/;
903   $work{vgrid} =
904     {
905      color => "lookup(fg)",
906      style => "solid",
907     };
908
909   return \%work;
910 }
911
912 sub _composite {
913   my ($self) = @_;
914   return ( $self->SUPER::_composite(), "graph", "vgrid" );
915 }
916
917 1;
918
919 =back
920
921 =head1 AUTHOR
922
923 Patrick Michaud, Tony Cook
924
925 =cut