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