PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jpgraph/src/jpgraph_pie3d.php

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