lib/Chart/Pie.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Chart/StackedBars.pm ↑ Summary lib/Chart/Points.pm »
1  
2 # pie and doughnut (ring) charts
3  
4 11 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Chart::Pie;
7 our @ISA = qw(Chart::Base);
8 our $VERSION = 'v2.403.9';
9  
10 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Chart::Constants;
13 use Chart::Base ;
14  
15  
16 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
17 # public methods go here #
18 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
19  
20 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
21 # private methods go here #
22 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
23  
24 ## @fn private _draw_data
25 # @brief
26 # finally get around to plotting the data
27 #
28 # @details
29 # The user may define the kind of labelling the data by setting\n
30 # 'label_values' to 'percent' if she wants to have the percentages\n
31 # 'label_values' to 'values' if she wants to have the absolut values\n
32 # 'label_values' to 'both' if she wants to have absolut values and percentages\n
33 # 'label_values' to 'none' if she wants to have neither absolute values nor percentages\n
34 # 'ring' to a number less then 1 to define a ring as output;
35 # if 'ring' is 1 ore greater a full pie is plotted\n
36 #
37 sub _draw_data
38 {
39 my $self = shift;
40 my $data = $self->{'dataref'};
41 my $misccolor = $self->_color_role_to_index('misc');
42 my $textcolor = $self->_color_role_to_index('text');
43 my $background = $self->_color_role_to_index('background');
44 my ( $width, $height, $centerX, $centerY, $diameter, $diameter_previous, $text_diameter );
45 my $dataset_sum;
46 my ( $start_degrees, $end_degrees, $label_degrees, $label_old_degrees, $labelY_repeat_count );
47 my ( $pi, $rd2dg, $dg2rd );
48 my ( $font, $fontW, $fontH, $labelX, $labelY );
49 my $label;
50 my ( $i, $j, $color );
51 my $label_length = 0;
52 my $degrees = 0;
53 my $insidecolor;
54 my $forbidden_degrees = 0; # last occupied degree
55 my %labelinfo;
56 my $max_val_len = 0;
57 my $max_label_len = 0;
58  
59 # set up initial constant values
60 $pi = Chart::Constants::PI;
61  
62 $dg2rd = $pi / 180; # Degree to Radians
63 $rd2dg = 180 / $pi; # Radian to Degree
64 $start_degrees = 0;
65 $end_degrees = 0;
66 $font = $self->{'legend_font'};
67 $fontW = $self->{'legend_font'}->width;
68 $fontH = $self->{'legend_font'}->height;
69 $label_degrees = $labelY_repeat_count = 0;
70  
71 # init the imagemap data field if they wanted it
72 if ( $self->true( $self->{'imagemap'} ) )
73 {
74 $self->{'imagemap_data'} = [];
75 }
76  
77 # find width and height of the plotting area
78 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
79 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
80  
81 # okay, add up all the numbers of all the datasets, to get the
82 # sum total. This will be used to determine the percentage
83 # of each dataset. Obviously, negative numbers might be bad :)
84 $dataset_sum = 0;
85 for $j ( 0 .. $self->{'num_datapoints'} )
86 {
87  
88 if ( defined $data->[1][$j] && $data->[1][$j] > 0 )
89 {
90  
91 #add to sum
92 $dataset_sum += $data->[1][$j];
93  
94 #don't allow negativ values
95 if ( $data->[1][$j] < 0 )
96 {
97 croak "We need positiv data for a pie chart (which is not true for data[$j])!";
98 }
99 }
100 }
101  
102 # find the longest label
103 # first we need the length of the values
104 $max_label_len = 1;
105 for $j ( 0 .. ( $self->{'num_datapoints'} - 1 ) )
106 {
107  
108 # don't try to draw anything if there's no data
109 $labelinfo{$j}{data} = 'undefined';
110 if ( defined( $data->[1][$j] ) && $data->[1][$j] > 0 )
111 {
112 $labelinfo{$j}{data} = $data->[1][$j];
113 $label = $data->[0][$j];
114 $labelinfo{$j}{labeldata} = $label;
115  
116 if ( defined $self->{'label_values'} )
117 {
118 if ( $self->{'label_values'} =~ /^percent$/i )
119 {
120 $label = sprintf( "%s %4.2f%%", $label, $data->[1][$j] / ( $dataset_sum || 1 ) * 100 );
121 }
122 elsif ( $self->{'label_values'} =~ /^value$/i )
123 {
124 if ( $data->[1][$j] =~ /\./ )
125 {
126 $label = sprintf( "%s %.2f", $label, $data->[1][$j] );
127 }
128 else
129 {
130 $label = sprintf( "%s %d", $label, $data->[1][$j] );
131 }
132 }
133 elsif ( $self->{'label_values'} =~ /^both$/i )
134 {
135 if ( $data->[1][$j] =~ /\./ )
136 {
137 $label =
138 sprintf( "%s %4.2f%% %.2f", $label, $data->[1][$j] / ( $dataset_sum || 1 ) * 100, $data->[1][$j] );
139 }
140 else
141 {
142 $label =
143 sprintf( "%s %4.2f%% %d", $label, $data->[1][$j] / ( $dataset_sum || 1 ) * 100, $data->[1][$j] );
144 }
145 }
146 elsif ( $self->{'label_values'} =~ /^none$/i )
147 {
148 $label = sprintf( "%s", $label );
149 }
150 }
151 $label_length = length($label);
152 $labelinfo{$j}{labelstring} = $label, $labelinfo{$j}{labellength} = $label_length;
153  
154 }
155 $max_label_len = $label_length if ( $max_label_len < $label_length );
156 }
157 $max_label_len *= $fontW;
158  
159 # find center point, from which the pie will be drawn around
160 $centerX = int( $width / 2 ) + $self->{'curr_x_min'};
161 $centerY = int( $height / 2 ) + $self->{'curr_y_min'};
162  
163 # @details
164 # always draw a circle, which means the diameter will be the smaller
165 # of the width and height. let enough space for the labels.\n
166 # Calculate the space needed for the labels by taking
167 # into account the angles where the labels will be plotted
168  
169 my $labeldistance = 2 * $self->maximum( $fontW, $fontH );
170 $start_degrees = 0;
171 $end_degrees = 0;
172 my $max_radius = $self->minimum( $width, $height ) / 2;
173 my $radius = $max_radius;
174  
175 for $j ( 0 .. ( $self->{'num_datapoints'} - 1 ) )
176 {
177  
178 # So, get the degree offset for this dataset
179 $end_degrees = $start_degrees + ( $data->[1][$j] / ( $dataset_sum || 1 ) * 360 );
180  
181 $degrees = $start_degrees + ( $end_degrees - $start_degrees ) / 2;
182  
183 # stick the label in the middle of the slice
184 $label_degrees = ( $start_degrees + $end_degrees ) / 2;
185  
186 #print "Vor Modulo: label_degrees = $label_degrees\n";
187 $label_degrees = $self->modulo( $label_degrees, 360.0 );
188  
189 #print "Nach Modulo: label_degrees = $label_degrees\n";
190  
191 $label = $labelinfo{$j}{labelstring};
192 $label_length = $labelinfo{$j}{labellength} || 0;
193  
194 #!!DEBUG!!!!
195 #print "Text=".$label.", StartDegrees=".$start_degrees.
196 # ", end=".$end_degrees.
197 # ", label_degree=".$label_degrees."\n";
198  
199 # 0 degrees means east
200 # 90 degrees means south
201 # 180 degrees means west
202 # 270 degrees means north
203 # 360 degrees means east again
204 if ( ( $label_degrees >= 270 && $label_degrees <= 360 )
205 || ( $label_degrees >= 0 && $label_degrees <= 90 ) )
206 {
207  
208 # right side of the circle
209 {
210  
211 # $x value in respect of label arc
212 my $x = $radius * sin( $dg2rd * $label_degrees );
213 my $y = $radius * cos( $dg2rd * $label_degrees );
214  
215 #!!DEBUG!!!!
216 #print "Text=".$label.": x=$x, y=$y\n";
217  
218 # i.e. the startpoint is at $centerX+$x, $centerY-$y
219 #test
220 #$self->{'gd_obj'}->rectangle( $centerX, $centerY, $centerX+$x, $centerY-$y, $misccolor );
221  
222 # theoretical right value in respect to radius and length of label
223 my $right_pos = $centerX + $x + $label_length * $fontW;
224 if ( $right_pos > $self->{'curr_x_max'} )
225 {
226  
227 # too far right, correct x
228 $right_pos = $self->{'curr_x_max'};
229 $x = $right_pos - $centerX - $label_length * $fontW;
230  
231 #!!DEBUG!!!!
232 #print "too far right: Text=".$label.": x=$x, y=$y\n";
233 }
234  
235 # theoretical top position in respect to radius and height of label
236 # (Remark: direction to the top of the picture counts backwards!)
237 my $top_pos = $centerY - $y - $fontH;
238 if ( $top_pos < $self->{'curr_y_min'} )
239 {
240  
241 # too far up, correct $y
242 $top_pos = $self->{'curr_y_min'};
243 $y = $centerY - $top_pos - $fontH;
244  
245 #!!DEBUG!!!!
246 #print "too far up: Text=".$label.": x=$x, y=$y\n";
247 }
248  
249 my $down_pos = $centerY + $y + $fontH;
250 if ( $down_pos > $self->{'curr_y_max'} )
251 {
252 $down_pos = $self->{'curr_y_max'};
253 $y = $down_pos - $centerY - $fontH;
254  
255 #!!DEBUG!!!!
256 #print "too far down: Text=".$label.": x=$x, y=$y\n";
257 }
258  
259 #test
260 #$self->{'gd_obj'}->rectangle( $centerX+$x, $centerY-$y, $right_pos, $top_pos, $textcolor );
261 #$self->{'gd_obj'}
262 # ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
263 #return;
264  
265 #!!DEBUG!!!!
266 #print "before Line 270: label_degrees=$label_degrees, cos()=". cos( $dg2rd * $label_degrees ). ", sin()=". sin( $dg2rd * $label_degrees )."\n";
267 if ( $label_degrees == 0 )
268 {
269 $radius = $self->minimum( $radius, abs( $y / cos( $dg2rd * $label_degrees ) ) );
270 }
271 else
272 {
273 $radius =
274 $self->minimum( $radius, $x / sin( $dg2rd * $label_degrees ),
275 abs( $y / cos( $dg2rd * $label_degrees ) ) );
276 }
277 $radius = int( $radius + 0.5 );
278  
279 #!!DEBUG!!
280 #$self->{'gd_obj'}->line( $centerX, $centerY, $centerX+$radius, $centerY, gdBrushed );
281  
282 }
283 if ( $radius <= 0 ) { croak "radius < 0!"; }
284 }
285 else
286 { # left side of the circle
287 # as 0 degrees means east
288  
289 if ( abs($label_degrees) < 0.1 )
290 {
291  
292 # too small angle
293 $radius = $self->{'curr_x_max'} - $label_length * $fontW;
294 }
295 else
296 {
297  
298 # $x value in respect of label arc
299 my $x = $radius * sin( $dg2rd * $label_degrees );
300 my $y = $radius * cos( $dg2rd * $label_degrees );
301  
302 # i.e. the startpoint is at $centerX-$x, $centerY+$y
303 #test
304 #$self->{'gd_obj'}->rectangle( $centerX, $centerY, $centerX-$x, $centerY+$y, $misccolor );
305  
306 # theoretical right value in respect to radius and length of label
307 my $left_pos = $centerX - $x - $label_length * $fontW;
308 if ( $left_pos < $self->{'curr_x_min'} )
309 {
310  
311 # too far right, correct x
312 $left_pos = $self->{'curr_x_min'};
313 $x = $centerX - $left_pos - $label_length * $fontW;
314 }
315  
316 # theoretical top position in respect to radius and height of label
317 # (Remark: direction to the top of the picture counts backwards!)
318 my $top_pos = $centerY + $y - $fontH;
319 if ( $top_pos < $self->{'curr_y_min'} )
320 {
321  
322 # too far up, correct $y
323 $top_pos = $self->{'curr_y_min'};
324 $y = $centerY - $top_pos - $fontH;
325 }
326  
327 my $down_pos = $centerY - $y + $fontH;
328 if ( $down_pos > $self->{'curr_y_max'} )
329 {
330 $down_pos = $self->{'curr_y_max'};
331 $y = $centerY + $fontH - $down_pos;
332 }
333  
334 #test
335 #$self->{'gd_obj'}->rectangle( $centerX-$x, $centerY+$y, $left_pos, $top_pos, $textcolor );
336 #$self->{'gd_obj'}
337 # ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
338 #return;
339  
340 $radius =
341 $self->minimum( $radius, $x / sin( $dg2rd * $label_degrees ), abs( $y / cos( $dg2rd * $label_degrees ) ) );
342 $radius = int( $radius + 0.5 );
343  
344 #test
345 #$self->{'gd_obj'}->line( $centerX, $centerY, $centerX+$radius, $centerY, gdBrushed );
346  
347 }
348 if ( $radius <= 0 ) { croak "radius < 0!"; }
349 }
350  
351 # reset starting point for next dataset and continue.
352 $start_degrees = $end_degrees;
353  
354 }
355 $diameter = $radius * 2 - 2 * $labeldistance;
356 $text_diameter = $diameter + $labeldistance;
357 $self->{'gd_obj'}->arc( $centerX, $centerY, $diameter, $diameter, 0, 360, $misccolor );
358  
359 # for DEBUG!!
360 #$self->{'gd_obj'}->arc($centerX, $centerY, $text_diameter, $text_diameter,
361 # 0, 360, $misccolor);
362  
363 # for DEBUG!!
364 #print "-------------------------------------------\n";
365  
366 # @details
367 # Plot the pies
368 $start_degrees = 0;
369 $end_degrees = 0;
370 for $j ( 0 .. ( $self->{'num_datapoints'} - 1 ) )
371 {
372  
373 next if $labelinfo{$j}{data} eq 'undefined';
374  
375 # get the color for this datapoint, take the color of the datasets
376 $color = $self->_color_role_to_index( 'dataset' . $j );
377  
378 $label = $labelinfo{$j}{labelstring};
379 $label_length = $labelinfo{$j}{labellength};
380  
381 # The first value starts at 0 degrees, each additional dataset
382 # stops where the previous left off, and since I've already
383 # calculated the sum_total for the whole graph, I know that
384 # the final pie slice will end at 360 degrees.
385  
386 # So, get the degree offset for this dataset
387 $end_degrees = $start_degrees + ( $data->[1][$j] / ( $dataset_sum || 1 ) * 360 );
388  
389 $degrees = ( $start_degrees + $end_degrees ) / 2;
390  
391 # stick the label in the middle of the slice
392 $label_degrees = $degrees;
393 $label_degrees = $self->modulo( $label_degrees, 360.0 );
394  
395 if ( $start_degrees < $end_degrees )
396 {
397  
398 # draw filled Arc
399 # test
400 #print "centerX=$centerX, centerY=$centerY, diameter=$diameter, $start_degrees, ".int($start_degrees) . ", $end_degrees\n";
401 #$self->{'gd_obj'}->filledArc( $centerX, $centerY, $diameter, $diameter, $start_degrees, $end_degrees, $color );
402 $self->{'gd_obj'}->filledArc(
403 $centerX, $centerY, $diameter, $diameter,
404 int( $start_degrees - 0.5 ),
405 int( $end_degrees + 0.5 ), $color
406 );
407 }
408  
409 # Figure out where to place the label
410 # $forbidden_degrees = angle of the center, representing the height of the label
411 if ( $j == 0 )
412 {
413 $forbidden_degrees = $rd2dg * atan2( $fontH, 0.5 * $text_diameter );
414 $label_old_degrees = 0;
415 }
416 else
417 {
418 my $winkel;
419 my $h;
420  
421 if ( ( $label_old_degrees <= 90.0 && $label_degrees > 90.0 )
422 || ( $label_old_degrees <= 270.0 && $label_degrees > 270.0 ) )
423 {
424  
425 # at 90 degrees there the reference point to the text changes
426 # from the beginning to the back
427 $forbidden_degrees = 0;
428 }
429 $label_degrees = $self->maximum( $label_degrees, $forbidden_degrees );
430 $label_old_degrees = $label_degrees; # remember old label_degrees
431  
432 $winkel = cos( $label_degrees * $dg2rd );
433  
434 $winkel = abs($winkel);
435 if ( abs($winkel) < 0.01 )
436 {
437 $h = 0;
438 }
439 else
440 {
441 $h = $fontH / $winkel;
442 }
443  
444 my $atan = atan2( $h, 0.5 * $text_diameter ); # -pi ... +pi
445  
446 $forbidden_degrees = $label_degrees + $rd2dg * $atan;
447  
448 # for debugging
449 #printf("Index=%2d winkel=%6.2f, H=%3d atan=%5.2f label=%6.2f forbidden=%6.2f\n",
450 # $j, $winkel*$dg2rd,$h, $atan*$rd2dg, $label_degrees,$forbidden_degrees);
451 # end for debugging
452  
453 }
454 $labelX = $centerX + $text_diameter * 0.5 * cos( $label_degrees * $dg2rd );
455 $labelY = $centerY + $text_diameter * 0.5 * sin( $label_degrees * $dg2rd );
456  
457 #!!DEBUG!!!!
458 #print "Text=".$label.": labelX=$labelX, y=$labelY\n";
459 # # For debugging
460 # # Draw Point
461 # # reset the brush for points
462 # my $brush = $self->_prepare_brush($color, 'point',
463 # $self->{'pointStyle' . '0'});
464 # $self->{'gd_obj'}->setBrush($brush);
465 #
466 # # draw the point
467 # $self->{'gd_obj'}->line($labelX, $labelY, $labelX, $labelY, gdBrushed);
468 # # end for debugging
469  
470 # Okay, if a bunch of very small datasets are close together, they can
471 # overwrite each other. The following if statement is to help keep
472 # labels of neighbor datasets from being overlapped. It ain't perfect,
473 # but it does a pretty good job.
474  
475 if ( ( $label_degrees >= 270 && $label_degrees <= 360 )
476 || ( $label_degrees >= 0 && $label_degrees <= 90 ) )
477 {
478  
479 # right side of the circle
480 # as 0 degrees means east
481 # $textcolor marks everything black
482 $self->{'gd_obj'}->string( $font, $labelX, $labelY - $fontH * 0.5, $label, $textcolor );
483  
484 }
485 else
486 {
487  
488 # $textcolor marks everything black
489 $self->{'gd_obj'}->string( $font, $labelX - length($label) * $fontW, $labelY - $fontH * 0.5, $label, $textcolor );
490 }
491  
492 if ( $self->true( $self->{'legend_lines'} ) )
493 {
494 $self->{'gd_obj'}->line(
495 $centerX + 0.5 * $diameter * cos( $degrees * $dg2rd ),
496 $centerY + 0.5 * $diameter * sin( $degrees * $dg2rd ),
497 $labelX, $labelY, $color
498 );
499 }
500  
501 # reset starting point for next dataset and continue.
502 $start_degrees = $end_degrees;
503  
504 } # end for $j
505  
506 # print "Center $centerX, $centerY\n";
507 # print "Durchmesser $diameter\n";
508 # print "Hintergrund $background\n";
509  
510 if ( defined( $self->{'ring'} ) && abs( $self->{'ring'} ) < 1 )
511 {
512  
513 # print "bground $bground\n";
514 my $hole = ( 1 - abs( $self->{'ring'} ) );
515 if ( $self->true( $self->{'grey_background'} ) )
516 {
517 $insidecolor = $self->_color_role_to_index('grey_background');
518 }
519 else
520 {
521 $insidecolor = $background;
522 }
523  
524 $self->{'gd_obj'}->filledArc( $centerX, $centerY, $hole * $diameter, $hole * $diameter, 0, 360, $insidecolor );
525  
526 $self->{'gd_obj'}->arc( $centerX, $centerY, $hole * $diameter, $hole * $diameter, 0, 360, $misccolor );
527  
528 }
529  
530 # and finaly box it off
531  
532 $self->{'gd_obj'}
533 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
534 return;
535  
536 }
537  
538 ## @fn private _draw_right_legend
539 # Overwrite the legend methods to get the right legend
540 sub _draw_right_legend
541 {
542 my $self = shift;
543 my $data = $self->{'dataref'};
544 my @labels = @{ $data->[0] };
545 my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush );
546 my $font = $self->{'legend_font'};
547 my $l1 = 0;
548 my $l2 = 0;
549 my ( $i, $j, $label, $dataset_sum );
550 my $max_label_len = 1;
551  
552 # make sure we're using a real font
553 unless ( ( ref($font) ) eq 'GD::Font' )
554 {
555 croak "The font you specified isn\'t a GD Font object";
556 }
557  
558 # get the size of the font
559 ( $h, $w ) = ( $font->height, $font->width );
560  
561 # get the miscellaneous color
562 $misccolor = $self->_color_role_to_index('misc');
563  
564 #find out what the sum of all datapoits is, needed for the Labels with percent
565 $dataset_sum = 0;
566 for my $j ( 0 .. $self->{'num_datapoints'} )
567 {
568 if ( defined $data->[1][$j] )
569 {
570 $dataset_sum += $data->[1][$j];
571 }
572 }
573  
574 # find out how who wide the largest label text is
575 foreach (@labels)
576 {
577 if ( length($_) > $l1 )
578 {
579 $l1 = length($_);
580 }
581 }
582 for ( my $i = 0 ; $i < ( $self->{'num_datapoints'} ) ; $i++ )
583 {
584 if ( length( $data->[1][$i] ) > $l2 )
585 {
586 $l2 = length( $data->[1][$i] );
587 }
588 }
589  
590 if ( $self->{'legend_label_values'} =~ /^value$/i )
591 {
592 $max_label_len = $l1 + $l2 + 1;
593 }
594 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
595 {
596 $max_label_len = $l1 + 7;
597 }
598 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
599 {
600 $max_label_len = $l1 + $l2 + 9;
601 }
602 else
603 {
604 $max_label_len = $l1;
605 }
606  
607 # find out how wide the largest label is
608 $width = ( 2 * $self->{'text_space'} )
609  
610 #+ ($self->{'max_legend_label'} * $w)
611 + $max_label_len * $w + $self->{'legend_example_size'} + ( 2 * $self->{'legend_space'} );
612  
613 # get some starting x-y values
614 $x1 = $self->{'curr_x_max'} - $width;
615 $x2 = $self->{'curr_x_max'};
616 $y1 = $self->{'curr_y_min'} + $self->{'graph_border'};
617 $y2 =
618 $self->{'curr_y_min'} +
619 $self->{'graph_border'} +
620 $self->{'text_space'} +
621 ( $self->{'num_datapoints'} * ( $h + $self->{'text_space'} ) ) +
622 ( 2 * $self->{'legend_space'} );
623  
624 # box the legend off
625 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor );
626  
627 # leave that nice space inside the legend box
628 $x1 += $self->{'legend_space'};
629 $y1 += $self->{'legend_space'} + $self->{'text_space'};
630  
631 # now draw the actual legend
632 for ( 0 .. $#labels )
633 {
634  
635 # get the color
636 $color = $self->_color_role_to_index( 'dataset' . $_ );
637  
638 # find the x-y coords
639 $x2 = $x1;
640 $x3 = $x2 + $self->{'legend_example_size'};
641 $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2;
642  
643 # do the line first
644 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color );
645  
646 # reset the brush for points
647 $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $_ } );
648 $self->{'gd_obj'}->setBrush($brush);
649  
650 # draw the point
651 $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed );
652  
653 # now the label
654 $x2 = $x3 + ( 2 * $self->{'text_space'} );
655 $y2 -= $h / 2;
656 if ( defined $data->[1][$_] )
657 {
658 if ( $self->{'legend_label_values'} =~ /^value$/i )
659 {
660 $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_] . ' ' . $data->[1][$_], $color );
661 }
662 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
663 {
664 $label = sprintf( "%s %4.2f%%", $labels[$_], $data->[1][$_] / ( $dataset_sum || 1 ) * 100 );
665 $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $color );
666 }
667 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
668 {
669 if ( $data->[1][$_] =~ /\./ )
670 {
671 $label =
672 sprintf( "%s %4.2f%% %.2f", $labels[$_], $data->[1][$_] / ( $dataset_sum || 1 ) * 100, $data->[1][$_] );
673 }
674 else
675 {
676 $label =
677 sprintf( "%s %4.2f%% %d", $labels[$_], $data->[1][$_] / ( $dataset_sum || 1 ) * 100, $data->[1][$_] );
678 }
679 $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $color );
680 }
681 else
682 {
683 $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color );
684 }
685  
686 }
687 }
688  
689 # mark off the used space
690 $self->{'curr_x_max'} -= $width;
691  
692 # and return
693 return 1;
694 }
695  
696 ## @fn private _draw_left_legend
697 # put the legend on the left of the chart
698 sub _draw_left_legend
699 {
700 my $self = shift;
701  
702 my $data = $self->{'dataref'};
703 my @labels = @{ $data->[0] };
704 my ( $x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush );
705 my $font = $self->{'legend_font'};
706 my $max_label_len = 1;
707 my $l1 = 0;
708 my $l2 = 0;
709 my ( $dataset_sum, $label );
710  
711 # make sure we're using a real font
712 unless ( ( ref($font) ) eq 'GD::Font' )
713 {
714 croak "The font you specified isn\'t a GD Font object";
715 }
716  
717 # get the size of the font
718 ( $h, $w ) = ( $font->height, $font->width );
719  
720 # get the miscellaneous color
721 $misccolor = $self->_color_role_to_index('misc');
722  
723 #find out what the sum of all datapoits is, needed for the Labels with percent
724 $dataset_sum = 0;
725 for my $j ( 0 .. $self->{'num_datapoints'} )
726 {
727 if ( defined $data->[1][$j] )
728 {
729 $dataset_sum += $data->[1][$j];
730 }
731 }
732  
733 # find out how who wide the largest label text is
734 foreach (@labels)
735 {
736 if ( length($_) > $l1 )
737 {
738 $l1 = length($_);
739 }
740 }
741 for ( my $i = 0 ; $i < ( $self->{'num_datapoints'} ) ; $i++ )
742 {
743 if ( length( $data->[1][$i] ) > $l2 )
744 {
745 $l2 = length( $data->[1][$i] );
746 }
747 }
748  
749 if ( $self->{'legend_label_values'} =~ /^value$/i )
750 {
751 $max_label_len = $l1 + $l2 + 1;
752 }
753 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
754 {
755 $max_label_len = $l1 + 7;
756 }
757 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
758 {
759 $max_label_len = $l1 + $l2 + 9;
760 }
761 else
762 {
763 $max_label_len = $l1;
764 }
765  
766 # find out how wide the largest label is
767 $width =
768 ( 2 * $self->{'text_space'} ) +
769 ( $max_label_len * $w ) +
770 $self->{'legend_example_size'} +
771 ( 2 * $self->{'legend_space'} );
772  
773 # get some base x-y coordinates
774 $x1 = $self->{'curr_x_min'};
775 $x2 = $self->{'curr_x_min'} + $width;
776 $y1 = $self->{'curr_y_min'} + $self->{'graph_border'};
777 $y2 =
778 $self->{'curr_y_min'} +
779 $self->{'graph_border'} +
780 $self->{'text_space'} +
781 ( $self->{'num_datapoints'} * ( $h + $self->{'text_space'} ) ) +
782 ( 2 * $self->{'legend_space'} );
783  
784 # box the legend off
785 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $misccolor );
786  
787 # leave that nice space inside the legend box
788 $x1 += $self->{'legend_space'};
789 $y1 += $self->{'legend_space'} + $self->{'text_space'};
790  
791 # now draw the actual legend
792 for ( 0 .. $#labels )
793 {
794  
795 # get the color
796 $color = $self->_color_role_to_index( 'dataset' . $_ );
797  
798 # find the x-y coords
799 $x2 = $x1;
800 $x3 = $x2 + $self->{'legend_example_size'};
801 $y2 = $y1 + ( $_ * ( $self->{'text_space'} + $h ) ) + $h / 2;
802  
803 # do the line first
804 $self->{'gd_obj'}->line( $x2, $y2, $x3, $y2, $color );
805  
806 # reset the brush for points
807 $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $_ } );
808 $self->{'gd_obj'}->setBrush($brush);
809  
810 # draw the point
811 $self->{'gd_obj'}->line( int( ( $x3 + $x2 ) / 2 ), $y2, int( ( $x3 + $x2 ) / 2 ), $y2, gdBrushed );
812  
813 # now the label
814 $x2 = $x3 + ( 2 * $self->{'text_space'} );
815 $y2 -= $h / 2;
816 if ( $self->{'legend_label_values'} =~ /^value$/i )
817 {
818 $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_] . ' ' . $data->[1][$_], $color );
819 }
820 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
821 {
822 $label = sprintf( "%s %4.2f%%", $labels[$_], $data->[1][$_] / ( $dataset_sum || 1 ) * 100 );
823 $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $color );
824 }
825 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
826 {
827 if ( $data->[1][$_] =~ /\./ )
828 {
829 $label =
830 sprintf( "%s %4.2f%% %.2f", $labels[$_], $data->[1][$_] / ( $dataset_sum || 1 ) * 100, $data->[1][$_] );
831 }
832 else
833 {
834 $label = sprintf( "%s %4.2f%% %d", $labels[$_], $data->[1][$_] / ( $dataset_sum || 1 ) * 100, $data->[1][$_] );
835 }
836 $self->{'gd_obj'}->string( $font, $x2, $y2, $label, $color );
837 }
838 else
839 {
840 $self->{'gd_obj'}->string( $font, $x2, $y2, $labels[$_], $color );
841 }
842  
843 }
844  
845 # mark off the used space
846 $self->{'curr_x_min'} += $width;
847  
848 # and return
849 return 1;
850 }
851  
852 ## @fn private _draw_bottom_legend
853 # put the legend on the bottom of the chart
854 sub _draw_bottom_legend
855 {
856 my $self = shift;
857 my $data = $self->{'dataref'};
858 my @labels = @{ $data->[0] };
859 my ( $x1, $y1, $x2, $x3, $y2, $empty_width, $max_label_width, $cols, $rows, $color, $brush );
860 my ( $col_width, $row_height, $r, $c, $index, $x, $y, $w, $h );
861 my $font = $self->{'legend_font'};
862 my $max_label_len;
863 my $l1 = 0;
864 my $l2 = 0;
865 my ( $dataset_sum, $j );
866 my $label;
867 my $text_color = $self->_color_role_to_index( 'text' );
868  
869 # make sure we're using a real font
870 unless ( ( ref($font) ) eq 'GD::Font' )
871 {
872 croak "The font you specified isn\'t a GD Font object";
873 }
874  
875 # get the size of the font
876 ( $h, $w ) = ( $font->height, $font->width );
877  
878 # find the base x values
879 $x1 = $self->{'curr_x_min'} + $self->{'graph_border'};
880  
881 # + ($self->{'y_tick_label_length'} * $self->{'tick_label_font'}->width)
882 # + $self->{'tick_len'} + (3 * $self->{'text_space'});
883 $x2 = $self->{'curr_x_max'} - $self->{'graph_border'};
884 if ( $self->{'y_label'} )
885 {
886 $x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'};
887 }
888 if ( $self->{'y_label2'} )
889 {
890 $x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'};
891 }
892  
893 #find out what the sum of all datapoits is, needed for the Labels with percent
894 $dataset_sum = 0;
895 for $j ( 0 .. $self->{'num_datapoints'} )
896 {
897 if ( defined $data->[1][$j] )
898 {
899 $dataset_sum += $data->[1][$j];
900 }
901 }
902  
903 # find out how who wide the largest label text is, especially look what kind of
904 # label is needed
905 foreach (@labels)
906 {
907 if ( length($_) > $l1 )
908 {
909 $l1 = length($_);
910 }
911 }
912 for ( my $i = 0 ; $i < ( $self->{'num_datapoints'} ) ; $i++ )
913 {
914 if ( length( $data->[1][$i] ) > $l2 )
915 {
916 $l2 = length( $data->[1][$i] );
917 }
918 }
919  
920 if ( $self->{'legend_label_values'} =~ /^value$/i )
921 {
922 $max_label_len = $l1 + $l2 + 1;
923 }
924 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
925 {
926 $max_label_len = $l1 + 7;
927 }
928 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
929 {
930 $max_label_len = $l1 + $l2 + 9;
931 }
932 else
933 {
934 $max_label_len = $l1;
935 }
936  
937 # figure out how wide the columns need to be, and how many we
938 # can fit in the space available
939 $empty_width = ( $x2 - $x1 ) - ( 2 * $self->{'legend_space'} );
940 $max_label_width = $max_label_len * $w
941  
942 #$self->{'max_legend_label'} * $w
943 + ( 4 * $self->{'text_space'} ) + $self->{'legend_example_size'};
944 $cols = int( $empty_width / $max_label_width );
945 unless ($cols)
946 {
947 $cols = 1;
948 }
949 $col_width = $empty_width / $cols;
950  
951 # figure out how many rows we need, remember how tall they are
952 $rows = int( $self->{'num_datapoints'} / $cols );
953 unless ( ( $self->{'num_datapoints'} % $cols ) == 0 )
954 {
955 $rows++;
956 }
957 unless ($rows)
958 {
959 $rows = 1;
960 }
961 $row_height = $h + $self->{'text_space'};
962  
963 # box the legend off
964 $y1 = $self->{'curr_y_max'} - $self->{'text_space'} - ( $rows * $row_height ) - ( 2 * $self->{'legend_space'} );
965 $y2 = $self->{'curr_y_max'};
966 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $self->_color_role_to_index('misc') );
967 $x1 += $self->{'legend_space'} + $self->{'text_space'};
968 $x2 -= $self->{'legend_space'};
969 $y1 += $self->{'legend_space'} + $self->{'text_space'};
970 $y2 -= $self->{'legend_space'} + $self->{'text_space'};
971
972 # draw in the actual legend
973 for $r ( 0 .. $rows - 1 )
974 {
975 for $c ( 0 .. $cols - 1 )
976 {
977 $index = ( $r * $cols ) + $c; # find the index in the label array
978 if ( $labels[$index] )
979 {
980  
981 # get the color
982 $color = $self->_color_role_to_index( 'dataset' . $index );
983  
984 # get the x-y coordinate for the start of the example line
985 $x = $x1 + ( $col_width * $c );
986 $y = $y1 + ( $row_height * $r ) + $h / 2;
987  
988 # now draw the example line
989 $self->{'gd_obj'}->line( $x, $y, $x + $self->{'legend_example_size'}, $y, $color );
990  
991 # reset the brush for points
992 $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $index } );
993 $self->{'gd_obj'}->setBrush($brush);
994  
995 # draw the point
996 $x3 = int( $x + $self->{'legend_example_size'} / 2 );
997 $self->{'gd_obj'}->line( $x3, $y, $x3, $y, gdBrushed );
998  
999 # adjust the x-y coordinates for the start of the label
1000 $x += $self->{'legend_example_size'} + ( 2 * $self->{'text_space'} );
1001 $y = $y1 + ( $row_height * $r );
1002  
1003 # now draw the label
1004 if ( $self->{'legend_label_values'} =~ /^value$/i )
1005 {
1006 $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index] . ' ' . $data->[1][$index], $text_color );
1007  
1008 #$self->{'gd_obj'}->stringTTF($color, FONT, 10, 0, $x, $y+10, $labels[$index].' '.$data->[1][$index]); ############
1009 }
1010 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
1011 {
1012 $label = sprintf( "%s %4.2f%%", $labels[$index], $data->[1][$index] / ( $dataset_sum || 1 ) * 100 );
1013 $self->{'gd_obj'}->string( $font, $x, $y, $label, $color );
1014 }
1015 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
1016 {
1017 if ( $data->[1][$index] =~ /\./ )
1018 {
1019 $label = sprintf( "%s %4.2f%% %.2f",
1020 $labels[$index],
1021 $data->[1][$index] / ( $dataset_sum || 1 ) * 100,
1022 $data->[1][$index] );
1023 }
1024 else
1025 {
1026 $label = sprintf( "%s %4.2f%% %d",
1027 $labels[$index],
1028 $data->[1][$index] / ( $dataset_sum || 1 ) * 100,
1029 $data->[1][$index] );
1030 }
1031 $self->{'gd_obj'}->string( $font, $x, $y, $label, $text_color ); ###
1032 # $self->{'gd_obj'}->stringTTF($color, FONT, 10, 0, $x, $y, $label);
1033  
1034 }
1035 else
1036 {
1037 $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index], $text_color );
1038 }
1039 }
1040 }
1041 }
1042  
1043 # mark off the space used
1044 $self->{'curr_y_max'} -= ( $rows * $row_height ) + $self->{'text_space'} + ( 2 * $self->{'legend_space'} );
1045  
1046 # now return
1047 return 1;
1048 }
1049  
1050 ## @fn private _draw_top_legend
1051 # put the legend on top of the chart
1052 sub _draw_top_legend
1053 {
1054 my $self = shift;
1055 my $data = $self->{'dataref'};
1056 my ($max_label_len);
1057 my $l1 = 0;
1058 my $l2 = 0;
1059 my @labels = @{ $data->[0] };
1060 my ( $x1, $y1, $x2, $x3, $y2, $empty_width, $max_label_width, $cols, $rows, $color, $brush );
1061 my ( $col_width, $row_height, $r, $c, $index, $x, $y, $w, $h, $dataset_sum, $label );
1062 my $font = $self->{'legend_font'};
1063  
1064 # make sure we're using a real font
1065 unless ( ( ref($font) ) eq 'GD::Font' )
1066 {
1067 croak "The font you specified isn\'t a GD Font object";
1068 }
1069  
1070 # get the size of the font
1071 ( $h, $w ) = ( $font->height, $font->width );
1072  
1073 #find out what the sum of all datapoits is, needed for the Labels with percent
1074 $dataset_sum = 0;
1075 for my $j ( 0 .. $self->{'num_datapoints'} )
1076 {
1077 if ( defined $data->[1][$j] )
1078 {
1079 $dataset_sum += $data->[1][$j];
1080 }
1081 }
1082  
1083 # get some base x coordinates
1084 $x1 = $self->{'curr_x_min'} + $self->{'graph_border'};
1085  
1086 # + $self->{'y_tick_label_length'} * $self->{'tick_label_font'}->width
1087 # + $self->{'tick_len'} + (3 * $self->{'text_space'});
1088 $x2 = $self->{'curr_x_max'} - $self->{'graph_border'};
1089 if ( $self->{'y_label'} )
1090 {
1091 $x1 += $self->{'label_font'}->height + 2 * $self->{'text_space'};
1092 }
1093 if ( $self->{'y_label2'} )
1094 {
1095 $x2 -= $self->{'label_font'}->height + 2 * $self->{'text_space'};
1096 }
1097  
1098 # find out how who wide the largest label text is
1099 foreach (@labels)
1100 {
1101 if ( length($_) > $l1 )
1102 {
1103 $l1 = length($_);
1104 }
1105 }
1106 for ( my $i = 0 ; $i < ( $self->{'num_datapoints'} ) ; $i++ )
1107 {
1108 if ( length( $data->[1][$i] ) > $l2 )
1109 {
1110 $l2 = length( $data->[1][$i] );
1111 }
1112 }
1113  
1114 if ( $self->{'legend_label_values'} =~ /^value$/i )
1115 {
1116 $max_label_len = $l1 + $l2 + 1;
1117 }
1118 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
1119 {
1120 $max_label_len = $l1 + 7;
1121 }
1122 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
1123 {
1124 $max_label_len = $l1 + $l2 + 9;
1125 }
1126 else
1127 {
1128 $max_label_len = $l1;
1129 }
1130  
1131 # figure out how wide the columns can be, and how many will fit
1132 $empty_width = ( $x2 - $x1 ) - ( 2 * $self->{'legend_space'} );
1133 $max_label_width = ( 4 * $self->{'text_space'} ) + $max_label_len * $w + $self->{'legend_example_size'};
1134 $cols = int( $empty_width / $max_label_width );
1135  
1136 unless ($cols)
1137 {
1138 $cols = 1;
1139 }
1140 $col_width = $empty_width / $cols;
1141  
1142 # figure out how many rows we need and remember how tall they are
1143 $rows = int( $self->{'num_datapoints'} / $cols );
1144 unless ( ( $self->{'num_datapoints'} % $cols ) == 0 )
1145 {
1146 $rows++;
1147 }
1148 unless ($rows)
1149 {
1150 $rows = 1;
1151 }
1152 $row_height = $h + $self->{'text_space'};
1153  
1154 # box the legend off
1155 $y1 = $self->{'curr_y_min'};
1156 $y2 = $self->{'curr_y_min'} + $self->{'text_space'} + ( $rows * $row_height ) + ( 2 * $self->{'legend_space'} );
1157 $self->{'gd_obj'}->rectangle( $x1, $y1, $x2, $y2, $self->_color_role_to_index('misc') );
1158  
1159 # leave some space inside the legend
1160 $x1 += $self->{'legend_space'} + $self->{'text_space'};
1161 $x2 -= $self->{'legend_space'};
1162 $y1 += $self->{'legend_space'} + $self->{'text_space'};
1163 $y2 -= $self->{'legend_space'} + $self->{'text_space'};
1164  
1165 # draw in the actual legend
1166 for $r ( 0 .. $rows - 1 )
1167 {
1168 for $c ( 0 .. $cols - 1 )
1169 {
1170 $index = ( $r * $cols ) + $c; # find the index in the label array
1171 if ( $labels[$index] )
1172 {
1173  
1174 # get the color
1175 $color = $self->_color_role_to_index( 'dataset' . $index );
1176  
1177 # find the x-y coords
1178 $x = $x1 + ( $col_width * $c );
1179 $y = $y1 + ( $row_height * $r ) + $h / 2;
1180  
1181 # draw the line first
1182 $self->{'gd_obj'}->line( $x, $y, $x + $self->{'legend_example_size'}, $y, $color );
1183  
1184 # reset the brush for points
1185 $brush = $self->_prepare_brush( $color, 'point', $self->{ 'pointStyle' . $index } );
1186 $self->{'gd_obj'}->setBrush($brush);
1187  
1188 # draw the point
1189 $x3 = int( $x + $self->{'legend_example_size'} / 2 );
1190 $self->{'gd_obj'}->line( $x3, $y, $x3, $y, gdBrushed );
1191  
1192 # now the label
1193 $x += $self->{'legend_example_size'} + ( 2 * $self->{'text_space'} );
1194 $y -= $h / 2;
1195 if ( $self->{'legend_label_values'} =~ /^value$/i )
1196 {
1197 $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index] . ' ' . $data->[1][$index], $color );
1198 }
1199 elsif ( $self->{'legend_label_values'} =~ /^percent$/i )
1200 {
1201 $label = sprintf( "%s %4.2f%%", $labels[$index], $data->[1][$index] / ( $dataset_sum || 1 ) * 100 );
1202 $self->{'gd_obj'}->string( $font, $x, $y, $label, $color );
1203 }
1204 elsif ( $self->{'legend_label_values'} =~ /^both$/i )
1205 {
1206 if ( $data->[1][$index] =~ /\./ )
1207 {
1208 $label = sprintf( "%s %4.2f%% %.2f",
1209 $labels[$index],
1210 $data->[1][$index] / ( $dataset_sum || 1 ) * 100,
1211 $data->[1][$index] );
1212 }
1213 else
1214 {
1215 $label = sprintf( "%s %4.2f%% %d",
1216 $labels[$index],
1217 $data->[1][$index] / ( $dataset_sum || 1 ) * 100,
1218 $data->[1][$index] );
1219 }
1220 $self->{'gd_obj'}->string( $font, $x, $y, $label, $color );
1221 }
1222 else
1223 {
1224 $self->{'gd_obj'}->string( $font, $x, $y, $labels[$index], $color );
1225 }
1226 }
1227 }
1228 }
1229  
1230 # mark off the space used
1231 $self->{'curr_y_min'} += ( $rows * $row_height ) + $self->{'text_space'} + 2 * $self->{'legend_space'};
1232  
1233 # now return
1234 return 1;
1235 }
1236  
1237 ## @fn private _draw_x_ticks
1238 # Override the ticks methods for the pie charts.\n
1239 # Here: do nothing
1240 sub _draw_x_ticks
1241 {
1242 my $self = shift;
1243  
1244 return;
1245 }
1246  
1247 ## @fn private _draw_y_ticks
1248 # Override the ticks methods for the pie charts.\n
1249 sub _draw_y_ticks
1250 {
1251 my $self = shift;
1252  
1253 return;
1254 }
1255  
1256 ## @fn private _find_y_scale
1257 # Override the find_y_scale methods for the pie charts.\n
1258 # Here: do nothing
1259 sub _find_y_scale
1260 {
1261 my $self = shift;
1262  
1263 return;
1264 }
1265  
1266 ## be a good module and return 1
1267 1;
« lib/Chart/StackedBars.pm ↑ Summary lib/Chart/Points.pm »