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