lib/Chart/Direction.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/Composite.pm ↑ Summary lib/Chart/ErrorBars.pm »
1  
2 # circular oriented chart like rotating vectors
3  
4 4 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Chart::Direction;
7 our @ISA = qw(Chart::Base);
8 our $VERSION = 'v2.403.9';
9  
10 0 ▶use Chart::Base;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use GD;
12 use Carp;
13 use POSIX;
14  
15 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
16 # public methods go here #
17 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
18  
19 ## @method int set(%opts)
20 # @param[in] %opts Hash of options to the Chart
21 # @return ok or croak
22 #
23 # @brief
24 # Set all options
25 #
26 # @details
27 # main method for customizing the chart, lets users
28 # specify values for different parameters\n
29 # dont check the number of points in the added datasets in a polarplot\n
30 # overwrite Base method
31 #
32 sub set
33 {
34 my $self = shift;
35 my %opts = @_;
36  
37 # basic error checking on the options, just warn 'em
38 unless ( $#_ % 2 )
39 {
40 carp "Whoops, some option to be set didn't have a value.\n", "You might want to look at that.\n";
41 }
42  
43 # set the options
44 for ( keys %opts )
45 {
46 $self->{$_} = $opts{$_};
47  
48 # if someone wants to change the grid_lines color, we should set all
49 # the colors of the grid_lines
50 if ( $_ =~ /^colors$/ )
51 {
52 my %hash = %{ $opts{$_} };
53 foreach my $key ( sort keys %hash )
54 {
55 if ( $key =~ /^grid_lines$/ )
56 {
57 $self->{'colors'}{'y_grid_lines'} = $hash{'grid_lines'};
58 $self->{'colors'}{'x_grid_lines'} = $hash{'grid_lines'};
59 $self->{'colors'}{'y2_grid_lines'} = $hash{'grid_lines'};
60 }
61 }
62 }
63 }
64  
65 if ( $self->false( $self->{'polar'} ) && ( defined $self->{'croak'} ) )
66 {
67 carp "New data set to be added has an incorrect number of points";
68 }
69  
70 # now return
71 return 1;
72 }
73  
74 ## @method int add_dataset(@data)
75 # Add many datasets to the dataref
76 #
77 # Graph API\n
78 # Overwrite Base method
79 #
80 # @param @data Dataset to add
81 #
82 sub add_dataset
83 {
84 my $self = shift;
85 my @data = @_;
86  
87 # error check the data (carp, don't croak)
88 if ( $self->{'dataref'} && ( $#{ $self->{'dataref'}->[0] } != $#data ) )
89 {
90  
91 # carp "New data set to be added has an incorrect number of points";
92 $self->{'croak'} = 'true';
93 }
94  
95 # copy it into the dataref
96 push @{ $self->{'dataref'} }, [@data];
97  
98 # now return
99 return 1;
100 }
101  
102 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
103 # private methods go here #
104 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
105  
106 ## @fn private int _find_y_scale()
107 # we use the find_y_scale methode to determine the labels of the circles
108 # and the amount of them
109 # @return status
110 #
111 # This function is an overwrite to the same function found in the base class
112 # Chart::Base
113 #
114 sub _find_y_scale
115 {
116 my $self = shift;
117  
118 # Predeclare vars.
119 my ( $d_min, $d_max ); # Dataset min & max.
120 my ( $p_min, $p_max ); # Plot min & max.
121 my ( $tickInterval, $tickCount, $skip );
122 my @tickLabels; # List of labels for each tick.
123 my $maxtickLabelLen = 0; # The length of the longest tick label.
124  
125 # Find the datatset minimum and maximum.
126 ( $d_min, $d_max ) = $self->_find_y_range();
127  
128 # Force the inclusion of zero if the user has requested it.
129 if ( $self->true( $self->{'include_zero'} ) )
130 {
131 if ( ( $d_min * $d_max ) > 0 ) # If both are non zero and of the same sign.
132 {
133 if ( $d_min > 0 ) # If the whole scale is positive.
134 {
135 $d_min = 0;
136 }
137 else # The scale is entirely negative.
138 {
139 $d_max = 0;
140 }
141 }
142 }
143  
144 # Allow the dataset range to be overidden by the user.
145 # f_min/max are booleans which indicate that the min & max should not be modified.
146 my $f_min = defined $self->{'min_val'};
147 $d_min = $self->{'min_val'} if $f_min;
148  
149 my $f_max = defined $self->{'max_val'};
150 $d_max = $self->{'max_val'} if $f_max;
151  
152 # Assert against the min is larger than the max.
153 if ( $d_min > $d_max )
154 {
155 croak "The the specified 'min_val' & 'max_val' values are reversed (min > max: $d_min>$d_max)";
156 }
157  
158 # Calculate the width of the dataset. (posibly modified by the user)
159 my $d_width = $d_max - $d_min;
160  
161 # If the width of the range is zero, forcibly widen it
162 # (to avoid division by zero errors elsewhere in the code).
163 if ( 0 == $d_width )
164 {
165 $d_min--;
166 $d_max++;
167 $d_width = 2;
168 }
169  
170 # Descale the range by converting the dataset width into
171 # a floating point exponent & mantisa pair.
172 my ( $rangeExponent, $rangeMantisa ) = $self->_sepFP($d_width);
173 my $rangeMuliplier = 10**$rangeExponent;
174  
175 # Find what tick
176 # to use & how many ticks to plot,
177 # round the plot min & max to suatable round numbers.
178 ( $tickInterval, $tickCount, $p_min, $p_max ) = $self->_calcTickInterval(
179 $d_min / $rangeMuliplier,
180 $d_max / $rangeMuliplier,
181 $f_min, $f_max,
182 $self->{'min_circles'} + 1,
183 $self->{'max_circles'} + 1
184 );
185  
186 # Restore the tickInterval etc to the correct scale
187 $_ *= $rangeMuliplier foreach ( $tickInterval, $p_min, $p_max );
188  
189 #get the precision for the labels
190 my $precision = $self->{'precision'};
191  
192 # Now sort out an array of tick labels.
193  
194 if ( $self->false( $self->{'polar'} ) )
195 {
196 for ( my $labelNum = $p_min ; $labelNum <= $p_max ; $labelNum += $tickInterval )
197 {
198 my $labelText;
199  
200 if ( defined $self->{f_y_tick} )
201 {
202  
203 # Is _default_f_tick function used?
204 if ( $self->{f_y_tick} == \&Chart::Base::_default_f_tick )
205 {
206 $labelText = sprintf( "%." . $precision . "f", $labelNum );
207 }
208 else
209 {
210  
211 # print \&_default_f_tick;
212 $labelText = $self->{f_y_tick}->($labelNum);
213 }
214 }
215 else
216 {
217 $labelText = sprintf( "%." . $precision . "f", $labelNum );
218 }
219 push @tickLabels, $labelText;
220 $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText;
221 }
222 }
223 else
224 {
225  
226 # polar == true
227 for ( my $labelNum = $p_max ; $labelNum >= $p_min ; $labelNum -= $tickInterval )
228 {
229 my $labelText;
230  
231 if ( defined $self->{f_y_tick} )
232 {
233  
234 # Is _default_f_tick function used?
235 if ( $self->{f_y_tick} == \&Chart::Base::_default_f_tick )
236 {
237 $labelText = sprintf( "%." . $precision . "f", $labelNum );
238 }
239 else
240 {
241  
242 # print \&_default_f_tick;
243 $labelText = $self->{f_y_tick}->($labelNum);
244 }
245 }
246 else
247 {
248 $labelText = sprintf( "%." . $precision . "f", $labelNum );
249 }
250 push @tickLabels, $labelText;
251 $maxtickLabelLen = length $labelText if $maxtickLabelLen < length $labelText;
252 }
253 }
254  
255 # Store the calculated data.
256 $self->{'min_val'} = $p_min,
257 $self->{'max_val'} = $p_max,
258 $self->{'y_ticks'} = $tickCount,
259 $self->{'y_tick_labels'} = \@tickLabels,
260 $self->{'y_tick_label_length'} = $maxtickLabelLen;
261  
262 # and return.
263 return 1;
264 }
265  
266 ## @fn private _calcTickInterval($dataset_min, $dataset_max, $flag_fixed_min, $flag_fixed_max, $minTicks, $maxTicks)
267 # @brief
268 # Calculates the ticks for direction in normalised units.
269 #
270 # @details
271 # Calculate the Interval between ticks in y direction
272 # and compare the number of ticks to
273 # the user given values min_y_ticks, max_y_ticks
274 #
275 # @param[in] dataset_min Minimal value in y direction
276 # @param[in] dataset_max Maximal value in y direction
277 # @param[in] flag_fixed_min Indicator whether the dataset_min value is fixed
278 # @param[in] flag_fixed_max Indicator whether the dataset_max value is fixed
279 # @param[in] minTicks Minimal number of ticks wanted
280 # @param[in] maxTicks Maximal number of ticks wanted
281 # @return $tickInterval, $tickCount, $pMin, $pMax
282 #
283  
284 sub _calcTickInterval
285 {
286 my $self = shift;
287 my (
288 $min, $max, # The dataset min & max.
289 $minF, $maxF, # Indicates if those min/max are fixed.
290 $minTicks, $maxTicks, # The minimum & maximum number of ticks.
291 ) = @_;
292  
293 # Verify the supplied 'min_y_ticks' & 'max_y_ticks' are sensible.
294 if ( $minTicks < 2 )
295 {
296 carp "Chart::Direction : Incorrect value for 'min_circles', too small.\n";
297 $minTicks = 2;
298 }
299  
300 if ( $maxTicks < 5 * $minTicks )
301 {
302 carp "Chart::Direction : Incorrect value for 'max_circles', too small.\n";
303 $maxTicks = 5 * $minTicks;
304 }
305  
306 my $width = $max - $min;
307 my @divisorList;
308  
309 for ( my $baseMul = 1 ; ; $baseMul *= 10 )
310 {
311 TRY: foreach my $tryMul ( 1, 2, 5 )
312 {
313  
314 # Calc a fresh, smaller tick interval.
315 my $divisor = $baseMul * $tryMul;
316  
317 # Count the number of ticks.
318 my ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor );
319  
320 # Look a the number of ticks.
321 if ( $maxTicks < $tickCount )
322 {
323  
324 # If it is too high, Backtrack.
325 $divisor = pop @divisorList;
326  
327 # just for security:
328 if ( !defined($divisor) || $divisor == 0 ) { $divisor = 1; }
329 ( $tickCount, $pMin, $pMax ) = $self->_countTicks( $min, $max, 1 / $divisor );
330 carp "Chart::Direction : Caution: Tick limit of $maxTicks exceeded. Backing of to an interval of "
331 . 1 / $divisor
332 . " which plots $tickCount ticks\n";
333  
334 return ( 1 / $divisor, $tickCount, $pMin, $pMax );
335 }
336 elsif ( $minTicks > $tickCount )
337 {
338  
339 # If it is too low, try again.
340 next TRY;
341 }
342 else
343 {
344  
345 # Store the divisor for possible later backtracking.
346 push @divisorList, $divisor;
347  
348 # if the min or max is fixed, check they will fit in the interval.
349 next TRY if ( $minF && ( int( $min * $divisor ) != ( $min * $divisor ) ) );
350 next TRY if ( $maxF && ( int( $max * $divisor ) != ( $max * $divisor ) ) );
351  
352 # If everything passes the tests, return.
353 return ( 1 / $divisor, $tickCount, $pMin, $pMax );
354 }
355 }
356 }
357 die "can't happen!";
358 }
359  
360 ## @fn private int _draw_y_ticks()
361 # draw the circles and the axes
362 #
363 # Overwrites _draw_y_ticks() of Base class
364 #
365 # @return status
366 sub _draw_y_ticks
367 {
368 my $self = shift;
369 my $data = $self->{'dataref'};
370 my $misccolor = $self->_color_role_to_index('misc');
371 my $textcolor = $self->_color_role_to_index('text');
372 my $background = $self->_color_role_to_index('background');
373 my @labels = @{ $self->{'y_tick_labels'} };
374 my ( $width, $height, $centerX, $centerY, $diameter );
375 my ( $pi, $font, $fontW, $fontH, $labelX, $labelY, $label_offset );
376 my ( $dia_delta, $dia, $x, $y, @label_degrees, $arc, $angle_interval );
377  
378 # set up initial constant values
379 $pi = 3.14159265358979323846,
380 $font = $self->{'legend_font'},
381 $fontW = $self->{'legend_font'}->width,
382 $fontH = $self->{'legend_font'}->height,
383 $angle_interval = $self->{'angle_interval'};
384  
385 if ( $self->true( $self->{'grey_background'} ) )
386 {
387 $background = $self->_color_role_to_index('grey_background');
388 }
389  
390 # init the imagemap data field if they wanted it
391 if ( $self->true( $self->{'imagemap'} ) )
392 {
393 $self->{'imagemap_data'} = [];
394 }
395  
396 # find width and height
397 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
398 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
399  
400 # find center point, from which the pie will be drawn around
401 $centerX = int( $width / 2 + $self->{'curr_x_min'} );
402 $centerY = int( $height / 2 + $self->{'curr_y_min'} );
403  
404 # always draw a circle, which means the diameter will be the smaller
405 # of the width and height. let enough space for the labels.
406  
407 ## @todo calculate the width of the labels
408  
409 if ( $width < $height )
410 {
411 $diameter = $width - 110;
412 }
413 else
414 {
415 $diameter = $height - 80;
416 }
417  
418 #the difference between the diameter of two following circles;
419 $dia_delta = ceil( $diameter / ( $self->{'y_ticks'} - 1 ) );
420  
421 #store the calculated data
422 $self->{'centerX'} = $centerX;
423 $self->{'centerY'} = $centerY;
424 $self->{'diameter'} = $diameter;
425  
426 #draw the axes and its labels
427 # set up an array of labels for the axes
428 if ( $angle_interval == 0 )
429 {
430 @label_degrees = ();
431 }
432 elsif ( $angle_interval <= 5 && $angle_interval > 0 )
433 {
434 @label_degrees = qw(180 175 170 165 160 155 150 145 140 135 130 125 120 115
435 110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 355 350
436 345 340 335 330 325 320 315 310 305 300 295 290 285 280 275 270 265 260 255
437 250 245 240 235 230 225 220 215 210 205 200 195 190 185);
438 $angle_interval = 5;
439 }
440 elsif ( $angle_interval <= 10 && $angle_interval > 5 )
441 {
442 @label_degrees = qw(180 170 160 150 140 130 120 110 100 90 80 70 60 50 40
443 30 20 10 0 350 340 330 320 310 300 290 280 270 260 250 240 230 220 210 200 190);
444 $angle_interval = 10;
445 }
446 elsif ( $angle_interval <= 15 && $angle_interval > 10 )
447 {
448 @label_degrees = qw(180 165 150 135 120 105 90 75 60 45 30 15 0 345 330 315 300
449 285 270 255 240 225 210 195);
450 $angle_interval = 15;
451 }
452 elsif ( $angle_interval <= 20 && $angle_interval > 15 )
453 {
454 @label_degrees = qw(180 160 140 120 100 80 60 40 20 0 340 320 300 280 260 240
455 220 200);
456 $angle_interval = 20;
457 }
458 elsif ( $angle_interval <= 30 && $angle_interval > 20 )
459 {
460 @label_degrees = qw(180 150 120 90 60 30 0 330 300 270 240 210);
461 $angle_interval = 30;
462 }
463 elsif ( $angle_interval <= 45 && $angle_interval > 30 )
464 {
465 @label_degrees = qw(180 135 90 45 0 315 270 225);
466 $angle_interval = 45;
467 }
468 elsif ( $angle_interval <= 90 && $angle_interval > 45 )
469 {
470 @label_degrees = qw(180 90 0 270);
471 $angle_interval = 90;
472 }
473 else
474 {
475 carp "The angle_interval must be between 0 and 90!\nCorrected value: 30";
476 @label_degrees = qw(180 150 120 90 60 30 0 330 300 270 240 210);
477 $angle_interval = 30;
478 }
479 $arc = 0;
480  
481 foreach (@label_degrees)
482 {
483  
484 #calculated the coordinates of the end point of the line
485 $x = sin($arc) * ( $diameter / 2 + 10 ) + $centerX;
486 $y = cos($arc) * ( $diameter / 2 + 10 ) + $centerY;
487  
488 #some ugly correcture
489 if ( $_ == '270' ) { $y++; }
490  
491 #draw the line
492 $self->{'gd_obj'}->line( $centerX, $centerY, $x, $y, $misccolor );
493  
494 #calculate the string point
495 $x = sin($arc) * ( $diameter / 2 + 30 ) + $centerX - 8;
496 $y = cos($arc) * ( $diameter / 2 + 28 ) + $centerY - 6;
497  
498 #draw the labels
499 $self->{'gd_obj'}->string( $font, $x, $y, $_ . '\B0', $textcolor );
500 $arc += ( ($angle_interval) / 360 ) * 2 * $pi;
501 }
502  
503 #draw the circles
504 $dia = 0;
505 foreach (@labels)
506 {
507 $self->{'gd_obj'}->arc( $centerX, $centerY, $dia, $dia, 0, 360, $misccolor );
508 $dia += $dia_delta;
509 }
510  
511 $self->{'gd_obj'}->filledRectangle(
512 $centerX - length( $labels[0] ) / 2 * $fontW - 2,
513 $centerY + 2,
514 $centerX + 2 + $diameter / 2,
515 $centerY + $fontH + 2, $background
516 );
517  
518 #draw the labels of the circles
519 $dia = 0;
520 foreach (@labels)
521 {
522 $self->{'gd_obj'}->string( $font, $centerX + $dia / 2 - length($_) / 2 * $fontW, $centerY + 2, $_, $textcolor );
523 $dia += $dia_delta;
524 }
525  
526 return 1;
527 }
528  
529 ## @fn private int _draw_x_ticks()
530 # We don't need x ticks, it's all done in _draw_y_ticks
531 # @return status
532 #
533 # Overwrites the corresponding function in Base
534 #
535 sub _draw_x_ticks
536 {
537 my $self = shift;
538  
539 return 1;
540 }
541  
542 ## @fn private _draw_data
543 # finally get around to plotting the data for direction charts
544 sub _draw_data
545 {
546 my $self = shift;
547 my $data = $self->{'dataref'};
548 my $misccolor = $self->_color_role_to_index('misc');
549 my $textcolor = $self->_color_role_to_index('text');
550 my $background = $self->_color_role_to_index('background');
551 my ( $width, $height, $centerX, $centerY, $diameter );
552 my ( $mod, $map, $i, $j, $brush, $color, $x, $y, $winkel, $first_x, $first_y );
553 my ( $arrow_x, $arrow_y, $m );
554 $color = 1;
555  
556 my $pi = 3.14159265358979323846;
557 my $len = 10;
558 my $alpha = 1;
559 my $last_x = undef;
560 my $last_y = undef;
561 my $diff;
562 my $n = 0;
563  
564 if ( $self->true( $self->{'pairs'} ) )
565 {
566 my $a = $self->{'num_datasets'} / 2;
567 my $b = ceil($a);
568 my $c = $b - $a;
569  
570 if ( $c == 0 )
571 {
572 croak "Wrong number of datasets for 'pairs'";
573 }
574 }
575  
576 # init the imagemap data field if they wanted it
577 if ( $self->true( $self->{'imagemap'} ) )
578 {
579 $self->{'imagemap_data'} = [];
580 }
581  
582 # find width and height
583 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
584 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
585  
586 # get the base values
587  
588 if ( $self->false( $self->{'polar'} ) )
589 {
590 $mod = $self->{'min_val'};
591 }
592 else
593 {
594 $mod = $self->{'max_val'};
595 }
596 $centerX = $self->{'centerX'};
597 $centerY = $self->{'centerY'};
598 $diameter = $self->{'diameter'};
599 $diff = $self->{'max_val'} - $self->{'min_val'};
600 $diff = 1 if $diff < 1;
601 $map = $diameter / 2 / $diff;
602  
603 $brush = $self->_prepare_brush( $color, 'point' );
604 $self->{'gd_obj'}->setBrush($brush);
605  
606 # draw every line for this dataset
607  
608 if ( $self->false( $self->{'pairs'} ) )
609 {
610 for $j ( 1 .. $self->{'num_datasets'} )
611 {
612 $color = $self->_color_role_to_index( 'dataset' . ( $j - 1 ) );
613  
614 for $i ( 0 .. $self->{'num_datapoints'} - 1 )
615 {
616  
617 # don't try to draw anything if there's no data
618 if ( defined( $data->[$j][$i] )
619 && $data->[$j][$i] <= $self->{'max_val'}
620 && $data->[$j][$i] >= $self->{'min_val'} )
621 { #calculate the point
622 $winkel = ( 180 - ( $data->[0][$i] % 360 ) ) / 360 * 2 * $pi;
623  
624 if ( $self->false( $self->{'polar'} ) )
625 {
626 $x = ceil( $centerX + sin($winkel) * ( $data->[$j][$i] - $mod ) * $map );
627 $y = ceil( $centerY + cos($winkel) * ( $data->[$j][$i] - $mod ) * $map );
628 }
629 else
630 {
631 $x = ceil( $centerX + sin($winkel) * ( $mod - $data->[$j][$i] ) * $map );
632 $y = ceil( $centerY + cos($winkel) * ( $mod - $data->[$j][$i] ) * $map );
633 }
634  
635 # set the x and y values back
636 if ( $i == 0 )
637 {
638 $first_x = $x;
639 $first_y = $y;
640 $last_x = $x;
641 $last_y = $y;
642 }
643  
644 if ( $self->true( $self->{'point'} ) )
645 {
646 $brush = $self->_prepare_brush( $color, 'point' );
647 $self->{'gd_obj'}->setBrush($brush);
648  
649 #draw the point
650 $self->{'gd_obj'}->line( $x + 1, $y, $x, $y, gdBrushed );
651 }
652 if ( $self->true( $self->{'line'} ) )
653 {
654 $brush = $self->_prepare_brush( $color, 'line' );
655 $self->{'gd_obj'}->setBrush($brush);
656  
657 #draw the line
658 if ( defined $last_x )
659 {
660 $self->{'gd_obj'}->line( $x, $y, $last_x, $last_y, gdBrushed );
661 }
662 }
663  
664 if ( $self->true( $self->{'arrow'} ) )
665 {
666 $brush = $self->_prepare_brush( $color, 'line' );
667 $self->{'gd_obj'}->setBrush($brush);
668  
669 #draw the arrow
670 if ( $data->[$j][$i] > $self->{'min_val'} )
671 {
672 $self->{'gd_obj'}->line( $x, $y, $centerX, $centerY, gdBrushed );
673  
674 $arrow_x = $x - cos( $winkel - $alpha ) * $len;
675 $arrow_y = $y + sin( $winkel - $alpha ) * $len;
676 $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );
677  
678 $arrow_x = $x + sin( $pi / 2 - $winkel - $alpha ) * $len;
679 $arrow_y = $y - cos( $pi / 2 - $winkel - $alpha ) * $len;
680 $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );
681  
682 }
683 }
684  
685 $last_x = $x;
686 $last_y = $y;
687  
688 # store the imagemap data if they asked for it
689 if ( $self->true( $self->{'imagemap'} ) )
690 {
691 $self->{'imagemap_data'}->[$j][$i] = [ $x, $y ];
692 }
693 }
694 else
695 {
696 if ( $self->true( $self->{'imagemap'} ) )
697 {
698 $self->{'imagemap_data'}->[$j][$i] = [ undef(), undef() ];
699 }
700 }
701 } # end for
702  
703 # draw the last line to the first point
704 if ( $self->true( $self->{'line'} ) )
705 {
706 $self->{'gd_obj'}->line( $x, $y, $first_x, $first_y, gdBrushed );
707 }
708  
709 } # end for $j
710 }
711  
712 if ( $self->true( $self->{'pairs'} ) )
713 {
714 for ( $j = 1 ; $j <= $self->{'num_datasets'} ; $j += 2 )
715 {
716 if ( $j == 1 )
717 {
718 $color = $self->_color_role_to_index( 'dataset' . ( $j - 1 ) );
719 }
720 else
721 {
722 $color = $self->_color_role_to_index( 'dataset' . ( $j / 2 - 0.5 ) );
723 }
724  
725 ##### $color = $self->_color_role_to_index('dataset'.(1)); #####################
726  
727 for $i ( 0 .. $self->{'num_datapoints'} - 1 )
728 {
729  
730 # don't try to draw anything if there's no data
731 if ( defined( $data->[$j][$i] )
732 && $data->[$j][$i] <= $self->{'max_val'}
733 && $data->[$j][$i] >= $self->{'min_val'} )
734 {
735  
736 # calculate the point
737 $winkel = ( 180 - ( $data->[$n][$i] % 360 ) ) / 360 * 2 * $pi;
738  
739 if ( $self->false( $self->{'polar'} ) )
740 {
741 $x = ceil( $centerX + sin($winkel) * ( $data->[$j][$i] - $mod ) * $map );
742 $y = ceil( $centerY + cos($winkel) * ( $data->[$j][$i] - $mod ) * $map );
743 }
744 else
745 {
746 $x = ceil( $centerX + sin($winkel) * ( $mod - $data->[$j][$i] ) * $map );
747 $y = ceil( $centerY + cos($winkel) * ( $mod - $data->[$j][$i] ) * $map );
748 }
749  
750 # set the x and y values back
751 if ( $i == 0 )
752 {
753 $first_x = $x;
754 $first_y = $y;
755 $last_x = $x;
756 $last_y = $y;
757 }
758  
759 if ( $self->true( $self->{'point'} ) )
760 {
761 $brush = $self->_prepare_brush( $color, 'point' );
762 $self->{'gd_obj'}->setBrush($brush);
763  
764 #draw the point
765 $self->{'gd_obj'}->line( $x + 1, $y, $x, $y, gdBrushed );
766 }
767 if ( $self->true( $self->{'line'} ) )
768 {
769 $brush = $self->_prepare_brush( $color, 'line' );
770 $self->{'gd_obj'}->setBrush($brush);
771  
772 #draw the line
773 if ( defined $last_x )
774 {
775 $self->{'gd_obj'}->line( $x, $y, $last_x, $last_y, gdBrushed );
776 }
777 else { }
778 }
779  
780 if ( $self->true( $self->{'arrow'} ) )
781 {
782 $brush = $self->_prepare_brush( $color, 'line' );
783 $self->{'gd_obj'}->setBrush($brush);
784  
785 #draw the arrow
786 if ( $data->[$j][$i] > $self->{'min_val'} )
787 {
788 $self->{'gd_obj'}->line( $x, $y, $centerX, $centerY, gdBrushed );
789  
790 $arrow_x = $x - cos( $winkel - $alpha ) * $len;
791 $arrow_y = $y + sin( $winkel - $alpha ) * $len;
792 $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );
793  
794 $arrow_x = $x + sin( $pi / 2 - $winkel - $alpha ) * $len;
795 $arrow_y = $y - cos( $pi / 2 - $winkel - $alpha ) * $len;
796 $self->{'gd_obj'}->line( $x, $y, $arrow_x, $arrow_y, gdBrushed );
797 }
798 }
799  
800 $last_x = $x;
801 $last_y = $y;
802  
803 # store the imagemap data if they asked for it
804 if ( $self->true( $self->{'imagemap'} ) )
805 {
806 $self->{'imagemap_data'}->[$j][$i] = [ $x, $y ];
807 }
808 } # end if ( defined ...
809 else
810 {
811 if ( $self->true( $self->{'imagemap'} ) )
812 {
813 $self->{'imagemap_data'}->[$j][$i] = [ undef(), undef() ];
814 }
815 }
816 } #end for $i
817  
818 # draw the last line to the first point
819 if ( $self->true( $self->{'line'} ) )
820 {
821 $self->{'gd_obj'}->line( $x, $y, $first_x, $first_y, gdBrushed );
822 }
823 $n += 2;
824 } # end for $j
825 } # end if pairs
826  
827 # now outline it
828 $self->{'gd_obj'}
829 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
830  
831 return;
832 }
833  
834 ## @fn private int _prepare_brush($color,$type)
835 # set the gdBrush object to trick GD into drawing fat lines
836 #
837 #
838 # @param[in] color Color to be used
839 # @param[in] type Type of line
840 # @return status
841 sub _prepare_brush
842 {
843 my $self = shift;
844 my $color = shift;
845 my $type = shift;
846 my ( $radius, @rgb, $brush, $white, $newcolor );
847  
848 @rgb = $self->{'gd_obj'}->rgb($color);
849  
850 # get the appropriate brush size
851 if ( $type eq 'line' )
852 {
853 $radius = $self->{'brush_size'} / 2;
854 }
855 elsif ( $type eq 'point' )
856 {
857 $radius = $self->{'pt_size'} / 2;
858 }
859  
860 # create the new image
861 $brush = GD::Image->new( $radius * 2, $radius * 2 );
862  
863 # get the colors, make the background transparent
864 $white = $brush->colorAllocate( 255, 255, 255 );
865 $newcolor = $brush->colorAllocate(@rgb);
866 $brush->transparent($white);
867  
868 # draw the circle
869 $brush->arc( $radius - 1, $radius - 1, $radius, $radius, 0, 360, $newcolor );
870  
871 # fill it if we're using lines
872 $brush->fill( $radius - 1, $radius - 1, $newcolor );
873  
874 # set the new image as the main object's brush
875 return $brush;
876 }
877  
878 ## @fn private int _draw_legend()
879 # let them know what all the pretty colors mean
880 # @return status
881 #
882 # Overwrite corresponding function of Base
883 #
884 sub _draw_legend
885 {
886 my $self = shift;
887 my $length;
888  
889 # check to see if legend type is none..
890 if ( $self->{'legend'} =~ /^none$/ )
891 {
892 return 1;
893 }
894  
895 # check to see if they have as many labels as datasets,
896 # warn them if not
897 if ( ( $#{ $self->{'legend_labels'} } >= 0 )
898 && ( ( scalar( @{ $self->{'legend_labels'} } ) ) != $self->{'num_datasets'} ) )
899 {
900 carp "The number of legend labels and datasets doesn\'t match";
901 }
902  
903 # init a field to store the length of the longest legend label
904 unless ( $self->{'max_legend_label'} )
905 {
906 $self->{'max_legend_label'} = 0;
907 }
908  
909 # fill in the legend labels, find the longest one
910  
911 if ( $self->false( $self->{'pairs'} ) )
912 {
913 for ( 1 .. $self->{'num_datasets'} )
914 {
915 unless ( $self->{'legend_labels'}[ $_ - 1 ] )
916 {
917 $self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
918 }
919 $length = length( $self->{'legend_labels'}[ $_ - 1 ] );
920 if ( $length > $self->{'max_legend_label'} )
921 {
922 $self->{'max_legend_label'} = $length;
923 }
924 } #end for
925 }
926  
927 if ( $self->true( $self->{'pairs'} ) )
928 {
929  
930 for ( 1 .. ceil( $self->{'num_datasets'} / 2 ) )
931 {
932 unless ( $self->{'legend_labels'}[ $_ - 1 ] )
933 {
934 $self->{'legend_labels'}[ $_ - 1 ] = "Dataset $_";
935 }
936 $length = length( $self->{'legend_labels'}[ $_ - 1 ] );
937 if ( $length > $self->{'max_legend_label'} )
938 {
939 $self->{'max_legend_label'} = $length;
940 }
941 }
942 }
943  
944 # different legend types
945 if ( $self->{'legend'} eq 'bottom' )
946 {
947 $self->_draw_bottom_legend;
948 }
949 elsif ( $self->{'legend'} eq 'right' )
950 {
951 $self->_draw_right_legend;
952 }
953 elsif ( $self->{'legend'} eq 'left' )
954 {
955 $self->_draw_left_legend;
956 }
957 elsif ( $self->{'legend'} eq 'top' )
958 {
959 $self->_draw_top_legend;
960 }
961 else
962 {
963 carp "I can't put a legend there (at " . $self->{'legend'} . ")\n";
964 }
965  
966 # and return
967 return 1;
968 }
969  
970 ## @fn private array _find_y_range()
971 # Find minimum and maximum value of y data sets.
972 #
973 # @return ( min, max, flag_all_integers )
974 #
975 # Overwrites corresponding Base function
976 #
977 sub _find_y_range
978 {
979 my $self = shift;
980 my $data = $self->{'dataref'};
981  
982 my $max = undef;
983 my $min = undef;
984 my $k = 1;
985 my $dataset = 1;
986 my $datum;
987  
988 if ( $self->false( $self->{'pairs'} ) )
989 {
990 for $dataset ( @$data[ 1 .. $#$data ] )
991 {
992  
993 # print "dataset @$dataset\n";
994 for $datum (@$dataset)
995 {
996 if ( defined $datum )
997 {
998  
999 # Prettier, but probably slower:
1000 # $max = $datum unless defined $max && $max >= $datum;
1001 # $min = $datum unless defined $min && $min <= $datum;
1002 if ( defined $max )
1003 {
1004 if ( $datum > $max ) { $max = $datum; }
1005 elsif ( $datum < $min ) { $min = $datum; }
1006 }
1007 else
1008 {
1009 $min = $max = $datum;
1010 }
1011 } #endif defined
1012 } # end for
1013 }
1014 }
1015  
1016 if ( $self->true( $self->{'pairs'} ) )
1017 {
1018  
1019 # only every second dataset must be checked
1020 for $dataset ( @$data[$k] )
1021 {
1022 for $datum (@$dataset)
1023 {
1024 if ( defined $datum )
1025 {
1026  
1027 # Prettier, but probably slower:
1028 # $max = $datum unless defined $max && $max >= $datum;
1029 # $min = $datum unless defined $min && $min <= $datum;
1030 if ( defined $max )
1031 {
1032 if ( $datum > $max ) { $max = $datum; }
1033 elsif ( $datum < $min ) { $min = $datum; }
1034 }
1035 else
1036 {
1037 $min = $max = $datum;
1038 }
1039 }
1040 }
1041 $k += 2;
1042 }
1043 }
1044  
1045 ( $min, $max );
1046 }
1047  
1048 ## be a good module and return 1
1049 1;
« lib/Chart/Composite.pm ↑ Summary lib/Chart/ErrorBars.pm »