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