- Imager::Graph::Horizontal::add_column_data_series() renamed to
[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
473sub _line_marker {
474 my ($self, $index) = @_;
475
476 my $markers = $self->{'_style'}{'line_markers'};
477 if (!$markers) {
478 return;
479 }
480 my $marker = $markers->[$index % @$markers];
481
482 return $marker;
483}
484
485sub _draw_line_marker {
486 my $self = shift;
487 my ($x1, $y1, $series_counter) = @_;
488
489 my $img = $self->_get_image();
490
491 my $style = $self->_line_marker($series_counter);
492 return unless $style;
493
494 my $type = $style->{'shape'};
495 my $radius = $style->{'radius'};
496
497 my $line_aa = $self->_get_number("lineaa");
498 my $fill_aa = $self->_get_number("fill.aa");
499 if ($type eq 'circle') {
500 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
501 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
502 }
503 elsif ($type eq 'square') {
504 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
505 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
506 }
507 elsif ($type eq 'diamond') {
508 # The gradient really doesn't work for diamond
509 my $color = $self->_data_color($series_counter);
510 $img->polygon(
511 points => [
512 [$x1 - $radius, $y1],
513 [$x1, $y1 + $radius],
514 [$x1 + $radius, $y1],
515 [$x1, $y1 - $radius],
516 ],
517 filled => 1, color => $color, aa => $fill_aa);
518 }
519 elsif ($type eq 'triangle') {
520 # The gradient really doesn't work for triangle
521 my $color = $self->_data_color($series_counter);
522 $img->polygon(
523 points => [
524 [$x1 - $radius, $y1 + $radius],
525 [$x1 + $radius, $y1 + $radius],
526 [$x1, $y1 - $radius],
527 ],
528 filled => 1, color => $color, aa => $fill_aa);
529
530 }
531 elsif ($type eq 'x') {
532 my $color = $self->_data_color($series_counter);
533 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
534 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
535 }
536 elsif ($type eq 'plus') {
537 my $color = $self->_data_color($series_counter);
538 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
539 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
540 }
541}
542
543sub _draw_bars {
544 my $self = shift;
545 my $style = $self->{'_style'};
546
547 my $img = $self->_get_image();
548
549 my $max_value = $self->_get_max_value();
550 my $min_value = $self->_get_min_value();
551 my $column_count = $self->_get_column_count();
552
553 my $value_range = $max_value - $min_value;
554
555 my $width = $self->_get_number('width');
556 my $height = $self->_get_number('height');
557
558 my $graph_width = $self->_get_number('graph_width');
559 my $graph_height = $self->_get_number('graph_height');
560
561
562 my $graph_box = $self->_get_graph_box();
563 my $bottom = $graph_box->[1] + 1;
564 my $left = $graph_box->[0];
565
566 my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1));
567
95289584 568 my $bar_height = $graph_height / $column_count;
f0b01a81 569
570 my $outline_color;
571 if ($style->{'features'}{'outline'}) {
572 $outline_color = $self->_get_color('outline.line');
573 }
574
575 my $series_counter = $self->_get_series_counter() || 0;
576 my $col_series = $self->_get_data_series()->{'bar'};
577 my $column_padding = $self->_get_number('column_padding') || 0;
578
579 # 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.
580 my $column_series = 0;
581
582 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
583 my $series = $col_series->[$series_pos];
584 my @data = @{$series->{'data'}};
585 my $data_size = scalar @data;
f0b01a81 586 for (my $i = 0; $i < $data_size; $i++) {
587
95289584 588 my $part1 = $bar_height * (scalar @$col_series * $i);
589 my $part2 = ($series_pos) * $bar_height;
590 my $y1 = int($bottom + $part1 + $part2);
f0b01a81 591
95289584 592 my $y2 = int($y1 + $bar_height - $column_padding)-1;
593 # Special case for when bar_height is less than 1.
594 if ($y2 < $y1) {
595 $y2 = $y1;
596 }
f0b01a81 597
598 my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width);
599
600 my $color = $self->_data_color($series_counter);
601
f0b01a81 602 if ($data[$i] > 0) {
7422650e 603 my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]);
604 $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill);
f0b01a81 605 if ($style->{'features'}{'outline'}) {
606 $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color);
607 }
608 }
609 elsif ($data[$i] == 0) {
610 }
611 else {
7422650e 612 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]);
613 $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill);
f0b01a81 614 if ($style->{'features'}{'outline'}) {
615 $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color);
616 }
617 }
618 }
619
620 $series_counter++;
621 $column_series++;
622 }
623 $self->_set_series_counter($series_counter);
624 return;
625}
626
627sub _add_data_series {
628 my $self = shift;
629 my $series_type = shift;
630 my $data_ref = shift;
631 my $series_name = shift;
632
633 my $graph_data = $self->{'graph_data'} || {};
634
635 my $series = $graph_data->{$series_type} || [];
636
637 push @$series, { data => $data_ref, series_name => $series_name };
638
639 $graph_data->{$series_type} = $series;
640
641 $self->{'graph_data'} = $graph_data;
642 return;
643}
644
645=over
646
647=item show_vertical_gridlines()
648
649Shows vertical gridlines at the y-tics.
650
86f73dc2
TC
651Feature: vertical_gridlines
652
f0b01a81 653=cut
654
655sub show_vertical_gridlines {
56b495c0 656 $_[0]->{'custom_style'}{features}{'vertical_gridlines'} = 1;
f0b01a81 657}
658
86f73dc2
TC
659=item set_vertical_gridline_style(color => ..., style => ...)
660
661Set the color and style of the lines drawn for gridlines.
662
663Style equivalent: vgrid
664
665=cut
666
667sub set_vertical_gridline_style {
668 my ($self, %opts) = @_;
669
670 $self->{custom_style}{vgrid} ||= {};
671 @{$self->{custom_style}{vgrid}}{keys %opts} = values %opts;
672
673 return 1;
674}
675
f0b01a81 676=item use_automatic_axis()
677
678Automatically 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.
679
680=cut
681
682sub use_automatic_axis {
683 eval { require Chart::Math::Axis; };
684 if ($@) {
685 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
686 }
687 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
688 return 1;
689}
690
691
692=item set_x_tics($count)
693
694Set the number of X tics to use. Their value and position will be determined by the data range.
695
696=cut
697
698sub set_x_tics {
699 $_[0]->{'x_tics'} = $_[1];
700}
701
702sub _get_x_tics {
703 return $_[0]->{'x_tics'} || 0;
704}
705
706sub _remove_tics_from_chart_box {
86f73dc2 707 my ($self, $chart_box, $opts) = @_;
f0b01a81 708
709 # XXX - bad default
86f73dc2 710 my $tic_width = $self->_get_y_tic_width($opts) || 10;
f0b01a81 711 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
712
713 # XXX - bad default
714 my $tic_height = $self->_get_x_tic_height() || 10;
715 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
716
717 $self->_remove_box($chart_box, \@y_tic_box);
718 $self->_remove_box($chart_box, \@x_tic_box);
719
720 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
721 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
722 $self->_remove_box($chart_box, \@y_tic_tops);
723
267997be 724 if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) {
725 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
726 $chart_box->[1],
727 $chart_box->[2],
728 $chart_box->[3]
729 );
730
731 $self->_remove_box($chart_box, \@remove_box);
732 }
733
734
f0b01a81 735}
736
737sub _get_y_tic_width {
86f73dc2 738 my ($self, $opts) = @_;
f0b01a81 739
86f73dc2 740 my $labels = $self->_get_labels($opts);
43c5d51b 741
742 if (!$labels) {
743 return;
744 }
745
746 my %text_info = $self->_text_style('legend')
747 or return;
748
f0b01a81 749 my $max_width = 0;
750 foreach my $label (@$labels) {
751 my @box = $self->_text_bbox($label, 'legend');
752 my $width = $box[2] + 5;
753 # For the tic itself...
754 $width += 10;
755 if ($width > $max_width) {
756 $max_width = $width;
757 }
758 }
759 return $max_width;
760}
761
762sub _get_x_tic_height {
763 my $self = shift;
764
765 my $min = $self->_get_min_value();
766 my $max = $self->_get_max_value();
767 my $tic_count = $self->_get_x_tics();
768
769 my $interval = ($max - $min) / ($tic_count - 1);
770
771 my %text_info = $self->_text_style('legend')
772 or return;
773
774 my $max_height = 0;
775 for my $count (0 .. $tic_count - 1) {
776 my $value = sprintf("%.2f", ($count*$interval)+$min);
777
778 my @box = $self->_text_bbox($value, 'legend');
779 my $height = $box[3] - $box[1];
780
781 # For the tic width
782 $height += 10;
783 if ($height > $max_height) {
784 $max_height = $height;
785 }
786 }
787
788
789 return $max_height;
790}
791
792sub _draw_y_tics {
86f73dc2 793 my ($self, $opts) = @_;
f0b01a81 794
795 my $img = $self->_get_image();
796 my $graph_box = $self->_get_graph_box();
797 my $image_box = $self->_get_image_box();
798
86f73dc2 799 my $labels = $self->_get_labels($opts);
f0b01a81 800
801 my $tic_count = (scalar @$labels) - 1;
802
803 my $has_columns = defined $self->_get_data_series()->{'bar'};
804
805 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
806 my $denominator = $tic_count;
807 if ($has_columns) {
808 $denominator ++;
809 }
810 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator);
811 my %text_info = $self->_text_style('legend')
812 or return;
813
814 for my $count (0 .. $tic_count) {
815 my $label = $labels->[$count];
816
817 my $x1 = $graph_box->[0] - 5;
818 my $x2 = $graph_box->[0] + 5;
819
820 my $y1 = $graph_box->[1] + ($tic_distance * $count);
821
822 if ($has_columns) {
823 $y1 += $tic_distance / 2;
824 }
825
826 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000');
827
828 my @box = $self->_text_bbox($label, 'legend')
829 or return;
830
831 my $width = $box[2];
832 my $height = $box[3];
833
834 $img->string(%text_info,
835 x => ($x1 - ($width + 5)),
836 y => ($y1 + ($height / 2)),
837 text => $label
838 );
839
840 }
841
842}
843
844sub _draw_x_tics {
845 my $self = shift;
846
847 my $img = $self->_get_image();
848 my $graph_box = $self->_get_graph_box();
849 my $image_box = $self->_get_image_box();
850
851 my $tic_count = $self->_get_x_tics();
852 my $min = $self->_get_min_value();
853 my $max = $self->_get_max_value();
854 my $interval = ($max - $min) / ($tic_count - 1);
855
856 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
857 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1);
858
859 my %text_info = $self->_text_style('legend')
860 or return;
861
56b495c0
TC
862 my $show_gridlines = $self->{_style}{features}{'vertical_gridlines'};
863 my @grid_line = $self->_get_line("vgrid");
f0b01a81 864 for my $count (0 .. $tic_count-1) {
865 my $x1 = $graph_box->[0] + ($tic_distance * $count);
866
867 my $y1 = $graph_box->[3] + 5;
868 my $y2 = $graph_box->[3] - 5;
869
870 my $value = ($count*$interval)+$min;
871
872 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000');
873
874 my @box = $self->_text_bbox($value, 'legend')
875 or return;
876
877 my $width = $box[2];
878 my $height = $box[3];
879
880 $img->string(%text_info,
881 x => ($x1 - ($width / 2)),
882 y => ($y1 + $height + 5),
883 text => $value
884 );
885
56b495c0
TC
886 if ($show_gridlines && $x1 != $graph_box->[0] && $x1 != $graph_box->[2]) {
887 $self->_line(x1 => $x1, x2 => $x1,
86f73dc2 888 y1 => $graph_box->[1], y2 => $graph_box->[3],
56b495c0
TC
889 img => $img,
890 @grid_line);
f0b01a81 891 }
892 }
893}
894
895sub _valid_input {
896 my $self = shift;
897
898 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
899 return $self->_error("No data supplied");
900 }
901
902 my $data = $self->_get_data_series();
903 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
904 return $self->_error("No values in data series");
905 }
906 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
907 return $self->_error("No values in data series");
908 }
909 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
910 return $self->_error("No values in data series");
911 }
912
913 return 1;
914}
915
916sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
917sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
918sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
919sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
920sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
921sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
922sub _get_column_count { return $_[0]->{'column_count'} }
923sub _get_min_value { return $_[0]->{'min_value'} }
924sub _get_max_value { return $_[0]->{'max_value'} }
925sub _get_image_box { return $_[0]->{'image_box'} }
926sub _get_graph_box { return $_[0]->{'graph_box'} }
86f73dc2 927sub _reset_series_counter { $_[0]->{series_counter} = 0 }
f0b01a81 928sub _get_series_counter { return $_[0]->{'series_counter'} }
929
86f73dc2
TC
930sub _style_defs {
931 my ($self) = @_;
932
933 my %work = %{$self->SUPER::_style_defs()};
934 push @{$work{features}}, qw/graph_outline graph_fill/;
935 $work{vgrid} =
936 {
937 color => "lookup(fg)",
938 style => "solid",
939 };
940
941 return \%work;
942}
943
56b495c0
TC
944sub _composite {
945 my ($self) = @_;
946 return ( $self->SUPER::_composite(), "graph", "vgrid" );
947}
948
f0b01a81 9491;
950