Source for file jpgraph_pie3d.php

Documentation is available at jpgraph_pie3d.php

  1. <?php
  2. /**
  3. * File: JPGRAPH_PIE3D.PHP
  4. * Description: 3D Pie plot extension for JpGraph
  5. * Created: 2001-03-24
  6. * Author: Johan Persson (johanp@aditus.nu)
  7. * Ver: $Id: jpgraph_pie3d.php,v 1.26 2002/05/09 14:43:48 aditus Exp $
  8. *
  9. * License: This code is released under QPL
  10. * Copyright (C) 2001,2002 Johan Persson
  11. * @package JPGraph
  12. ***/
  13.  
  14.  
  15. // Debug print
  16. function dbgp($str) {
  17. // echo $str;
  18. }
  19.  
  20. /**
  21. * CLASS PiePlot3D
  22. * Description: Plots a 3D pie with a specified projection
  23. * angle between 20 and 70 degrees.
  24. **/
  25.  
  26. class PiePlot3D extends PiePlot {
  27. var $labelhintcolor="red",$showlabelhint=true,$labelmargin=0.30;
  28. var $angle=35;
  29. var $edgecolor="", $edgeweight=1;
  30. //---------------
  31. // CONSTRUCTOR
  32. function PiePlot3d(&$data) {
  33. $this->radius = 0.5;
  34. $this->data = $data;
  35. $this->title = new Text("");
  36. $this->title->SetFont(FF_FONT1,FS_BOLD);
  37. $this->value = new DisplayValue();
  38. }
  39.  
  40. //---------------
  41. // PUBLIC METHODS
  42. // Should the slices be separated by a line? If color is specified as "" no line
  43. // will be used to separate pie slices.
  44. function SetEdge($aColor,$aWeight=1) {
  45. $this->edgecolor = $aColor;
  46. $this->edgeweight = $aWeight;
  47. }
  48.  
  49. // Specify projection angle for 3D in degrees
  50. // Must be between 20 and 70 degrees
  51. function SetAngle($a) {
  52. if( $a<5 || $a>90 )
  53. JpGraphError::Raise("PiePlot3D::SetAngle() 3D Pie projection angle must be between 5 and 85 degrees.");
  54. else
  55. $this->angle = $a;
  56. }
  57.  
  58. function AddSliceToCSIM($i,$xc,$yc,$height,$width,$thick,$sa,$ea) { //Slice number, ellipse centre (x,y), height, width, start angle, end angle
  59.  
  60. $sa *= M_PI/180;
  61. $ea *= M_PI/180;
  62.  
  63. //add coordinates of the centre to the map
  64. $coords = "$xc, $yc";
  65.  
  66. //add coordinates of the first point on the arc to the map
  67. $xp = floor($width*cos($sa)/2+$xc);
  68. $yp = floor($yc-$height*sin($sa)/2);
  69. $coords.= ", $xp, $yp";
  70.  
  71. //If on the front half, add the thickness offset
  72. if ($sa >= M_PI && $sa <= 2*M_PI*1.01) {
  73. $yp = $yp+$thick;
  74. $coords.= ", $xp, $yp";
  75. }
  76. //add coordinates every 0.2 radians
  77. $a=$sa+0.2;
  78. while ($a<$ea) {
  79. $xp = floor($width*cos($a)/2+$xc);
  80. if ($a >= M_PI && $a <= 2*M_PI*1.01) {
  81. $yp = floor($yc-($height*sin($a)/2)+$thick);
  82. } else {
  83. $yp = floor($yc-$height*sin($a)/2);
  84. }
  85. $coords.= ", $xp, $yp";
  86. $a += 0.2;
  87. }
  88. //Add the last point on the arc
  89. $xp = floor($width*cos($ea)/2+$xc);
  90. $yp = floor($yc-$height*sin($ea)/2);
  91.  
  92.  
  93. if ($ea >= M_PI && $ea <= 2*M_PI*1.01) {
  94. $coords.= ", $xp, ".floor($yp+$thick);
  95. }
  96. $coords.= ", $xp, $yp";
  97. if( !empty($this->csimalts[$i]) ) {
  98. $tmp=sprintf($this->csimalts[$i],$this->data[$i]);
  99. $alt="alt=\"$tmp\" title=\"$tmp\"";
  100. }
  101. if( !empty($this->csimtargets[$i]) )
  102. $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->csimtargets[$i]."\" $alt>\r\n";
  103. }
  104.  
  105. // Distance from the pie to the labels
  106. function SetLabelMargin($m) {
  107. assert($m>0 && $m<1);
  108. $this->labelmargin=$m;
  109. }
  110. // Show a thin line from the pie to the label for a specific slice
  111. function ShowLabelHint($f=true) {
  112. $this->showlabelhint=$f;
  113. }
  114. // Set color of hint line to label for each slice
  115. function SetLabelHintColor($c) {
  116. $this->labelhintcolor=$c;
  117. }
  118.  
  119. // Normalize Angle between 0-360
  120. function NormAngle($a) {
  121. // Normalize anle to 0 to 2M_PI
  122. //
  123. if( $a > 0 ) {
  124. while($a > 360) $a -= 360;
  125. }
  126. else {
  127. while($a < 0) $a += 360;
  128. }
  129. if( $a < 0 )
  130. $a = 360 + $a;
  131.  
  132. if( $a == 360 ) $a=0;
  133. return $a;
  134. }
  135.  
  136.  
  137. // Draw one 3D pie slice at position ($xc,$yc) with height $z
  138. function Pie3DSlice($img,$xc,$yc,$w,$h,$sa,$ea,$z,$fillcolor,
  139. $shadow=0.65,$edgecolor="",$arccolor="") {
  140.  
  141. dbgp( "s=$sa, e=$ea<br>\n" );
  142.  
  143. $img->SetColor($fillcolor.":".$shadow);
  144. for( $i=0; $i<$z; ++$i ) {
  145. $img->CakeSlice($xc,$yc+$z-$i,$w,$h,360-$ea,360-$sa,$fillcolor.":".$shadow,"",3500);
  146. }
  147. if( $edgecolor == "" )
  148. $img->SetColor($fillcolor);
  149. else
  150. $img->SetColor($edgecolor);
  151. $img->CakeSlice($xc,$yc+$z-$i,$w,$h,360-$ea,360-$sa,$fillcolor,$edgecolor ,2500);
  152.  
  153. }
  154. // Draw a 3D Pie
  155. function Pie3D($img,$data,$colors,$xc,$yc,$d,$angle,$z,
  156. $shadow=0.65,$startangle=0,$edgecolor="",$edgeweight=2) {
  157.  
  158. //---------------------------------------------------------------------------
  159. // As usual the algorithm get more complicated than I originally
  160. // envisioned. I believe that this is as simple as it is possible
  161. // to do it with the features I want. It's a good exercise to start
  162. // thinking on how to do this to convince your self that all this
  163. // is really needed for the general case.
  164. //
  165. // The algorithm two draw 3D pies without "real 3D" is done in
  166. // two steps.
  167. // First imagine the pie cut in half through a thought line between
  168. // 12'a clock and 6'a clock. It now easy to imagine that we can plot
  169. // the individual slices for each half by starting with the topmost
  170. // pie slice and continue down to 6'a clock.
  171. //
  172. // In the algortithm this is done in three principal steps
  173. // Step 1. Do the knife cut to ensure by splitting slices that extends
  174. // over the cut line. This is done by splitting the original slices into
  175. // upto 3 subslices.
  176. // Step 2. Find the top slice for each half
  177. // Step 3. Draw the slices from top to bottom
  178. //
  179. // The thing that slightly complicates this scheme with all the
  180. // angle comparisons below is that we can have an arbitrary start
  181. // angle so we must take into account the different equivalence classes.
  182. // For the same reason we must walk through the angle array in a
  183. // modulo fashion.
  184. //
  185. // Limitations of algorithm:
  186. // * A small exploded slice which crosses the 270 degree point
  187. // will get slightly nagged close to the center due to the fact that
  188. // we print the slices in Z-order and that the slice left part
  189. // get printed first and might get slightly nagged by a larger
  190. // slice on the right side just before the right part of the small
  191. // slice. Not a major problem though.
  192. //---------------------------------------------------------------------------
  193.  
  194. // Determine the height of the ellippse which gives an
  195. // indication of the inclination angle
  196. $h = ($angle/90.0)*$d;
  197. $sum = 0;
  198. for($i=0; $i<count($data); ++$i ) {
  199. $sum += $data[$i];
  200. }
  201. // Special optimization
  202. if( $sum==0 ) return;
  203.  
  204. // Setup the start
  205. $accsum = 0;
  206. $a = $startangle;
  207. $a = $this->NormAngle($a);
  208.  
  209. //
  210. // Step 1 . Split all slices that crosses 90 or 270
  211. //
  212. $idx=0;
  213. $adjexplode=array();
  214. for($i=0; $i<count($data); ++$i, ++$idx ) {
  215. $da = $data[$i]/$sum * 360;
  216.  
  217. if( empty($this->explode_radius[$i]) )
  218. $this->explode_radius[$i]=0;
  219.  
  220. $la = $a + $da/2;
  221. $explode = array( $xc + $this->explode_radius[$i]*cos($la*M_PI/180),
  222. $yc - $this->explode_radius[$i]*sin($la*M_PI/180)*($h/$d) );
  223. $adjexplode[$idx] = $explode;
  224. $labeldata[$i] = array($la,$explode[0],$explode[1]);
  225. $originalangles[$i] = array($a,$a+$da);
  226.  
  227. $ne = $this->NormAngle($a+$da);
  228. if( $da <= 180 ) {
  229. // If the slice size is <= 90 it can at maximum cut across
  230. // one boundary (either 90 or 270) where it needs to be split
  231. dbgp( "da<=180, a=$a, ne=$ne, da=$da<br>" );
  232. $split=-1; // no split
  233. if( ($da<=90 && ($a <= 90 && $ne > 90)) ||
  234. (($da <= 180 && $da >90) && (($a < 90 || $a >= 270) && $ne > 90)) ) {
  235. dbgp( "&nbsp; a<=90 && ne>=90, a=$a, ne=$ne, da=$da<br>" );
  236. $split = 90;
  237. }
  238. elseif( ($da<=90 && ($a <= 270 && $ne > 270)) ||
  239. (($da<=180 && $da>90) && ($a >= 90 && $a < 270 && ($a+$da) > 270 )) ) {
  240. dbgp( "&nbsp; a<=270 && ne>270, a=$a, ne=$ne, da=$da<br>" );
  241. $split = 270;
  242. }
  243. if( $split > 0 ) { // split in two
  244. $angles[$idx] = array($a,$split);
  245. $adjcolors[$idx] = $colors[$i];
  246. $adjexplode[$idx] = $explode;
  247. $angles[++$idx] = array($split,$ne);
  248. $adjcolors[$idx] = $colors[$i];
  249. $adjexplode[$idx] = $explode;
  250. }
  251. else { // no split
  252. $angles[$idx] = array($a,$ne);
  253. $adjcolors[$idx] = $colors[$i];
  254. $adjexplode[$idx] = $explode;
  255. }
  256. }
  257. else {
  258. // da>180
  259. // Slice may, depending on position, cross one or two
  260. // bonudaries
  261. dbgp( "da<=180, a=$a, ne=$ne, da=$da, " );
  262.  
  263. if( $a < 90 )
  264. $split = 90;
  265. elseif( $a <= 270 )
  266. $split = 270;
  267. else
  268. $split = 90;
  269.  
  270. dbgp("split=$split<br>");
  271.  
  272. $angles[$idx] = array($a,$split);
  273. $adjcolors[$idx] = $colors[$i];
  274. $adjexplode[$idx] = $explode;
  275. //if( $a+$da > 360-$split ) {
  276. // For slices larger than 270 degrees we might cross
  277. // another boundary as well. This means that we must
  278. // split the slice further. The comparison gets a little
  279. // bit complicated since we must take into accound that
  280. // a pie might have a startangle >0 and hence a slice might
  281. // wrap around the 0 angle.
  282. // Three cases:
  283. // a) Slice starts before 90 and hence gets a split=90, but
  284. // we must also check if we need to split at 270
  285. // b) Slice starts after 90 but before 270 and slices
  286. // crosses 90 (after a wrap around of 0)
  287. // c) If start is > 270 (hence the firstr split is at 90)
  288. // and the slice is so large that it goes all the way
  289. // around 270.
  290. if( ($a < 90 && ($a+$da > 270)) ||
  291. ($a > 90 && $a<=270 && ($a+$da>360+90) ) ||
  292. ($a > 270 && $this->NormAngle($a+$da)>270) ) {
  293. dbgp("&nbsp; a+da > 360-$split, a=$a, da=$da<br>");
  294. $angles[++$idx] = array($split,360-$split);
  295. $adjcolors[$idx] = $colors[$i];
  296. $adjexplode[$idx] = $explode;
  297. $angles[++$idx] = array(360-$split,$ne);
  298. $adjcolors[$idx] = $colors[$i];
  299. $adjexplode[$idx] = $explode;
  300. }
  301. else {
  302. // Just a simple split to the previous decided
  303. // angle.
  304. $angles[++$idx] = array($split,$ne);
  305. $adjcolors[$idx] = $colors[$i];
  306. $adjexplode[$idx] = $explode;
  307. }
  308. }
  309. $a += $da;
  310. $a = $this->NormAngle($a);
  311. }
  312.  
  313. // Total number of slices
  314. $n = count($angles);
  315.  
  316. dbgp("<br>Splitted pie:<br>");
  317. for($i=0; $i<$n; ++$i) {
  318. list($dbgs,$dbge) = $angles[$i];
  319. dbgp("&nbsp;#$i: s=$dbgs, e=$dbge<br>");
  320. }
  321.  
  322. //
  323. // Step 2. Find start index (first pie that starts in upper left quadrant)
  324. //
  325. $minval = $angles[0][0];
  326. $min = 0;
  327. for( $i=0; $i<$n; ++$i ) {
  328. if( $angles[$i][0] < $minval ) {
  329. $minval = $angles[$i][0];
  330. $min = $i;
  331. }
  332. }
  333. $j = $min;
  334. $cnt = 0;
  335. while( $angles[$j][1] <= 90 ) {
  336. $j++;
  337. if( $j>=$n) {
  338. $j=0;
  339. }
  340. if( $cnt > $n ) {
  341. JpGraphError::Raise("Pie3D Internal error (#1). Trying to wrap twice when looking for start index");
  342. }
  343. ++$cnt;
  344. }
  345. $start = $j;
  346. dbgp( "Start index: $start<br>" );
  347.  
  348. //
  349. // Step 3. Print slices in z-order
  350. //
  351. $cnt = 0;
  352. // First stroke all the slices between 90 and 270 (left half circle)
  353. // counterclockwise
  354. while( $angles[$j][0] < 270 ) {
  355.  
  356. list($x,$y) = $adjexplode[$j];
  357.  
  358. $this->Pie3DSlice($img,$x,$y,$d,$h,$angles[$j][0],$angles[$j][1],$z,$adjcolors[$j],
  359. $shadow);
  360. $last = array($x,$y,$j);
  361.  
  362. $j++;
  363. if( $j >= $n ) $j=0;
  364. if( $cnt > $n ) {
  365. JpGraphError::Raise("Pie3D Internal Error: Z-Sorting algorithm for 3D Pies is not working properly (2). Trying to wrap twice while stroking.");
  366. }
  367. ++$cnt;
  368. }
  369. $slice_left = $n-$cnt;
  370. $j=$start-1;
  371. if($j<0) $j=$n-1;
  372. $cnt = 0;
  373. // The stroke all slices from 90 to -90 (right half circle)
  374. // clockwise
  375. while( $cnt < $slice_left ) {
  376.  
  377. list($x,$y) = $adjexplode[$j];
  378.  
  379. $this->Pie3DSlice($img,$x,$y,$d,$h,$angles[$j][0],$angles[$j][1],$z,$adjcolors[$j],
  380. $shadow);
  381. $j--;
  382. if( $cnt > $n ) {
  383. JpGraphError::Raise("Pie3D Internal Error: Z-Sorting algorithm for 3D Pies is not working properly (2). Trying to wrap twice while stroking.");
  384. }
  385. if($j<0) $j=$n-1;
  386. $cnt++;
  387. }
  388. // Now do a special thing. Stroke the last slice on the left
  389. // halfcircle one more time. This is needed in the case where
  390. // the slice close to 270 have been exploded. In that case the
  391. // part of the slice close to the center of the pie might be
  392. // slightly nagged.
  393. $this->Pie3DSlice($img,$last[0],$last[1],$d,$h,$angles[$last[2]][0],$angles[$last[2]][1],$z,$adjcolors[$last[2]],$shadow);
  394.  
  395.  
  396. // Now print possible labels and add csim
  397. $img->SetFont($this->value->ff,$this->value->fs);
  398. $margin = $img->GetFontHeight()/2;
  399. for($i=0; $i < count($data); ++$i ) {
  400. $la = $labeldata[$i][0];
  401. $x = $labeldata[$i][1] + cos($la*M_PI/180)*($d+$margin);
  402. $y = $labeldata[$i][2] - sin($la*M_PI/180)*($h+$margin);
  403. if( $la > 180 && $la < 360 ) $y += $z;
  404. if( $this->labeltype == 0 )
  405. if( $sum > 0 )
  406. $l = 100*$data[$i]/$sum;
  407. else
  408. $l = 0;
  409. else
  410. $l = $data[$i];
  411.  
  412. $this->StrokeLabels($l,$img,$labeldata[$i][0]*M_PI/180,$x,$y);
  413. $this->AddSliceToCSIM($i,$labeldata[$i][1],$labeldata[$i][2],$h*2,$d*2,$z,
  414. $originalangles[$i][0],$originalangles[$i][1]);
  415. }
  416.  
  417. //
  418. // Finally add potential lines in pie
  419. //
  420.  
  421. if( $edgecolor=="" ) return;
  422.  
  423. $accsum = 0;
  424. $a = $startangle;
  425. $a = $this->NormAngle($a);
  426.  
  427. $idx=0;
  428. $img->PushColor($edgecolor);
  429.  
  430. $img->SetLineWeight($edgeweight);
  431. for($i=0; $i < count($data); ++$i, ++$idx ) {
  432.  
  433. $x = $xc + floor(cos($a*M_PI/180) * $d);
  434. $y = $yc - floor(sin($a*M_PI/180) * $h);
  435. $img->Line($xc,$yc,$x,$y);
  436. $da = $data[$i]/$sum * 360;
  437.  
  438. if( empty($this->explode_radius[$i]) )
  439. $this->explode_radius[$i]=0;
  440.  
  441. $la = $a + $da/2;
  442. $explode = array( $xc + $this->explode_radius[$i]*cos($la*M_PI/180),
  443. $yc - $this->explode_radius[$i]*sin($la*M_PI/180)*($h/$d) );
  444.  
  445. $a += $da;
  446. }
  447.  
  448. $img->SetLineWeight(2);
  449.  
  450. // Right sideline
  451. $img->Line($xc+$d,$yc,$xc+$d,$yc+$z);
  452.  
  453. // Left sideline
  454. $img->Line($xc-$d+1,$yc,$xc-$d+1,$yc+$z);
  455.  
  456. // Major full ellipse
  457. $img->Ellipse($xc,$yc+1,$d*2.01,$h*2.01);
  458. $img->Ellipse($xc+1,$yc,$d*2.01,$h*2.01);
  459. $img->Ellipse($xc,$yc,$d*2.01,$h*2.01);
  460.  
  461. // Lower half ellipse
  462. $img->Arc($xc,$yc+$z,$d*2,$h*2,0,180);
  463. $img->Arc($xc,$yc+$z+1,$d*2,$h*2,0,180);
  464.  
  465. $img->PopColor();
  466. $img->SetLineWeight(1);
  467. }
  468.  
  469.  
  470. function Stroke(&$img) {
  471.  
  472. $colors = array_keys($img->rgb->rgb_table);
  473. sort($colors);
  474. if( $this->setslicecolors==null ) {
  475. $idx_a=$this->themearr[$this->theme];
  476. $numcolors = count($idx_a);
  477. $ca = array();
  478. for($i=0; $i<$numcolors; ++$i)
  479. $ca[$i] = $colors[$idx_a[$i]];
  480. }
  481. else {
  482. $ca = $this->setslicecolors;
  483. }
  484.  
  485. $numcolors=count($ca);
  486.  
  487. $xc = $this->posx*$img->width;
  488. $yc = $this->posy*$img->height;
  489. if( $this->radius < 1 ) {
  490. $width = floor($this->radius*min($img->width,$img->height));
  491. // Make sure that the pie doesn't overflow the image border
  492. // The 0.9 factor is simply an extra margin to leave some space
  493. // between the pie an the border of the image.
  494. $width = min($width,min($xc*0.9,($yc*90/$this->angle-$width/4)*0.9));
  495. }
  496. else
  497. $width = $this->radius ;
  498.  
  499. // Establish a thickness. By default the thickness is a fifth of the
  500. // pie slice width (=pie radius) but since the perspective depends
  501. // on the inclination angle we use some heuristics to make the edge
  502. // slightly thicker the less the angle.
  503. $thick = $width/5;
  504. $a = $this->angle;
  505. if( $a <= 30 ) $thick *= 1.6;
  506. elseif( $a <= 40 ) $thick *= 1.4;
  507. elseif( $a <= 50 ) $thick *= 1.2;
  508. elseif( $a <= 60 ) $thick *= 1.0;
  509. elseif( $a <= 70 ) $thick *= 0.8;
  510. elseif( $a <= 80 ) $thick *= 0.7;
  511. else $thick *= 0.6;
  512.  
  513. if( $this->explode_all )
  514. for($i=0;$i<count($this->data);++$i)
  515. $this->explode_radius[$i]=$this->explode_r;
  516.  
  517. $this->Pie3D($img,$this->data, $ca, $xc, $yc, $width, $this->angle,
  518. $thick, 0.65, $this->startangle, $this->edgecolor, $this->edgeweight);
  519.  
  520. // Adjust title position
  521. $this->title->Pos($xc,$yc-$img->GetFontHeight()-$this->radius,"center","bottom");
  522. $this->title->Stroke($img);
  523. }
  524.  
  525. //---------------
  526. // PRIVATE METHODS
  527.  
  528. // Position the labels of each slice
  529. function StrokeLabels($label,$img,$a,$xp,$yp) {
  530. $this->value->halign="left";
  531. $this->value->valign="top";
  532. $this->value->margin=0;
  533.  
  534. // Position the axis title.
  535. // dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
  536. // that intersects with the extension of the corresponding axis. The code looks a little
  537. // bit messy but this is really the only way of having a reasonable position of the
  538. // axis titles.
  539. $img->SetFont($this->value->ff,$this->value->fs,$this->value->fsize);
  540. $h=$img->GetTextHeight($label);
  541. $w=$img->GetTextWidth(sprintf($this->value->format,$label));
  542. while( $a > 2*M_PI ) $a -= 2*M_PI;
  543. if( $a>=7*M_PI/4 || $a <= M_PI/4 ) $dx=0;
  544. if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dx=($a-M_PI/4)*2/M_PI;
  545. if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dx=1;
  546. if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dx=(1-($a-M_PI*5/4)*2/M_PI);
  547. if( $a>=7*M_PI/4 ) $dy=(($a-M_PI)-3*M_PI/4)*2/M_PI;
  548. if( $a<=M_PI/4 ) $dy=(1-$a*2/M_PI);
  549. if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dy=1;
  550. if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dy=(1-($a-3*M_PI/4)*2/M_PI);
  551. if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dy=0;
  552. $this->value->Stroke($img,$label,$xp-$dx*$w,$yp-$dy*$h);
  553. }
  554. } // Class
  555. /* EOF */
  556. ?>

Documentation generated on Sun, 13 Mar 2005 14:25:23 +0100 by phpDocumentor 1.3.0RC3