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