lib/Chart/ErrorBars.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« lib/Chart/Direction.pm ↑ Summary lib/Chart/HorizontalBars.pm »
1  
2 # chart type: error bar - vertical bars
3 # which represent errors or standard deviations
4  
5 2 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Chart::ErrorBars;
8 our @ISA = qw(Chart::Base);
9 our $VERSION = 'v2.403.9';
10  
11 0 ▶use Chart::Base;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use GD;
13 use Carp;
14  
15 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
16 # public methods go here #
17 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
18  
19 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
20 # private methods go here #
21 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
22  
23 ## @fn private _draw_data
24 # finally get around to plotting the data
25 #
26 # Overwrites Base function
27 #
28 sub _draw_data {
29 my $self = shift;
30 my $data = $self->{'dataref'};
31 my $misccolor = $self->_color_role_to_index('misc');
32 my ( $x1, $x2, $x3, $y1, $y2, $y3, $mod, $y_error_up, $y_error_down );
33 my ( $width, $height, $delta, $map, $delta_num, $zero_offset, $flag );
34 my ( $i, $j, $color, $brush );
35 my $dataset = 0;
36 my $diff;
37  
38 # init the imagemap data field if they want it
39 if ( $self->true( $self->{'imagemap'} ) )
40 {
41 $self->{'imagemap_data'} = [];
42 }
43  
44 # find the delta value between data points, as well
45 # as the mapping constant
46 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
47 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
48 $delta = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 );
49 $diff = $self->{'max_val'} - $self->{'min_val'};
50 $diff = 1 if $diff == 0;
51 $map = $height / $diff;
52  
53 # for a xy-plot, use this delta and maybe an offset for the zero-axes
54 if ( $self->true( $self->{'xy_plot'} ) )
55 {
56 $diff = $self->{'x_max_val'} - $self->{'x_min_val'};
57 $diff = 1 if $diff == 0;
58 $delta_num = $width / $diff;
59  
60 if ( $self->{'x_min_val'} <= 0 && $self->{'x_max_val'} >= 0 )
61 {
62 $zero_offset = abs( $self->{'x_min_val'} ) * abs($delta_num);
63 }
64 elsif ( $self->{'x_min_val'} > 0 || $self->{'x_max_val'} < 0 )
65 {
66 $zero_offset = -$self->{'x_min_val'} * $delta_num;
67 }
68 else
69 {
70 $zero_offset = 0;
71 }
72 }
73  
74 # get the base x-y values
75 if ( $self->false( $self->{'xy_plot'} ) )
76 {
77 $x1 = $self->{'curr_x_min'} + ( $delta / 2 );
78 }
79 else
80 {
81 $x1 = $self->{'curr_x_min'};
82 }
83 if ( $self->{'min_val'} >= 0 )
84 {
85 $y1 = $self->{'curr_y_max'};
86 $mod = $self->{'min_val'};
87 }
88 elsif ( $self->{'max_val'} <= 0 )
89 {
90 $y1 = $self->{'curr_y_min'};
91 $mod = $self->{'max_val'};
92 }
93 else
94 {
95 $y1 = $self->{'curr_y_min'} + ( $map * $self->{'max_val'} );
96 $mod = 0;
97 $self->{'gd_obj'}->line( $self->{'curr_x_min'}, $y1, $self->{'curr_x_max'}, $y1, $misccolor );
98 }
99  
100 # first of all box it off
101 $self->{'gd_obj'}
102 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
103  
104 # draw the points
105 for $i ( 1 .. $self->{'num_datasets'} )
106 {
107 if ( $self->false( $self->{'same_error'} ) )
108 {
109  
110 # get the color for this dataset, and set the brush
111 $color = $self->_color_role_to_index( 'dataset' . ($dataset) ); # draw every point for this dataset
112 $dataset++ if ( ( $i - 1 ) % 3 == 0 );
113 for $j ( 0 .. $self->{'num_datapoints'} )
114 {
115  
116 #get the brush for points
117 $brush = $self->_prepare_brush( $color, 'point' );
118 $self->{'gd_obj'}->setBrush($brush);
119  
120 # only draw if the current set is really a dataset and no errorset
121 if ( ( $i - 1 ) % 3 == 0 )
122 {
123  
124 # don't try to draw anything if there's no data
125 if ( defined( $data->[$i][$j] ) )
126 {
127 if ( $self->true( $self->{'xy_plot'} ) )
128 {
129 $x2 = $x1 + $delta_num * $data->[0][$j] + $zero_offset + 1;
130 $x3 = $x2;
131 }
132 else
133 {
134 $x2 = $x1 + ( $delta * $j ) + 1;
135 $x3 = $x2;
136 }
137 $y2 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map );
138 $y3 = $y2;
139 $y_error_up = $y2 - abs( $data->[ $i + 1 ][$j] ) * $map;
140 $y_error_down = $y2 + abs( $data->[ $i + 2 ][$j] ) * $map;
141  
142 # draw the point only if it is within the chart borders
143 if ( $data->[$i][$j] <= $self->{'max_val'}
144 && $data->[$i][$j] >= $self->{'min_val'} )
145 {
146 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed );
147 $flag = 'true';
148 }
149  
150 #reset the brush for lines
151 $brush = $self->_prepare_brush( $color, 'line' );
152 $self->{'gd_obj'}->setBrush($brush);
153  
154 #draw the error bars
155 if ( $self->true($flag) )
156 {
157  
158 # the upper lines
159 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y_error_up, gdBrushed );
160 $self->{'gd_obj'}->line( $x2 - 3, $y_error_up, $x3 + 3, $y_error_up, gdBrushed );
161  
162 # the down lines
163 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y_error_down, gdBrushed );
164 $self->{'gd_obj'}->line( $x2 - 3, $y_error_down, $x3 + 3, $y_error_down, gdBrushed );
165 $flag = 'false';
166 }
167  
168 # store the imagemap data if they asked for it
169 if ( $self->true( $self->{'imagemap'} ) )
170 {
171 $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2 ];
172 }
173 }
174 }
175 }
176 }
177 else
178 {
179  
180 # get the color for this dataset, and set the brush
181 $color = $self->_color_role_to_index( 'dataset' . ($dataset) ); # draw every point for this dataset
182 $dataset++ if ( ( $i - 1 ) % 2 == 0 );
183 for $j ( 0 .. $self->{'num_datapoints'} )
184 {
185  
186 #get the brush for points
187 $brush = $self->_prepare_brush( $color, 'point' );
188 $self->{'gd_obj'}->setBrush($brush);
189  
190 # only draw if the current set is really a dataset and no errorset
191 if ( ( $i - 1 ) % 2 == 0 )
192 {
193  
194 # don't try to draw anything if there's no data
195 if ( defined( $data->[$i][$j] ) )
196 {
197 if ( $self->true( $self->{'xy_plot'} ) )
198 {
199 $x2 = $x1 + $delta_num * $data->[0][$j] + $zero_offset;
200 $x3 = $x2;
201 }
202 else
203 {
204 $x2 = $x1 + ( $delta * $j );
205 $x3 = $x2;
206 }
207 $y2 = $y1 - ( ( $data->[$i][$j] - $mod ) * $map );
208 $y3 = $y2;
209 $y_error_up = $y2 - abs( $data->[ $i + 1 ][$j] ) * $map;
210 $y_error_down = $y2 + abs( $data->[ $i + 1 ][$j] ) * $map;
211  
212 # draw the point only if it is within the chart borders
213 if ( $data->[$i][$j] <= $self->{'max_val'}
214 && $data->[$i][$j] >= $self->{'min_val'} )
215 {
216 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y3, gdBrushed );
217 $flag = 'true';
218 }
219  
220 #reset the brush for lines
221 $brush = $self->_prepare_brush( $color, 'line' );
222 $self->{'gd_obj'}->setBrush($brush);
223  
224 #draw the error bars
225 if ( $self->true($flag) )
226 {
227  
228 # the upper lines
229 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y_error_up, gdBrushed );
230 $self->{'gd_obj'}->line( $x2 - 3, $y_error_up, $x3 + 3, $y_error_up, gdBrushed );
231  
232 # the down lines
233 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y_error_down, gdBrushed );
234 $self->{'gd_obj'}->line( $x2 - 3, $y_error_down, $x3 + 3, $y_error_down, gdBrushed );
235 $flag = 'false';
236 }
237  
238 # store the imagemap data if they asked for it
239 if ( $self->true( $self->{'imagemap'} ) )
240 {
241 $self->{'imagemap_data'}->[$i][$j] = [ $x2, $y2 ];
242 }
243 }
244 } #end for
245 }
246 }
247 }
248 return 1;
249 }
250  
251 ## @fn private _prepare_brush
252 # set the gdBrush object to trick GD into drawing fat lines
253 #
254 # Overwrite Base function
255 #
256 sub _prepare_brush
257 {
258 my $self = shift;
259 my $color = shift;
260 my $type = shift;
261 my ( $radius, @rgb, $brush, $white, $newcolor );
262  
263 # get the rgb values for the desired color
264 @rgb = $self->{'gd_obj'}->rgb($color);
265  
266 # get the appropriate brush size
267 if ( $type eq 'line' )
268 {
269 $radius = $self->{'brush_size'} / 2;
270 }
271 elsif ( $type eq 'point' )
272 {
273 $radius = $self->{'pt_size'} / 2;
274 }
275  
276 # create the new image
277 $brush = GD::Image->new( $radius * 2, $radius * 2 );
278  
279 # get the colors, make the background transparent
280 $white = $brush->colorAllocate( 255, 255, 255 );
281 $newcolor = $brush->colorAllocate(@rgb);
282 $brush->transparent($white);
283  
284 # draw the circle
285 $brush->arc( $radius - 1, $radius - 1, $radius, $radius, 0, 360, $newcolor );
286  
287 # fill it if we're using lines
288 $brush->fill( $radius - 1, $radius - 1, $newcolor );
289  
290 # set the new image as the main object's brush
291 return $brush;
292 }
293  
294 ## @fn private int _draw_legend()
295 # let them know what all the pretty colors mean
296 # @return status
297 ## let them know what all the pretty colors mean
298 sub _draw_legend
299 {
300 my $self = shift;
301 my ( $length, $step, $temp, $post_length );
302 my $j = 0;
303  
304 # check to see if legend type is none..
305 if ( $self->{'legend'} =~ /^none$/ )
306 {
307 return 1;
308 }
309  
310 #just for later checking and warning
311 if ( $#{ $self->{'legend_labels'} } >= 0 )
312 {
313 $post_length = scalar( @{ $self->{'legend_labels'} } );
314 }
315  
316 #look if every second or eyery third dataset is a set for data
317 if ( $self->false( $self->{'same_error'} ) )
318 {
319 $step = 3;
320 }
321 else
322 {
323 $step = 2;
324 }
325  
326 # init a field to store the length of the longest legend label
327 unless ( $self->{'max_legend_label'} )
328 {
329 $self->{'max_legend_label'} = 0;
330 }
331  
332 # fill in the legend labels, find the longest one
333 for ( my $i = 1 ; $i < $self->{'num_datasets'} ; $i += $step )
334 {
335 my $label = $j + 1;
336 unless ( $self->{'legend_labels'}[$j] )
337 {
338 $self->{'legend_labels'}[$j] = "Dataset $label";
339 }
340 $length = length( $self->{'legend_labels'}[$j] );
341 if ( $length > $self->{'max_legend_label'} )
342 {
343 $self->{'max_legend_label'} = $length;
344 }
345 $j++;
346 }
347  
348 #we just have to label the datasets in the legend
349 #we'll reset it, to draw the sets
350 $temp = $self->{'num_datasets'};
351 $self->{'num_datasets'} = $j;
352  
353 # check to see if they have as many labels as datasets,
354 # warn them if not
355 if ( ( $post_length > 0 ) && ( $post_length != $j ) )
356 {
357 carp "The number of legend labels and datasets doesn\'t match";
358 }
359  
360 # different legend types
361 if ( $self->{'legend'} eq 'bottom' )
362 {
363 $self->_draw_bottom_legend;
364 }
365 elsif ( $self->{'legend'} eq 'right' )
366 {
367 $self->_draw_right_legend;
368 }
369 elsif ( $self->{'legend'} eq 'left' )
370 {
371 $self->_draw_left_legend;
372 }
373 elsif ( $self->{'legend'} eq 'top' )
374 {
375 $self->_draw_top_legend;
376 }
377 else
378 {
379 carp "I can't put a legend there (at " . $self->{'legend'} . ")\n";
380 }
381  
382 #reset the number of dataset to make sure that everything goes right
383 $self->{'num_datasets'} = $temp;
384  
385 # and return
386 return 1;
387 }
388  
389 #find the range of the x scale, don't forget the errors!
390 sub _find_y_range
391 {
392 my $self = shift;
393 my $data = $self->{'dataref'};
394  
395 my $max = undef;
396 my $min = undef;
397 if ( $self->false( $self->{'same_error'} ) )
398 {
399 for my $i ( 1 .. $self->{'num_datasets'} )
400 {
401 if ( ( $i - 1 ) % 3 == 0 )
402 {
403 for my $j ( 0 .. $self->{'num_datapoints'} )
404 {
405 if ( defined( $data->[$i][$j] ) && defined( $data->[ $i + 1 ][$j] ) && defined( $data->[ $i + 2 ][$j] ) )
406 {
407 if ( defined $max )
408 {
409 if ( ( $data->[$i][$j] + abs( $data->[ $i + 1 ][$j] ) ) > $max )
410 {
411 $max = $data->[$i][$j] + abs( $data->[ $i + 1 ][$j] );
412 }
413 if ( ( $data->[$i][$j] - abs( $data->[ $i + 2 ][$j] ) ) < $min )
414 {
415 $min = $data->[$i][$j] - abs( $data->[ $i + 2 ][$j] );
416 }
417 }
418 else { $min = $max = $data->[$i][$j]; }
419 }
420 }
421 }
422 }
423 return ( $min, $max );
424 }
425 else
426 {
427 for my $i ( 1 .. $self->{'num_datasets'} )
428 {
429 if ( ( $i - 1 ) % 2 == 0 )
430 {
431 for my $j ( 0 .. $self->{'num_datapoints'} )
432 {
433 if ( defined( $data->[$i][$j] ) && defined( $data->[ $i + 1 ][$j] ) )
434 {
435 if ( defined $max )
436 {
437 if ( ( $data->[$i][$j] + $data->[ $i + 1 ][$j] ) > $max )
438 {
439 $max = $data->[$i][$j] + $data->[ $i + 1 ][$j];
440 }
441 if ( ( $data->[$i][$j] - $data->[ $i + 1 ][$j] ) < $min )
442 {
443 $min = $data->[$i][$j] - $data->[ $i + 1 ][$j];
444 }
445 }
446 else { $min = $max = $data->[$i][$j]; }
447 }
448 }
449 }
450 }
451 return ( $min, $max );
452 }
453 }
454 ## be a good module and return 1
455 1;
« lib/Chart/Direction.pm ↑ Summary lib/Chart/HorizontalBars.pm »