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