f0b01a81 |
1 | package Imager::Graph::Horizontal; |
2 | |
3 | =head1 NAME |
4 | |
5 | Imager::Graph::Horizontal - A super class for line/bar charts |
6 | |
7 | =cut |
8 | |
9 | use strict; |
10 | use vars qw(@ISA); |
11 | use Imager::Graph; |
12 | @ISA = qw(Imager::Graph); |
13 | |
14 | use constant STARTING_MIN_VALUE => 99999; |
15 | |
16 | =over 4 |
17 | |
18 | =item add_data_series(\@data, $series_name) |
19 | |
20 | Add a data series to the graph, of the default type. |
21 | |
22 | =cut |
23 | |
24 | sub add_data_series { |
25 | my $self = shift; |
26 | my $data_ref = shift; |
27 | my $series_name = shift; |
28 | |
29 | my $series_type = $self->_get_default_series_type(); |
30 | $self->_add_data_series($series_type, $data_ref, $series_name); |
31 | |
32 | return; |
33 | } |
34 | |
35 | =item add_column_data_series(\@data, $series_name) |
36 | |
37 | Add a column data series to the graph. |
38 | |
39 | =cut |
40 | |
41 | sub add_column_data_series { |
42 | my $self = shift; |
43 | my $data_ref = shift; |
44 | my $series_name = shift; |
45 | |
46 | $self->_add_data_series('column', $data_ref, $series_name); |
47 | |
48 | return; |
49 | } |
50 | |
51 | =item add_line_data_series(\@data, $series_name) |
52 | |
53 | Add a line data series to the graph. |
54 | |
55 | =cut |
56 | |
57 | sub add_line_data_series { |
58 | my $self = shift; |
59 | my $data_ref = shift; |
60 | my $series_name = shift; |
61 | |
62 | $self->_add_data_series('line', $data_ref, $series_name); |
63 | |
64 | return; |
65 | } |
66 | |
67 | =item set_column_padding($int) |
68 | |
69 | Sets the number of pixels that should go between columns of data. |
70 | |
71 | =cut |
72 | |
73 | sub set_column_padding { |
74 | $_[0]->{'custom_style'}->{'column_padding'} = $_[1]; |
75 | } |
76 | |
77 | =item set_negative_background($color) |
78 | |
79 | Sets the background color used below the x axis. |
80 | |
81 | =cut |
82 | |
83 | sub set_negative_background { |
84 | $_[0]->{'custom_style'}->{'negative_bg'} = $_[1]; |
85 | } |
86 | |
87 | =item draw() |
88 | |
89 | Draw the graph |
90 | |
91 | =cut |
92 | |
93 | sub draw { |
94 | my ($self, %opts) = @_; |
95 | |
96 | if (!$self->_valid_input()) { |
97 | return; |
98 | } |
99 | |
100 | $self->_style_setup(\%opts); |
101 | |
102 | my $style = $self->{_style}; |
103 | |
104 | my $img = $self->_get_image() |
105 | or return; |
106 | |
107 | my @image_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 ); |
108 | $self->_set_image_box(\@image_box); |
109 | |
110 | my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 ); |
111 | $self->_draw_legend(\@chart_box); |
112 | if ($style->{title}{text}) { |
113 | $self->_draw_title($img, \@chart_box) |
114 | or return; |
115 | } |
116 | |
117 | # Scale the graph box down to the widest graph that can cleanly hold the # of columns. |
118 | return unless $self->_get_data_range(); |
119 | $self->_remove_tics_from_chart_box(\@chart_box); |
120 | my $column_count = $self->_get_column_count(); |
121 | |
122 | my $width = $self->_get_number('width'); |
123 | my $height = $self->_get_number('height'); |
124 | |
125 | my $graph_width = $chart_box[2] - $chart_box[0]; |
126 | my $graph_height = $chart_box[3] - $chart_box[1]; |
127 | |
95289584 |
128 | my $col_height = ($graph_height - 1) / $column_count; |
129 | if ($col_height > 1) { |
130 | $graph_height = int($col_height) * $column_count + 1; |
131 | } |
132 | else { |
133 | $graph_height = $col_height * $column_count + 1; |
134 | } |
f0b01a81 |
135 | |
136 | my $tic_count = $self->_get_x_tics(); |
137 | my $tic_distance = int(($graph_width -1) / ($tic_count - 1)); |
138 | $graph_width = $tic_distance * ($tic_count - 1); |
139 | |
140 | my $bottom = $chart_box[1]; |
141 | my $left = $chart_box[0]; |
142 | |
143 | $self->{'_style'}{'graph_width'} = $graph_width; |
144 | $self->{'_style'}{'graph_height'} = $graph_height; |
145 | |
146 | my @graph_box = ($left, $bottom, $left + $graph_width, $bottom + $graph_height); |
147 | $self->_set_graph_box(\@graph_box); |
148 | |
149 | $img->box( |
150 | color => $self->_get_color('outline.line'), |
151 | xmin => $left, |
152 | xmax => $left+$graph_width, |
153 | ymin => $bottom, |
154 | ymax => $bottom+$graph_height, |
155 | ); |
156 | |
157 | $img->box( |
158 | color => $self->_get_color('bg'), |
159 | xmin => $left + 1, |
160 | xmax => $left+$graph_width - 1, |
161 | ymin => $bottom + 1, |
162 | ymax => $bottom+$graph_height-1 , |
163 | filled => 1, |
164 | ); |
165 | |
166 | my $min_value = $self->_get_min_value(); |
167 | my $max_value = $self->_get_max_value(); |
168 | my $value_range = $max_value - $min_value; |
169 | |
170 | my $zero_position; |
171 | if ($value_range) { |
267997be |
172 | $zero_position = $left + $graph_width + (-1*$min_value / $value_range) * ($graph_width-1); |
f0b01a81 |
173 | } |
174 | |
175 | if ($min_value < 0) { |
176 | $img->box( |
177 | color => $self->_get_color('negative_bg'), |
178 | xmin => $left+1, |
179 | xmax => $zero_position, |
180 | ymin => $bottom+1, |
181 | ymax => $bottom+$graph_height - 1, |
182 | filled => 1, |
183 | ); |
184 | $img->line( |
185 | x1 => $zero_position, |
186 | y1 => $bottom, |
187 | x2 => $zero_position, |
188 | y2 => $bottom + $graph_height, |
189 | color => $self->_get_color('outline.line'), |
190 | ); |
191 | } |
192 | |
193 | if ($self->_get_data_series()->{'bar'}) { |
194 | $self->_draw_bars(); |
195 | } |
196 | |
197 | if ($self->_get_x_tics()) { |
198 | $self->_draw_x_tics(); |
199 | } |
200 | if ($self->_get_labels()) { |
201 | $self->_draw_y_tics(); |
202 | } |
203 | |
204 | return $self->_get_image(); |
205 | } |
206 | |
207 | sub _get_data_range { |
208 | my $self = shift; |
209 | |
210 | my $max_value = 0; |
211 | my $min_value = 0; |
212 | my $column_count = 0; |
213 | |
214 | my ($b_min, $b_max, $b_cols) = $self->_get_bar_range(); |
215 | |
267997be |
216 | $min_value = $self->_min(STARTING_MIN_VALUE, $b_min); |
217 | $max_value = $self->_max(0, $b_max); |
f0b01a81 |
218 | $column_count = $b_cols; |
267997be |
219 | |
220 | my $config_min = $self->_get_number('x_min'); |
221 | my $config_max = $self->_get_number('x_max'); |
222 | |
223 | if (defined $config_max && $config_max < $max_value) { |
224 | $config_max = undef; |
225 | } |
226 | if (defined $config_min && $config_min > $min_value) { |
227 | $config_min = undef; |
228 | } |
229 | |
230 | my $range_padding = $self->_get_number('range_padding'); |
231 | if (defined $config_min) { |
232 | $min_value = $config_min; |
233 | } |
234 | else { |
235 | if ($min_value > 0) { |
236 | $min_value = 0; |
237 | } |
238 | if ($range_padding && $min_value < 0) { |
239 | my $difference = $min_value * $range_padding / 100; |
240 | if ($min_value < -1 && $difference > -1) { |
241 | $difference = -1; |
242 | } |
243 | $min_value += $difference; |
244 | } |
245 | } |
246 | if (defined $config_max) { |
247 | $max_value = $config_max; |
248 | } |
249 | else { |
250 | if ($range_padding && $max_value > 0) { |
251 | my $difference = $max_value * $range_padding / 100; |
252 | if ($max_value > 1 && $difference < 1) { |
253 | $difference = 1; |
254 | } |
255 | $max_value += $difference; |
256 | } |
257 | } |
f0b01a81 |
258 | |
259 | if ($self->_get_number('automatic_axis')) { |
260 | # In case this was set via a style, and not by the api method |
261 | eval { require Chart::Math::Axis; }; |
262 | if ($@) { |
263 | return $self->_error("Can't use automatic_axis - $@"); |
264 | } |
265 | |
266 | my $axis = Chart::Math::Axis->new(); |
267 | $axis->include_zero(); |
268 | $axis->add_data($min_value, $max_value); |
269 | $max_value = $axis->top; |
270 | $min_value = $axis->bottom; |
271 | my $ticks = $axis->ticks; |
272 | # The +1 is there because we have the bottom tick as well |
273 | $self->set_x_tics($ticks+1); |
274 | } |
275 | |
276 | $self->_set_max_value($max_value); |
277 | $self->_set_min_value($min_value); |
278 | $self->_set_column_count($column_count); |
279 | |
280 | return 1; |
281 | } |
282 | |
283 | sub _min { |
284 | my $self = shift; |
285 | my $min = shift; |
286 | |
287 | foreach my $value (@_) { |
288 | next unless defined $value; |
289 | if ($value < $min) { $min = $value; } |
290 | } |
291 | return $min; |
292 | } |
293 | |
294 | sub _max { |
295 | my $self = shift; |
296 | my $min = shift; |
297 | |
298 | foreach my $value (@_) { |
299 | next unless defined $value; |
300 | if ($value > $min) { $min = $value; } |
301 | } |
302 | return $min; |
303 | } |
304 | |
305 | sub _get_bar_range { |
306 | my $self = shift; |
307 | |
308 | my $series = $self->_get_data_series()->{'bar'}; |
309 | return (undef, undef, 0) unless $series; |
310 | |
311 | my $max_value = 0; |
312 | my $min_value = STARTING_MIN_VALUE; |
313 | my $column_count = 0; |
314 | |
315 | my @series = @{$series}; |
316 | foreach my $series (@series) { |
317 | my @data = @{$series->{'data'}}; |
318 | |
319 | foreach my $value (@data) { |
320 | $column_count++; |
321 | if ($value > $max_value) { $max_value = $value; } |
322 | if ($value < $min_value) { $min_value = $value; } |
323 | } |
324 | } |
325 | |
326 | return ($min_value, $max_value, $column_count); |
327 | } |
328 | |
329 | |
330 | sub _draw_legend { |
331 | my $self = shift; |
332 | my $chart_box = shift; |
333 | my $style = $self->{'_style'}; |
334 | |
335 | my @labels; |
336 | my $img = $self->_get_image(); |
337 | if (my $series = $self->_get_data_series()->{'bar'}) { |
338 | push @labels, map { $_->{'series_name'} } @$series; |
339 | } |
340 | |
341 | if ($style->{features}{legend} && (scalar @labels)) { |
342 | $self->SUPER::_draw_legend($self->_get_image(), \@labels, $chart_box) |
343 | or return; |
344 | } |
345 | return; |
346 | } |
347 | |
348 | sub _draw_flat_legend { |
349 | return 1; |
350 | } |
351 | |
352 | sub _draw_lines { |
353 | my $self = shift; |
354 | my $style = $self->{'_style'}; |
355 | |
356 | my $img = $self->_get_image(); |
357 | |
358 | my $max_value = $self->_get_max_value(); |
359 | my $min_value = $self->_get_min_value(); |
360 | my $column_count = $self->_get_column_count(); |
361 | |
362 | my $value_range = $max_value - $min_value; |
363 | |
364 | my $width = $self->_get_number('width'); |
365 | my $height = $self->_get_number('height'); |
366 | |
367 | my $graph_width = $self->_get_number('graph_width'); |
368 | my $graph_height = $self->_get_number('graph_height'); |
369 | |
370 | my $line_series = $self->_get_data_series()->{'line'}; |
371 | my $series_counter = $self->_get_series_counter() || 0; |
372 | |
373 | my $has_columns = (defined $self->_get_data_series()->{'column'} || $self->_get_data_series->{'stacked_column'}) ? 1 : 0; |
374 | |
375 | my $col_width = int($graph_width / $column_count) -1; |
376 | |
377 | my $graph_box = $self->_get_graph_box(); |
378 | my $left = $graph_box->[0] + 1; |
379 | my $bottom = $graph_box->[1]; |
380 | |
381 | my $zero_position = $bottom + $graph_height - (-1*$min_value / $value_range) * ($graph_height - 1); |
382 | |
383 | |
384 | my $line_aa = $self->_get_number("lineaa"); |
385 | foreach my $series (@$line_series) { |
386 | my @data = @{$series->{'data'}}; |
387 | my $data_size = scalar @data; |
388 | |
389 | my $interval; |
390 | if ($has_columns) { |
391 | $interval = $graph_width / ($data_size); |
392 | } |
393 | else { |
394 | $interval = $graph_width / ($data_size - 1); |
395 | } |
396 | my $color = $self->_data_color($series_counter); |
397 | |
398 | # We need to add these last, otherwise the next line segment will overwrite half of the marker |
399 | my @marker_positions; |
400 | for (my $i = 0; $i < $data_size - 1; $i++) { |
401 | my $x1 = $left + $i * $interval; |
402 | my $x2 = $left + ($i + 1) * $interval; |
403 | |
404 | $x1 += $has_columns * $interval / 2; |
405 | $x2 += $has_columns * $interval / 2; |
406 | |
407 | my $y1 = $bottom + ($value_range - $data[$i] + $min_value)/$value_range * $graph_height; |
408 | my $y2 = $bottom + ($value_range - $data[$i + 1] + $min_value)/$value_range * $graph_height; |
409 | |
410 | push @marker_positions, [$x1, $y1]; |
411 | $img->line(x1 => $x1, y1 => $y1, x2 => $x2, y2 => $y2, aa => $line_aa, color => $color) || die $img->errstr; |
412 | } |
413 | |
414 | my $x2 = $left + ($data_size - 1) * $interval; |
415 | $x2 += $has_columns * $interval / 2; |
416 | |
417 | my $y2 = $bottom + ($value_range - $data[$data_size - 1] + $min_value)/$value_range * $graph_height; |
418 | |
419 | push @marker_positions, [$x2, $y2]; |
420 | foreach my $position (@marker_positions) { |
421 | $self->_draw_line_marker($position->[0], $position->[1], $series_counter); |
422 | } |
423 | $series_counter++; |
424 | } |
425 | |
426 | $self->_set_series_counter($series_counter); |
427 | return; |
428 | } |
429 | |
430 | sub _line_marker { |
431 | my ($self, $index) = @_; |
432 | |
433 | my $markers = $self->{'_style'}{'line_markers'}; |
434 | if (!$markers) { |
435 | return; |
436 | } |
437 | my $marker = $markers->[$index % @$markers]; |
438 | |
439 | return $marker; |
440 | } |
441 | |
442 | sub _draw_line_marker { |
443 | my $self = shift; |
444 | my ($x1, $y1, $series_counter) = @_; |
445 | |
446 | my $img = $self->_get_image(); |
447 | |
448 | my $style = $self->_line_marker($series_counter); |
449 | return unless $style; |
450 | |
451 | my $type = $style->{'shape'}; |
452 | my $radius = $style->{'radius'}; |
453 | |
454 | my $line_aa = $self->_get_number("lineaa"); |
455 | my $fill_aa = $self->_get_number("fill.aa"); |
456 | if ($type eq 'circle') { |
457 | my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]); |
458 | $img->circle(x => $x1, y => $y1, r => $radius, aa => $fill_aa, filled => 1, @fill); |
459 | } |
460 | elsif ($type eq 'square') { |
461 | my @fill = $self->_data_fill($series_counter, [$x1 - $radius, $y1 - $radius, $x1 + $radius, $y1 + $radius]); |
462 | $img->box(xmin => $x1 - $radius, ymin => $y1 - $radius, xmax => $x1 + $radius, ymax => $y1 + $radius, @fill); |
463 | } |
464 | elsif ($type eq 'diamond') { |
465 | # The gradient really doesn't work for diamond |
466 | my $color = $self->_data_color($series_counter); |
467 | $img->polygon( |
468 | points => [ |
469 | [$x1 - $radius, $y1], |
470 | [$x1, $y1 + $radius], |
471 | [$x1 + $radius, $y1], |
472 | [$x1, $y1 - $radius], |
473 | ], |
474 | filled => 1, color => $color, aa => $fill_aa); |
475 | } |
476 | elsif ($type eq 'triangle') { |
477 | # The gradient really doesn't work for triangle |
478 | my $color = $self->_data_color($series_counter); |
479 | $img->polygon( |
480 | points => [ |
481 | [$x1 - $radius, $y1 + $radius], |
482 | [$x1 + $radius, $y1 + $radius], |
483 | [$x1, $y1 - $radius], |
484 | ], |
485 | filled => 1, color => $color, aa => $fill_aa); |
486 | |
487 | } |
488 | elsif ($type eq 'x') { |
489 | my $color = $self->_data_color($series_counter); |
490 | $img->line(x1 => $x1 - $radius, y1 => $y1 -$radius, x2 => $x1 + $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr; |
491 | $img->line(x1 => $x1 + $radius, y1 => $y1 -$radius, x2 => $x1 - $radius, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr; |
492 | } |
493 | elsif ($type eq 'plus') { |
494 | my $color = $self->_data_color($series_counter); |
495 | $img->line(x1 => $x1, y1 => $y1 -$radius, x2 => $x1, y2 => $y1+$radius, aa => $line_aa, color => $color) || die $img->errstr; |
496 | $img->line(x1 => $x1 + $radius, y1 => $y1, x2 => $x1 - $radius, y2 => $y1, aa => $line_aa, color => $color) || die $img->errstr; |
497 | } |
498 | } |
499 | |
500 | sub _draw_bars { |
501 | my $self = shift; |
502 | my $style = $self->{'_style'}; |
503 | |
504 | my $img = $self->_get_image(); |
505 | |
506 | my $max_value = $self->_get_max_value(); |
507 | my $min_value = $self->_get_min_value(); |
508 | my $column_count = $self->_get_column_count(); |
509 | |
510 | my $value_range = $max_value - $min_value; |
511 | |
512 | my $width = $self->_get_number('width'); |
513 | my $height = $self->_get_number('height'); |
514 | |
515 | my $graph_width = $self->_get_number('graph_width'); |
516 | my $graph_height = $self->_get_number('graph_height'); |
517 | |
518 | |
519 | my $graph_box = $self->_get_graph_box(); |
520 | my $bottom = $graph_box->[1] + 1; |
521 | my $left = $graph_box->[0]; |
522 | |
523 | my $zero_position = int($left + (-1*$min_value / $value_range) * ($graph_width-1)); |
524 | |
95289584 |
525 | my $bar_height = $graph_height / $column_count; |
f0b01a81 |
526 | |
527 | my $outline_color; |
528 | if ($style->{'features'}{'outline'}) { |
529 | $outline_color = $self->_get_color('outline.line'); |
530 | } |
531 | |
532 | my $series_counter = $self->_get_series_counter() || 0; |
533 | my $col_series = $self->_get_data_series()->{'bar'}; |
534 | my $column_padding = $self->_get_number('column_padding') || 0; |
535 | |
536 | # 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. |
537 | my $column_series = 0; |
538 | |
539 | for (my $series_pos = 0; $series_pos < scalar @$col_series; $series_pos++) { |
540 | my $series = $col_series->[$series_pos]; |
541 | my @data = @{$series->{'data'}}; |
542 | my $data_size = scalar @data; |
f0b01a81 |
543 | for (my $i = 0; $i < $data_size; $i++) { |
544 | |
95289584 |
545 | my $part1 = $bar_height * (scalar @$col_series * $i); |
546 | my $part2 = ($series_pos) * $bar_height; |
547 | my $y1 = int($bottom + $part1 + $part2); |
f0b01a81 |
548 | |
95289584 |
549 | my $y2 = int($y1 + $bar_height - $column_padding)-1; |
550 | # Special case for when bar_height is less than 1. |
551 | if ($y2 < $y1) { |
552 | $y2 = $y1; |
553 | } |
f0b01a81 |
554 | |
555 | my $x1 = int($left - ($min_value - $data[$i]) / $value_range * $graph_width); |
556 | |
557 | my $color = $self->_data_color($series_counter); |
558 | |
f0b01a81 |
559 | if ($data[$i] > 0) { |
7422650e |
560 | my @fill = $self->_data_fill($series_counter, [$zero_position+1, $y1, $x1, $y2]); |
561 | $img->box(xmax => $x1, xmin => $zero_position+1, ymin => $y1, ymax => $y2, @fill); |
f0b01a81 |
562 | if ($style->{'features'}{'outline'}) { |
563 | $img->box(xmax => $x1, xmin => $zero_position, ymin => $y1, ymax => $y2, color => $outline_color); |
564 | } |
565 | } |
566 | elsif ($data[$i] == 0) { |
567 | } |
568 | else { |
7422650e |
569 | my @fill = $self->_data_fill($series_counter, [$x1, $y1, $zero_position, $y2]); |
570 | $img->box(xmax => $zero_position , xmin => $x1, ymin => $y1, ymax => $y2, @fill); |
f0b01a81 |
571 | if ($style->{'features'}{'outline'}) { |
572 | $img->box(xmax => $zero_position, xmin => $x1, ymin => $y1, ymax => $y2, color => $outline_color); |
573 | } |
574 | } |
575 | } |
576 | |
577 | $series_counter++; |
578 | $column_series++; |
579 | } |
580 | $self->_set_series_counter($series_counter); |
581 | return; |
582 | } |
583 | |
584 | sub _add_data_series { |
585 | my $self = shift; |
586 | my $series_type = shift; |
587 | my $data_ref = shift; |
588 | my $series_name = shift; |
589 | |
590 | my $graph_data = $self->{'graph_data'} || {}; |
591 | |
592 | my $series = $graph_data->{$series_type} || []; |
593 | |
594 | push @$series, { data => $data_ref, series_name => $series_name }; |
595 | |
596 | $graph_data->{$series_type} = $series; |
597 | |
598 | $self->{'graph_data'} = $graph_data; |
599 | return; |
600 | } |
601 | |
602 | =over |
603 | |
604 | =item show_vertical_gridlines() |
605 | |
606 | Shows vertical gridlines at the y-tics. |
607 | |
608 | =cut |
609 | |
610 | sub show_vertical_gridlines { |
611 | $_[0]->{'custom_style'}->{'vertical_gridlines'} = 1; |
612 | } |
613 | |
614 | =item use_automatic_axis() |
615 | |
616 | Automatically 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. |
617 | |
618 | =cut |
619 | |
620 | sub use_automatic_axis { |
621 | eval { require Chart::Math::Axis; }; |
622 | if ($@) { |
623 | return $_[0]->_error("use_automatic_axis - $@\nCalled from ".join(' ', caller)."\n"); |
624 | } |
625 | $_[0]->{'custom_style'}->{'automatic_axis'} = 1; |
626 | return 1; |
627 | } |
628 | |
629 | |
630 | =item set_x_tics($count) |
631 | |
632 | Set the number of X tics to use. Their value and position will be determined by the data range. |
633 | |
634 | =cut |
635 | |
636 | sub set_x_tics { |
637 | $_[0]->{'x_tics'} = $_[1]; |
638 | } |
639 | |
640 | sub _get_x_tics { |
641 | return $_[0]->{'x_tics'} || 0; |
642 | } |
643 | |
644 | sub _remove_tics_from_chart_box { |
645 | my $self = shift; |
646 | my $chart_box = shift; |
647 | |
648 | # XXX - bad default |
649 | my $tic_width = $self->_get_y_tic_width() || 10; |
650 | my @y_tic_box = ($chart_box->[0], $chart_box->[1], $chart_box->[0] + $tic_width, $chart_box->[3]); |
651 | |
652 | # XXX - bad default |
653 | my $tic_height = $self->_get_x_tic_height() || 10; |
654 | my @x_tic_box = ($chart_box->[0], $chart_box->[3] - $tic_height, $chart_box->[2], $chart_box->[3]); |
655 | |
656 | $self->_remove_box($chart_box, \@y_tic_box); |
657 | $self->_remove_box($chart_box, \@x_tic_box); |
658 | |
659 | # If there's no title, the y-tics will be part off-screen. Half of the x-tic height should be more than sufficient. |
660 | my @y_tic_tops = ($chart_box->[0], $chart_box->[1], $chart_box->[2], $chart_box->[1] + int($tic_height / 2)); |
661 | $self->_remove_box($chart_box, \@y_tic_tops); |
662 | |
267997be |
663 | if (my @box = $self->_text_bbox($self->_get_max_value(), 'legend')) { |
664 | my @remove_box = ($chart_box->[2] - int($box[2] / 2) - 1, |
665 | $chart_box->[1], |
666 | $chart_box->[2], |
667 | $chart_box->[3] |
668 | ); |
669 | |
670 | $self->_remove_box($chart_box, \@remove_box); |
671 | } |
672 | |
673 | |
f0b01a81 |
674 | } |
675 | |
676 | sub _get_y_tic_width { |
677 | my $self = shift; |
678 | |
679 | my $labels = $self->_get_labels(); |
680 | my $max_width = 0; |
681 | foreach my $label (@$labels) { |
682 | my @box = $self->_text_bbox($label, 'legend'); |
683 | my $width = $box[2] + 5; |
684 | # For the tic itself... |
685 | $width += 10; |
686 | if ($width > $max_width) { |
687 | $max_width = $width; |
688 | } |
689 | } |
690 | return $max_width; |
691 | } |
692 | |
693 | sub _get_x_tic_height { |
694 | my $self = shift; |
695 | |
696 | my $min = $self->_get_min_value(); |
697 | my $max = $self->_get_max_value(); |
698 | my $tic_count = $self->_get_x_tics(); |
699 | |
700 | my $interval = ($max - $min) / ($tic_count - 1); |
701 | |
702 | my %text_info = $self->_text_style('legend') |
703 | or return; |
704 | |
705 | my $max_height = 0; |
706 | for my $count (0 .. $tic_count - 1) { |
707 | my $value = sprintf("%.2f", ($count*$interval)+$min); |
708 | |
709 | my @box = $self->_text_bbox($value, 'legend'); |
710 | my $height = $box[3] - $box[1]; |
711 | |
712 | # For the tic width |
713 | $height += 10; |
714 | if ($height > $max_height) { |
715 | $max_height = $height; |
716 | } |
717 | } |
718 | |
719 | |
720 | return $max_height; |
721 | } |
722 | |
723 | sub _draw_y_tics { |
724 | my $self = shift; |
725 | |
726 | my $img = $self->_get_image(); |
727 | my $graph_box = $self->_get_graph_box(); |
728 | my $image_box = $self->_get_image_box(); |
729 | |
730 | my $labels = $self->_get_labels(); |
731 | |
732 | my $tic_count = (scalar @$labels) - 1; |
733 | |
734 | my $has_columns = defined $self->_get_data_series()->{'bar'}; |
735 | |
736 | # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge |
737 | my $denominator = $tic_count; |
738 | if ($has_columns) { |
739 | $denominator ++; |
740 | } |
741 | my $tic_distance = ($graph_box->[3] - $graph_box->[1]) / ($denominator); |
742 | my %text_info = $self->_text_style('legend') |
743 | or return; |
744 | |
745 | for my $count (0 .. $tic_count) { |
746 | my $label = $labels->[$count]; |
747 | |
748 | my $x1 = $graph_box->[0] - 5; |
749 | my $x2 = $graph_box->[0] + 5; |
750 | |
751 | my $y1 = $graph_box->[1] + ($tic_distance * $count); |
752 | |
753 | if ($has_columns) { |
754 | $y1 += $tic_distance / 2; |
755 | } |
756 | |
757 | $img->line(x1 => $x1, x2 => $x2, y1 => $y1, y2 => $y1, aa => 1, color => '000000'); |
758 | |
759 | my @box = $self->_text_bbox($label, 'legend') |
760 | or return; |
761 | |
762 | my $width = $box[2]; |
763 | my $height = $box[3]; |
764 | |
765 | $img->string(%text_info, |
766 | x => ($x1 - ($width + 5)), |
767 | y => ($y1 + ($height / 2)), |
768 | text => $label |
769 | ); |
770 | |
771 | } |
772 | |
773 | } |
774 | |
775 | sub _draw_x_tics { |
776 | my $self = shift; |
777 | |
778 | my $img = $self->_get_image(); |
779 | my $graph_box = $self->_get_graph_box(); |
780 | my $image_box = $self->_get_image_box(); |
781 | |
782 | my $tic_count = $self->_get_x_tics(); |
783 | my $min = $self->_get_min_value(); |
784 | my $max = $self->_get_max_value(); |
785 | my $interval = ($max - $min) / ($tic_count - 1); |
786 | |
787 | # If we have columns, we want the x-ticks to show up in the middle of the column, not on the left edge |
788 | my $tic_distance = ($graph_box->[2] - $graph_box->[0]) / ($tic_count -1); |
789 | |
790 | my %text_info = $self->_text_style('legend') |
791 | or return; |
792 | |
793 | my $show_gridlines = $self->_get_number('vertical_gridlines'); |
794 | for my $count (0 .. $tic_count-1) { |
795 | my $x1 = $graph_box->[0] + ($tic_distance * $count); |
796 | |
797 | my $y1 = $graph_box->[3] + 5; |
798 | my $y2 = $graph_box->[3] - 5; |
799 | |
800 | my $value = ($count*$interval)+$min; |
801 | |
802 | $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000'); |
803 | |
804 | my @box = $self->_text_bbox($value, 'legend') |
805 | or return; |
806 | |
807 | my $width = $box[2]; |
808 | my $height = $box[3]; |
809 | |
810 | $img->string(%text_info, |
811 | x => ($x1 - ($width / 2)), |
812 | y => ($y1 + $height + 5), |
813 | text => $value |
814 | ); |
815 | |
816 | if ($show_gridlines) { |
817 | # XXX - line styles! |
818 | for (my $i = $graph_box->[1]; $i < $graph_box->[3]; $i += 6) { |
819 | my $y1 = $i; |
820 | my $y2 = $i + 2; |
821 | if ($y2 > $graph_box->[3]) { $y2 = $graph_box->[3]; } |
822 | $img->line(x1 => $x1, x2 => $x1, y1 => $y1, y2 => $y2, aa => 1, color => '000000'); |
823 | } |
824 | } |
825 | } |
826 | } |
827 | |
828 | sub _valid_input { |
829 | my $self = shift; |
830 | |
831 | if (!defined $self->_get_data_series() || !keys %{$self->_get_data_series()}) { |
832 | return $self->_error("No data supplied"); |
833 | } |
834 | |
835 | my $data = $self->_get_data_series(); |
836 | if (defined $data->{'line'} && !scalar @{$data->{'line'}->[0]->{'data'}}) { |
837 | return $self->_error("No values in data series"); |
838 | } |
839 | if (defined $data->{'column'} && !scalar @{$data->{'column'}->[0]->{'data'}}) { |
840 | return $self->_error("No values in data series"); |
841 | } |
842 | if (defined $data->{'stacked_column'} && !scalar @{$data->{'stacked_column'}->[0]->{'data'}}) { |
843 | return $self->_error("No values in data series"); |
844 | } |
845 | |
846 | return 1; |
847 | } |
848 | |
849 | sub _set_column_count { $_[0]->{'column_count'} = $_[1]; } |
850 | sub _set_min_value { $_[0]->{'min_value'} = $_[1]; } |
851 | sub _set_max_value { $_[0]->{'max_value'} = $_[1]; } |
852 | sub _set_image_box { $_[0]->{'image_box'} = $_[1]; } |
853 | sub _set_graph_box { $_[0]->{'graph_box'} = $_[1]; } |
854 | sub _set_series_counter { $_[0]->{'series_counter'} = $_[1]; } |
855 | sub _get_column_count { return $_[0]->{'column_count'} } |
856 | sub _get_min_value { return $_[0]->{'min_value'} } |
857 | sub _get_max_value { return $_[0]->{'max_value'} } |
858 | sub _get_image_box { return $_[0]->{'image_box'} } |
859 | sub _get_graph_box { return $_[0]->{'graph_box'} } |
860 | sub _get_series_counter { return $_[0]->{'series_counter'} } |
861 | |
862 | 1; |
863 | |