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