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