* changed t31tic_color.t to check against different images based on the presence...
[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
c2c2cb9e 651 my $bar_width = int($graph_width / $column_count - 1);
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++) {
488bc70c 674 my $x1 = int($left + $bar_width * (scalar @$col_series * $i + $series_pos)) + scalar @$col_series * $i + $series_pos + ($column_padding / 2);
2eac77fc 675 if ($has_stacked_columns) {
676 $x1 += ($i + 1) * $bar_width + $i + 1;
677 }
488bc70c 678 my $x2 = $x1 + $bar_width - $column_padding;
2eac77fc 679
c2c2cb9e 680 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
2eac77fc 681
682 my $color = $self->_data_color($series_counter);
683
2eac77fc 684 if ($data[$i] > 0) {
7422650e 685 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
686 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
2eac77fc 687 if ($style->{'features'}{'outline'}) {
688 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
689 }
690 }
691 else {
7422650e 692 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
693 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
2eac77fc 694 if ($style->{'features'}{'outline'}) {
695 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
696 }
697 }
698 }
699
700 $series_counter++;
701 $column_series++;
702 }
703 $self->_set_series_counter($series_counter);
488bc70c 704 return 1;
2eac77fc 705}
706
707sub _draw_stacked_columns {
708 my $self = shift;
709 my $style = $self->{'_style'};
710
711 my $img = $self->_get_image();
712
713 my $max_value = $self->_get_max_value();
714 my $min_value = $self->_get_min_value();
715 my $column_count = $self->_get_column_count();
716 my $value_range = $max_value - $min_value;
717
718 my $graph_box = $self->_get_graph_box();
719 my $left = $graph_box->[0] + 1;
720 my $bottom = $graph_box->[1];
2eac77fc 721
c2c2cb9e 722 my $graph_width = $self->_get_number('graph_width');
723 my $graph_height = $self->_get_number('graph_height');
2eac77fc 724
c2c2cb9e 725 my $bar_width = int($graph_width / $column_count -1);
2eac77fc 726 my $column_series = 0;
727 if (my $column_series_data = $self->_get_data_series()->{'column'}) {
728 $column_series = (scalar @$column_series_data);
729 }
730 $column_series++;
731
488bc70c 732 my $column_padding_percent = $self->_get_number('column_padding') || 0;
733 if ($column_padding_percent < 0) {
734 return $self->_error("Column padding less than 0");
735 }
736 if ($column_padding_percent > 100) {
737 return $self->_error("Column padding greater than 0");
738 }
739 my $column_padding = int($column_padding_percent * $bar_width / 100);
740
2eac77fc 741 my $outline_color;
742 if ($style->{'features'}{'outline'}) {
743 $outline_color = $self->_get_color('outline.line');
744 }
745
c2c2cb9e 746 my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height -1);
2eac77fc 747 my $col_series = $self->_get_data_series()->{'stacked_column'};
748 my $series_counter = $self->_get_series_counter() || 0;
488bc70c 749
2eac77fc 750 foreach my $series (@$col_series) {
751 my @data = @{$series->{'data'}};
752 my $data_size = scalar @data;
2eac77fc 753 for (my $i = 0; $i < $data_size; $i++) {
488bc70c 754 my $x1 = int($left + $bar_width * ($column_series * $i)) + $column_series * $i + ($column_padding / 2);
755 my $x2 = $x1 + $bar_width - $column_padding;
2eac77fc 756
488bc70c 757 my $y1 = int($bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height);
2eac77fc 758
759 if ($data[$i] > 0) {
7422650e 760 my @fill = $self->_data_fill($series_counter, [$x1, $y1, $x2, $zero_position-1]);
761 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position-1, @fill);
2eac77fc 762 if ($style->{'features'}{'outline'}) {
763 $img->box(xmin => $x1, xmax => $x2, ymin => $y1, ymax => $zero_position, color => $outline_color);
764 }
765 }
766 else {
7422650e 767 my @fill = $self->_data_fill($series_counter, [$x1, $zero_position+1, $x2, $y1]);
768 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1, @fill);
2eac77fc 769 if ($style->{'features'}{'outline'}) {
770 $img->box(xmin => $x1, xmax => $x2, ymin => $zero_position+1, ymax => $y1+1, color => $outline_color);
771 }
772 }
773 }
774
775 $series_counter++;
776 }
777 $self->_set_series_counter($series_counter);
488bc70c 778 return 1;
2eac77fc 779}
780
781sub _add_data_series {
782 my $self = shift;
783 my $series_type = shift;
784 my $data_ref = shift;
785 my $series_name = shift;
786
787 my $graph_data = $self->{'graph_data'} || {};
788
789 my $series = $graph_data->{$series_type} || [];
790
791 push @$series, { data => $data_ref, series_name => $series_name };
792
793 $graph_data->{$series_type} = $series;
794
795 $self->{'graph_data'} = $graph_data;
796 return;
797}
798
799=over
800
c2c2cb9e 801=item show_horizontal_gridlines()
802
803Shows horizontal gridlines at the y-tics.
804
805=cut
806
807sub show_horizontal_gridlines {
808 $_[0]->{'custom_style'}->{'horizontal_gridlines'} = 1;
809}
810
811=item use_automatic_axis()
812
e554917f 813Automatically 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 814
815=cut
816
817sub use_automatic_axis {
e554917f 818 eval { require Chart::Math::Axis; };
c2c2cb9e 819 if ($@) {
e554917f 820 return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n");
c2c2cb9e 821 }
822 $_[0]->{'custom_style'}->{'automatic_axis'} = 1;
e554917f 823 return 1;
c2c2cb9e 824}
825
826
2eac77fc 827=item set_y_tics($count)
828
829Set the number of Y tics to use. Their value and position will be determined by the data range.
830
831=cut
832
833sub set_y_tics {
834 $_[0]->{'y_tics'} = $_[1];
835}
836
837sub _get_y_tics {
28acfd43 838 return $_[0]->{'y_tics'} || 0;
2eac77fc 839}
840
c2c2cb9e 841sub _remove_tics_from_chart_box {
842 my $self = shift;
843 my $chart_box = shift;
844
845 # XXX - bad default
846 my $tic_width = $self->_get_y_tic_width() || 10;
847 my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]);
848
849 # XXX - bad default
850 my $tic_height = $self->_get_x_tic_height() || 10;
851 my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]);
852
853 $self->_remove_box($chart_box, \@y_tic_box);
854 $self->_remove_box($chart_box, \@x_tic_box);
b748d4ed 855
856 # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient.
857 my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2));
858 $self->_remove_box($chart_box, \@y_tic_tops);
859
267997be 860 # Make sure that the first and last label fit
861 if (my $labels = $self->_get_labels()) {
862 if (my @box = $self->_text_bbox($labels->[0], 'legend')) {
863 my @remove_box = ($chart_box->[0],
864 $chart_box->[1],
865 $chart_box->[0] + int($box[2] / 2) + 1,
866 $chart_box->[3]
867 );
868
869 $self->_remove_box($chart_box, \@remove_box);
870 }
871 if (my @box = $self->_text_bbox($labels->[-1], 'legend')) {
872 my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1,
873 $chart_box->[1],
874 $chart_box->[2],
875 $chart_box->[3]
876 );
877
878 $self->_remove_box($chart_box, \@remove_box);
879 }
880 }
c2c2cb9e 881}
882
883sub _get_y_tic_width{
884 my $self = shift;
885 my $min = $self->_get_min_value();
886 my $max = $self->_get_max_value();
887 my $tic_count = $self->_get_y_tics();
888
c2c2cb9e 889 my $interval = ($max - $min) / ($tic_count - 1);
890
891 my %text_info = $self->_text_style('legend')
892 or return;
893
894 my $max_width = 0;
895 for my $count (0 .. $tic_count - 1) {
896 my $value = sprintf("%.2f", ($count*$interval)+$min);
897
898 my @box = $self->_text_bbox($value, 'legend');
899 my $width = $box[2] - $box[0];
900
901 # For the tic width
902 $width += 10;
903 if ($width > $max_width) {
904 $max_width = $width;
905 }
906 }
907
908 return $max_width;
909}
910
911sub _get_x_tic_height {
912 my $self = shift;
913
914 my $labels = $self->_get_labels();
915
488bc70c 916 if (!$labels) {
917 return;
918 }
919
c2c2cb9e 920 my $tic_count = (scalar @$labels) - 1;
921
922 my %text_info = $self->_text_style('legend')
923 or return;
924
925 my $max_height = 0;
926 for my $count (0 .. $tic_count) {
927 my $label = $labels->[$count];
928
929 my @box = $self->_text_bbox($label, 'legend');
930
931 my $height = $box[3] - $box[1];
932
933 # Padding + the tic
934 $height += 10;
935 if ($height > $max_height) {
936 $max_height = $height;
937 }
938 }
939
940 return $max_height;
941}
942
2eac77fc 943sub _draw_y_tics {
944 my $self = shift;
945 my $min = $self->_get_min_value();
946 my $max = $self->_get_max_value();
947 my $tic_count = $self->_get_y_tics();
948
949 my $img = $self->_get_image();
950 my $graph_box = $self->_get_graph_box();
951 my $image_box = $self->_get_image_box();
952
953 my $interval = ($max - $min) / ($tic_count - 1);
954
955 my %text_info = $self->_text_style('legend')
956 or return;
957
1509eee7 958 my $line_style = $self->_get_color('outline.line');
c2c2cb9e 959 my $show_gridlines = $self->_get_number('horizontal_gridlines');
1509eee7 960 my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($tic_count - 1);
2eac77fc 961 for my $count (0 .. $tic_count - 1) {
962 my $x1 = $graph_box->[0] - 5;
963 my $x2 = $graph_box->[0] + 5;
1509eee7 964 my $y1 = int($graph_box->[3] - ($count * $tic_distance));
2eac77fc 965
c2c2cb9e 966 my $value = ($count*$interval)+$min;
967 if ($interval < 1 || ($value != int($value))) {
968 $value = sprintf("%.2f", $value);
969 }
23a3585a 970
2eac77fc 971 my @box = $self->_text_bbox($value, 'legend')
972 or return;
973
1509eee7 974 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
2eac77fc 975
976 my $width = $box[2];
977 my $height = $box[3];
978
979 $img->string(%text_info,
980 x => ($x1 - $width - 3),
981 y => ($y1 + ($height / 2)),
982 text => $value
983 );
c2c2cb9e 984
985 if ($show_gridlines) {
986 # XXX - line styles!
987 for (my $i = $graph_box->[0]; $i < $graph_box->[2]; $i += 6) {
988 my $x1 = $i;
989 my $x2 = $i + 2;
990 if ($x2 > $graph_box->[2]) { $x2 = $graph_box->[2]; }
1509eee7 991 $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => $line_style);
c2c2cb9e 992 }
993 }
2eac77fc 994 }
995
996}
997
998sub _draw_x_tics {
999 my $self = shift;
1000
1001 my $img = $self->_get_image();
1002 my $graph_box = $self->_get_graph_box();
1003 my $image_box = $self->_get_image_box();
1004
1005 my $labels = $self->_get_labels();
1006
1007 my $tic_count = (scalar @$labels) - 1;
1008
1009 my $has_columns = (defined $self->_get_data_series()->{'column'} || defined $self->_get_data_series()->{'stacked_column'});
1010
1011 # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge
1012 my $denominator = $tic_count;
1013 if ($has_columns) {
1014 $denominator ++;
1015 }
1016 my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($denominator);
1017 my %text_info = $self->_text_style('legend')
1018 or return;
1019
1509eee7 1020 # If automatic axis is turned on, let's be selective about what labels we draw.
1021 my $max_size = 0;
1022 my $tic_skip = 0;
1023 if ($self->_get_number('automatic_axis')) {
1024 foreach my $label (@$labels) {
1025 my @box = $self->_text_bbox($label, 'legend');
1026 if ($box[2] > $max_size) {
1027 $max_size = $box[2];
1028 }
1029 }
1030
1031 # Give the max_size some padding...
1032 $max_size *= 1.2;
1033
1034 $tic_skip = int($max_size / $tic_distance) + 1;
1035 }
1036
1037 my $line_style = $self->_get_color('outline.line');
1038
2eac77fc 1039 for my $count (0 .. $tic_count) {
1509eee7 1040 next if ($count % ($tic_skip + 1));
2eac77fc 1041 my $label = $labels->[$count];
1042 my $x1 = $graph_box->[0] + ($tic_distance * $count);
1043
1044 if ($has_columns) {
1045 $x1 += $tic_distance / 2;
1046 }
1509eee7 1047
1048 $x1 = int($x1);
1049
2eac77fc 1050 my $y1 = $graph_box->[3] + 5;
1051 my $y2 = $graph_box->[3] - 5;
1052
1509eee7 1053 $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => $line_style);
2eac77fc 1054
1055 my @box = $self->_text_bbox($label, 'legend')
1056 or return;
1057
1058 my $width = $box[2];
1059 my $height = $box[3];
1060
1061 $img->string(%text_info,
1062 x => ($x1 - ($width / 2)),
1063 y => ($y1 + ($height + 5)),
1064 text => $label
1065 );
1066
1067 }
1068}
1069
1070sub _valid_input {
1071 my $self = shift;
1072
1073 if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) {
1074 return $self->_error("No data supplied");
1075 }
1076
1077 my $data = $self->_get_data_series();
1078 if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) {
1079 return $self->_error("No values in data series");
1080 }
1081 if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) {
1082 return $self->_error("No values in data series");
1083 }
1084 if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) {
1085 return $self->_error("No values in data series");
1086 }
1087
1088 return 1;
1089}
1090
1091sub _set_column_count { $_[0]->{'column_count'} = $_[1]; }
1092sub _set_min_value { $_[0]->{'min_value'} = $_[1]; }
1093sub _set_max_value { $_[0]->{'max_value'} = $_[1]; }
1094sub _set_image_box { $_[0]->{'image_box'} = $_[1]; }
1095sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; }
1096sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; }
1097sub _get_column_count { return $_[0]->{'column_count'} }
1098sub _get_min_value { return $_[0]->{'min_value'} }
1099sub _get_max_value { return $_[0]->{'max_value'} }
1100sub _get_image_box { return $_[0]->{'image_box'} }
1101sub _get_graph_box { return $_[0]->{'graph_box'} }
1102sub _get_series_counter { return $_[0]->{'series_counter'} }
1103
2eac77fc 11041;