2 /*=======================================================================
3 // File: JPGRAPH_PIE3D.PHP
4 // Description: 3D Pie plot extension for JpGraph
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_pie3d.php,v 1.1 2006/07/07 13:37:14 powles Exp $
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
13 //===================================================
15 // Description: Plots a 3D pie with a specified projection
16 // angle between 20 and 70 degrees.
17 //===================================================
18 class PiePlot3D
extends PiePlot
{
19 var $labelhintcolor="red",$showlabelhint=true;
21 var $edgecolor="", $edgeweight=1;
22 var $iThickness=false;
26 function PiePlot3d(&$data) {
29 $this->title
= new Text("");
30 $this->title
->SetFont(FF_FONT1
,FS_BOLD
);
31 $this->value
= new DisplayValue();
33 $this->value
->SetFormat('%.0f%%');
40 function SetLegends($aLegend) {
41 $this->legends
= array_reverse(array_slice($aLegend,0,count($this->data
)));
44 function SetSliceColors($aColors) {
45 $this->setslicecolors
= $aColors;
48 function Legend(&$aGraph) {
49 parent
::Legend($aGraph);
50 $aGraph->legend
->txtcol
= array_reverse($aGraph->legend
->txtcol
);
53 function SetCSIMTargets($targets,$alts=null) {
54 $this->csimtargets
= $targets;
55 $this->csimalts
= $alts;
58 // Should the slices be separated by a line? If color is specified as "" no line
59 // will be used to separate pie slices.
60 function SetEdge($aColor='black',$aWeight=1) {
61 $this->edgecolor
= $aColor;
62 $this->edgeweight
= $aWeight;
65 // Dummy function to make Pie3D behave in a similair way to 2D
66 function ShowBorder($exterior=true,$interior=true) {
67 JpGraphError
::RaiseL(14001);
68 //('Pie3D::ShowBorder() . Deprecated function. Use Pie3D::SetEdge() to control the edges around slices.');
71 // Specify projection angle for 3D in degrees
72 // Must be between 20 and 70 degrees
73 function SetAngle($a) {
75 JpGraphError
::RaiseL(14002);
76 //("PiePlot3D::SetAngle() 3D Pie projection angle must be between 5 and 85 degrees.");
81 function AddSliceToCSIM($i,$xc,$yc,$height,$width,$thick,$sa,$ea) { //Slice number, ellipse centre (x,y), height, width, start angle, end angle
86 //add coordinates of the centre to the map
89 //add coordinates of the first point on the arc to the map
90 $xp = floor($width*cos($sa)/2+
$xc);
91 $yp = floor($yc-$height*sin($sa)/2);
92 $coords.= ", $xp, $yp";
94 //If on the front half, add the thickness offset
95 if ($sa >= M_PI
&& $sa <= 2*M_PI
*1.01) {
96 $yp = floor($yp+
$thick);
97 $coords.= ", $xp, $yp";
100 //add coordinates every 0.2 radians
103 $xp = floor($width*cos($a)/2+
$xc);
104 if ($a >= M_PI
&& $a <= 2*M_PI
*1.01) {
105 $yp = floor($yc-($height*sin($a)/2)+
$thick);
107 $yp = floor($yc-$height*sin($a)/2);
109 $coords.= ", $xp, $yp";
113 //Add the last point on the arc
114 $xp = floor($width*cos($ea)/2+
$xc);
115 $yp = floor($yc-$height*sin($ea)/2);
118 if ($ea >= M_PI
&& $ea <= 2*M_PI
*1.01) {
119 $coords.= ", $xp, ".floor($yp+
$thick);
121 $coords.= ", $xp, $yp";
123 if( !empty($this->csimalts
[$i]) ) {
124 $tmp=sprintf($this->csimalts
[$i],$this->data
[$i]);
125 $alt="alt=\"$tmp\" title=\"$tmp\"";
127 if( !empty($this->csimtargets
[$i]) )
128 $this->csimareas
.= "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->csimtargets
[$i]."\" $alt />\n";
131 function SetLabels($aLabels,$aLblPosAdj="auto") {
132 $this->labels
= $aLabels;
133 $this->ilabelposadj
=$aLblPosAdj;
137 // Distance from the pie to the labels
138 function SetLabelMargin($m) {
139 $this->value
->SetMargin($m);
142 // Show a thin line from the pie to the label for a specific slice
143 function ShowLabelHint($f=true) {
144 $this->showlabelhint
=$f;
147 // Set color of hint line to label for each slice
148 function SetLabelHintColor($c) {
149 $this->labelhintcolor
=$c;
152 function SetHeight($aHeight) {
153 $this->iThickness
= $aHeight;
157 // Normalize Angle between 0-360
158 function NormAngle($a) {
159 // Normalize anle to 0 to 2M_PI
162 while($a > 360) $a -= 360;
165 while($a < 0) $a +
= 360;
170 if( $a == 360 ) $a=0;
176 // Draw one 3D pie slice at position ($xc,$yc) with height $z
177 function Pie3DSlice($img,$xc,$yc,$w,$h,$sa,$ea,$z,$fillcolor,$shadow=0.65) {
179 // Due to the way the 3D Pie algorithm works we are
180 // guaranteed that any slice we get into this method
181 // belongs to either the left or right side of the
182 // pie ellipse. Hence, no slice will cross 90 or 270
184 if( ($sa < 90 && $ea > 90) ||
( ($sa > 90 && $sa < 270) && $ea > 270) ) {
185 JpGraphError
::RaiseL(14003);//('Internal assertion failed. Pie3D::Pie3DSlice');
191 // Setup pre-calculated values
192 $rsa = $sa/180*M_PI
; // to Rad
193 $rea = $ea/180*M_PI
; // to Rad
199 // p[] is the points for the overall slice and
200 // pt[] is the points for the top pie
202 // Angular step when approximating the arc with a polygon train.
206 if( $ea > 360 ||
($ea > 0 && $ea <= 90) ) {
207 if( $ea > 0 && $ea <= 90 ) {
208 // Adjust angle to simplify conditions in loops
212 $p = array($xc,$yc,$xc,$yc+
$z,
213 $xc+
$w*$cossa,$z+
$yc-$h*$sinsa);
214 $pt = array($xc,$yc,$xc+
$w*$cossa,$yc-$h*$sinsa);
216 for( $a=$rsa; $a < 2*M_PI
; $a +
= $step ) {
220 $p[] = $z+
$yc-$h*$tsa;
235 for( $a=2*M_PI+
$step; $a < $rea; $a +
= $step ) {
236 $pt[] = $xc +
$w*cos($a);
237 $pt[] = $yc - $h*sin($a);
240 $pt[] = $xc+
$w*$cosea;
241 $pt[] = $yc-$h*$sinea;
247 $p = array($xc,$yc,$xc,$yc+
$z,
248 $xc+
$w*$cossa,$z+
$yc-$h*$sinsa);
249 $pt = array($xc,$yc,$xc+
$w*$cossa,$yc-$h*$sinsa);
251 $rea = $rea == 0.0 ?
2*M_PI
: $rea;
252 for( $a=$rsa; $a < $rea; $a +
= $step ) {
256 $p[] = $z+
$yc-$h*$tsa;
261 $pt[] = $xc+
$w*$cosea;
262 $pt[] = $yc-$h*$sinea;
266 $p[] = $xc+
$w*$cosea;
267 $p[] = $z+
$yc-$h*$sinea;
268 $p[] = $xc+
$w*$cosea;
269 $p[] = $yc-$h*$sinea;
274 elseif( $sa >= 180 ) {
275 $p = array($xc,$yc,$xc,$yc+
$z,$xc+
$w*$cosea,$z+
$yc-$h*$sinea);
276 $pt = array($xc,$yc,$xc+
$w*$cosea,$yc-$h*$sinea);
278 for( $a=$rea; $a>$rsa; $a -= $step ) {
282 $p[] = $z+
$yc-$h*$tsa;
287 $pt[] = $xc+
$w*$cossa;
288 $pt[] = $yc-$h*$sinsa;
292 $p[] = $xc+
$w*$cossa;
293 $p[] = $z+
$yc-$h*$sinsa;
294 $p[] = $xc+
$w*$cossa;
295 $p[] = $yc-$h*$sinsa;
300 elseif( $sa >= 90 ) {
302 $p = array($xc,$yc,$xc,$yc+
$z,$xc+
$w*$cosea,$z+
$yc-$h*$sinea);
303 $pt = array($xc,$yc,$xc+
$w*$cosea,$yc-$h*$sinea);
305 for( $a=$rea; $a > M_PI
; $a -= $step ) {
309 $p[] = $z +
$yc - $h*$tsa;
326 for( $a=M_PI
-$step; $a > $rsa; $a -= $step ) {
327 $pt[] = $xc +
$w*cos($a);
328 $pt[] = $yc - $h*sin($a);
331 $pt[] = $xc+
$w*$cossa;
332 $pt[] = $yc-$h*$sinsa;
337 else { // $sa >= 90 && $ea <= 180
338 $p = array($xc,$yc,$xc,$yc+
$z,
339 $xc+
$w*$cosea,$z+
$yc-$h*$sinea,
340 $xc+
$w*$cosea,$yc-$h*$sinea,
343 $pt = array($xc,$yc,$xc+
$w*$cosea,$yc-$h*$sinea);
345 for( $a=$rea; $a>$rsa; $a -= $step ) {
346 $pt[] = $xc +
$w*cos($a);
347 $pt[] = $yc - $h*sin($a);
350 $pt[] = $xc+
$w*$cossa;
351 $pt[] = $yc-$h*$sinsa;
357 else { // sa > 0 && ea < 90
359 $p = array($xc,$yc,$xc,$yc+
$z,
360 $xc+
$w*$cossa,$z+
$yc-$h*$sinsa,
361 $xc+
$w*$cossa,$yc-$h*$sinsa,
364 $pt = array($xc,$yc,$xc+
$w*$cossa,$yc-$h*$sinsa);
366 for( $a=$rsa; $a < $rea; $a +
= $step ) {
367 $pt[] = $xc +
$w*cos($a);
368 $pt[] = $yc - $h*sin($a);
371 $pt[] = $xc+
$w*$cosea;
372 $pt[] = $yc-$h*$sinea;
377 $img->PushColor($fillcolor.":".$shadow);
378 $img->FilledPolygon($p);
381 $img->PushColor($fillcolor);
382 $img->FilledPolygon($pt);
386 function SetStartAngle($aStart) {
387 if( $aStart < 0 ||
$aStart > 360 ) {
388 JpGraphError
::RaiseL(14004);//('Slice start angle must be between 0 and 360 degrees.');
390 $this->startangle
= $aStart;
394 function Pie3D($aaoption,$img,$data,$colors,$xc,$yc,$d,$angle,$z,
395 $shadow=0.65,$startangle=0,$edgecolor="",$edgeweight=1) {
397 //---------------------------------------------------------------------------
398 // As usual the algorithm get more complicated than I originally
399 // envisioned. I believe that this is as simple as it is possible
400 // to do it with the features I want. It's a good exercise to start
401 // thinking on how to do this to convince your self that all this
402 // is really needed for the general case.
404 // The algorithm two draw 3D pies without "real 3D" is done in
406 // First imagine the pie cut in half through a thought line between
407 // 12'a clock and 6'a clock. It now easy to imagine that we can plot
408 // the individual slices for each half by starting with the topmost
409 // pie slice and continue down to 6'a clock.
411 // In the algortithm this is done in three principal steps
412 // Step 1. Do the knife cut to ensure by splitting slices that extends
413 // over the cut line. This is done by splitting the original slices into
415 // Step 2. Find the top slice for each half
416 // Step 3. Draw the slices from top to bottom
418 // The thing that slightly complicates this scheme with all the
419 // angle comparisons below is that we can have an arbitrary start
420 // angle so we must take into account the different equivalence classes.
421 // For the same reason we must walk through the angle array in a
424 // Limitations of algorithm:
425 // * A small exploded slice which crosses the 270 degree point
426 // will get slightly nagged close to the center due to the fact that
427 // we print the slices in Z-order and that the slice left part
428 // get printed first and might get slightly nagged by a larger
429 // slice on the right side just before the right part of the small
430 // slice. Not a major problem though.
431 //---------------------------------------------------------------------------
434 // Determine the height of the ellippse which gives an
435 // indication of the inclination angle
436 $h = ($angle/90.0)*$d;
438 for($i=0; $i<count($data); ++
$i ) {
442 // Special optimization
443 if( $sum==0 ) return;
445 if( $this->labeltype
== 2 ) {
446 $this->adjusted_data
= $this->AdjPercentage($data);
452 $a = $this->NormAngle($a);
455 // Step 1 . Split all slices that crosses 90 or 270
459 $numcolors = count($colors);
460 for($i=0; $i<count($data); ++
$i, ++
$idx ) {
461 $da = $data[$i]/$sum * 360;
463 if( empty($this->explode_radius
[$i]) )
464 $this->explode_radius
[$i]=0;
471 $explode = array( $xc +
$this->explode_radius
[$i]*cos($la*M_PI
/180)*$expscale,
472 $yc - $this->explode_radius
[$i]*sin($la*M_PI
/180) * ($h/$d) *$expscale );
473 $adjexplode[$idx] = $explode;
474 $labeldata[$i] = array($la,$explode[0],$explode[1]);
475 $originalangles[$i] = array($a,$a+
$da);
477 $ne = $this->NormAngle($a+
$da);
479 // If the slice size is <= 90 it can at maximum cut across
480 // one boundary (either 90 or 270) where it needs to be split
481 $split=-1; // no split
482 if( ($da<=90 && ($a <= 90 && $ne > 90)) ||
483 (($da <= 180 && $da >90) && (($a < 90 ||
$a >= 270) && $ne > 90)) ) {
486 elseif( ($da<=90 && ($a <= 270 && $ne > 270)) ||
487 (($da<=180 && $da>90) && ($a >= 90 && $a < 270 && ($a+
$da) > 270 )) ) {
490 if( $split > 0 ) { // split in two
491 $angles[$idx] = array($a,$split);
492 $adjcolors[$idx] = $colors[$i %
$numcolors];
493 $adjexplode[$idx] = $explode;
494 $angles[++
$idx] = array($split,$ne);
495 $adjcolors[$idx] = $colors[$i %
$numcolors];
496 $adjexplode[$idx] = $explode;
499 $angles[$idx] = array($a,$ne);
500 $adjcolors[$idx] = $colors[$i %
$numcolors];
501 $adjexplode[$idx] = $explode;
506 // Slice may, depending on position, cross one or two
516 $angles[$idx] = array($a,$split);
517 $adjcolors[$idx] = $colors[$i %
$numcolors];
518 $adjexplode[$idx] = $explode;
519 //if( $a+$da > 360-$split ) {
520 // For slices larger than 270 degrees we might cross
521 // another boundary as well. This means that we must
522 // split the slice further. The comparison gets a little
523 // bit complicated since we must take into accound that
524 // a pie might have a startangle >0 and hence a slice might
525 // wrap around the 0 angle.
527 // a) Slice starts before 90 and hence gets a split=90, but
528 // we must also check if we need to split at 270
529 // b) Slice starts after 90 but before 270 and slices
530 // crosses 90 (after a wrap around of 0)
531 // c) If start is > 270 (hence the firstr split is at 90)
532 // and the slice is so large that it goes all the way
534 if( ($a < 90 && ($a+
$da > 270)) ||
535 ($a > 90 && $a<=270 && ($a+
$da>360+
90) ) ||
536 ($a > 270 && $this->NormAngle($a+
$da)>270) ) {
537 $angles[++
$idx] = array($split,360-$split);
538 $adjcolors[$idx] = $colors[$i %
$numcolors];
539 $adjexplode[$idx] = $explode;
540 $angles[++
$idx] = array(360-$split,$ne);
541 $adjcolors[$idx] = $colors[$i %
$numcolors];
542 $adjexplode[$idx] = $explode;
545 // Just a simple split to the previous decided
547 $angles[++
$idx] = array($split,$ne);
548 $adjcolors[$idx] = $colors[$i %
$numcolors];
549 $adjexplode[$idx] = $explode;
553 $a = $this->NormAngle($a);
556 // Total number of slices
559 for($i=0; $i<$n; ++
$i) {
560 list($dbgs,$dbge) = $angles[$i];
564 // Step 2. Find start index (first pie that starts in upper left quadrant)
566 $minval = $angles[0][0];
568 for( $i=0; $i<$n; ++
$i ) {
569 if( $angles[$i][0] < $minval ) {
570 $minval = $angles[$i][0];
576 while( $angles[$j][1] <= 90 ) {
582 JpGraphError
::RaiseL(14005);
583 //("Pie3D Internal error (#1). Trying to wrap twice when looking for start index");
590 // Step 3. Print slices in z-order
594 // First stroke all the slices between 90 and 270 (left half circle)
597 while( $angles[$j][0] < 270 && $aaoption !== 2 ) {
599 list($x,$y) = $adjexplode[$j];
601 $this->Pie3DSlice($img,$x,$y,$d,$h,$angles[$j][0],$angles[$j][1],
602 $z,$adjcolors[$j],$shadow);
604 $last = array($x,$y,$j);
609 JpGraphError
::RaiseL(14006);
610 //("Pie3D Internal Error: Z-Sorting algorithm for 3D Pies is not working properly (2). Trying to wrap twice while stroking.");
615 $slice_left = $n-$cnt;
620 // The stroke all slices from 90 to -90 (right half circle)
622 while( $cnt < $slice_left && $aaoption !== 2 ) {
624 list($x,$y) = $adjexplode[$j];
626 $this->Pie3DSlice($img,$x,$y,$d,$h,$angles[$j][0],$angles[$j][1],
627 $z,$adjcolors[$j],$shadow);
630 JpGraphError
::RaiseL(14006);
631 //("Pie3D Internal Error: Z-Sorting algorithm for 3D Pies is not working properly (2). Trying to wrap twice while stroking.");
637 // Now do a special thing. Stroke the last slice on the left
638 // halfcircle one more time. This is needed in the case where
639 // the slice close to 270 have been exploded. In that case the
640 // part of the slice close to the center of the pie might be
642 if( $aaoption !== 2 )
643 $this->Pie3DSlice($img,$last[0],$last[1],$d,$h,$angles[$last[2]][0],
644 $angles[$last[2]][1],$z,$adjcolors[$last[2]],$shadow);
647 if( $aaoption !== 1 ) {
648 // Now print possible labels and add csim
649 $img->SetFont($this->value
->ff
,$this->value
->fs
);
650 $margin = $img->GetFontHeight()/2 +
$this->value
->margin
;
651 for($i=0; $i < count($data); ++
$i ) {
652 $la = $labeldata[$i][0];
653 $x = $labeldata[$i][1] +
cos($la*M_PI
/180)*($d+
$margin);
654 $y = $labeldata[$i][2] - sin($la*M_PI
/180)*($h+
$margin);
655 if( $la > 180 && $la < 360 ) $y +
= $z;
656 if( $this->labeltype
== 0 ) {
658 $l = 100*$data[$i]/$sum;
662 elseif( $this->labeltype
== 1 ) {
666 $l = $this->adjusted_data
[$i];
668 if( isset($this->labels
[$i]) && is_string($this->labels
[$i]) )
669 $l=sprintf($this->labels
[$i],$l);
671 $this->StrokeLabels($l,$img,$labeldata[$i][0]*M_PI
/180,$x,$y,$z);
673 $this->AddSliceToCSIM($i,$labeldata[$i][1],$labeldata[$i][2],$h*2,$d*2,$z,
674 $originalangles[$i][0],$originalangles[$i][1]);
679 // Finally add potential lines in pie
682 if( $edgecolor=="" ||
$aaoption !== 0 ) return;
686 $a = $this->NormAngle($a);
691 $img->PushColor($edgecolor);
692 $img->SetLineWeight($edgeweight);
695 for($i=0; $i < count($data) && $fulledge; ++
$i ) {
696 if( empty($this->explode_radius
[$i]) )
697 $this->explode_radius
[$i]=0;
698 if( $this->explode_radius
[$i] > 0 ) {
704 for($i=0; $i < count($data); ++
$i, ++
$idx ) {
706 $da = $data[$i]/$sum * 2*M_PI
;
707 $this->StrokeFullSliceFrame($img,$xc,$yc,$a,$a+
$da,$d,$h,$z,$edgecolor,
708 $this->explode_radius
[$i],$fulledge);
714 function StrokeFullSliceFrame($img,$xc,$yc,$sa,$ea,$w,$h,$z,$edgecolor,$exploderadius,$fulledge) {
717 if( $exploderadius > 0 ) {
719 $xc +
= $exploderadius*cos($la);
720 $yc -= $exploderadius*sin($la) * ($h/$w) ;
724 $p = array($xc,$yc,$xc+
$w*cos($sa),$yc-$h*sin($sa));
726 for($a=$sa; $a < $ea; $a +
= $step ) {
727 $p[] = $xc +
$w*cos($a);
728 $p[] = $yc - $h*sin($a);
731 $p[] = $xc+
$w*cos($ea);
732 $p[] = $yc-$h*sin($ea);
736 $img->SetColor($edgecolor);
739 // Unfortunately we can't really draw the full edge around the whole of
740 // of the slice if any of the slices are exploded. The reason is that
741 // this algorithm is to simply. There are cases where the edges will
742 // "overwrite" other slices when they have been exploded.
743 // Doing the full, proper 3D hidden lines stiff is actually quite
744 // tricky. So for exploded pies we only draw the top edge. Not perfect
745 // but the "real" solution is much more complicated.
746 if( $fulledge && !( $sa > 0 && $sa < M_PI
&& $ea < M_PI
) ) {
748 if($sa < M_PI
&& $ea > M_PI
)
751 if($sa < 2*M_PI
&& (($ea >= 2*M_PI
) ||
($ea > 0 && $ea < $sa ) ) )
754 if( $sa >= M_PI
&& $ea <= 2*M_PI
) {
755 $p = array($xc +
$w*cos($sa),$yc - $h*sin($sa),
756 $xc +
$w*cos($sa),$z +
$yc - $h*sin($sa));
758 for($a=$sa+
$step; $a < $ea; $a +
= $step ) {
759 $p[] = $xc +
$w*cos($a);
760 $p[] = $z +
$yc - $h*sin($a);
762 $p[] = $xc +
$w*cos($ea);
763 $p[] = $z +
$yc - $h*sin($ea);
764 $p[] = $xc +
$w*cos($ea);
765 $p[] = $yc - $h*sin($ea);
766 $img->SetColor($edgecolor);
772 function Stroke($img,$aaoption=0) {
773 $n = count($this->data
);
775 // If user hasn't set the colors use the theme array
776 if( $this->setslicecolors
==null ) {
777 $colors = array_keys($img->rgb
->rgb_table
);
779 $idx_a=$this->themearr
[$this->theme
];
782 for($i=0; $i < $m; ++
$i)
783 $ca[$i] = $colors[$idx_a[$i]];
784 $ca = array_reverse(array_slice($ca,0,$n));
787 $ca = $this->setslicecolors
;
791 if( $this->posx
<= 1 && $this->posx
> 0 )
792 $xc = round($this->posx
*$img->width
);
796 if( $this->posy
<= 1 && $this->posy
> 0 )
797 $yc = round($this->posy
*$img->height
);
801 if( $this->radius
<= 1 ) {
802 $width = floor($this->radius
*min($img->width
,$img->height
));
803 // Make sure that the pie doesn't overflow the image border
804 // The 0.9 factor is simply an extra margin to leave some space
805 // between the pie an the border of the image.
806 $width = min($width,min($xc*0.9,($yc*90/$this->angle
-$width/4)*0.9));
809 $width = $this->radius
* ($aaoption === 1 ?
2 : 1 ) ;
812 // Add a sanity check for width
814 JpGraphError
::RaiseL(14007);//("Width for 3D Pie is 0. Specify a size > 0");
817 // Establish a thickness. By default the thickness is a fifth of the
818 // pie slice width (=pie radius) but since the perspective depends
819 // on the inclination angle we use some heuristics to make the edge
820 // slightly thicker the less the angle.
822 // Has user specified an absolute thickness? In that case use
825 if( $this->iThickness
) {
826 $thick = $this->iThickness
;
827 $thick *= ($aaoption === 1 ?
2 : 1 );
832 if( $a <= 30 ) $thick *= 1.6;
833 elseif( $a <= 40 ) $thick *= 1.4;
834 elseif( $a <= 50 ) $thick *= 1.2;
835 elseif( $a <= 60 ) $thick *= 1.0;
836 elseif( $a <= 70 ) $thick *= 0.8;
837 elseif( $a <= 80 ) $thick *= 0.7;
840 $thick = floor($thick);
842 if( $this->explode_all
)
843 for($i=0; $i < $n; ++
$i)
844 $this->explode_radius
[$i]=$this->explode_r
;
846 $this->Pie3D($aaoption,$img,$this->data
, $ca, $xc, $yc, $width, $this->angle
,
847 $thick, 0.65, $this->startangle
, $this->edgecolor
, $this->edgeweight
);
849 // Adjust title position
850 if( $aaoption != 1 ) {
851 $this->title
->Pos($xc,$yc-$this->title
->GetFontHeight($img)-$width/2-$this->title
->margin
, "center","bottom");
852 $this->title
->Stroke($img);
859 // Position the labels of each slice
860 function StrokeLabels($label,$img,$a,$xp,$yp,$z) {
861 $this->value
->halign
="left";
862 $this->value
->valign
="top";
864 // Position the axis title.
865 // dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
866 // that intersects with the extension of the corresponding axis. The code looks a little
867 // bit messy but this is really the only way of having a reasonable position of the
869 $img->SetFont($this->value
->ff
,$this->value
->fs
,$this->value
->fsize
);
870 $h=$img->GetTextHeight($label);
871 // For numeric values the format of the display value
872 // must be taken into account
873 if( is_numeric($label) ) {
875 $w=$img->GetTextWidth(sprintf($this->value
->format
,$label));
877 $w=$img->GetTextWidth(sprintf($this->value
->negformat
,$label));
880 $w=$img->GetTextWidth($label);
881 while( $a > 2*M_PI
) $a -= 2*M_PI
;
882 if( $a>=7*M_PI
/4 ||
$a <= M_PI
/4 ) $dx=0;
883 if( $a>=M_PI
/4 && $a <= 3*M_PI
/4 ) $dx=($a-M_PI
/4)*2/M_PI
;
884 if( $a>=3*M_PI
/4 && $a <= 5*M_PI
/4 ) $dx=1;
885 if( $a>=5*M_PI
/4 && $a <= 7*M_PI
/4 ) $dx=(1-($a-M_PI
*5/4)*2/M_PI
);
887 if( $a>=7*M_PI
/4 ) $dy=(($a-M_PI
)-3*M_PI
/4)*2/M_PI
;
888 if( $a<=M_PI
/4 ) $dy=(1-$a*2/M_PI
);
889 if( $a>=M_PI
/4 && $a <= 3*M_PI
/4 ) $dy=1;
890 if( $a>=3*M_PI
/4 && $a <= 5*M_PI
/4 ) $dy=(1-($a-3*M_PI
/4)*2/M_PI
);
891 if( $a>=5*M_PI
/4 && $a <= 7*M_PI
/4 ) $dy=0;
893 $x = round($xp-$dx*$w);
894 $y = round($yp-$dy*$h);
897 // Mark anchor point for debugging
899 $img->SetColor('red');
900 $img->Line($xp-10,$yp,$xp+10,$yp);
901 $img->Line($xp,$yp-10,$xp,$yp+10);
903 $oldmargin = $this->value
->margin
;
904 $this->value
->margin
=0;
905 $this->value
->Stroke($img,$label,$x,$y);
906 $this->value
->margin
=$oldmargin;