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