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