File: jpgraph_pie3d.php

package info (click to toggle)
libphp-jpgraph 1.5.2-11+u1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 888 kB
  • ctags: 2,689
  • sloc: php: 8,992; makefile: 24
file content (306 lines) | stat: -rw-r--r-- 9,211 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/*=======================================================================
// File:	JPGRAPH_PIE3D.PHP
// Description: 3D Pie plot extension for JpGraph
// Created: 	2001-03-24
// Author:	Johan Persson (johanp@aditus.nu)
// Ver:		$Id: jpgraph_pie3d.php,v 1.17 2002/01/24 21:51:15 aditus Exp $
//
// License:	This code is released under GPL 2.0
// Copyright (C) 2001 Johan Persson
//========================================================================
*/

//===================================================
// CLASS PiePlot3D
// Description: Plots a 3D pie with a specified projection 
// angle between 20 and 70 degrees.
//===================================================
class PiePlot3D extends PiePlot {
    var $labelhintcolor="red",$showlabelhint=true,$labelmargin=0.30;
    var $angle=30;	
	
//---------------
// CONSTRUCTOR
    function PiePlot3d(&$data) {
	$this->data = $data;
	$this->title = new Text("");
	$this->title->SetFont(FF_FONT1,FS_BOLD);
    }

//---------------
// PUBLIC METHODS	
	
    // Specify projection angle for 3D in degrees
    // Must be between 20 and 70 degrees
    function SetAngle($a) {
	if( $a<30 || $a>70 )
	    JpGraphError::Raise("JpGraph: 3D Pie projection angle must be between 30 and 70 degrees.");
	else
	    $this->angle = $a;
    }

    function AddSliceToCSIM($i,$xc,$yc,$height,$width,$thick,$sa,$ea) {  //Slice number, ellipse centre (x,y), height, width, start angle, end angle
		
	//add coordinates of the centre to the map
	$coords = "$xc, $yc";

	//add coordinates of the first point on the arc to the map
	$xp = floor($width*cos($sa)/2+$xc);
	$yp = floor($yc-$height*sin($sa)/2);
	$coords.= ", $xp, $yp";

	//If on the front half, add the thickness offset
	if ($sa >= M_PI && $sa <= 2*M_PI*1.01) {
	    $yp = floor($yp+($thick*$width));
	    $coords.= ", $xp, $yp";
	}
		
	//add coordinates every 0.2 radians
	$a=$sa+0.2;
	while ($a<$ea) {
	    $xp = floor($width*cos($a)/2+$xc);
	    if ($a >= M_PI && $a <= 2*M_PI*1.01) {
		$yp = floor($yc-($height*sin($a)/2)+$thick*$width);
	    } else {
		$yp = floor($yc-$height*sin($a)/2);
	    }
	    $coords.= ", $xp, $yp";
	    $a += 0.2;
	}
		
	//Add the last point on the arc
	$xp = floor($width*cos($ea)/2+$xc);
	$yp = floor($yc-$height*sin($ea)/2);

	//If on the front half, add the thickness offset
	if ($ea >= M_PI && $ea <= 2*M_PI*1.01) {
	    $coords.= ", $xp, ".floor($yp+($thick*$width));
	}
	$coords.= ", $xp, $yp";
	if( !empty($this->csimalts[$i]) ) {										
	    $tmp=sprintf($this->csimalts[$i],$this->data[$i]);
	    $alt="alt=\"$tmp\"";
	}
	if( !empty($this->csimtargets[$i]) )
	    $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->csimtargets[$i]."\" $alt>\r\n";
    }

	
    function ExplodeSlice($e) {
	//JpGraphError::Raise("JpGraph Error: Exploding slices are not (yet) implemented for 3d pies graphs.");
	//$this->explode_slice=$e;
    }
	
    // Distance from the pie to the labels
    function SetLabelMargin($m) {
	assert($m>0 && $m<1);
	$this->labelmargin=$m;
    }
	
    // Show a thin line from the pie to the label for a specific slice
    function ShowLabelHint($f=true) {
	$this->showlabelhint=$f;
    }
	
    // Set color of hint line to label for each slice
    function SetLabelHintColor($c) {
	$this->labelhintcolor=$c;
    }
		
    function Stroke(&$img) {
		
	$colors = array_keys($img->rgb->rgb_table);
   	sort($colors);	
   	$ta=$this->themearr[$this->theme];	
   	
   	if( $this->setslicecolors==null ) 
	    $numcolors=count($ta);
   	else
	    $numcolors=count($this->setslicecolors);
   	
	// Draw the slices
	$sum=0;
	foreach($this->data as $d)
	    $sum += $d;
		
	// Format the titles for each slice
	for( $i=0; $i<count($this->data); ++$i) {
	    if( $this->labeltype==0 )
		if( $sum != 0 )
		    $l = round(100*$this->data[$i]/$sum,$this->precision);
		else
		    $l=0;
	    else
		$l = $this->data[$i];
	    $l = sprintf($this->labelformat,$l);
	    if( $this->show_psign ) $l .= "%";
	    $this->labels[$i]=$l;			
	}
		
	// Set up the pie-circle with some heuristic constants
	$thick=0.16-($this->angle-20)/60*0.07;
	
	$width = floor(2.0*$this->radius*min($img->width,$img->height));
	$height = ($this->angle/90.0)*$width;
	$xc = $this->posx*$img->width;
	$yc = $this->posy*$img->height;

	$img->SetColor($this->color);			
	$img->Ellipse($xc,$yc,$width,$height);
	$img->Arc($xc,$yc+$width*$thick,$width,$height,0,180);
	$img->Line($xc+$width/2,$yc,$xc+$width/2,$yc+$width*$thick);
	$img->Line($xc-$width/2,$yc,$xc-$width/2,$yc+$width*$thick);
	$fillPerimeter[0] = array('x' => round((($xc - ($width / 2)) + 1)),
				  'y' => round(($yc + ($width * $thick) / 2)));
		
	// Draw the first slice first line
	$img->SetColor($this->color);			
	$img->SetLineWeight($this->weight);
	$a = $this->startangle;

	$xp = $width*cos($a)/2+$xc;
	$yp = $yc-$height*sin($a)/2;
	$img->Line($xc,$yc,$xp,$yp);

	for($i=0; $sum>0 && $i<count($this->data); $i++) {
	    $img->SetColor($this->color);	
	    $d = $this->data[$i];
	    $la = $a + M_PI*$d/$sum;
	    $old_a = $a;
	    $a += 2*M_PI*$d/$sum;

	    if ($this->csimtargets[$i]) {
		$this->AddSliceToCSIM($i,$xc,$yc,$height,$width,$thick,$old_a,$a);
	    }

	    $xp = $width*cos($a)/2+$xc;
	    $yp = $yc-$height*sin($a)/2;

	    if( $i<count($this->data)-1)
		$img->Line($xc,$yc,$xp,$yp);

	    if( $a > M_PI && $a < 0.999*2*M_PI )
		$img->Line($xp,$yp,$xp,$yp+$width*$thick-1);

	    if($a < M_PI) {
		$fillPerimeter[$i + 1] = $fillPerimeter[$i];
	    } else {
		$fillPerimeter[$i + 1] = array('x' => round(($xp + 1)),
					       'y' => round(($yp + ($width * $thick) / 2)));
	    }
			
	    if( $this->setslicecolors==null )
		$slicecolor=$colors[$ta[$i%$numcolors]];
	    else
		$slicecolor=$this->setslicecolors[$i%$numcolors];
				
	    if( $this->show_labels ) {
		$margin = 1 + $this->labelmargin;
		$xp = $width*cos($la)/2*$margin;
		$yp = $height*sin($la)/2*$margin;
									
		if( ($la >= 0 && $la <= M_PI) || $la>2*M_PI*0.98 ) {
		    $this->StrokeLabels($this->labels[$i],$img,$la,$xc+$xp,$yc-$yp);	
		    if( $this->showlabelhint ) {
			$img->SetColor($this->labelhintcolor);
			$img->Line($xc+$xp/$margin,$yc-$yp/$margin,$xc+$xp,$yc-$yp);
		    }
		}
		else {
		    $this->StrokeLabels($this->labels[$i],$img,$la,$xc+$xp,$yc-$yp+$width*$thick);	
		    if( $this->showlabelhint ) {
			$img->SetColor($this->labelhintcolor);
			$img->Line($xc+$xp/$margin,$yc-$yp/$margin+$width*$thick,$xc+$xp,$yc-$yp+$width*$thick);
		    }
		}					


		$img->SetColor($slicecolor);
		$xp = $width*cos($la)/3+$xc;
		$yp = $yc-$height*sin($la)/3;
		$img->Fill(round($xp), round($yp)); 

		// Make the edge color 35% darker
		$img->SetColor($slicecolor.":0.65");
				
		if($fillPerimeter[$i]['x'] <= $xc + ($width / 2)) {
		    $img->Fill($fillPerimeter[$i]['x'],$fillPerimeter[$i]['y']); 
		}
	    }
	}	
		
	// Adjust title position
	$this->title->Pos($xc,$yc-$img->GetFontHeight()-$this->radius,"center","bottom");
	$this->title->Stroke($img);

	// Draw the pie ellipse one more time since the filling might have
	// written partly on the lines due to the filling in the edges.
	$img->SetColor($this->color);			
	$img->Ellipse($xc,$yc,$width,$height);
	$img->Arc($xc,$yc+$width*$thick,$width,$height,0,180);

	// Draw the first slice first line
	$a = $this->startangle;
	$xp = $width*cos($a)/2+$xc;
	$yp = $yc-$height*sin($a)/2;
	$img->Line($xc,$yc,$xp,$yp);

	// Draw the rest of the slice lines
	for($i=0, $a=0; $sum>0 && $i<count($this->data); $i++) {
	    $d = $this->data[$i];
	    $la = $a + M_PI*$d/$sum;
	    $old_a = $a;
	    $a += 2*M_PI*$d/$sum;
		  
	    $xp = $width*cos($a)/2+$xc;
	    $yp = $yc-$height*sin($a)/2;

	    if( $a > M_PI && $a < 0.999*2*M_PI )
		$img->Line($xp,$yp,$xp,$yp+$width*$thick-1);
		  
	    if( $i<count($this->data)-1)
		$img->Line($xc,$yc,$xp,$yp);
	}

	$img->Line($xc+$width/2,$yc,$xc+$width/2,$yc+$width*$thick);
	$img->Line($xc-$width/2,$yc,$xc-$width/2,$yc+$width*$thick);
		
    }

//---------------
// PRIVATE METHODS	

    // Position the labels of each slice
    function StrokeLabels($label,$img,$a,$xp,$yp) {
		
	$img->SetFont($this->font_family,$this->font_style,$this->font_size);
	$img->SetColor($this->font_color);
	$img->SetTextAlign("left","top");
	$marg=6;

	// Position the axis title. 
	// dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
	// that intersects with the extension of the corresponding axis. The code looks a little
	// bit messy but this is really the only way of having a reasonable position of the
	// axis titles.
	$h=$img->GetTextHeight($label);
	$w=$img->GetTextWidth($label);
	while( $a > 2*M_PI ) $a -= 2*M_PI;
	if( $a>=7*M_PI/4 || $a <= M_PI/4 ) $dx=0;
	if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dx=($a-M_PI/4)*2/M_PI; 
	if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dx=1;
	if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dx=(1-($a-M_PI*5/4)*2/M_PI);
		
	if( $a>=7*M_PI/4 ) $dy=(($a-M_PI)-3*M_PI/4)*2/M_PI;
	if( $a<=M_PI/4 ) $dy=(1-$a*2/M_PI);
	if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dy=1;
	if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dy=(1-($a-3*M_PI/4)*2/M_PI);
	if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dy=0;
		
	$img->StrokeText($xp-$dx*$w,$yp-$dy*$h,$label);		
    }	
} // Class

/* EOF */
?>