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