PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/application/plugins/reports/library/jpgraph/src/jpgraph_pie3d.php

https://github.com/fb83/Project-Pier
PHP | 912 lines | 594 code | 145 blank | 173 comment | 157 complexity | e5c69194338494f847a81f16c6d0d0f5 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  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.2.10.1 2006/02/28 19:46:57 gregorerhardt Exp $
  8. //
  9. // Copyright (c) Aditus Consulting. All rights reserved.
  10. //========================================================================
  11. */
  12. //===================================================
  13. // CLASS PiePlot3D
  14. // Description: Plots a 3D pie with a specified projection
  15. // angle between 20 and 70 degrees.
  16. //===================================================
  17. class PiePlot3D extends PiePlot {
  18. var $labelhintcolor="red",$showlabelhint=true;
  19. var $angle=50;
  20. var $edgecolor="", $edgeweight=1;
  21. var $iThickness=false;
  22. //---------------
  23. // CONSTRUCTOR
  24. function PiePlot3d(&$data) {
  25. $this->radius = 0.5;
  26. $this->data = $data;
  27. $this->title = new Text("");
  28. $this->title->SetFont(FF_FONT1,FS_BOLD);
  29. $this->value = new DisplayValue();
  30. $this->value->Show();
  31. $this->value->SetFormat('%.0f%%');
  32. }
  33. //---------------
  34. // PUBLIC METHODS
  35. // Set label arrays
  36. function SetLegends($aLegend) {
  37. $this->legends = array_reverse(array_slice($aLegend,0,count($this->data)));
  38. }
  39. function SetSliceColors($aColors) {
  40. $this->setslicecolors = $aColors;
  41. }
  42. function Legend(&$aGraph) {
  43. parent::Legend($aGraph);
  44. $aGraph->legend->txtcol = array_reverse($aGraph->legend->txtcol);
  45. }
  46. function SetCSIMTargets($targets,$alts=null) {
  47. $this->csimtargets = $targets;
  48. $this->csimalts = $alts;
  49. }
  50. // Should the slices be separated by a line? If color is specified as "" no line
  51. // will be used to separate pie slices.
  52. function SetEdge($aColor='black',$aWeight=1) {
  53. $this->edgecolor = $aColor;
  54. $this->edgeweight = $aWeight;
  55. }
  56. // Dummy function to make Pie3D behave in a similair way to 2D
  57. function ShowBorder($exterior=true,$interior=true) {
  58. JpGraphError::RaiseL(14001);
  59. //('Pie3D::ShowBorder() . Deprecated function. Use Pie3D::SetEdge() to control the edges around slices.');
  60. }
  61. // Specify projection angle for 3D in degrees
  62. // Must be between 20 and 70 degrees
  63. function SetAngle($a) {
  64. if( $a<5 || $a>90 )
  65. JpGraphError::RaiseL(14002);
  66. //("PiePlot3D::SetAngle() 3D Pie projection angle must be between 5 and 85 degrees.");
  67. else
  68. $this->angle = $a;
  69. }
  70. function AddSliceToCSIM($i,$xc,$yc,$height,$width,$thick,$sa,$ea) { //Slice number, ellipse centre (x,y), height, width, start angle, end angle
  71. $sa *= M_PI/180;
  72. $ea *= M_PI/180;
  73. //add coordinates of the centre to the map
  74. $coords = "$xc, $yc";
  75. //add coordinates of the first point on the arc to the map
  76. $xp = floor($width*cos($sa)/2+$xc);
  77. $yp = floor($yc-$height*sin($sa)/2);
  78. $coords.= ", $xp, $yp";
  79. //If on the front half, add the thickness offset
  80. if ($sa >= M_PI && $sa <= 2*M_PI*1.01) {
  81. $yp = floor($yp+$thick);
  82. $coords.= ", $xp, $yp";
  83. }
  84. //add coordinates every 0.2 radians
  85. $a=$sa+0.2;
  86. while ($a<$ea) {
  87. $xp = floor($width*cos($a)/2+$xc);
  88. if ($a >= M_PI && $a <= 2*M_PI*1.01) {
  89. $yp = floor($yc-($height*sin($a)/2)+$thick);
  90. } else {
  91. $yp = floor($yc-$height*sin($a)/2);
  92. }
  93. $coords.= ", $xp, $yp";
  94. $a += 0.2;
  95. }
  96. //Add the last point on the arc
  97. $xp = floor($width*cos($ea)/2+$xc);
  98. $yp = floor($yc-$height*sin($ea)/2);
  99. if ($ea >= M_PI && $ea <= 2*M_PI*1.01) {
  100. $coords.= ", $xp, ".floor($yp+$thick);
  101. }
  102. $coords.= ", $xp, $yp";
  103. $alt='';
  104. if( !empty($this->csimalts[$i]) ) {
  105. $tmp=sprintf($this->csimalts[$i],$this->data[$i]);
  106. $alt="alt=\"$tmp\" title=\"$tmp\"";
  107. }
  108. if( !empty($this->csimtargets[$i]) )
  109. $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->csimtargets[$i]."\" $alt />\n";
  110. }
  111. function SetLabels($aLabels,$aLblPosAdj="auto") {
  112. $this->labels = $aLabels;
  113. $this->ilabelposadj=$aLblPosAdj;
  114. }
  115. // Distance from the pie to the labels
  116. function SetLabelMargin($m) {
  117. $this->value->SetMargin($m);
  118. }
  119. // Show a thin line from the pie to the label for a specific slice
  120. function ShowLabelHint($f=true) {
  121. $this->showlabelhint=$f;
  122. }
  123. // Set color of hint line to label for each slice
  124. function SetLabelHintColor($c) {
  125. $this->labelhintcolor=$c;
  126. }
  127. function SetHeight($aHeight) {
  128. $this->iThickness = $aHeight;
  129. }
  130. // Normalize Angle between 0-360
  131. function NormAngle($a) {
  132. // Normalize anle to 0 to 2M_PI
  133. //
  134. if( $a > 0 ) {
  135. while($a > 360) $a -= 360;
  136. }
  137. else {
  138. while($a < 0) $a += 360;
  139. }
  140. if( $a < 0 )
  141. $a = 360 + $a;
  142. if( $a == 360 ) $a=0;
  143. return $a;
  144. }
  145. // Draw one 3D pie slice at position ($xc,$yc) with height $z
  146. function Pie3DSlice($img,$xc,$yc,$w,$h,$sa,$ea,$z,$fillcolor,$shadow=0.65) {
  147. // Due to the way the 3D Pie algorithm works we are
  148. // guaranteed that any slice we get into this method
  149. // belongs to either the left or right side of the
  150. // pie ellipse. Hence, no slice will cross 90 or 270
  151. // point.
  152. if( ($sa < 90 && $ea > 90) || ( ($sa > 90 && $sa < 270) && $ea > 270) ) {
  153. JpGraphError::RaiseL(14003);//('Internal assertion failed. Pie3D::Pie3DSlice');
  154. exit(1);
  155. }
  156. $p[] = array();
  157. // Setup pre-calculated values
  158. $rsa = $sa/180*M_PI; // to Rad
  159. $rea = $ea/180*M_PI; // to Rad
  160. $sinsa = sin($rsa);
  161. $cossa = cos($rsa);
  162. $sinea = sin($rea);
  163. $cosea = cos($rea);
  164. // p[] is the points for the overall slice and
  165. // pt[] is the points for the top pie
  166. // Angular step when approximating the arc with a polygon train.
  167. $step = 0.05;
  168. if( $sa >= 270 ) {
  169. if( $ea > 360 || ($ea > 0 && $ea <= 90) ) {
  170. if( $ea > 0 && $ea <= 90 ) {
  171. // Adjust angle to simplify conditions in loops
  172. $rea += 2*M_PI;
  173. }
  174. $p = array($xc,$yc,$xc,$yc+$z,
  175. $xc+$w*$cossa,$z+$yc-$h*$sinsa);
  176. $pt = array($xc,$yc,$xc+$w*$cossa,$yc-$h*$sinsa);
  177. for( $a=$rsa; $a < 2*M_PI; $a += $step ) {
  178. $tca = cos($a);
  179. $tsa = sin($a);
  180. $p[] = $xc+$w*$tca;
  181. $p[] = $z+$yc-$h*$tsa;
  182. $pt[] = $xc+$w*$tca;
  183. $pt[] = $yc-$h*$tsa;
  184. }
  185. $pt[] = $xc+$w;
  186. $pt[] = $yc;
  187. $p[] = $xc+$w;
  188. $p[] = $z+$yc;
  189. $p[] = $xc+$w;
  190. $p[] = $yc;
  191. $p[] = $xc;
  192. $p[] = $yc;
  193. for( $a=2*M_PI+$step; $a < $rea; $a += $step ) {
  194. $pt[] = $xc + $w*cos($a);
  195. $pt[] = $yc - $h*sin($a);
  196. }
  197. $pt[] = $xc+$w*$cosea;
  198. $pt[] = $yc-$h*$sinea;
  199. $pt[] = $xc;
  200. $pt[] = $yc;
  201. }
  202. else {
  203. $p = array($xc,$yc,$xc,$yc+$z,
  204. $xc+$w*$cossa,$z+$yc-$h*$sinsa);
  205. $pt = array($xc,$yc,$xc+$w*$cossa,$yc-$h*$sinsa);
  206. $rea = $rea == 0.0 ? 2*M_PI : $rea;
  207. for( $a=$rsa; $a < $rea; $a += $step ) {
  208. $tca = cos($a);
  209. $tsa = sin($a);
  210. $p[] = $xc+$w*$tca;
  211. $p[] = $z+$yc-$h*$tsa;
  212. $pt[] = $xc+$w*$tca;
  213. $pt[] = $yc-$h*$tsa;
  214. }
  215. $pt[] = $xc+$w*$cosea;
  216. $pt[] = $yc-$h*$sinea;
  217. $pt[] = $xc;
  218. $pt[] = $yc;
  219. $p[] = $xc+$w*$cosea;
  220. $p[] = $z+$yc-$h*$sinea;
  221. $p[] = $xc+$w*$cosea;
  222. $p[] = $yc-$h*$sinea;
  223. $p[] = $xc;
  224. $p[] = $yc;
  225. }
  226. }
  227. elseif( $sa >= 180 ) {
  228. $p = array($xc,$yc,$xc,$yc+$z,$xc+$w*$cosea,$z+$yc-$h*$sinea);
  229. $pt = array($xc,$yc,$xc+$w*$cosea,$yc-$h*$sinea);
  230. for( $a=$rea; $a>$rsa; $a -= $step ) {
  231. $tca = cos($a);
  232. $tsa = sin($a);
  233. $p[] = $xc+$w*$tca;
  234. $p[] = $z+$yc-$h*$tsa;
  235. $pt[] = $xc+$w*$tca;
  236. $pt[] = $yc-$h*$tsa;
  237. }
  238. $pt[] = $xc+$w*$cossa;
  239. $pt[] = $yc-$h*$sinsa;
  240. $pt[] = $xc;
  241. $pt[] = $yc;
  242. $p[] = $xc+$w*$cossa;
  243. $p[] = $z+$yc-$h*$sinsa;
  244. $p[] = $xc+$w*$cossa;
  245. $p[] = $yc-$h*$sinsa;
  246. $p[] = $xc;
  247. $p[] = $yc;
  248. }
  249. elseif( $sa >= 90 ) {
  250. if( $ea > 180 ) {
  251. $p = array($xc,$yc,$xc,$yc+$z,$xc+$w*$cosea,$z+$yc-$h*$sinea);
  252. $pt = array($xc,$yc,$xc+$w*$cosea,$yc-$h*$sinea);
  253. for( $a=$rea; $a > M_PI; $a -= $step ) {
  254. $tca = cos($a);
  255. $tsa = sin($a);
  256. $p[] = $xc+$w*$tca;
  257. $p[] = $z + $yc - $h*$tsa;
  258. $pt[] = $xc+$w*$tca;
  259. $pt[] = $yc-$h*$tsa;
  260. }
  261. $p[] = $xc-$w;
  262. $p[] = $z+$yc;
  263. $p[] = $xc-$w;
  264. $p[] = $yc;
  265. $p[] = $xc;
  266. $p[] = $yc;
  267. $pt[] = $xc-$w;
  268. $pt[] = $z+$yc;
  269. $pt[] = $xc-$w;
  270. $pt[] = $yc;
  271. for( $a=M_PI-$step; $a > $rsa; $a -= $step ) {
  272. $pt[] = $xc + $w*cos($a);
  273. $pt[] = $yc - $h*sin($a);
  274. }
  275. $pt[] = $xc+$w*$cossa;
  276. $pt[] = $yc-$h*$sinsa;
  277. $pt[] = $xc;
  278. $pt[] = $yc;
  279. }
  280. else { // $sa >= 90 && $ea <= 180
  281. $p = array($xc,$yc,$xc,$yc+$z,
  282. $xc+$w*$cosea,$z+$yc-$h*$sinea,
  283. $xc+$w*$cosea,$yc-$h*$sinea,
  284. $xc,$yc);
  285. $pt = array($xc,$yc,$xc+$w*$cosea,$yc-$h*$sinea);
  286. for( $a=$rea; $a>$rsa; $a -= $step ) {
  287. $pt[] = $xc + $w*cos($a);
  288. $pt[] = $yc - $h*sin($a);
  289. }
  290. $pt[] = $xc+$w*$cossa;
  291. $pt[] = $yc-$h*$sinsa;
  292. $pt[] = $xc;
  293. $pt[] = $yc;
  294. }
  295. }
  296. else { // sa > 0 && ea < 90
  297. $p = array($xc,$yc,$xc,$yc+$z,
  298. $xc+$w*$cossa,$z+$yc-$h*$sinsa,
  299. $xc+$w*$cossa,$yc-$h*$sinsa,
  300. $xc,$yc);
  301. $pt = array($xc,$yc,$xc+$w*$cossa,$yc-$h*$sinsa);
  302. for( $a=$rsa; $a < $rea; $a += $step ) {
  303. $pt[] = $xc + $w*cos($a);
  304. $pt[] = $yc - $h*sin($a);
  305. }
  306. $pt[] = $xc+$w*$cosea;
  307. $pt[] = $yc-$h*$sinea;
  308. $pt[] = $xc;
  309. $pt[] = $yc;
  310. }
  311. $img->PushColor($fillcolor.":".$shadow);
  312. $img->FilledPolygon($p);
  313. $img->PopColor();
  314. $img->PushColor($fillcolor);
  315. $img->FilledPolygon($pt);
  316. $img->PopColor();
  317. }
  318. function SetStartAngle($aStart) {
  319. if( $aStart < 0 || $aStart > 360 ) {
  320. JpGraphError::RaiseL(14004);//('Slice start angle must be between 0 and 360 degrees.');
  321. }
  322. $this->startangle = $aStart;
  323. }
  324. // Draw a 3D Pie
  325. function Pie3D($aaoption,$img,$data,$colors,$xc,$yc,$d,$angle,$z,
  326. $shadow=0.65,$startangle=0,$edgecolor="",$edgeweight=1) {
  327. //---------------------------------------------------------------------------
  328. // As usual the algorithm get more complicated than I originally
  329. // envisioned. I believe that this is as simple as it is possible
  330. // to do it with the features I want. It's a good exercise to start
  331. // thinking on how to do this to convince your self that all this
  332. // is really needed for the general case.
  333. //
  334. // The algorithm two draw 3D pies without "real 3D" is done in
  335. // two steps.
  336. // First imagine the pie cut in half through a thought line between
  337. // 12'a clock and 6'a clock. It now easy to imagine that we can plot
  338. // the individual slices for each half by starting with the topmost
  339. // pie slice and continue down to 6'a clock.
  340. //
  341. // In the algortithm this is done in three principal steps
  342. // Step 1. Do the knife cut to ensure by splitting slices that extends
  343. // over the cut line. This is done by splitting the original slices into
  344. // upto 3 subslices.
  345. // Step 2. Find the top slice for each half
  346. // Step 3. Draw the slices from top to bottom
  347. //
  348. // The thing that slightly complicates this scheme with all the
  349. // angle comparisons below is that we can have an arbitrary start
  350. // angle so we must take into account the different equivalence classes.
  351. // For the same reason we must walk through the angle array in a
  352. // modulo fashion.
  353. //
  354. // Limitations of algorithm:
  355. // * A small exploded slice which crosses the 270 degree point
  356. // will get slightly nagged close to the center due to the fact that
  357. // we print the slices in Z-order and that the slice left part
  358. // get printed first and might get slightly nagged by a larger
  359. // slice on the right side just before the right part of the small
  360. // slice. Not a major problem though.
  361. //---------------------------------------------------------------------------
  362. // Determine the height of the ellippse which gives an
  363. // indication of the inclination angle
  364. $h = ($angle/90.0)*$d;
  365. $sum = 0;
  366. for($i=0; $i<count($data); ++$i ) {
  367. $sum += $data[$i];
  368. }
  369. // Special optimization
  370. if( $sum==0 ) return;
  371. if( $this->labeltype == 2 ) {
  372. $this->adjusted_data = $this->AdjPercentage($data);
  373. }
  374. // Setup the start
  375. $accsum = 0;
  376. $a = $startangle;
  377. $a = $this->NormAngle($a);
  378. //
  379. // Step 1 . Split all slices that crosses 90 or 270
  380. //
  381. $idx=0;
  382. $adjexplode=array();
  383. $numcolors = count($colors);
  384. for($i=0; $i<count($data); ++$i, ++$idx ) {
  385. $da = $data[$i]/$sum * 360;
  386. if( empty($this->explode_radius[$i]) )
  387. $this->explode_radius[$i]=0;
  388. $expscale=1;
  389. if( $aaoption == 1 )
  390. $expscale=2;
  391. $la = $a + $da/2;
  392. $explode = array( $xc + $this->explode_radius[$i]*cos($la*M_PI/180)*$expscale,
  393. $yc - $this->explode_radius[$i]*sin($la*M_PI/180) * ($h/$d) *$expscale );
  394. $adjexplode[$idx] = $explode;
  395. $labeldata[$i] = array($la,$explode[0],$explode[1]);
  396. $originalangles[$i] = array($a,$a+$da);
  397. $ne = $this->NormAngle($a+$da);
  398. if( $da <= 180 ) {
  399. // If the slice size is <= 90 it can at maximum cut across
  400. // one boundary (either 90 or 270) where it needs to be split
  401. $split=-1; // no split
  402. if( ($da<=90 && ($a <= 90 && $ne > 90)) ||
  403. (($da <= 180 && $da >90) && (($a < 90 || $a >= 270) && $ne > 90)) ) {
  404. $split = 90;
  405. }
  406. elseif( ($da<=90 && ($a <= 270 && $ne > 270)) ||
  407. (($da<=180 && $da>90) && ($a >= 90 && $a < 270 && ($a+$da) > 270 )) ) {
  408. $split = 270;
  409. }
  410. if( $split > 0 ) { // split in two
  411. $angles[$idx] = array($a,$split);
  412. $adjcolors[$idx] = $colors[$i % $numcolors];
  413. $adjexplode[$idx] = $explode;
  414. $angles[++$idx] = array($split,$ne);
  415. $adjcolors[$idx] = $colors[$i % $numcolors];
  416. $adjexplode[$idx] = $explode;
  417. }
  418. else { // no split
  419. $angles[$idx] = array($a,$ne);
  420. $adjcolors[$idx] = $colors[$i % $numcolors];
  421. $adjexplode[$idx] = $explode;
  422. }
  423. }
  424. else {
  425. // da>180
  426. // Slice may, depending on position, cross one or two
  427. // bonudaries
  428. if( $a < 90 )
  429. $split = 90;
  430. elseif( $a <= 270 )
  431. $split = 270;
  432. else
  433. $split = 90;
  434. $angles[$idx] = array($a,$split);
  435. $adjcolors[$idx] = $colors[$i % $numcolors];
  436. $adjexplode[$idx] = $explode;
  437. //if( $a+$da > 360-$split ) {
  438. // For slices larger than 270 degrees we might cross
  439. // another boundary as well. This means that we must
  440. // split the slice further. The comparison gets a little
  441. // bit complicated since we must take into accound that
  442. // a pie might have a startangle >0 and hence a slice might
  443. // wrap around the 0 angle.
  444. // Three cases:
  445. // a) Slice starts before 90 and hence gets a split=90, but
  446. // we must also check if we need to split at 270
  447. // b) Slice starts after 90 but before 270 and slices
  448. // crosses 90 (after a wrap around of 0)
  449. // c) If start is > 270 (hence the firstr split is at 90)
  450. // and the slice is so large that it goes all the way
  451. // around 270.
  452. if( ($a < 90 && ($a+$da > 270)) ||
  453. ($a > 90 && $a<=270 && ($a+$da>360+90) ) ||
  454. ($a > 270 && $this->NormAngle($a+$da)>270) ) {
  455. $angles[++$idx] = array($split,360-$split);
  456. $adjcolors[$idx] = $colors[$i % $numcolors];
  457. $adjexplode[$idx] = $explode;
  458. $angles[++$idx] = array(360-$split,$ne);
  459. $adjcolors[$idx] = $colors[$i % $numcolors];
  460. $adjexplode[$idx] = $explode;
  461. }
  462. else {
  463. // Just a simple split to the previous decided
  464. // angle.
  465. $angles[++$idx] = array($split,$ne);
  466. $adjcolors[$idx] = $colors[$i % $numcolors];
  467. $adjexplode[$idx] = $explode;
  468. }
  469. }
  470. $a += $da;
  471. $a = $this->NormAngle($a);
  472. }
  473. // Total number of slices
  474. $n = count($angles);
  475. for($i=0; $i<$n; ++$i) {
  476. list($dbgs,$dbge) = $angles[$i];
  477. }
  478. //
  479. // Step 2. Find start index (first pie that starts in upper left quadrant)
  480. //
  481. $minval = $angles[0][0];
  482. $min = 0;
  483. for( $i=0; $i<$n; ++$i ) {
  484. if( $angles[$i][0] < $minval ) {
  485. $minval = $angles[$i][0];
  486. $min = $i;
  487. }
  488. }
  489. $j = $min;
  490. $cnt = 0;
  491. while( $angles[$j][1] <= 90 ) {
  492. $j++;
  493. if( $j>=$n) {
  494. $j=0;
  495. }
  496. if( $cnt > $n ) {
  497. JpGraphError::RaiseL(14005);
  498. //("Pie3D Internal error (#1). Trying to wrap twice when looking for start index");
  499. }
  500. ++$cnt;
  501. }
  502. $start = $j;
  503. //
  504. // Step 3. Print slices in z-order
  505. //
  506. $cnt = 0;
  507. // First stroke all the slices between 90 and 270 (left half circle)
  508. // counterclockwise
  509. while( $angles[$j][0] < 270 && $aaoption !== 2 ) {
  510. list($x,$y) = $adjexplode[$j];
  511. $this->Pie3DSlice($img,$x,$y,$d,$h,$angles[$j][0],$angles[$j][1],
  512. $z,$adjcolors[$j],$shadow);
  513. $last = array($x,$y,$j);
  514. $j++;
  515. if( $j >= $n ) $j=0;
  516. if( $cnt > $n ) {
  517. JpGraphError::RaiseL(14006);
  518. //("Pie3D Internal Error: Z-Sorting algorithm for 3D Pies is not working properly (2). Trying to wrap twice while stroking.");
  519. }
  520. ++$cnt;
  521. }
  522. $slice_left = $n-$cnt;
  523. $j=$start-1;
  524. if($j<0) $j=$n-1;
  525. $cnt = 0;
  526. // The stroke all slices from 90 to -90 (right half circle)
  527. // clockwise
  528. while( $cnt < $slice_left && $aaoption !== 2 ) {
  529. list($x,$y) = $adjexplode[$j];
  530. $this->Pie3DSlice($img,$x,$y,$d,$h,$angles[$j][0],$angles[$j][1],
  531. $z,$adjcolors[$j],$shadow);
  532. $j--;
  533. if( $cnt > $n ) {
  534. JpGraphError::RaiseL(14006);
  535. //("Pie3D Internal Error: Z-Sorting algorithm for 3D Pies is not working properly (2). Trying to wrap twice while stroking.");
  536. }
  537. if($j<0) $j=$n-1;
  538. $cnt++;
  539. }
  540. // Now do a special thing. Stroke the last slice on the left
  541. // halfcircle one more time. This is needed in the case where
  542. // the slice close to 270 have been exploded. In that case the
  543. // part of the slice close to the center of the pie might be
  544. // slightly nagged.
  545. if( $aaoption !== 2 )
  546. $this->Pie3DSlice($img,$last[0],$last[1],$d,$h,$angles[$last[2]][0],
  547. $angles[$last[2]][1],$z,$adjcolors[$last[2]],$shadow);
  548. if( $aaoption !== 1 ) {
  549. // Now print possible labels and add csim
  550. $img->SetFont($this->value->ff,$this->value->fs);
  551. $margin = $img->GetFontHeight()/2 + $this->value->margin ;
  552. for($i=0; $i < count($data); ++$i ) {
  553. $la = $labeldata[$i][0];
  554. $x = $labeldata[$i][1] + cos($la*M_PI/180)*($d+$margin);
  555. $y = $labeldata[$i][2] - sin($la*M_PI/180)*($h+$margin);
  556. if( $la > 180 && $la < 360 ) $y += $z;
  557. if( $this->labeltype == 0 ) {
  558. if( $sum > 0 )
  559. $l = 100*$data[$i]/$sum;
  560. else
  561. $l = 0;
  562. }
  563. elseif( $this->labeltype == 1 ) {
  564. $l = $data[$i];
  565. }
  566. else {
  567. $l = $this->adjusted_data[$i];
  568. }
  569. if( isset($this->labels[$i]) && is_string($this->labels[$i]) )
  570. $l=sprintf($this->labels[$i],$l);
  571. $this->StrokeLabels($l,$img,$labeldata[$i][0]*M_PI/180,$x,$y,$z);
  572. $this->AddSliceToCSIM($i,$labeldata[$i][1],$labeldata[$i][2],$h*2,$d*2,$z,
  573. $originalangles[$i][0],$originalangles[$i][1]);
  574. }
  575. }
  576. //
  577. // Finally add potential lines in pie
  578. //
  579. if( $edgecolor=="" || $aaoption !== 0 ) return;
  580. $accsum = 0;
  581. $a = $startangle;
  582. $a = $this->NormAngle($a);
  583. $a *= M_PI/180.0;
  584. $idx=0;
  585. $img->PushColor($edgecolor);
  586. $img->SetLineWeight($edgeweight);
  587. $fulledge = true;
  588. for($i=0; $i < count($data) && $fulledge; ++$i ) {
  589. if( empty($this->explode_radius[$i]) )
  590. $this->explode_radius[$i]=0;
  591. if( $this->explode_radius[$i] > 0 ) {
  592. $fulledge = false;
  593. }
  594. }
  595. for($i=0; $i < count($data); ++$i, ++$idx ) {
  596. $da = $data[$i]/$sum * 2*M_PI;
  597. $this->StrokeFullSliceFrame($img,$xc,$yc,$a,$a+$da,$d,$h,$z,$edgecolor,
  598. $this->explode_radius[$i],$fulledge);
  599. $a += $da;
  600. }
  601. $img->PopColor();
  602. }
  603. function StrokeFullSliceFrame($img,$xc,$yc,$sa,$ea,$w,$h,$z,$edgecolor,$exploderadius,$fulledge) {
  604. $step = 0.02;
  605. if( $exploderadius > 0 ) {
  606. $la = ($sa+$ea)/2;
  607. $xc += $exploderadius*cos($la);
  608. $yc -= $exploderadius*sin($la) * ($h/$w) ;
  609. }
  610. $p = array($xc,$yc,$xc+$w*cos($sa),$yc-$h*sin($sa));
  611. for($a=$sa; $a < $ea; $a += $step ) {
  612. $p[] = $xc + $w*cos($a);
  613. $p[] = $yc - $h*sin($a);
  614. }
  615. $p[] = $xc+$w*cos($ea);
  616. $p[] = $yc-$h*sin($ea);
  617. $p[] = $xc;
  618. $p[] = $yc;
  619. $img->SetColor($edgecolor);
  620. $img->Polygon($p);
  621. // Unfortunately we can't really draw the full edge around the whole of
  622. // of the slice if any of the slices are exploded. The reason is that
  623. // this algorithm is to simply. There are cases where the edges will
  624. // "overwrite" other slices when they have been exploded.
  625. // Doing the full, proper 3D hidden lines stiff is actually quite
  626. // tricky. So for exploded pies we only draw the top edge. Not perfect
  627. // but the "real" solution is much more complicated.
  628. if( $fulledge && !( $sa > 0 && $sa < M_PI && $ea < M_PI) ) {
  629. if($sa < M_PI && $ea > M_PI)
  630. $sa = M_PI;
  631. if($sa < 2*M_PI && (($ea >= 2*M_PI) || ($ea > 0 && $ea < $sa ) ) )
  632. $ea = 2*M_PI;
  633. if( $sa >= M_PI && $ea <= 2*M_PI ) {
  634. $p = array($xc + $w*cos($sa),$yc - $h*sin($sa),
  635. $xc + $w*cos($sa),$z + $yc - $h*sin($sa));
  636. for($a=$sa+$step; $a < $ea; $a += $step ) {
  637. $p[] = $xc + $w*cos($a);
  638. $p[] = $z + $yc - $h*sin($a);
  639. }
  640. $p[] = $xc + $w*cos($ea);
  641. $p[] = $z + $yc - $h*sin($ea);
  642. $p[] = $xc + $w*cos($ea);
  643. $p[] = $yc - $h*sin($ea);
  644. $img->SetColor($edgecolor);
  645. $img->Polygon($p);
  646. }
  647. }
  648. }
  649. function Stroke($img,$aaoption=0) {
  650. $n = count($this->data);
  651. // If user hasn't set the colors use the theme array
  652. if( $this->setslicecolors==null ) {
  653. $colors = array_keys($img->rgb->rgb_table);
  654. sort($colors);
  655. $idx_a=$this->themearr[$this->theme];
  656. $ca = array();
  657. $m = count($idx_a);
  658. for($i=0; $i < $m; ++$i)
  659. $ca[$i] = $colors[$idx_a[$i]];
  660. $ca = array_reverse(array_slice($ca,0,$n));
  661. }
  662. else {
  663. $ca = $this->setslicecolors;
  664. }
  665. if( $this->posx <= 1 && $this->posx > 0 )
  666. $xc = round($this->posx*$img->width);
  667. else
  668. $xc = $this->posx ;
  669. if( $this->posy <= 1 && $this->posy > 0 )
  670. $yc = round($this->posy*$img->height);
  671. else
  672. $yc = $this->posy ;
  673. if( $this->radius <= 1 ) {
  674. $width = floor($this->radius*min($img->width,$img->height));
  675. // Make sure that the pie doesn't overflow the image border
  676. // The 0.9 factor is simply an extra margin to leave some space
  677. // between the pie an the border of the image.
  678. $width = min($width,min($xc*0.9,($yc*90/$this->angle-$width/4)*0.9));
  679. }
  680. else {
  681. $width = $this->radius * ($aaoption === 1 ? 2 : 1 ) ;
  682. }
  683. // Add a sanity check for width
  684. if( $width < 1 ) {
  685. JpGraphError::RaiseL(14007);//("Width for 3D Pie is 0. Specify a size > 0");
  686. }
  687. // Establish a thickness. By default the thickness is a fifth of the
  688. // pie slice width (=pie radius) but since the perspective depends
  689. // on the inclination angle we use some heuristics to make the edge
  690. // slightly thicker the less the angle.
  691. // Has user specified an absolute thickness? In that case use
  692. // that instead
  693. if( $this->iThickness ) {
  694. $thick = $this->iThickness;
  695. $thick *= ($aaoption === 1 ? 2 : 1 );
  696. }
  697. else
  698. $thick = $width/12;
  699. $a = $this->angle;
  700. if( $a <= 30 ) $thick *= 1.6;
  701. elseif( $a <= 40 ) $thick *= 1.4;
  702. elseif( $a <= 50 ) $thick *= 1.2;
  703. elseif( $a <= 60 ) $thick *= 1.0;
  704. elseif( $a <= 70 ) $thick *= 0.8;
  705. elseif( $a <= 80 ) $thick *= 0.7;
  706. else $thick *= 0.6;
  707. $thick = floor($thick);
  708. if( $this->explode_all )
  709. for($i=0; $i < $n; ++$i)
  710. $this->explode_radius[$i]=$this->explode_r;
  711. $this->Pie3D($aaoption,$img,$this->data, $ca, $xc, $yc, $width, $this->angle,
  712. $thick, 0.65, $this->startangle, $this->edgecolor, $this->edgeweight);
  713. // Adjust title position
  714. if( $aaoption != 1 ) {
  715. $this->title->Pos($xc,$yc-$this->title->GetFontHeight($img)-$width/2-$this->title->margin, "center","bottom");
  716. $this->title->Stroke($img);
  717. }
  718. }
  719. //---------------
  720. // PRIVATE METHODS
  721. // Position the labels of each slice
  722. function StrokeLabels($label,$img,$a,$xp,$yp,$z) {
  723. $this->value->halign="left";
  724. $this->value->valign="top";
  725. // Position the axis title.
  726. // dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
  727. // that intersects with the extension of the corresponding axis. The code looks a little
  728. // bit messy but this is really the only way of having a reasonable position of the
  729. // axis titles.
  730. $img->SetFont($this->value->ff,$this->value->fs,$this->value->fsize);
  731. $h=$img->GetTextHeight($label);
  732. // For numeric values the format of the display value
  733. // must be taken into account
  734. if( is_numeric($label) ) {
  735. if( $label >= 0 )
  736. $w=$img->GetTextWidth(sprintf($this->value->format,$label));
  737. else
  738. $w=$img->GetTextWidth(sprintf($this->value->negformat,$label));
  739. }
  740. else
  741. $w=$img->GetTextWidth($label);
  742. while( $a > 2*M_PI ) $a -= 2*M_PI;
  743. if( $a>=7*M_PI/4 || $a <= M_PI/4 ) $dx=0;
  744. if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dx=($a-M_PI/4)*2/M_PI;
  745. if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dx=1;
  746. if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dx=(1-($a-M_PI*5/4)*2/M_PI);
  747. if( $a>=7*M_PI/4 ) $dy=(($a-M_PI)-3*M_PI/4)*2/M_PI;
  748. if( $a<=M_PI/4 ) $dy=(1-$a*2/M_PI);
  749. if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dy=1;
  750. if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dy=(1-($a-3*M_PI/4)*2/M_PI);
  751. if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dy=0;
  752. $x = round($xp-$dx*$w);
  753. $y = round($yp-$dy*$h);
  754. // Mark anchor point for debugging
  755. /*
  756. $img->SetColor('red');
  757. $img->Line($xp-10,$yp,$xp+10,$yp);
  758. $img->Line($xp,$yp-10,$xp,$yp+10);
  759. */
  760. $oldmargin = $this->value->margin;
  761. $this->value->margin=0;
  762. $this->value->Stroke($img,$label,$x,$y);
  763. $this->value->margin=$oldmargin;
  764. }
  765. } // Class
  766. /* EOF */
  767. ?>