]> git.imager.perl.org - imager-graph.git/blame - lib/Imager/Graph/Vertical.pm
allow the outline and background of the graph data area to be
[imager-graph.git] / lib / Imager / Graph / Vertical.pm
CommitLineData
2eac77fc 1package Imager::Graph::Vertical;
2
3=head1 NAME
4
5 Imager::Graph::Vertical- A super class for line/bar/column charts
6
7=cut
8
9use strict;
10use vars qw(@ISA);
11use Imager::Graph;
12@ISA = qw(Imager::Graph);
13
23a3585a 14use constant STARTING_MIN_VALUE => 99999;
f94f373e 15
2eac77fc 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_stacked_column_data_series(\@data, $series_name)
52
53Add a stacked column data series to the graph.
54
55=cut
56
57sub add_stacked_column_data_series {
58 my $self = shift;
59 my $data_ref = shift;
60 my $series_name = shift;
61
62 $self->_add_data_series('stacked_column', $data_ref, $series_name);
63
64 return;
65}
66
67=item add_line_data_series(\@data, $series_name)
68
69Add a line data series to the graph.
70
71=cut
72
73sub add_line_data_series {
74 my $self = shift;
75 my $data_ref = shift;
76 my $series_name = shift;
77
78 $self->_add_data_series('line', $data_ref, $series_name);
79
80 return;
81}
82
e2c4cb9a 83=item add_area_data_series(\@data, $series_name)
84
85Add a area data series to the graph.
86
87=cut
88
89sub add_area_data_series {
90 my $self = shift;
91 my $data_ref = shift;
92 my $series_name = shift;
93
94 $self->_add_data_series('area', $data_ref, $series_name);
95
96 return;
97}
98
99
23a3585a
TC
100=item set_y_max($value)
101
102Sets the maximum y value to be displayed. This will be ignored if the y_max is lower than the highest value.
103
104=cut
105
106sub set_y_max {
107 $_[0]->{'custom_style'}->{'y_max'} = $_[1];
108}
109
110=item set_y_min($value)
111
112Sets the minimum y value to be displayed. This will be ignored if the y_min is higher than the lowest value.
113
114=cut
115
116sub set_y_min {
117 $_[0]->{'custom_style'}->{'y_min'} = $_[1];
118}
119
488bc70c 120=item set_column_padding($int)
121
122Sets the padding between columns. This is a percentage of the column width. Defaults to 0.
123
124=cut
125
126sub set_column_padding {
127 $_[0]->{'custom_style'}->{'column_padding'} = $_[1];
128}
129
2eac77fc 130=item set_range_padding($percentage)
131
132Sets the padding to be used, as a percentage. For example, if your data ranges from 0 to 10, and you have a 20 percent padding, the y axis will go to 12.
133
23a3585a 134Defaults to 10. This attribute is ignored for positive numbers if set_y_max() has been called, and ignored for negative numbers if set_y_min() has been called.
2eac77fc 135
136=cut
137
138sub set_range_padding {
139 $_[0]->{'custom_style'}->{'range_padding'} = $_[1];
140}
141
23a3585a
TC
142=item set_negative_background($color)
143
144Sets the background color used below the x axis.
145
146=cut
147
148sub set_negative_background {
149 $_[0]->{'custom_style'}->{'negative_bg'} = $_[1];
150}
151
2eac77fc 152=item draw()
153
154Draw the graph
155
156=cut
157
158sub draw {
159 my ($self, %opts) = @_;
160
161 if (!$self->_valid_input()) {
162 return;
163 }
164
165 $self->_style_setup(\%opts);
166
167 my $style = $self->{_style};
168
169 my $img = $self->_get_image()
170 or return;
171
172 my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
173 $self->_set_image_box(\@image_box);
174
c2c2cb9e 175 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
176 $self->_draw_legend(\@chart_box);
177 if ($style->{title}{text}) {
178 $self->_draw_title($img, \@chart_box)
179 or return;
180 }
181
2eac77fc 182 # Scale the graph box down to the widest graph that can cleanly hold the # of columns.
e554917f 183 return unless $self->_get_data_range();
c2c2cb9e 184 $self->_remove_tics_from_chart_box(\@chart_box);
2eac77fc 185 my $column_count = $self->_get_column_count();
186
187 my $width = $self->_get_number('width');
188 my $height = $self->_get_number('height');
2eac77fc 189
c2c2cb9e 190 my $graph_width = $chart_box[2] - $chart_box[0];
191 my $graph_height = $chart_box[3] - $chart_box[1];
2eac77fc 192
1509eee7 193 my $col_width = ($graph_width - 1) / $column_count;
267997be 194 if ($col_width > 1) {
195 $graph_width = int($col_width) * $column_count + 1;
196 }
197 else {
198 $graph_width = $col_width * $column_count + 1;
199 }
2eac77fc 200
c2c2cb9e 201 my $tic_count = $self->_get_y_tics();
1509eee7 202 my $tic_distance = ($graph_height-1) / ($tic_count - 1);
203 $graph_height = int($tic_distance * ($tic_count - 1));
c2c2cb9e 204
1a36ef75
TC
205 my $top = $chart_box[1];
206 my $left = $chart_box[0];
2eac77fc 207
c2c2cb9e 208 $self->{'_style'}{'graph_width'} = $graph_width;
209 $self->{'_style'}{'graph_height'} = $graph_height;
210
1a36ef75 211 my @graph_box = ($left, $top, $left + $graph_width, $top + $graph_height);
c2c2cb9e 212 $self->_set_graph_box(\@graph_box);
2eac77fc 213
1a36ef75
TC
214 my @fill_box = ( $left, $top, $left+$graph_width, $top+$graph_height );
215 if ($self->{_style}{features}{graph_outline}) {
216 $img->box(
217 color => $self->_get_color('graph.outline'),
218 xmin => $left,
219 xmax => $left+$graph_width,
220 ymin => $top,
221 ymax => $top+$graph_height,
222 );
223 ++$fill_box[0];
224 ++$fill_box[1];
225 --$fill_box[2];
226 --$fill_box[3];
227 }
2eac77fc 228
229 $img->box(
1a36ef75
TC
230 $self->_get_fill('graph.fill'),
231 box => \@fill_box,
2eac77fc 232 );
233
234 my $min_value = $self->_get_min_value();
235 my $max_value = $self->_get_max_value();
236 my $value_range = $max_value - $min_value;
237
238 my $zero_position;
239 if ($value_range) {
1a36ef75 240 $zero_position = $top + $graph_height - (-1*$min_value / $value_range) * ($graph_height-1);
2eac77fc 241 }
242
243 if ($min_value < 0) {
244 $img->box(
245 color => $self->_get_color('negative_bg'),
246 xmin => $left + 1,
247 xmax => $left+$graph_width- 1,
248 ymin => $zero_position,
1a36ef75 249 ymax => $top+$graph_height - 1,
2eac77fc 250 filled => 1,
251 );
252 $img->line(
253 x1 => $left+1,
254 y1 => $zero_position,
255 x2 => $left + $graph_width,
256 y2 => $zero_position,
257 color => $self->_get_color('outline.line'),
258 );
259 }
260
261 if ($self->_get_data_series()->{'stacked_column'}) {
488bc70c 262 return unless $self->_draw_stacked_columns();
2eac77fc 263 }
264 if ($self->_get_data_series()->{'column'}) {
488bc70c 265 return unless $self->_draw_columns();
2eac77fc 266 }
267 if ($self->_get_data_series()->{'line'}) {
488bc70c 268 return unless $self->_draw_lines();
2eac77fc 269 }
e2c4cb9a 270 if ($self->_get_data_series()->{'area'}) {
271 return unless $self->_draw_area();
272 }
c2c2cb9e 273
274 if ($self->_get_y_tics()) {
275 $self->_draw_y_tics();
276 }
277 if ($self->_get_labels()) {
278 $self->_draw_x_tics();
279 }
280
2eac77fc 281 return $self->_get_image();
282}
283
284sub _get_data_range {
285 my $self = shift;
286
287 my $max_value = 0;
288 my $min_value = 0;
289 my $column_count = 0;
290
291 my ($sc_min, $sc_max, $sc_cols) = $self->_get_stacked_column_range();
292 my ($c_min, $c_max, $c_cols) = $self->_get_column_range();
293 my ($l_min, $l_max, $l_cols) = $self->_get_line_range();
e2c4cb9a 294 my ($a_min, $a_max, $a_cols) = $self->_get_area_range();
2eac77fc 295
296 # These are side by side...
297 $sc_cols += $c_cols;
298
e2c4cb9a 299 $min_value = $self->_min(STARTING_MIN_VALUE, $sc_min, $c_min, $l_min, $a_min);
300 $max_value = $self->_max(0, $sc_max, $c_max, $l_max, $a_max);
2eac77fc 301
23a3585a
TC
302 my $config_min = $self->_get_number('y_min');
303 my $config_max = $self->_get_number('y_max');
304
305 if (defined $config_max && $config_max < $max_value) {
306 $config_max = undef;
307 }
308 if (defined $config_min && $config_min > $min_value) {
309 $config_min = undef;
310 }
311
2eac77fc 312 my $range_padding = $self->_get_number('range_padding');
23a3585a
TC
313 if (defined $config_min) {
314 $min_value = $config_min;
315 }
316 else {
317 if ($min_value > 0) {
318 $min_value = 0;
319 }
320 if ($range_padding && $min_value < 0) {
321 my $difference = $min_value * $range_padding / 100;
322 if ($min_value < -1 && $difference > -1) {
323 $difference = -1;
324 }
325 $min_value += $difference;
2eac77fc 326 }
2eac77fc 327 }
23a3585a
TC
328 if (defined $config_max) {
329 $max_value = $config_max;
330 }
331 else {
332 if ($range_padding && $max_value > 0) {
333 my $difference = $max_value * $range_padding / 100;
334 if ($max_value > 1 && $difference < 1) {
335 $difference = 1;
336 }
337 $max_value += $difference;
2eac77fc 338 }
2eac77fc 339 }
e2c4cb9a 340 $column_count = $self->_max(0, $sc_cols, $l_cols, $a_cols);
2eac77fc 341
c2c2cb9e 342 if ($self->_get_number('automatic_axis')) {
343 # In case this was set via a style, and not by the api method
e554917f 344 eval { require Chart::Math::Axis; };
c2c2cb9e 345 if ($@) {
e554917f 346 return $self->_error("Can't use automatic_axis - $@");
c2c2cb9e 347 }
348
349 my $axis = Chart::Math::Axis->new();
f0b01a81 350 $axis->include_zero();
c2c2cb9e 351 $axis->add_data($min_value, $max_value);
352 $max_value = $axis->top;
353 $min_value = $axis->bottom;
354 my $ticks = $axis->ticks;
355 # The +1 is there because we have the bottom tick as well
356 $self->set_y_tics($ticks+1);
357 }
358
2eac77fc 359 $self->_set_max_value($max_value);
360 $self->_set_min_value($min_value);
361 $self->_set_column_count($column_count);
e554917f 362
363 return 1;
2eac77fc 364}
365
366sub _min {
367 my $self = shift;
368 my $min = shift;
369
370 foreach my $value (@_) {
23a3585a 371 next unless defined $value;
2eac77fc 372 if ($value < $min) { $min = $value; }
373 }
374 return $min;
375}
376
377sub _max {
378 my $self = shift;
379 my $min = shift;
380
381 foreach my $value (@_) {
23a3585a 382 next unless defined $value;
2eac77fc 383 if ($value > $min) { $min = $value; }
384 }
385 return $min;
386}
387
388sub _get_line_range {
389 my $self = shift;
390 my $series = $self->_get_data_series()->{'line'};
23a3585a 391 return (undef, undef, 0) unless $series;
2eac77fc 392
393 my $max_value = 0;
23a3585a 394 my $min_value = STARTING_MIN_VALUE;
2eac77fc 395 my $column_count = 0;
396
397 my @series = @{$series};
398 foreach my $series (@series) {
399 my @data = @{$series->{'data'}};
400
401 if (scalar @data > $column_count) {
402 $column_count = scalar @data;
403 }
404
405 foreach my $value (@data) {
406 if ($value > $max_value) { $max_value = $value; }
407 if ($value < $min_value) { $min_value = $value; }
408 }
409 }
410
411 return ($min_value, $max_value, $column_count);
412}
413
e2c4cb9a 414sub _get_area_range {
415 my $self = shift;
416 my $series = $self->_get_data_series()->{'area'};
417 return (undef, undef, 0) unless $series;
418
419 my $max_value = 0;
420 my $min_value = STARTING_MIN_VALUE;
421 my $column_count = 0;
422
423 my @series = @{$series};
424 foreach my $series (@series) {
425 my @data = @{$series->{'data'}};
426
427 if (scalar @data > $column_count) {
428 $column_count = scalar @data;
429 }
430
431 foreach my $value (@data) {
432 if ($value > $max_value) { $max_value = $value; }
433 if ($value < $min_value) { $min_value = $value; }
434 }
435 }
436
437 return ($min_value, $max_value, $column_count);
438}
439
440
2eac77fc 441sub _get_column_range {
442 my $self = shift;
443
444 my $series = $self->_get_data_series()->{'column'};
23a3585a 445 return (undef, undef, 0) unless $series;
2eac77fc 446
447 my $max_value = 0;
23a3585a 448 my $min_value = STARTING_MIN_VALUE;
2eac77fc 449 my $column_count = 0;
450
451 my @series = @{$series};
452 foreach my $series (@series) {
453 my @data = @{$series->{'data'}};
454
455 foreach my $value (@data) {
456 $column_count++;
457 if ($value > $max_value) { $max_value = $value; }
458 if ($value < $min_value) { $min_value = $value; }
459 }
460 }
461
462 return ($min_value, $max_value, $column_count);
463}
464
465sub _get_stacked_column_range {
466 my $self = shift;
467
468 my $max_value = 0;
23a3585a 469 my $min_value = STARTING_MIN_VALUE;
2eac77fc 470 my $column_count = 0;
471
23a3585a 472 return (undef, undef, 0) unless $self->_get_data_series()->{'stacked_column'};
2eac77fc 473 my @series = @{$self->_get_data_series()->{'stacked_column'}};
474
475 my @max_entries;
476 my @min_entries;
477 for (my $i = scalar @series - 1; $i >= 0; $i--) {
478 my $series = $series[$i];
479 my $data = $series->{'data'};
480
481 for (my $i = 0; $i < scalar @$data; $i++) {
482 my $value = 0;
483 if ($data->[$i] > 0) {
484 $value = $data->[$i] + ($max_entries[$i] || 0);
485 $data->[$i] = $value;
486 $max_entries[$i] = $value;
487 }
488 elsif ($data->[$i] < 0) {
489 $value = $data->[$i] + ($min_entries[$i] || 0);
490 $data->[$i] = $value;
491 $min_entries[$i] = $value;
492 }
493 if ($value > $max_value) { $max_value = $value; }
494 if ($value < $min_value) { $min_value = $value; }
495 }
496 if (scalar @$data > $column_count) {
497 $column_count = scalar @$data;
498 }
499 }
500
501 return ($min_value, $max_value, $column_count);
502}
503
504sub _draw_legend {
505 my $self = shift;
c2c2cb9e 506 my $chart_box = shift;
2eac77fc 507 my $style = $self->{'_style'};
508
509 my @labels;
510 my $img = $self->_get_image();
511 if (my $series = $self->_get_data_series()->{'stacked_column'}) {
512 push @labels, map { $_->{'series_name'} } @$series;
513 }
514 if (my $series = $self->_get_data_series()->{'column'}) {
515 push @labels, map { $_->{'series_name'} } @$series;
516 }
517 if (my $series = $self->_get_data_series()->{'line'}) {
518 push @labels, map { $_->{'series_name'} } @$series;
519 }
f94f373e 520 if (my $series = $self->_get_data_series()->{'area'}) {
521 push @labels, map { $_->{'series_name'} } @$series;
522 }
2eac77fc 523
524 if ($style->{features}{legend} && (scalar @labels)) {
c2c2cb9e 525 $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box)
2eac77fc 526 or return;
527 }
528 return;
529}
530
531sub _draw_flat_legend {
532 return 1;
533}
534
535sub _draw_lines {
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');
2eac77fc 549
c2c2cb9e 550 my $graph_width = $self->_get_number('graph_width');
551 my $graph_height = $self->_get_number('graph_height');
2eac77fc 552
553 my $line_series = $self->_get_data_series()->{'line'};
554 my $series_counter = $self->_get_series_counter() || 0;
555
556 my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0;
557
c2c2cb9e 558 my $col_width = int($graph_width / $column_count) -1;
559
560 my $graph_box = $self->_get_graph_box();
561 my $left = $graph_box->[0] + 1;
562 my $bottom = $graph_box->[1];
563
564 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
565
49a35584 566 my $line_aa = $self->_get_number("lineaa");
2eac77fc 567 foreach my $series (@$line_series) {
568 my @data = @{$series->{'data'}};
569 my $data_size = scalar @data;
570
571 my $interval;
572 if ($has_columns) {
573 $interval = $graph_width / ($data_size);
574 }
575 else {
576 $interval = $graph_width / ($data_size - 1);
577 }
2eac77fc 578 my $color = $self->_data_color($series_counter);
8e140388
TC
579
580 # We need to add these last, otherwise the next line segment will overwrite half of the marker
581 my @marker_positions;
2eac77fc 582 for (my $i = 0; $i < $data_size - 1; $i++) {
583 my $x1 = $left + $i * $interval;
584 my $x2 = $left + ($i + 1) * $interval;
585
586 $x1 += $has_columns * $interval / 2;
587 $x2 += $has_columns * $interval / 2;
588
c2c2cb9e 589 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
590 my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height;
2eac77fc 591
8e140388 592 push @marker_positions, [$x1, $y1];
49a35584 593 $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr;
2eac77fc 594 }
595
596 my $x2 = $left + ($data_size - 1) * $interval;
597 $x2 += $has_columns * $interval / 2;
598
c2c2cb9e 599 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
2eac77fc 600
8e140388
TC
601 push @marker_positions, [$x2, $y2];
602 foreach my $position (@marker_positions) {
603 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
604 }
2eac77fc 605 $series_counter++;
606 }
607
608 $self->_set_series_counter($series_counter);
488bc70c 609 return 1;
2eac77fc 610}
611
f94f373e 612sub _area_data_fill {
613 my ($self, $index, $box) = @_;
614
615 my %fill = $self->_data_fill($index, $box);
616
617 my $opacity = $self->_get_number("area.opacity");
618 $opacity == 1
619 and return %fill;
620
621 my $orig_fill = $fill{fill};
622 unless ($orig_fill) {
623 $orig_fill = Imager::Fill->new
624 (
625 solid => $fill{color},
626 combine => "normal",
627 );
628 }
629 return
630 (
631 fill => Imager::Fill->new
632 (
633 type => "opacity",
634 other => $orig_fill,
635 opacity => $opacity,
636 ),
637 );
638}
639
e2c4cb9a 640sub _draw_area {
641 my $self = shift;
642 my $style = $self->{'_style'};
643
644 my $img = $self->_get_image();
645
646 my $max_value = $self->_get_max_value();
647 my $min_value = $self->_get_min_value();
648 my $column_count = $self->_get_column_count();
649
650 my $value_range = $max_value - $min_value;
651
652 my $width = $self->_get_number('width');
653 my $height = $self->_get_number('height');
654
655 my $graph_width = $self->_get_number('graph_width');
656 my $graph_height = $self->_get_number('graph_height');
657
658 my $area_series = $self->_get_data_series()->{'area'};
659 my $series_counter = $self->_get_series_counter() || 0;
660
e2c4cb9a 661 my $col_width = int($graph_width / $column_count) -1;
662
663 my $graph_box = $self->_get_graph_box();
664 my $left = $graph_box->[0] + 1;
665 my $bottom = $graph_box->[1];
666 my $right = $graph_box->[2];
667 my $top = $graph_box->[3];
668
669 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1);
670
671 my $line_aa = $self->_get_number("lineaa");
672 foreach my $series (@$area_series) {
673 my @data = @{$series->{'data'}};
674 my $data_size = scalar @data;
675
45de5577 676 my $interval = $graph_width / ($data_size - 1);
677
e2c4cb9a 678 my $color = $self->_data_color($series_counter);
679
680 # We need to add these last, otherwise the next line segment will overwrite half of the marker
681 my @marker_positions;
682 my @polygon_points;
683 for (my $i = 0; $i < $data_size - 1; $i++) {
684 my $x1 = $left + $i * $interval;
685
e2c4cb9a 686 my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height;
687
688 if ($i == 0) {
689 push @polygon_points, [$x1, $top];
690 }
691 push @polygon_points, [$x1, $y1];
692
693 push @marker_positions, [$x1, $y1];
694 }
695
696 my $x2 = $left + ($data_size - 1) * $interval;
e2c4cb9a 697
698 my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height;
699 push @polygon_points, [$x2, $y2];
700 push @polygon_points, [$x2, $top];
701 push @polygon_points, $polygon_points[0];
702
f94f373e 703 my @fill = $self->_area_data_fill($series_counter, [$left, $bottom, $right, $top]);
e2c4cb9a 704 $img->polygon(points => [@polygon_points], @fill);
705
706 push @marker_positions, [$x2, $y2];
707 foreach my $position (@marker_positions) {
708 $self->_draw_line_marker($position->[0], $position->[1], $series_counter);
709 }
710 $series_counter++;
711 }
712
713 $self->_set_series_counter($series_counter);
714 return 1;
715}
716
717
718
8e140388
TC
719sub _line_marker {
720 my ($self, $index) = @_;
721
722 my $markers = $self->{'_style'}{'line_markers'};
723 if (!$markers) {
724 return;
725 }
726 my $marker = $markers->[$index % @$markers];
727
728 return $marker;
729}
730
731sub _draw_line_marker {
732 my $self = shift;
733 my ($x1, $y1, $series_counter) = @_;
734
735 my $img = $self->_get_image();
736
737 my $style = $self->_line_marker($series_counter);
738 return unless $style;
739
740 my $type = $style->{'shape'};
741 my $radius = $style->{'radius'};
742
49a35584
TC
743 my $line_aa = $self->_get_number("lineaa");
744 my $fill_aa = $self->_get_number("fill.aa");
8e140388
TC
745 if ($type eq 'circle') {
746 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
49a35584 747 $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill);
8e140388
TC
748 }
749 elsif ($type eq 'square') {
750 my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]);
751 $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill);
752 }
753 elsif ($type eq 'diamond') {
754 # The gradient really doesn't work for diamond
755 my $color = $self->_data_color($series_counter);
756 $img->polygon(
757 points => [
758 [$x1 - $radius, $y1],
759 [$x1, $y1 + $radius],
760 [$x1 + $radius, $y1],
761 [$x1, $y1 - $radius],
762 ],
49a35584 763 filled => 1, color => $color, aa => $fill_aa);
8e140388
TC
764 }
765 elsif ($type eq 'triangle') {
766 # The gradient really doesn't work for triangle
767 my $color = $self->_data_color($series_counter);
768 $img->polygon(
769 points => [
770 [$x1 - $radius, $y1 + $radius],
771 [$x1 + $radius, $y1 + $radius],
772 [$x1, $y1 - $radius],
773 ],
49a35584 774 filled => 1, color => $color, aa => $fill_aa);
8e140388
TC
775
776 }
777 elsif ($type eq 'x') {
778 my $color = $self->_data_color($series_counter);
49a35584
TC
779 $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
780 $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
8e140388
TC
781 }
782 elsif ($type eq 'plus') {
783 my $color = $self->_data_color($series_counter);
49a35584
TC
784 $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr;
785 $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr;
8e140388
TC
786 }
787}
788
2eac77fc 789sub _draw_columns {
790 my $self = shift;
791 my $style = $self->{'_style'};
792
793 my $img = $self->_get_image();
794
795 my $max_value = $self->_get_max_value();
796 my $min_value = $self->_get_min_value();
797 my $column_count = $self->_get_column_count();
798
799 my $value_range = $max_value - $min_value;
800
801 my $width = $self->_get_number('width');
802 my $height = $self->_get_number('height');
2eac77fc 803
c2c2cb9e 804 my $graph_width = $self->_get_number('graph_width');
805 my $graph_height = $self->_get_number('graph_height');
2eac77fc 806
2eac77fc 807
c2c2cb9e 808 my $graph_box = $self->_get_graph_box();
809 my $left = $graph_box->[0] + 1;
810 my $bottom = $graph_box->[1];
811 my $zero_position = int($bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1));
2eac77fc 812
413ce87a 813 my $bar_width = $graph_width / $column_count;
2eac77fc 814
815 my $outline_color;
816 if ($style->{'features'}{'outline'}) {
817 $outline_color = $self->_get_color('outline.line');
818 }
819
820 my $series_counter = $self->_get_series_counter() || 0;
821 my $col_series = $self->_get_data_series()->{'column'};
488bc70c 822 my $column_padding_percent = $self->_get_number('column_padding') || 0;
823 my $column_padding = int($column_padding_percent * $bar_width / 100);
2eac77fc 824
825 # 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.
826 my $column_series = 0;
827
828 # If there are stacked columns, non-stacked columns need to start one to the right of where they would otherwise
829 my $has_stacked_columns = (defined $self->_get_data_series()->{'stacked_column'} ? 1 : 0);
830
831 for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) {
832 my $series = $col_series->[$series_pos];
833 my @data = @{$series->{'data'}};
834 my $data_size = scalar @data;
2eac77fc 835 for (my $i = 0; $i < $data_size; $i++) {
413ce87a 836 my $part1 = $bar_width * (scalar @$col_series * $i);
837 my $part2 = ($series_pos) * $bar_width;
838 my $x1 = $left + $part1 + $part2;
2eac77fc 839 if ($has_stacked_columns) {
413ce87a 840 $x1 += ($bar_width * ($i+1));
841 }
842 $x1 = int($x1);
843
844 my $x2 = int($x1 + $bar_width - $column_padding)-1;
845 # Special case for when bar_width is less than 1.
846 if ($x2 < $x1) {
847 $x2 = $x1;
2eac77fc 848 }
2eac77fc 849
c2c2cb9e 850 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
2eac77fc 851
852 my $color = $self->_data_color($series_counter);
853
2eac77fc 854 if ($data[$i] > 0) {
7422650e 855 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
856 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
2eac77fc 857 if ($style->{'features'}{'outline'}) {
858 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
859 }
860 }
861 else {
7422650e 862 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
863 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
2eac77fc 864 if ($style->{'features'}{'outline'}) {
865 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
866 }
867 }
868 }
869
870 $series_counter++;
871 $column_series++;
872 }
873 $self->_set_series_counter($series_counter);
488bc70c 874 return 1;
2eac77fc 875}
876
877sub _draw_stacked_columns {
878 my $self = shift;
879 my $style = $self->{'_style'};
880
881 my $img = $self->_get_image();
882
883 my $max_value = $self->_get_max_value();
884 my $min_value = $self->_get_min_value();
885 my $column_count = $self->_get_column_count();
886 my $value_range = $max_value - $min_value;
887
888 my $graph_box = $self->_get_graph_box();
889 my $left = $graph_box->[0] + 1;
890 my $bottom = $graph_box->[1];
2eac77fc 891
c2c2cb9e 892 my $graph_width = $self->_get_number('graph_width');
893 my $graph_height = $self->_get_number('graph_height');
2eac77fc 894
413ce87a 895 my $bar_width = $graph_width / $column_count;
2eac77fc 896 my $column_series = 0;
897 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
898 $column_series = (scalar @$column_series_data);
899 }
900 $column_series++;
901
488bc70c 902 my $column_padding_percent = $self->_get_number('column_padding') || 0;
903 if ($column_padding_percent < 0) {
904 return $self->_error("Column padding less than 0");
905 }
906 if ($column_padding_percent > 100) {
907 return $self->_error("Column padding greater than 0");
908 }
909 my $column_padding = int($column_padding_percent * $bar_width / 100);
910
2eac77fc 911 my $outline_color;
912 if ($style->{'features'}{'outline'}) {
913 $outline_color = $self->_get_color('outline.line');
914 }
915
c2c2cb9e 916 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
2eac77fc 917 my $col_series = $self->_get_data_series()->{'stacked_column'};
918 my $series_counter = $self->_get_series_counter() || 0;
488bc70c 919
2eac77fc 920 foreach my $series (@$col_series) {
921 my @data = @{$series->{'data'}};
922 my $data_size = scalar @data;
2eac77fc 923 for (my $i = 0; $i < $data_size; $i++) {
413ce87a 924 my $part1 = $bar_width * $i * $column_series;
925 my $part2 = 0;
926 my $x1 = int($left + $part1 + $part2);
927 my $x2 = int($x1 + $bar_width - $column_padding) - 1;
928 # Special case for when bar_width is less than 1.
929 if ($x2 < $x1) {
930 $x2 = $x1;
931 }
2eac77fc 932
488bc70c 933 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
2eac77fc 934
935 if ($data[$i] > 0) {
7422650e 936 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
937 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
2eac77fc 938 if ($style->{'features'}{'outline'}) {
939 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
940 }
941 }
942 else {
7422650e 943 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
944 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
2eac77fc 945 if ($style->{'features'}{'outline'}) {
946 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
947 }
948 }
949 }
950
951 $series_counter++;
952 }
953 $self->_set_series_counter($series_counter);
488bc70c 954 return 1;
2eac77fc 955}
956
957sub _add_data_series {
958 my $self = shift;
959 my $series_type = shift;
960 my $data_ref = shift;
961 my $series_name = shift;
962
963 my $graph_data = $self->{'graph_data'} || {};
964
965 my $series = $graph_data->{$series_type} || [];
966
967 push @$series, { data => $data_ref, series_name => $series_name };
968
969 $graph_data->{$series_type} = $series;
970
971 $self->{'graph_data'} = $graph_data;
972 return;
973}
974
975=over
976
c2c2cb9e 977=item show_horizontal_gridlines()
978
979Shows horizontal gridlines at the y-tics.
980
981=cut
982
983sub show_horizontal_gridlines {
984 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
985}
986
987=item use_automatic_axis()
988
e554917f 989Automatically 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.
c2c2cb9e 990
991=cut
992
993sub use_automatic_axis {
e554917f 994 eval { require Chart::Math::Axis; };
c2c2cb9e 995 if ($@) {
e554917f 996 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
c2c2cb9e 997 }
998 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
e554917f 999 return 1;
c2c2cb9e 1000}
1001
1002
2eac77fc 1003=item set_y_tics($count)
1004
1005Set the number of Y tics to use. Their value and position will be determined by the data range.
1006
1007=cut
1008
1009sub set_y_tics {
1010 $_[0]->{'y_tics'} = $_[1];
1011}
1012
1013sub _get_y_tics {
28acfd43 1014 return $_[0]->{'y_tics'} || 0;
2eac77fc 1015}
1016
c2c2cb9e 1017sub _remove_tics_from_chart_box {
1018 my $self = shift;
1019 my $chart_box = shift;
1020
1021 # XXX - bad default
1022 my $tic_width = $self->_get_y_tic_width() || 10;
1023 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
1024
1025 # XXX - bad default
1026 my $tic_height = $self->_get_x_tic_height() || 10;
1027 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
1028
1029 $self->_remove_box($chart_box, \@y_tic_box);
1030 $self->_remove_box($chart_box, \@x_tic_box);
b748d4ed 1031
1032 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
1033 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
1034 $self->_remove_box($chart_box, \@y_tic_tops);
1035
267997be 1036 # Make sure that the first and last label fit
1037 if (my $labels = $self->_get_labels()) {
1038 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
1039 my @remove_box = ($chart_box->[0],
1040 $chart_box->[1],
1041 $chart_box->[0] + int($box[2] / 2) + 1,
1042 $chart_box->[3]
1043 );
1044
1045 $self->_remove_box($chart_box, \@remove_box);
1046 }
1047 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
1048 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
1049 $chart_box->[1],
1050 $chart_box->[2],
1051 $chart_box->[3]
1052 );
1053
1054 $self->_remove_box($chart_box, \@remove_box);
1055 }
1056 }
c2c2cb9e 1057}
1058
1059sub _get_y_tic_width{
1060 my $self = shift;
1061 my $min = $self->_get_min_value();
1062 my $max = $self->_get_max_value();
1063 my $tic_count = $self->_get_y_tics();
1064
c2c2cb9e 1065 my $interval = ($max - $min) / ($tic_count - 1);
1066
1067 my %text_info = $self->_text_style('legend')
1068 or return;
1069
1070 my $max_width = 0;
1071 for my $count (0 .. $tic_count - 1) {
1072 my $value = sprintf("%.2f", ($count*$interval)+$min);
1073
1074 my @box = $self->_text_bbox($value, 'legend');
1075 my $width = $box[2] - $box[0];
1076
1077 # For the tic width
1078 $width += 10;
1079 if ($width > $max_width) {
1080 $max_width = $width;
1081 }
1082 }
1083
1084 return $max_width;
1085}
1086
1087sub _get_x_tic_height {
1088 my $self = shift;
1089
1090 my $labels = $self->_get_labels();
1091
488bc70c 1092 if (!$labels) {
1093 return;
1094 }
1095
c2c2cb9e 1096 my $tic_count = (scalar @$labels) - 1;
1097
1098 my %text_info = $self->_text_style('legend')
1099 or return;
1100
1101 my $max_height = 0;
1102 for my $count (0 .. $tic_count) {
1103 my $label = $labels->[$count];
1104
1105 my @box = $self->_text_bbox($label, 'legend');
1106
1107 my $height = $box[3] - $box[1];
1108
1109 # Padding + the tic
1110 $height += 10;
1111 if ($height > $max_height) {
1112 $max_height = $height;
1113 }
1114 }
1115
1116 return $max_height;
1117}
1118
2eac77fc 1119sub _draw_y_tics {
1120 my $self = shift;
1121 my $min = $self->_get_min_value();
1122 my $max = $self->_get_max_value();
1123 my $tic_count = $self->_get_y_tics();
1124
1125 my $img = $self->_get_image();
1126 my $graph_box = $self->_get_graph_box();
1127 my $image_box = $self->_get_image_box();
1128
1129 my $interval = ($max - $min) / ($tic_count - 1);
1130
1131 my %text_info = $self->_text_style('legend')
1132 or return;
1133
1509eee7 1134 my $line_style = $self->_get_color('outline.line');
c2c2cb9e 1135 my $show_gridlines = $self->_get_number('horizontal_gridlines');
1509eee7 1136 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
2eac77fc 1137 for my $count (0 .. $tic_count - 1) {
1138 my $x1 = $graph_box->[0] - 5;
1139 my $x2 = $graph_box->[0] + 5;
1509eee7 1140 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
2eac77fc 1141
c2c2cb9e 1142 my $value = ($count*$interval)+$min;
1143 if ($interval < 1 || ($value != int($value))) {
1144 $value = sprintf("%.2f", $value);
1145 }
23a3585a 1146
2eac77fc 1147 my @box = $self->_text_bbox($value, 'legend')
1148 or return;
1149
1509eee7 1150 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
2eac77fc 1151
1152 my $width = $box[2];
1153 my $height = $box[3];
1154
1155 $img->string(%text_info,
1156 x => ($x1 - $width - 3),
1157 y => ($y1 + ($height / 2)),
1158 text => $value
1159 );
c2c2cb9e 1160
1161 if ($show_gridlines) {
1162 # XXX - line styles!
1163 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
1164 my $x1 = $i;
1165 my $x2 = $i + 2;
1166 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1509eee7 1167 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
c2c2cb9e 1168 }
1169 }
2eac77fc 1170 }
1171
1172}
1173
1174sub _draw_x_tics {
1175 my $self = shift;
1176
1177 my $img = $self->_get_image();
1178 my $graph_box = $self->_get_graph_box();
1179 my $image_box = $self->_get_image_box();
1180
1181 my $labels = $self->_get_labels();
1182
1183 my $tic_count = (scalar @$labels) - 1;
1184
1185 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1186
1187 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1188 my $denominator = $tic_count;
1189 if ($has_columns) {
1190 $denominator ++;
1191 }
1192 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1193 my %text_info = $self->_text_style('legend')
1194 or return;
1195
1509eee7 1196 # If automatic axis is turned on, let's be selective about what labels we draw.
1197 my $max_size = 0;
1198 my $tic_skip = 0;
1199 if ($self->_get_number('automatic_axis')) {
1200 foreach my $label (@$labels) {
1201 my @box = $self->_text_bbox($label, 'legend');
1202 if ($box[2] > $max_size) {
1203 $max_size = $box[2];
1204 }
1205 }
1206
1207 # Give the max_size some padding...
1208 $max_size *= 1.2;
1209
1210 $tic_skip = int($max_size / $tic_distance) + 1;
1211 }
1212
1213 my $line_style = $self->_get_color('outline.line');
1214
2eac77fc 1215 for my $count (0 .. $tic_count) {
1509eee7 1216 next if ($count % ($tic_skip + 1));
2eac77fc 1217 my $label = $labels->[$count];
1218 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1219
1220 if ($has_columns) {
1221 $x1 += $tic_distance / 2;
1222 }
1509eee7 1223
1224 $x1 = int($x1);
1225
2eac77fc 1226 my $y1 = $graph_box->[3] + 5;
1227 my $y2 = $graph_box->[3] - 5;
1228
1509eee7 1229 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
2eac77fc 1230
1231 my @box = $self->_text_bbox($label, 'legend')
1232 or return;
1233
1234 my $width = $box[2];
1235 my $height = $box[3];
1236
1237 $img->string(%text_info,
1238 x => ($x1 - ($width / 2)),
1239 y => ($y1 + ($height + 5)),
1240 text => $label
1241 );
1242
1243 }
1244}
1245
1246sub _valid_input {
1247 my $self = shift;
1248
1249 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1250 return $self->_error("No data supplied");
1251 }
1252
1253 my $data = $self->_get_data_series();
1254 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1255 return $self->_error("No values in data series");
1256 }
1257 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1258 return $self->_error("No values in data series");
1259 }
1260 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1261 return $self->_error("No values in data series");
1262 }
1263
1264 return 1;
1265}
1266
1267sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1268sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1269sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1270sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1271sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1272sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1273sub _get_column_count { return $_[0]->{'column_count'} }
1274sub _get_min_value { return $_[0]->{'min_value'} }
1275sub _get_max_value { return $_[0]->{'max_value'} }
1276sub _get_image_box { return $_[0]->{'image_box'} }
1277sub _get_graph_box { return $_[0]->{'graph_box'} }
1278sub _get_series_counter { return $_[0]->{'series_counter'} }
1279
f94f373e 1280sub _style_defs {
1281 my ($self) = @_;
1282
1283 my %work = %{$self->SUPER::_style_defs()};
1284 $work{area} =
1285 {
1286 opacity => 0.5,
1287 };
1a36ef75 1288 push @{$work{features}}, qw/graph_outline graph_fill/;
f94f373e 1289
1290 return \%work;
1291}
1292
1a36ef75
TC
1293sub _composite {
1294 my ($self) = @_;
1295 return ( $self->SUPER::_composite(), "graph" );
1296}
1297
2eac77fc 12981;