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