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