| 1 | #========================================================================== | ||
| 2 | # Copyright (c) 1995-1998 Martien Verbruggen | ||
| 3 | #-------------------------------------------------------------------------- | ||
| 4 | # | ||
| 5 | # Name: | ||
| 6 | # GD::Graph::pie.pm | ||
| 7 | # | ||
| 8 | # $Id: pie.pm,v 1.22 2007/04/27 05:26:47 ben Exp $ | ||
| 9 | # | ||
| 10 | #========================================================================== | ||
| 11 | |||
| 12 | package GD::Graph::pie; | ||
| 13 | |||
| 14 | ($GD::Graph::pie::VERSION) = '$Revision: 1.22 $' =~ /\s([\d.]+)/; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | |||
| 18 | use constant PI => 4 * atan2(1,1); | ||
| 19 | |||
| 20 | use GD; | ||
| 21 | use GD::Graph; | ||
| 22 | use GD::Graph::utils qw(:all); | ||
| 23 | use GD::Graph::colour qw(:colours :lists); | ||
| 24 | use GD::Text::Align; | ||
| 25 | use Carp; | ||
| 26 | use POSIX qw(ceil floor); | ||
| 27 | |||
| 28 | @GD::Graph::pie::ISA = qw( GD::Graph ); | ||
| 29 | |||
| 30 | my $ANGLE_OFFSET = 90; | ||
| 31 | |||
| 32 | my %Defaults = ( | ||
| 33 | |||
| 34 | # Set the height of the pie. | ||
| 35 | # Because of the dependency of this on runtime information, this | ||
| 36 | # is being set in GD::Graph::pie::initialise | ||
| 37 | |||
| 38 | # pie_height => _round(0.1*${'width'}), | ||
| 39 | pie_height => undef, | ||
| 40 | |||
| 41 | # Do you want a 3D pie? | ||
| 42 | '3d' => 1, | ||
| 43 | |||
| 44 | # The angle at which to start the first data set | ||
| 45 | # 0 is at the front/bottom | ||
| 46 | start_angle => 0, | ||
| 47 | |||
| 48 | # Angle below which a label on a pie slice is suppressed. | ||
| 49 | suppress_angle => 0, # CONTRIB idea ryan <xomina@bitstream.net> | ||
| 50 | |||
| 51 | # and some public attributes without defaults | ||
| 52 | label => undef, | ||
| 53 | |||
| 54 | # This misnamed attribute is used for pie marker colours | ||
| 55 | axislabelclr => 'black', | ||
| 56 | |||
| 57 | aa => 0, | ||
| 58 | alpha => 0, | ||
| 59 | |||
| 60 | #the percentage for each slice to be displaced from the centre. | ||
| 61 | #if this is a reference to an array, each slice is displaced by | ||
| 62 | #the amount in the same array position, or not displaced if there | ||
| 63 | #are more slices than offsets. | ||
| 64 | #if it is just a number, all slices are exploded this amount | ||
| 65 | offsets => undef, | ||
| 66 | |||
| 67 | # Size of the legend markers | ||
| 68 | legend_marker_height => 8, | ||
| 69 | legend_marker_width => 12, | ||
| 70 | legend_spacing => 4, | ||
| 71 | legend_placement => 'BC', # '[BR][LCR]' | ||
| 72 | lg_cols => undef, | ||
| 73 | legend_frame_margin => 4, | ||
| 74 | legend_frame_size => undef, | ||
| 75 | ); | ||
| 76 | |||
| 77 | # PRIVATE | ||
| 78 | sub _has_default { | ||
| 79 | my $self = shift; | ||
| 80 | my $attr = shift || return; | ||
| 81 | exists $Defaults{$attr} || $self->SUPER::_has_default($attr); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub initialise | ||
| 85 | { | ||
| 86 | my $self = shift; | ||
| 87 | $self->SUPER::initialise(); | ||
| 88 | while (my($key, $val) = each %Defaults) | ||
| 89 | { $self->{$key} = $val } | ||
| 90 | $self->set( pie_height => _round(0.1 * $self->{height}) ); | ||
| 91 | $self->set_value_font(gdTinyFont); | ||
| 92 | $self->set_label_font(gdSmallFont); | ||
| 93 | $self->set_legend_font(gdTinyFont); | ||
| 94 | } | ||
| 95 | |||
| 96 | # PUBLIC methods, documented in pod | ||
| 97 | sub plot | ||
| 98 | { | ||
| 99 | my $self = shift; | ||
| 100 | my $data = shift; | ||
| 101 | |||
| 102 | $self->check_data($data) or return; | ||
| 103 | $self->init_graph() or return; | ||
| 104 | $self->setup_text() or return; | ||
| 105 | $self->setup_legend(); | ||
| 106 | $self->setup_coords() or return; | ||
| 107 | $self->draw_text() or return; | ||
| 108 | if ($self->{aa} && $self->{'3d'}){ | ||
| 109 | $self->draw_pie_data_aa() or return; | ||
| 110 | } else { | ||
| 111 | $self->draw_pie() or return; | ||
| 112 | $self->draw_data() or return; | ||
| 113 | } | ||
| 114 | $self->draw_legend(); | ||
| 115 | |||
| 116 | return $self->{graph}; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub set_label_font # (fontname) | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | $self->_set_font('gdta_label', @_) or return; | ||
| 123 | $self->{gdta_label}->set_align('bottom', 'center'); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub set_value_font # (fontname) | ||
| 127 | { | ||
| 128 | my $self = shift; | ||
| 129 | $self->_set_font('gdta_value', @_) or return; | ||
| 130 | $self->{gdta_value}->set_align('center', 'center'); | ||
| 131 | } | ||
| 132 | |||
| 133 | # Inherit defaults() from GD::Graph | ||
| 134 | |||
| 135 | # inherit checkdata from GD::Graph | ||
| 136 | |||
| 137 | # Setup the coordinate system and colours, calculate the | ||
| 138 | # relative axis coordinates in respect to the canvas size. | ||
| 139 | |||
| 140 | sub setup_coords() | ||
| 141 | { | ||
| 142 | my $self = shift; | ||
| 143 | |||
| 144 | # Make sure we're not reserving space we don't need. | ||
| 145 | $self->{'3d'} = 0 if $self->{pie_height} <= 0; | ||
| 146 | $self->set(pie_height => 0) unless $self->{'3d'}; | ||
| 147 | |||
| 148 | my $tfh = $self->{title} ? $self->{gdta_title}->get('height') : 0; | ||
| 149 | my $lfh = $self->{label} ? $self->{gdta_label}->get('height') : 0; | ||
| 150 | |||
| 151 | # Calculate the bounding box for the pie, and | ||
| 152 | # some width, height, and centre parameters (don't forget fenceposts!) | ||
| 153 | $self->{bottom} = | ||
| 154 | $self->{height} - $self->{pie_height} - $self->{b_margin} - | ||
| 155 | ( $lfh ? $lfh + $self->{text_space} : 0 ) - 1; | ||
| 156 | $self->{top} = | ||
| 157 | $self->{t_margin} + ( $tfh ? $tfh + $self->{text_space} : 0 ); | ||
| 158 | |||
| 159 | return $self->_set_error('Vertical size too small') | ||
| 160 | if $self->{bottom} - $self->{top} <= 0; | ||
| 161 | |||
| 162 | $self->{left} = $self->{l_margin}; | ||
| 163 | $self->{right} = $self->{width} - $self->{r_margin} - 1; | ||
| 164 | |||
| 165 | # ensure that the center is a single pixel, not a half-pixel position | ||
| 166 | $self->{right}-- if ($self->{right} - $self->{left}) % 2; | ||
| 167 | $self->{bottom}-- if ($self->{bottom} - $self->{top}) % 2; | ||
| 168 | |||
| 169 | return $self->_set_error('Horizontal size too small') | ||
| 170 | if $self->{right} - $self->{left} <= 0; | ||
| 171 | |||
| 172 | $self->{w} = $self->{right} - $self->{left} + 1; | ||
| 173 | $self->{h} = $self->{bottom} - $self->{top} + 1; | ||
| 174 | |||
| 175 | $self->{xc} = ($self->{right} + $self->{left})/2; | ||
| 176 | $self->{yc} = ($self->{bottom} + $self->{top})/2; | ||
| 177 | |||
| 178 | #make sure offsets is a reference to an array | ||
| 179 | if (exists($self->{offsets}) && !ref($self->{offsets})){ | ||
| 180 | my @offsets = (); | ||
| 181 | for (my $i = 0; $i < $self->{_data}->y_values(1);$i++){ | ||
| 182 | push (@offsets, $self->{offsets}); | ||
| 183 | } | ||
| 184 | $self->{offsets} = \@offsets; | ||
| 185 | } | ||
| 186 | |||
| 187 | if ($self->{aa} && $self->{'3d'}){ | ||
| 188 | $self->_top_ellipse_size(); | ||
| 189 | $self->_largest_displacement_ellipse_size(); | ||
| 190 | } | ||
| 191 | |||
| 192 | return $self; | ||
| 193 | } | ||
| 194 | |||
| 195 | # inherit open_graph from GD::Graph | ||
| 196 | |||
| 197 | # Setup the parameters for the text elements | ||
| 198 | sub setup_text | ||
| 199 | { | ||
| 200 | my $self = shift; | ||
| 201 | |||
| 202 | if ( $self->{title} ) | ||
| 203 | { | ||
| 204 | #print "'$s->{title}' at ($s->{xc},$s->{t_margin})\n"; | ||
| 205 | $self->{gdta_title}->set(colour => $self->{tci}); | ||
| 206 | $self->{gdta_title}->set_text($self->{title}); | ||
| 207 | } | ||
| 208 | |||
| 209 | if ( $self->{label} ) | ||
| 210 | { | ||
| 211 | $self->{gdta_label}->set(colour => $self->{lci}); | ||
| 212 | $self->{gdta_label}->set_text($self->{label}); | ||
| 213 | } | ||
| 214 | |||
| 215 | $self->{gdta_value}->set(colour => $self->{alci}); | ||
| 216 | |||
| 217 | $self->{gdta_legend}->set(colour => $self->{legendci}); | ||
| 218 | $self->{gdta_legend}->set_align('top', 'left'); | ||
| 219 | $self->{lgfh} = $self->{gdta_legend}->get('height'); | ||
| 220 | |||
| 221 | return $self; | ||
| 222 | } | ||
| 223 | |||
| 224 | # Put the text on the canvas. | ||
| 225 | sub draw_text | ||
| 226 | { | ||
| 227 | my $self = shift; | ||
| 228 | |||
| 229 | $self->{gdta_title}->draw($self->{xc}, $self->{t_margin}) | ||
| 230 | if $self->{title}; | ||
| 231 | $self->{gdta_label}->draw($self->{xc}, $self->{height} - $self->{b_margin}) | ||
| 232 | if $self->{label}; | ||
| 233 | |||
| 234 | return $self; | ||
| 235 | } | ||
| 236 | |||
| 237 | # draw the pie, without the data slices | ||
| 238 | sub draw_pie | ||
| 239 | { | ||
| 240 | my $self = shift; | ||
| 241 | |||
| 242 | my $left = $self->{xc} - $self->{w}/2; | ||
| 243 | |||
| 244 | $self->{graph}->arc( | ||
| 245 | $self->{xc}, $self->{yc}, | ||
| 246 | $self->{w}, $self->{h}, | ||
| 247 | 0, 360, $self->{acci} | ||
| 248 | ); | ||
| 249 | |||
| 250 | $self->{graph}->arc( | ||
| 251 | $self->{xc}, $self->{yc} + $self->{pie_height}, | ||
| 252 | $self->{w}, $self->{h}, | ||
| 253 | 0, 180, $self->{acci} | ||
| 254 | ) if ( $self->{'3d'} ); | ||
| 255 | |||
| 256 | $self->{graph}->line( | ||
| 257 | $left, $self->{yc}, | ||
| 258 | $left, $self->{yc} + $self->{pie_height}, | ||
| 259 | $self->{acci} | ||
| 260 | ); | ||
| 261 | |||
| 262 | $self->{graph}->line( | ||
| 263 | $left + $self->{w}, $self->{yc}, | ||
| 264 | $left + $self->{w}, $self->{yc} + $self->{pie_height}, | ||
| 265 | $self->{acci} | ||
| 266 | ); | ||
| 267 | |||
| 268 | return $self; | ||
| 269 | } | ||
| 270 | |||
| 271 | # Draw the data slices | ||
| 272 | |||
| 273 | sub draw_data | ||
| 274 | { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | my $total = 0; | ||
| 278 | my @values = $self->{_data}->y_values(1); # for now, only one pie.. | ||
| 279 | for (@values) | ||
| 280 | { | ||
| 281 | $total += $_ | ||
| 282 | } | ||
| 283 | |||
| 284 | return $self->_set_error("Pie data total is <= 0") | ||
| 285 | unless $total > 0; | ||
| 286 | |||
| 287 | my $ac = $self->{acci}; # Accent colour | ||
| 288 | my $pb = $self->{start_angle}; | ||
| 289 | |||
| 290 | for (my $i = 0; $i < @values; $i++) | ||
| 291 | { | ||
| 292 | # Set the data colour | ||
| 293 | my $dc = $self->set_clr_uniq($self->pick_data_clr($i + 1)); | ||
| 294 | |||
| 295 | # Set the angles of the pie slice | ||
| 296 | # Angle 0 faces down, positive angles are clockwise | ||
| 297 | # from there. | ||
| 298 | # --- | ||
| 299 | # / \ | ||
| 300 | # | | | ||
| 301 | # \ | / | ||
| 302 | # --- | ||
| 303 | # 0 | ||
| 304 | # $pa/$pb include the start_angle (so if start_angle | ||
| 305 | # is 90, there will be no pa/pb < 90. | ||
| 306 | my $pa = $pb; | ||
| 307 | $pb += my $slice_angle = 360 * $values[$i]/$total; | ||
| 308 | |||
| 309 | # Calculate the end points of the lines at the boundaries of | ||
| 310 | # the pie slice | ||
| 311 | my ($xe, $ye) = cartesian( | ||
| 312 | $self->{w}/2, $pa, | ||
| 313 | $self->{xc}, $self->{yc}, $self->{h}/$self->{w} | ||
| 314 | ); | ||
| 315 | |||
| 316 | $self->{graph}->line($self->{xc}, $self->{yc}, $xe, $ye, $ac); | ||
| 317 | |||
| 318 | # Draw the lines on the front of the pie | ||
| 319 | $self->{graph}->line($xe, $ye, $xe, $ye + $self->{pie_height}, $ac) | ||
| 320 | if in_front($pa) && $self->{'3d'}; | ||
| 321 | |||
| 322 | # Make an estimate of a point in the middle of the pie slice | ||
| 323 | # And fill it | ||
| 324 | ($xe, $ye) = cartesian( | ||
| 325 | 3 * $self->{w}/8, ($pa+$pb)/2, | ||
| 326 | $self->{xc}, $self->{yc}, $self->{h}/$self->{w} | ||
| 327 | ); | ||
| 328 | |||
| 329 | $self->{graph}->fillToBorder($xe, $ye, $ac, $dc); | ||
| 330 | |||
| 331 | # If it's 3d, colour the front ones as well | ||
| 332 | # | ||
| 333 | # if one slice is very large (>180 deg) then we will need to | ||
| 334 | # fill it twice. sbonds. | ||
| 335 | # | ||
| 336 | # Independently noted and fixed by Jeremy Wadsack, in a slightly | ||
| 337 | # different way. | ||
| 338 | if ($self->{'3d'}) | ||
| 339 | { | ||
| 340 | foreach my $fill ($self->_get_pie_front_coords($pa, $pb)) | ||
| 341 | { | ||
| 342 | my ($fx,$fy) = @$fill; | ||
| 343 | my $new_y = $fy + $self->{pie_height}/2; | ||
| 344 | # Edge case (literally): if lines have converged, back up | ||
| 345 | # looking for a gap to fill | ||
| 346 | while ( $new_y > $fy ) { | ||
| 347 | if ($self->{graph}->getPixel($fx,$new_y) != $ac) { | ||
| 348 | $self->{graph}->fillToBorder($fx, $new_y, $ac, $dc); | ||
| 349 | last; | ||
| 350 | } | ||
| 351 | } continue { $new_y-- } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | # CONTRIB Jeremy Wadsack | ||
| 357 | # | ||
| 358 | # Large text, sticking out over the pie edge, could cause 3D pies to | ||
| 359 | # fill improperly: Drawing the text for a given slice before the | ||
| 360 | # next slice was drawn and filled could make the slice boundary | ||
| 361 | # disappear, causing the fill colour to flow out. With this | ||
| 362 | # implementation, all the text is on top of the pie. | ||
| 363 | |||
| 364 | $pb = $self->{start_angle}; | ||
| 365 | for (my $i = 0; $i < @values; $i++) | ||
| 366 | { | ||
| 367 | next unless $values[$i]; | ||
| 368 | |||
| 369 | my $pa = $pb; | ||
| 370 | $pb += my $slice_angle = 360 * $values[$i]/$total; | ||
| 371 | |||
| 372 | next if $slice_angle <= $self->{suppress_angle}; | ||
| 373 | |||
| 374 | my ($xe, $ye) = | ||
| 375 | cartesian( | ||
| 376 | 3 * $self->{w}/8, ($pa+$pb)/2, | ||
| 377 | $self->{xc}, $self->{yc}, $self->{h}/$self->{w} | ||
| 378 | ); | ||
| 379 | |||
| 380 | $self->put_slice_label($xe, $ye, $self->{_data}->get_x($i)); | ||
| 381 | } | ||
| 382 | |||
| 383 | return $self; | ||
| 384 | |||
| 385 | } #GD::Graph::pie::draw_data | ||
| 386 | |||
| 387 | sub _get_pie_front_coords # (angle 1, angle 2) | ||
| 388 | { | ||
| 389 | my $self = shift; | ||
| 390 | my $pa = level_angle(shift); | ||
| 391 | my $pb = level_angle(shift); | ||
| 392 | my @fills = (); | ||
| 393 | |||
| 394 | if (in_front($pa)) | ||
| 395 | { | ||
| 396 | if (in_front($pb)) | ||
| 397 | { | ||
| 398 | # both in front | ||
| 399 | # don't do anything | ||
| 400 | # Ah, but if this wraps all the way around the back | ||
| 401 | # then both pieces of the front need to be filled. | ||
| 402 | # sbonds. | ||
| 403 | if ($pa > $pb ) | ||
| 404 | { | ||
| 405 | # This takes care of the left bit on the front | ||
| 406 | # Since we know exactly where we are, and in which | ||
| 407 | # direction this works, we can just get the coordinates | ||
| 408 | # for $pa. | ||
| 409 | my ($x, $y) = cartesian( | ||
| 410 | $self->{w}/2, $pa, | ||
| 411 | $self->{xc}, $self->{yc}, $self->{h}/$self->{w} | ||
| 412 | ); | ||
| 413 | |||
| 414 | # and move one pixel to the left, but only if we don't | ||
| 415 | # fall out of the pie!. | ||
| 416 | push @fills, [$x - 1, $y] | ||
| 417 | if $x - 1 > $self->{xc} - $self->{w}/2; | ||
| 418 | |||
| 419 | # Reset $pa to the right edge of the front arc, to do | ||
| 420 | # the right bit on the front. | ||
| 421 | $pa = level_angle(-$ANGLE_OFFSET); | ||
| 422 | } | ||
| 423 | } | ||
| 424 | else | ||
| 425 | { | ||
| 426 | # start in front, end in back | ||
| 427 | $pb = $ANGLE_OFFSET; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | else | ||
| 431 | { | ||
| 432 | if (in_front($pb)) | ||
| 433 | { | ||
| 434 | # start in back, end in front | ||
| 435 | $pa = $ANGLE_OFFSET - 180; | ||
| 436 | } | ||
| 437 | elsif ( # both in back, but wrapping around the front | ||
| 438 | # CONTRIB kedlubnowski, Dan Rosendorf | ||
| 439 | $pa > 90 && $pb > 90 && $pa >= $pb | ||
| 440 | or $pa < -90 && $pb < -90 && $pa >= $pb | ||
| 441 | or $pa < -90 && $pb > 90 | ||
| 442 | ) | ||
| 443 | { | ||
| 444 | $pa=$ANGLE_OFFSET - 180; | ||
| 445 | $pb=$ANGLE_OFFSET; | ||
| 446 | } | ||
| 447 | else | ||
| 448 | { | ||
| 449 | return; | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | my ($x, $y) = cartesian( | ||
| 454 | $self->{w}/2, ($pa + $pb)/2, | ||
| 455 | $self->{xc}, $self->{yc}, $self->{h}/$self->{w} | ||
| 456 | ); | ||
| 457 | |||
| 458 | push @fills, [$x, $y]; | ||
| 459 | |||
| 460 | return @fills; | ||
| 461 | } | ||
| 462 | |||
| 463 | # return true if this angle is on the front of the pie | ||
| 464 | # XXX UGLY! We need to leave a slight room for error because of rounding | ||
| 465 | # problems | ||
| 466 | sub in_front | ||
| 467 | { | ||
| 468 | my $a = level_angle(shift); | ||
| 469 | return | ||
| 470 | $a > ($ANGLE_OFFSET - 180 + 0.00000001) && | ||
| 471 | $a < $ANGLE_OFFSET - 0.000000001; | ||
| 472 | } | ||
| 473 | |||
| 474 | # XXX Ugh! I need to fix this. See the GD::Text module for better ways | ||
| 475 | # of doing this. | ||
| 476 | # return a value for angle between -180 and 180 | ||
| 477 | sub level_angle # (angle) | ||
| 478 | { | ||
| 479 | my $a = shift; | ||
| 480 | return level_angle($a-360) if ( $a > 180 ); | ||
| 481 | return level_angle($a+360) if ( $a <= -180 ); | ||
| 482 | return $a; | ||
| 483 | } | ||
| 484 | |||
| 485 | # put the slice label on the pie | ||
| 486 | sub put_slice_label | ||
| 487 | { | ||
| 488 | my $self = shift; | ||
| 489 | my ($x, $y, $label) = @_; | ||
| 490 | |||
| 491 | return unless defined $label; | ||
| 492 | |||
| 493 | $self->{gdta_value}->set_text($label); | ||
| 494 | $self->{gdta_value}->draw($x, $y); | ||
| 495 | } | ||
| 496 | |||
| 497 | # return x, y coordinates from input | ||
| 498 | # radius, angle, center x and y and a scaling factor (height/width) | ||
| 499 | # | ||
| 500 | # $ANGLE_OFFSET is used to define where 0 is meant to be | ||
| 501 | sub cartesian | ||
| 502 | { | ||
| 503 | my ($r, $phi, $xi, $yi, $cr) = @_; | ||
| 504 | |||
| 505 | return map _round($_), ( | ||
| 506 | $xi + $r * cos(PI * ($phi + $ANGLE_OFFSET)/180), | ||
| 507 | $yi + $cr * $r * sin(PI * ($phi + $ANGLE_OFFSET)/180) | ||
| 508 | ) | ||
| 509 | } | ||
| 510 | |||
| 511 | #functions specific to 3d + aa pies | ||
| 512 | sub draw_pie_data_aa | ||
| 513 | { | ||
| 514 | my $self = shift; | ||
| 515 | |||
| 516 | require GD::Graph::pie_slice; | ||
| 517 | |||
| 518 | $self->{pieSlices} = []; | ||
| 519 | |||
| 520 | my $sum = 0; | ||
| 521 | my @values = $self->{_data}->y_values(1); | ||
| 522 | for (@values) | ||
| 523 | { | ||
| 524 | $sum += $_ | ||
| 525 | } | ||
| 526 | |||
| 527 | return $self->_set_error("Pie data total is <= 0") | ||
| 528 | unless $sum > 0; | ||
| 529 | |||
| 530 | my $largest_displacement = $self->_largest_displacement(); | ||
| 531 | |||
| 532 | my @listPieSlices = (); | ||
| 533 | my @orderList = (); | ||
| 534 | my $clrIndex = 0; | ||
| 535 | my $backPieIndex = -1; | ||
| 536 | my $displacementIndex = 0; | ||
| 537 | my $startAngle = $self->{start_angle}; | ||
| 538 | |||
| 539 | for (my $i = 0; $i < @values; $i++){ | ||
| 540 | my $itemValue = $values[$i]; | ||
| 541 | #XXX: This isn't right... this round causes things to add up to more than 360 | ||
| 542 | my $sweepAngle = $itemValue / $sum * 360; | ||
| 543 | my $actualSweepAngle = $sweepAngle + $startAngle; | ||
| 544 | |||
| 545 | #get the angle within 360, but keep the decimal places... is there a better way? | ||
| 546 | my $startAngleMod = ($startAngle % 360) + ($startAngle - floor($startAngle)); | ||
| 547 | my $sweepAngleMod = ($actualSweepAngle % 360) + ($actualSweepAngle - floor($actualSweepAngle)); | ||
| 548 | #print STDERR "startAngle = $startAngle (".$startAngleMod. "), sweepAngle = $sweepAngle, actualSweepAngle = $actualSweepAngle (".$sweepAngleMod .")\n"; | ||
| 549 | |||
| 550 | my $xDisplacement = $self->{offsets}[$i]; | ||
| 551 | my $yDisplacement = $self->{offsets}[$i]; | ||
| 552 | if ($xDisplacement > 0){ | ||
| 553 | ($xDisplacement,$yDisplacement) = $self->_get_slice_displacement(($startAngle + $sweepAngle / 2), $xDisplacement); | ||
| 554 | } | ||
| 555 | |||
| 556 | if ($actualSweepAngle == 360 && $startAngle == $self->{start_angle}){ | ||
| 557 | $startAngleMod = 0; | ||
| 558 | $sweepAngleMod = 360; | ||
| 559 | } | ||
| 560 | |||
| 561 | my @pdc = $self->pick_data_clr($i + 1); | ||
| 562 | my $dc = $self->set_clr_uniq(@pdc,$self->{alpha}); | ||
| 563 | my $shadowClr = $self->set_clr(_darken(@pdc),$self->{alpha}); | ||
| 564 | my $slice = new GD::Graph::pie_slice( | ||
| 565 | $self->{largestDisplacementEllipseWidth} /2 + $xDisplacement+$self->{left}, | ||
| 566 | $self->{largestDisplacementEllipseHeight} /2 + $yDisplacement+$self->{top}, | ||
| 567 | $self->{topEllipseWidth}, | ||
| 568 | $self->{topEllipseHeight}, | ||
| 569 | $self->{pie_height}, | ||
| 570 | $startAngleMod, | ||
| 571 | $sweepAngleMod, | ||
| 572 | $self->{_data}->get_x($i), | ||
| 573 | $dc, | ||
| 574 | $shadowClr); | ||
| 575 | |||
| 576 | #decide when to draw it | ||
| 577 | if ($backPieIndex > -1 || (($startAngle <=270) && ($startAngle + $sweepAngle > 270)) || (($startAngle >=270) && ($startAngle + $sweepAngle > 630))){ | ||
| 578 | $backPieIndex++; | ||
| 579 | splice(@listPieSlices,$backPieIndex,0,$slice); | ||
| 580 | splice(@orderList,$backPieIndex,0,$i); | ||
| 581 | } else { | ||
| 582 | push(@listPieSlices,$slice); | ||
| 583 | push(@orderList,$i); | ||
| 584 | } | ||
| 585 | |||
| 586 | $startAngle += $sweepAngle; | ||
| 587 | if ($startAngle > 360){ | ||
| 588 | $startAngle -= 360; | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | $self->{pieSlices} = \@listPieSlices; | ||
| 593 | $self->{orderList} = \@orderList; | ||
| 594 | |||
| 595 | #draw the pie! | ||
| 596 | foreach my $slice (@listPieSlices){ | ||
| 597 | $slice->draw_bottom($self->{graph}); | ||
| 598 | } | ||
| 599 | $self->draw_sides(@listPieSlices); | ||
| 600 | foreach my $slice (@listPieSlices){ | ||
| 601 | $slice->draw_top($self->{graph}); | ||
| 602 | } | ||
| 603 | |||
| 604 | $self->_draw_data(); | ||
| 605 | |||
| 606 | return $self; | ||
| 607 | }#draw_pie_data_aa | ||
| 608 | |||
| 609 | #taken from axestype3d.pm | ||
| 610 | sub _darken | ||
| 611 | { | ||
| 612 | my( $r, $g, $b ) = @_; | ||
| 613 | my $p = ($r + $g + $b) / 70; | ||
| 614 | $p = 3 if $p < 3; | ||
| 615 | my $f = _max( $r / $p, _max( $g / $p, $b / $p) ); | ||
| 616 | $r = _max( 0, int( $r - $f ) ); | ||
| 617 | $g = _max( 0, int( $g - $f ) ); | ||
| 618 | $b = _max( 0, int( $b - $f ) ); | ||
| 619 | return( $r, $g, $b ); | ||
| 620 | } # end _darken | ||
| 621 | |||
| 622 | sub _largest_displacement | ||
| 623 | { | ||
| 624 | my $self = shift; | ||
| 625 | |||
| 626 | my $value = 0; | ||
| 627 | foreach my $offset (@{$self->{offsets}}){ | ||
| 628 | if ($offset > $value){ | ||
| 629 | $value = $offset; | ||
| 630 | } | ||
| 631 | } | ||
| 632 | return $value; | ||
| 633 | } | ||
| 634 | |||
| 635 | sub _largest_displacement_ellipse_size | ||
| 636 | { | ||
| 637 | my $self = shift; | ||
| 638 | |||
| 639 | my $factor = $self->_largest_displacement(); | ||
| 640 | $self->{largestDisplacementEllipseWidth} = $self->{topEllipseWidth}*$factor; | ||
| 641 | $self->{largestDisplacementEllipseHeight} = $self->{topEllipseHeight}*$factor; | ||
| 642 | |||
| 643 | } | ||
| 644 | |||
| 645 | sub _top_ellipse_size | ||
| 646 | { | ||
| 647 | my $self = shift; | ||
| 648 | |||
| 649 | my $factor = 1 + $self->_largest_displacement(); | ||
| 650 | $self->{topEllipseWidth} = $self->{w} / $factor; | ||
| 651 | $self->{topEllipseHeight}= $self->{h}/ $factor; | ||
| 652 | } | ||
| 653 | |||
| 654 | sub _get_slice_displacement | ||
| 655 | { | ||
| 656 | my $self = shift; | ||
| 657 | |||
| 658 | my ($angle,$displacementFactor) = @_; | ||
| 659 | return (($self->{topEllipseWidth} * $displacementFactor /2 *cos ($angle * PI /180), | ||
| 660 | ($self->{topEllipseHeight} * $displacementFactor /2 *sin ($angle * PI /180)))); | ||
| 661 | } | ||
| 662 | |||
| 663 | sub draw_sides | ||
| 664 | { | ||
| 665 | my $self = shift; | ||
| 666 | my @slices = @_; | ||
| 667 | |||
| 668 | my $inc = 1; | ||
| 669 | my $dec = @slices - 1; | ||
| 670 | |||
| 671 | $slices[0]->draw_sides($self->{graph}) ; | ||
| 672 | |||
| 673 | my ($sliceLeft,$sliceRight); | ||
| 674 | my ($angle1,$angle2); | ||
| 675 | while ($inc < $dec){ | ||
| 676 | $sliceLeft = $slices[$dec]; | ||
| 677 | $angle1 = $sliceLeft->{startAngle} - 90; | ||
| 678 | if ($angle1 > 180 || $angle1 < 0){ | ||
| 679 | $angle1 = 0; | ||
| 680 | } | ||
| 681 | $sliceRight = $slices[$inc]; | ||
| 682 | $angle2 = 450 - $sliceRight->{startAngle} % 360; | ||
| 683 | if ($angle2 > 180 || $angle2 < 0){ | ||
| 684 | $angle2 = 0; | ||
| 685 | } | ||
| 686 | if ($angle2 >= $angle1){ | ||
| 687 | $sliceRight->draw_sides($self->{graph}); | ||
| 688 | $inc++; | ||
| 689 | } elsif ($angle2 < $angle1){ | ||
| 690 | $sliceLeft->draw_sides($self->{graph}); | ||
| 691 | $dec--; | ||
| 692 | } | ||
| 693 | } | ||
| 694 | $slices[$dec]->draw_sides($self->{graph}); | ||
| 695 | } | ||
| 696 | |||
| 697 | sub get_slices | ||
| 698 | { | ||
| 699 | my $self = shift; | ||
| 700 | return $self->{pieSlices}; | ||
| 701 | } | ||
| 702 | |||
| 703 | sub get_display_order | ||
| 704 | { | ||
| 705 | my $self = shift; | ||
| 706 | my $index = shift; | ||
| 707 | return $self->{orderList}[$index]; | ||
| 708 | } | ||
| 709 | |||
| 710 | #private function to draw the labels of the slices for 3d-aa pies | ||
| 711 | sub _draw_data | ||
| 712 | { | ||
| 713 | my $self = shift; | ||
| 714 | my @slices = @{$self->{pieSlices}}; | ||
| 715 | |||
| 716 | my ($x,$y); | ||
| 717 | foreach my $slice (@slices){ | ||
| 718 | next if ($slice->{sweepAngle} - $slice->{startAngle} < $self->{suppress_angle}); | ||
| 719 | |||
| 720 | ($x,$y) = $slice->get_text_position(); | ||
| 721 | |||
| 722 | $self->put_slice_label($x, $y, $slice->{label}); | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | # Legend Support. Added 16.Feb.2001 - JW/WADG | ||
| 727 | |||
| 728 | sub set_legend # List of legend keys | ||
| 729 | { | ||
| 730 | my $self = shift; | ||
| 731 | $self->{legend} = [@_]; | ||
| 732 | } | ||
| 733 | |||
| 734 | sub set_legend_font # (font name) | ||
| 735 | { | ||
| 736 | my $self = shift; | ||
| 737 | $self->_set_font('gdta_legend', @_); | ||
| 738 | } | ||
| 739 | |||
| 740 | |||
| 741 | |||
| 742 | # | ||
| 743 | # Legend | ||
| 744 | # | ||
| 745 | sub setup_legend | ||
| 746 | { | ||
| 747 | my $self = shift; | ||
| 748 | |||
| 749 | return unless defined $self->{legend}; | ||
| 750 | |||
| 751 | my $maxlen = 0; | ||
| 752 | my $num = 0; | ||
| 753 | |||
| 754 | # Save some variables | ||
| 755 | $self->{r_margin_abs} = $self->{r_margin}; | ||
| 756 | $self->{b_margin_abs} = $self->{b_margin}; | ||
| 757 | |||
| 758 | foreach my $legend (@{$self->{legend}}) | ||
| 759 | { | ||
| 760 | if (defined($legend) and $legend ne "") | ||
| 761 | { | ||
| 762 | $self->{gdta_legend}->set_text($legend); | ||
| 763 | my $len = $self->{gdta_legend}->get('width'); | ||
| 764 | $maxlen = ($maxlen > $len) ? $maxlen : $len; | ||
| 765 | $num++; | ||
| 766 | } | ||
| 767 | # Legend for Pie goes over first set, and all points | ||
| 768 | last if $num >= $self->{_data}->num_points; | ||
| 769 | } | ||
| 770 | |||
| 771 | $self->{lg_num} = $num; | ||
| 772 | |||
| 773 | # calculate the height and width of each element | ||
| 774 | my $legend_height = _max($self->{lgfh}, $self->{legend_marker_height}); | ||
| 775 | |||
| 776 | $self->{lg_el_width} = | ||
| 777 | $maxlen + $self->{legend_marker_width} + 3 * $self->{legend_spacing}; | ||
| 778 | $self->{lg_el_height} = $legend_height + 2 * $self->{legend_spacing}; | ||
| 779 | |||
| 780 | my ($lg_pos, $lg_align) = split(//, $self->{legend_placement}); | ||
| 781 | |||
| 782 | if ($lg_pos eq 'R') | ||
| 783 | { | ||
| 784 | # Always work in one column | ||
| 785 | $self->{lg_cols} = 1; | ||
| 786 | $self->{lg_rows} = $num; | ||
| 787 | |||
| 788 | # Just for completeness, might use this in later versions | ||
| 789 | $self->{lg_x_size} = $self->{lg_cols} * $self->{lg_el_width}; | ||
| 790 | $self->{lg_y_size} = $self->{lg_rows} * $self->{lg_el_height}; | ||
| 791 | |||
| 792 | # Adjust the right margin for the rest of the graph | ||
| 793 | $self->{r_margin} += $self->{lg_x_size}; | ||
| 794 | |||
| 795 | # Adjust for frame if defined | ||
| 796 | if( $self->{legend_frame_size} ) { | ||
| 797 | $self->{r_margin} += 2 * ($self->{legend_frame_margin} + $self->{legend_frame_size}); | ||
| 798 | } # end if; | ||
| 799 | |||
| 800 | # Set the x starting point | ||
| 801 | $self->{lg_xs} = $self->{width} - $self->{r_margin}; | ||
| 802 | |||
| 803 | # Set the y starting point, depending on alignment | ||
| 804 | if ($lg_align eq 'T') | ||
| 805 | { | ||
| 806 | $self->{lg_ys} = $self->{t_margin}; | ||
| 807 | } | ||
| 808 | elsif ($lg_align eq 'B') | ||
| 809 | { | ||
| 810 | $self->{lg_ys} = $self->{height} - $self->{b_margin} - | ||
| 811 | $self->{lg_y_size}; | ||
| 812 | } | ||
| 813 | else # default 'C' | ||
| 814 | { | ||
| 815 | my $height = $self->{height} - $self->{t_margin} - | ||
| 816 | $self->{b_margin}; | ||
| 817 | |||
| 818 | $self->{lg_ys} = | ||
| 819 | int($self->{t_margin} + $height/2 - $self->{lg_y_size}/2) ; | ||
| 820 | } | ||
| 821 | } | ||
| 822 | else # 'B' is the default | ||
| 823 | { | ||
| 824 | # What width can we use | ||
| 825 | my $width = $self->{width} - $self->{l_margin} - $self->{r_margin}; | ||
| 826 | |||
| 827 | (!defined($self->{lg_cols})) and | ||
| 828 | $self->{lg_cols} = int($width/$self->{lg_el_width}); | ||
| 829 | |||
| 830 | $self->{lg_cols} = _min($self->{lg_cols}, $num); | ||
| 831 | |||
| 832 | $self->{lg_rows} = | ||
| 833 | int($num / $self->{lg_cols}) + (($num % $self->{lg_cols}) ? 1 : 0); | ||
| 834 | |||
| 835 | $self->{lg_x_size} = $self->{lg_cols} * $self->{lg_el_width}; | ||
| 836 | $self->{lg_y_size} = $self->{lg_rows} * $self->{lg_el_height}; | ||
| 837 | |||
| 838 | # Adjust the bottom margin for the rest of the graph | ||
| 839 | $self->{b_margin} += $self->{lg_y_size}; | ||
| 840 | # Adjust for frame if defined | ||
| 841 | if( $self->{legend_frame_size} ) { | ||
| 842 | $self->{b_margin} += 2 * ($self->{legend_frame_margin} + $self->{legend_frame_size}); | ||
| 843 | } # end if; | ||
| 844 | |||
| 845 | # Set the y starting point | ||
| 846 | $self->{lg_ys} = $self->{height} - $self->{b_margin}; | ||
| 847 | |||
| 848 | # Set the x starting point, depending on alignment | ||
| 849 | if ($lg_align eq 'R') | ||
| 850 | { | ||
| 851 | $self->{lg_xs} = $self->{width} - $self->{r_margin} - | ||
| 852 | $self->{lg_x_size}; | ||
| 853 | } | ||
| 854 | elsif ($lg_align eq 'L') | ||
| 855 | { | ||
| 856 | $self->{lg_xs} = $self->{l_margin}; | ||
| 857 | } | ||
| 858 | else # default 'C' | ||
| 859 | { | ||
| 860 | $self->{lg_xs} = | ||
| 861 | int($self->{l_margin} + $width/2 - $self->{lg_x_size}/2); | ||
| 862 | } | ||
| 863 | } | ||
| 864 | } | ||
| 865 | |||
| 866 | sub draw_legend | ||
| 867 | { | ||
| 868 | my $self = shift; | ||
| 869 | |||
| 870 | return unless defined $self->{legend}; | ||
| 871 | |||
| 872 | my $xl = $self->{lg_xs} + $self->{legend_spacing}; | ||
| 873 | my $y = $self->{lg_ys} + $self->{legend_spacing} - 1; | ||
| 874 | |||
| 875 | # If there's a frame, offset by the size and margin | ||
| 876 | $xl += $self->{legend_frame_margin} + $self->{legend_frame_size} if $self->{legend_frame_size}; | ||
| 877 | $y += $self->{legend_frame_margin} + $self->{legend_frame_size} if $self->{legend_frame_size}; | ||
| 878 | |||
| 879 | my $i = 0; | ||
| 880 | my $row = 1; | ||
| 881 | my $x = $xl; # start position of current element | ||
| 882 | |||
| 883 | foreach my $legend (@{$self->{legend}}) | ||
| 884 | { | ||
| 885 | $i++; | ||
| 886 | # Legend for Pie goes over first set, and all points | ||
| 887 | last if $i > $self->{_data}->num_points; | ||
| 888 | |||
| 889 | my $xe = $x; # position within an element | ||
| 890 | |||
| 891 | next unless defined($legend) && $legend ne ""; | ||
| 892 | |||
| 893 | $self->draw_legend_marker($i, $xe, $y); | ||
| 894 | |||
| 895 | $xe += $self->{legend_marker_width} + $self->{legend_spacing}; | ||
| 896 | my $ys = int($y + $self->{lg_el_height}/2 - $self->{lgfh}/2); | ||
| 897 | |||
| 898 | $self->{gdta_legend}->set_text($legend); | ||
| 899 | $self->{gdta_legend}->draw($xe, $ys); | ||
| 900 | |||
| 901 | $x += $self->{lg_el_width}; | ||
| 902 | |||
| 903 | if (++$row > $self->{lg_cols}) | ||
| 904 | { | ||
| 905 | $row = 1; | ||
| 906 | $y += $self->{lg_el_height}; | ||
| 907 | $x = $xl; | ||
| 908 | } | ||
| 909 | } | ||
| 910 | |||
| 911 | # If there's a frame, draw it now | ||
| 912 | if( $self->{legend_frame_size} ) { | ||
| 913 | $x = $self->{lg_xs} + $self->{legend_spacing}; | ||
| 914 | $y = $self->{lg_ys} + $self->{legend_spacing} - 1; | ||
| 915 | |||
| 916 | for $i ( 0 .. $self->{legend_frame_size} - 1 ) { | ||
| 917 | $self->{graph}->rectangle( | ||
| 918 | $x + $i, | ||
| 919 | $y + $i, | ||
| 920 | $x + $self->{lg_x_size} + 2 * $self->{legend_frame_margin} - $i - 1, | ||
| 921 | $y + $self->{lg_y_size} + 2 * $self->{legend_frame_margin} - $i - 1, | ||
| 922 | $self->{acci}, | ||
| 923 | ); | ||
| 924 | } # end for | ||
| 925 | } # end if | ||
| 926 | |||
| 927 | } | ||
| 928 | |||
| 929 | sub draw_legend_marker # data_set_number, x, y | ||
| 930 | { | ||
| 931 | my $s = shift; | ||
| 932 | my $n = shift; | ||
| 933 | my $x = shift; | ||
| 934 | my $y = shift; | ||
| 935 | |||
| 936 | my $g = $s->{graph}; | ||
| 937 | |||
| 938 | my $ci = $s->set_clr($s->pick_data_clr($n)); | ||
| 939 | |||
| 940 | $y += int($s->{lg_el_height}/2 - $s->{legend_marker_height}/2); | ||
| 941 | |||
| 942 | $g->filledRectangle( | ||
| 943 | $x, $y, | ||
| 944 | $x + $s->{legend_marker_width}, $y + $s->{legend_marker_height}, | ||
| 945 | $ci | ||
| 946 | ); | ||
| 947 | |||
| 948 | $g->rectangle( | ||
| 949 | $x, $y, | ||
| 950 | $x + $s->{legend_marker_width}, $y + $s->{legend_marker_height}, | ||
| 951 | $s->{acci} | ||
| 952 | ); | ||
| 953 | } | ||
| 954 | |||
| 955 | "Just another true value"; |