1 | <?php |
---|
2 | /*======================================================================= |
---|
3 | // File: JPGRAPH_POLAR.PHP |
---|
4 | // Description: Polar plot extension for JpGraph |
---|
5 | // Created: 2003-02-02 |
---|
6 | // Ver: $Id: jpgraph_polar.php 1796 2009-09-07 09:37:19Z ljp $ |
---|
7 | // |
---|
8 | // Copyright (c) Asial Corporation. All rights reserved. |
---|
9 | //======================================================================== |
---|
10 | */ |
---|
11 | |
---|
12 | require_once ('jpgraph_plotmark.inc.php'); |
---|
13 | require_once "jpgraph_log.php"; |
---|
14 | |
---|
15 | |
---|
16 | define('POLAR_360',1); |
---|
17 | define('POLAR_180',2); |
---|
18 | |
---|
19 | // |
---|
20 | // Note. Don't attempt to make sense of this code. |
---|
21 | // In order not to have to be able to inherit the scaling code |
---|
22 | // from the main graph package we have had to make some "tricks" since |
---|
23 | // the original scaling and axis was not designed to do what is |
---|
24 | // required here. |
---|
25 | // There were two option. 1: Re-implement everything and get a clean design |
---|
26 | // and 2: do some "small" trickery and be able to inherit most of |
---|
27 | // the functionlity from the main graph package. |
---|
28 | // We choose 2: here in order to save some time. |
---|
29 | // |
---|
30 | |
---|
31 | //-------------------------------------------------------------------------- |
---|
32 | // class PolarPlot |
---|
33 | //-------------------------------------------------------------------------- |
---|
34 | class PolarPlot { |
---|
35 | public $line_style='solid',$mark; |
---|
36 | public $legendcsimtarget=''; |
---|
37 | public $legendcsimalt=''; |
---|
38 | public $legend=""; |
---|
39 | public $csimtargets=array(); // Array of targets for CSIM |
---|
40 | public $csimareas=""; // Resultant CSIM area tags |
---|
41 | public $csimalts=null; // ALT:s for corresponding target |
---|
42 | public $scale=null; |
---|
43 | private $numpoints=0; |
---|
44 | private $iColor='navy',$iFillColor=''; |
---|
45 | private $iLineWeight=1; |
---|
46 | private $coord=null; |
---|
47 | |
---|
48 | function __construct($aData) { |
---|
49 | $n = count($aData); |
---|
50 | if( $n & 1 ) { |
---|
51 | JpGraphError::RaiseL(17001); |
---|
52 | //('Polar plots must have an even number of data point. Each data point is a tuple (angle,radius).'); |
---|
53 | } |
---|
54 | $this->numpoints = $n/2; |
---|
55 | $this->coord = $aData; |
---|
56 | $this->mark = new PlotMark(); |
---|
57 | } |
---|
58 | |
---|
59 | function SetWeight($aWeight) { |
---|
60 | $this->iLineWeight = $aWeight; |
---|
61 | } |
---|
62 | |
---|
63 | function SetColor($aColor){ |
---|
64 | $this->iColor = $aColor; |
---|
65 | } |
---|
66 | |
---|
67 | function SetFillColor($aColor){ |
---|
68 | $this->iFillColor = $aColor; |
---|
69 | } |
---|
70 | |
---|
71 | function Max() { |
---|
72 | $m = $this->coord[1]; |
---|
73 | $i=1; |
---|
74 | while( $i < $this->numpoints ) { |
---|
75 | $m = max($m,$this->coord[2*$i+1]); |
---|
76 | ++$i; |
---|
77 | } |
---|
78 | return $m; |
---|
79 | } |
---|
80 | // Set href targets for CSIM |
---|
81 | function SetCSIMTargets($aTargets,$aAlts=null) { |
---|
82 | $this->csimtargets=$aTargets; |
---|
83 | $this->csimalts=$aAlts; |
---|
84 | } |
---|
85 | |
---|
86 | // Get all created areas |
---|
87 | function GetCSIMareas() { |
---|
88 | return $this->csimareas; |
---|
89 | } |
---|
90 | |
---|
91 | function SetLegend($aLegend,$aCSIM="",$aCSIMAlt="") { |
---|
92 | $this->legend = $aLegend; |
---|
93 | $this->legendcsimtarget = $aCSIM; |
---|
94 | $this->legendcsimalt = $aCSIMAlt; |
---|
95 | } |
---|
96 | |
---|
97 | // Private methods |
---|
98 | |
---|
99 | function Legend($aGraph) { |
---|
100 | $color = $this->iColor ; |
---|
101 | if( $this->legend != "" ) { |
---|
102 | if( $this->iFillColor!='' ) { |
---|
103 | $color = $this->iFillColor; |
---|
104 | $aGraph->legend->Add($this->legend,$color,$this->mark,0, |
---|
105 | $this->legendcsimtarget,$this->legendcsimalt); |
---|
106 | } |
---|
107 | else { |
---|
108 | $aGraph->legend->Add($this->legend,$color,$this->mark,$this->line_style, |
---|
109 | $this->legendcsimtarget,$this->legendcsimalt); |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | function Stroke($img,$scale) { |
---|
115 | |
---|
116 | $i=0; |
---|
117 | $p=array(); |
---|
118 | $this->csimareas=''; |
---|
119 | while($i < $this->numpoints) { |
---|
120 | list($x1,$y1) = $scale->PTranslate($this->coord[2*$i],$this->coord[2*$i+1]); |
---|
121 | $p[2*$i] = $x1; |
---|
122 | $p[2*$i+1] = $y1; |
---|
123 | |
---|
124 | if( isset($this->csimtargets[$i]) ) { |
---|
125 | $this->mark->SetCSIMTarget($this->csimtargets[$i]); |
---|
126 | $this->mark->SetCSIMAlt($this->csimalts[$i]); |
---|
127 | $this->mark->SetCSIMAltVal($this->coord[2*$i], $this->coord[2*$i+1]); |
---|
128 | $this->mark->Stroke($img,$x1,$y1); |
---|
129 | $this->csimareas .= $this->mark->GetCSIMAreas(); |
---|
130 | } |
---|
131 | else { |
---|
132 | $this->mark->Stroke($img,$x1,$y1); |
---|
133 | } |
---|
134 | |
---|
135 | ++$i; |
---|
136 | } |
---|
137 | |
---|
138 | if( $this->iFillColor != '' ) { |
---|
139 | $img->SetColor($this->iFillColor); |
---|
140 | $img->FilledPolygon($p); |
---|
141 | } |
---|
142 | $img->SetLineWeight($this->iLineWeight); |
---|
143 | $img->SetColor($this->iColor); |
---|
144 | $img->Polygon($p,$this->iFillColor!=''); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | //-------------------------------------------------------------------------- |
---|
149 | // class PolarAxis |
---|
150 | //-------------------------------------------------------------------------- |
---|
151 | class PolarAxis extends Axis { |
---|
152 | private $angle_step=15,$angle_color='lightgray',$angle_label_color='black'; |
---|
153 | private $angle_fontfam=FF_FONT1,$angle_fontstyle=FS_NORMAL,$angle_fontsize=10; |
---|
154 | private $angle_fontcolor = 'navy'; |
---|
155 | private $gridminor_color='lightgray',$gridmajor_color='lightgray'; |
---|
156 | private $show_minor_grid = false, $show_major_grid = true ; |
---|
157 | private $show_angle_mark=true, $show_angle_grid=true, $show_angle_label=true; |
---|
158 | private $angle_tick_len=3, $angle_tick_len2=3, $angle_tick_color='black'; |
---|
159 | private $show_angle_tick=true; |
---|
160 | private $radius_tick_color='black'; |
---|
161 | |
---|
162 | function __construct($img,$aScale) { |
---|
163 | parent::__construct($img,$aScale); |
---|
164 | } |
---|
165 | |
---|
166 | function ShowAngleDegreeMark($aFlg=true) { |
---|
167 | $this->show_angle_mark = $aFlg; |
---|
168 | } |
---|
169 | |
---|
170 | function SetAngleStep($aStep) { |
---|
171 | $this->angle_step=$aStep; |
---|
172 | } |
---|
173 | |
---|
174 | function HideTicks($aFlg=true,$aAngleFlg=true) { |
---|
175 | parent::HideTicks($aFlg,$aFlg); |
---|
176 | $this->show_angle_tick = !$aAngleFlg; |
---|
177 | } |
---|
178 | |
---|
179 | function ShowAngleLabel($aFlg=true) { |
---|
180 | $this->show_angle_label = $aFlg; |
---|
181 | } |
---|
182 | |
---|
183 | function ShowGrid($aMajor=true,$aMinor=false,$aAngle=true) { |
---|
184 | $this->show_minor_grid = $aMinor; |
---|
185 | $this->show_major_grid = $aMajor; |
---|
186 | $this->show_angle_grid = $aAngle ; |
---|
187 | } |
---|
188 | |
---|
189 | function SetAngleFont($aFontFam,$aFontStyle=FS_NORMAL,$aFontSize=10) { |
---|
190 | $this->angle_fontfam = $aFontFam; |
---|
191 | $this->angle_fontstyle = $aFontStyle; |
---|
192 | $this->angle_fontsize = $aFontSize; |
---|
193 | } |
---|
194 | |
---|
195 | function SetColor($aColor,$aRadColor='',$aAngleColor='') { |
---|
196 | if( $aAngleColor == '' ) |
---|
197 | $aAngleColor=$aColor; |
---|
198 | parent::SetColor($aColor,$aRadColor); |
---|
199 | $this->angle_fontcolor = $aAngleColor; |
---|
200 | } |
---|
201 | |
---|
202 | function SetGridColor($aMajorColor,$aMinorColor='',$aAngleColor='') { |
---|
203 | if( $aMinorColor == '' ) |
---|
204 | $aMinorColor = $aMajorColor; |
---|
205 | if( $aAngleColor == '' ) |
---|
206 | $aAngleColor = $aMajorColor; |
---|
207 | |
---|
208 | $this->gridminor_color = $aMinorColor; |
---|
209 | $this->gridmajor_color = $aMajorColor; |
---|
210 | $this->angle_color = $aAngleColor; |
---|
211 | } |
---|
212 | |
---|
213 | function SetTickColors($aRadColor,$aAngleColor='') { |
---|
214 | $this->radius_tick_color = $aRadColor; |
---|
215 | $this->angle_tick_color = $aAngleColor; |
---|
216 | } |
---|
217 | |
---|
218 | // Private methods |
---|
219 | function StrokeGrid($pos) { |
---|
220 | $x = round($this->img->left_margin + $this->img->plotwidth/2); |
---|
221 | $this->scale->ticks->Stroke($this->img,$this->scale,$pos); |
---|
222 | |
---|
223 | // Stroke the minor arcs |
---|
224 | $pmin = array(); |
---|
225 | $p = $this->scale->ticks->ticks_pos; |
---|
226 | $n = count($p); |
---|
227 | $i = 0; |
---|
228 | $this->img->SetColor($this->gridminor_color); |
---|
229 | while( $i < $n ) { |
---|
230 | $r = $p[$i]-$x+1; |
---|
231 | $pmin[]=$r; |
---|
232 | if( $this->show_minor_grid ) { |
---|
233 | $this->img->Circle($x,$pos,$r); |
---|
234 | } |
---|
235 | $i++; |
---|
236 | } |
---|
237 | |
---|
238 | $limit = max($this->img->plotwidth,$this->img->plotheight)*1.4 ; |
---|
239 | while( $r < $limit ) { |
---|
240 | $off = $r; |
---|
241 | $i=1; |
---|
242 | $r = $off + round($p[$i]-$x+1); |
---|
243 | while( $r < $limit && $i < $n ) { |
---|
244 | $r = $off+$p[$i]-$x; |
---|
245 | $pmin[]=$r; |
---|
246 | if( $this->show_minor_grid ) { |
---|
247 | $this->img->Circle($x,$pos,$r); |
---|
248 | } |
---|
249 | $i++; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | // Stroke the major arcs |
---|
254 | if( $this->show_major_grid ) { |
---|
255 | // First determine how many minor step on |
---|
256 | // every major step. We have recorded the minor radius |
---|
257 | // in pmin and use these values. This is done in order |
---|
258 | // to avoid rounding errors if we were to recalculate the |
---|
259 | // different major radius. |
---|
260 | $pmaj = $this->scale->ticks->maj_ticks_pos; |
---|
261 | $p = $this->scale->ticks->ticks_pos; |
---|
262 | if( $this->scale->name == 'lin' ) { |
---|
263 | $step=round(($pmaj[1] - $pmaj[0])/($p[1] - $p[0])); |
---|
264 | } |
---|
265 | else { |
---|
266 | $step=9; |
---|
267 | } |
---|
268 | $n = round(count($pmin)/$step); |
---|
269 | $i = 0; |
---|
270 | $this->img->SetColor($this->gridmajor_color); |
---|
271 | $limit = max($this->img->plotwidth,$this->img->plotheight)*1.4 ; |
---|
272 | $off = $r; |
---|
273 | $i=0; |
---|
274 | $r = $pmin[$i*$step]; |
---|
275 | while( $r < $limit && $i < $n ) { |
---|
276 | $r = $pmin[$i*$step]; |
---|
277 | $this->img->Circle($x,$pos,$r); |
---|
278 | $i++; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | // Draw angles |
---|
283 | if( $this->show_angle_grid ) { |
---|
284 | $this->img->SetColor($this->angle_color); |
---|
285 | $d = max($this->img->plotheight,$this->img->plotwidth)*1.4 ; |
---|
286 | $a = 0; |
---|
287 | $p = $this->scale->ticks->ticks_pos; |
---|
288 | $start_radius = $p[1]-$x; |
---|
289 | while( $a < 360 ) { |
---|
290 | if( $a == 90 || $a == 270 ) { |
---|
291 | // Make sure there are no rounding problem with |
---|
292 | // exactly vertical lines |
---|
293 | $this->img->Line($x+$start_radius*cos($a/180*M_PI)+1, |
---|
294 | $pos-$start_radius*sin($a/180*M_PI), |
---|
295 | $x+$start_radius*cos($a/180*M_PI)+1, |
---|
296 | $pos-$d*sin($a/180*M_PI)); |
---|
297 | |
---|
298 | } |
---|
299 | else { |
---|
300 | $this->img->Line($x+$start_radius*cos($a/180*M_PI)+1, |
---|
301 | $pos-$start_radius*sin($a/180*M_PI), |
---|
302 | $x+$d*cos($a/180*M_PI), |
---|
303 | $pos-$d*sin($a/180*M_PI)); |
---|
304 | } |
---|
305 | $a += $this->angle_step; |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | function StrokeAngleLabels($pos,$type) { |
---|
311 | |
---|
312 | if( !$this->show_angle_label ) |
---|
313 | return; |
---|
314 | |
---|
315 | $x0 = round($this->img->left_margin+$this->img->plotwidth/2)+1; |
---|
316 | |
---|
317 | $d = max($this->img->plotwidth,$this->img->plotheight)*1.42; |
---|
318 | $a = $this->angle_step; |
---|
319 | $t = new Text(); |
---|
320 | $t->SetColor($this->angle_fontcolor); |
---|
321 | $t->SetFont($this->angle_fontfam,$this->angle_fontstyle,$this->angle_fontsize); |
---|
322 | $xright = $this->img->width - $this->img->right_margin; |
---|
323 | $ytop = $this->img->top_margin; |
---|
324 | $xleft = $this->img->left_margin; |
---|
325 | $ybottom = $this->img->height - $this->img->bottom_margin; |
---|
326 | $ha = 'left'; |
---|
327 | $va = 'center'; |
---|
328 | $w = $this->img->plotwidth/2; |
---|
329 | $h = $this->img->plotheight/2; |
---|
330 | $xt = $x0; $yt = $pos; |
---|
331 | $margin=5; |
---|
332 | |
---|
333 | $tl = $this->angle_tick_len ; // Outer len |
---|
334 | $tl2 = $this->angle_tick_len2 ; // Interior len |
---|
335 | |
---|
336 | $this->img->SetColor($this->angle_tick_color); |
---|
337 | $rot90 = $this->img->a == 90 ; |
---|
338 | |
---|
339 | if( $type == POLAR_360 ) { |
---|
340 | |
---|
341 | // Corner angles of the four corners |
---|
342 | $ca1 = atan($h/$w)/M_PI*180; |
---|
343 | $ca2 = 180-$ca1; |
---|
344 | $ca3 = $ca1+180; |
---|
345 | $ca4 = 360-$ca1; |
---|
346 | $end = 360; |
---|
347 | |
---|
348 | while( $a < $end ) { |
---|
349 | $ca = cos($a/180*M_PI); |
---|
350 | $sa = sin($a/180*M_PI); |
---|
351 | $x = $d*$ca; |
---|
352 | $y = $d*$sa; |
---|
353 | $xt=1000;$yt=1000; |
---|
354 | if( $a <= $ca1 || $a >= $ca4 ) { |
---|
355 | $yt = $pos - $w * $y/$x; |
---|
356 | $xt = $xright + $margin; |
---|
357 | if( $rot90 ) { |
---|
358 | $ha = 'center'; |
---|
359 | $va = 'top'; |
---|
360 | } |
---|
361 | else { |
---|
362 | $ha = 'left'; |
---|
363 | $va = 'center'; |
---|
364 | } |
---|
365 | $x1=$xright-$tl2; $x2=$xright+$tl; |
---|
366 | $y1=$y2=$yt; |
---|
367 | } |
---|
368 | elseif( $a > $ca1 && $a < $ca2 ) { |
---|
369 | $xt = $x0 + $h * $x/$y; |
---|
370 | $yt = $ytop - $margin; |
---|
371 | if( $rot90 ) { |
---|
372 | $ha = 'left'; |
---|
373 | $va = 'center'; |
---|
374 | } |
---|
375 | else { |
---|
376 | $ha = 'center'; |
---|
377 | $va = 'bottom'; |
---|
378 | } |
---|
379 | $y1=$ytop+$tl2;$y2=$ytop-$tl; |
---|
380 | $x1=$x2=$xt; |
---|
381 | } |
---|
382 | elseif( $a >= $ca2 && $a <= $ca3 ) { |
---|
383 | $yt = $pos + $w * $y/$x; |
---|
384 | $xt = $xleft - $margin; |
---|
385 | if( $rot90 ) { |
---|
386 | $ha = 'center'; |
---|
387 | $va = 'bottom'; |
---|
388 | } |
---|
389 | else { |
---|
390 | $ha = 'right'; |
---|
391 | $va = 'center'; |
---|
392 | } |
---|
393 | $x1=$xleft+$tl2;$x2=$xleft-$tl; |
---|
394 | $y1=$y2=$yt; |
---|
395 | } |
---|
396 | else { |
---|
397 | $xt = $x0 - $h * $x/$y; |
---|
398 | $yt = $ybottom + $margin; |
---|
399 | if( $rot90 ) { |
---|
400 | $ha = 'right'; |
---|
401 | $va = 'center'; |
---|
402 | } |
---|
403 | else { |
---|
404 | $ha = 'center'; |
---|
405 | $va = 'top'; |
---|
406 | } |
---|
407 | $y1=$ybottom-$tl2;$y2=$ybottom+$tl; |
---|
408 | $x1=$x2=$xt; |
---|
409 | } |
---|
410 | if( $a != 0 && $a != 180 ) { |
---|
411 | $t->Align($ha,$va); |
---|
412 | if( $this->scale->clockwise ) { |
---|
413 | $t->Set(360-$a); |
---|
414 | } |
---|
415 | else { |
---|
416 | $t->Set($a); |
---|
417 | } |
---|
418 | if( $this->show_angle_mark && $t->font_family > 4 ) { |
---|
419 | $a .= SymChar::Get('degree'); |
---|
420 | } |
---|
421 | $t->Stroke($this->img,$xt,$yt); |
---|
422 | if( $this->show_angle_tick ) { |
---|
423 | $this->img->Line($x1,$y1,$x2,$y2); |
---|
424 | } |
---|
425 | } |
---|
426 | $a += $this->angle_step; |
---|
427 | } |
---|
428 | } |
---|
429 | else { |
---|
430 | // POLAR_HALF |
---|
431 | $ca1 = atan($h/$w*2)/M_PI*180; |
---|
432 | $ca2 = 180-$ca1; |
---|
433 | $end = 180; |
---|
434 | while( $a < $end ) { |
---|
435 | $ca = cos($a/180*M_PI); |
---|
436 | $sa = sin($a/180*M_PI); |
---|
437 | $x = $d*$ca; |
---|
438 | $y = $d*$sa; |
---|
439 | if( $a <= $ca1 ) { |
---|
440 | $yt = $pos - $w * $y/$x; |
---|
441 | $xt = $xright + $margin; |
---|
442 | if( $rot90 ) { |
---|
443 | $ha = 'center'; |
---|
444 | $va = 'top'; |
---|
445 | } |
---|
446 | else { |
---|
447 | $ha = 'left'; |
---|
448 | $va = 'center'; |
---|
449 | } |
---|
450 | $x1=$xright-$tl2; $x2=$xright+$tl; |
---|
451 | $y1=$y2=$yt; |
---|
452 | } |
---|
453 | elseif( $a > $ca1 && $a < $ca2 ) { |
---|
454 | $xt = $x0 + 2*$h * $x/$y; |
---|
455 | $yt = $ytop - $margin; |
---|
456 | if( $rot90 ) { |
---|
457 | $ha = 'left'; |
---|
458 | $va = 'center'; |
---|
459 | } |
---|
460 | else { |
---|
461 | $ha = 'center'; |
---|
462 | $va = 'bottom'; |
---|
463 | } |
---|
464 | $y1=$ytop+$tl2;$y2=$ytop-$tl; |
---|
465 | $x1=$x2=$xt; |
---|
466 | } |
---|
467 | elseif( $a >= $ca2 ) { |
---|
468 | $yt = $pos + $w * $y/$x; |
---|
469 | $xt = $xleft - $margin; |
---|
470 | if( $rot90 ) { |
---|
471 | $ha = 'center'; |
---|
472 | $va = 'bottom'; |
---|
473 | } |
---|
474 | else { |
---|
475 | $ha = 'right'; |
---|
476 | $va = 'center'; |
---|
477 | } |
---|
478 | $x1=$xleft+$tl2;$x2=$xleft-$tl; |
---|
479 | $y1=$y2=$yt; |
---|
480 | } |
---|
481 | $t->Align($ha,$va); |
---|
482 | if( $this->show_angle_mark && $t->font_family > 4 ) { |
---|
483 | $a .= SymChar::Get('degree'); |
---|
484 | } |
---|
485 | $t->Set($a); |
---|
486 | $t->Stroke($this->img,$xt,$yt); |
---|
487 | if( $this->show_angle_tick ) { |
---|
488 | $this->img->Line($x1,$y1,$x2,$y2); |
---|
489 | } |
---|
490 | $a += $this->angle_step; |
---|
491 | } |
---|
492 | } |
---|
493 | } |
---|
494 | |
---|
495 | function Stroke($pos,$dummy=true) { |
---|
496 | |
---|
497 | $this->img->SetLineWeight($this->weight); |
---|
498 | $this->img->SetColor($this->color); |
---|
499 | $this->img->SetFont($this->font_family,$this->font_style,$this->font_size); |
---|
500 | if( !$this->hide_line ) { |
---|
501 | $this->img->FilledRectangle($this->img->left_margin,$pos, |
---|
502 | $this->img->width-$this->img->right_margin, |
---|
503 | $pos+$this->weight-1); |
---|
504 | } |
---|
505 | $y=$pos+$this->img->GetFontHeight()+$this->title_margin+$this->title->margin; |
---|
506 | if( $this->title_adjust=="high" ) { |
---|
507 | $this->title->SetPos($this->img->width-$this->img->right_margin,$y,"right","top"); |
---|
508 | } |
---|
509 | elseif( $this->title_adjust=="middle" || $this->title_adjust=="center" ) { |
---|
510 | $this->title->SetPos(($this->img->width-$this->img->left_margin-$this->img->right_margin)/2+$this->img->left_margin, |
---|
511 | $y,"center","top"); |
---|
512 | } |
---|
513 | elseif($this->title_adjust=="low") { |
---|
514 | $this->title->SetPos($this->img->left_margin,$y,"left","top"); |
---|
515 | } |
---|
516 | else { |
---|
517 | JpGraphError::RaiseL(17002,$this->title_adjust); |
---|
518 | //('Unknown alignment specified for X-axis title. ('.$this->title_adjust.')'); |
---|
519 | } |
---|
520 | |
---|
521 | |
---|
522 | if (!$this->hide_labels) { |
---|
523 | $this->StrokeLabels($pos,false); |
---|
524 | } |
---|
525 | $this->img->SetColor($this->radius_tick_color); |
---|
526 | $this->scale->ticks->Stroke($this->img,$this->scale,$pos); |
---|
527 | |
---|
528 | // |
---|
529 | // Mirror the positions for the left side of the scale |
---|
530 | // |
---|
531 | $mid = 2*($this->img->left_margin+$this->img->plotwidth/2); |
---|
532 | $n = count($this->scale->ticks->ticks_pos); |
---|
533 | $i=0; |
---|
534 | while( $i < $n ) { |
---|
535 | $this->scale->ticks->ticks_pos[$i] = |
---|
536 | $mid-$this->scale->ticks->ticks_pos[$i] ; |
---|
537 | ++$i; |
---|
538 | } |
---|
539 | |
---|
540 | $n = count($this->scale->ticks->maj_ticks_pos); |
---|
541 | $i=0; |
---|
542 | while( $i < $n ) { |
---|
543 | $this->scale->ticks->maj_ticks_pos[$i] = |
---|
544 | $mid-$this->scale->ticks->maj_ticks_pos[$i] ; |
---|
545 | ++$i; |
---|
546 | } |
---|
547 | |
---|
548 | $n = count($this->scale->ticks->maj_ticklabels_pos); |
---|
549 | $i=1; |
---|
550 | while( $i < $n ) { |
---|
551 | $this->scale->ticks->maj_ticklabels_pos[$i] = |
---|
552 | $mid-$this->scale->ticks->maj_ticklabels_pos[$i] ; |
---|
553 | ++$i; |
---|
554 | } |
---|
555 | |
---|
556 | // Draw the left side of the scale |
---|
557 | $n = count($this->scale->ticks->ticks_pos); |
---|
558 | $yu = $pos - $this->scale->ticks->direction*$this->scale->ticks->GetMinTickAbsSize(); |
---|
559 | |
---|
560 | |
---|
561 | // Minor ticks |
---|
562 | if( ! $this->scale->ticks->supress_minor_tickmarks ) { |
---|
563 | $i=1; |
---|
564 | while( $i < $n/2 ) { |
---|
565 | $x = round($this->scale->ticks->ticks_pos[$i]) ; |
---|
566 | $this->img->Line($x,$pos,$x,$yu); |
---|
567 | ++$i; |
---|
568 | } |
---|
569 | } |
---|
570 | |
---|
571 | $n = count($this->scale->ticks->maj_ticks_pos); |
---|
572 | $yu = $pos - $this->scale->ticks->direction*$this->scale->ticks->GetMajTickAbsSize(); |
---|
573 | |
---|
574 | |
---|
575 | // Major ticks |
---|
576 | if( ! $this->scale->ticks->supress_tickmarks ) { |
---|
577 | $i=1; |
---|
578 | while( $i < $n/2 ) { |
---|
579 | $x = round($this->scale->ticks->maj_ticks_pos[$i]) ; |
---|
580 | $this->img->Line($x,$pos,$x,$yu); |
---|
581 | ++$i; |
---|
582 | } |
---|
583 | } |
---|
584 | if (!$this->hide_labels) { |
---|
585 | $this->StrokeLabels($pos,false); |
---|
586 | } |
---|
587 | $this->title->Stroke($this->img); |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | class PolarScale extends LinearScale { |
---|
592 | private $graph; |
---|
593 | public $clockwise=false; |
---|
594 | |
---|
595 | function __construct($aMax,$graph,$aClockwise) { |
---|
596 | parent::__construct(0,$aMax,'x'); |
---|
597 | $this->graph = $graph; |
---|
598 | $this->clockwise = $aClockwise; |
---|
599 | } |
---|
600 | |
---|
601 | function SetClockwise($aFlg) { |
---|
602 | $this->clockwise = $aFlg; |
---|
603 | } |
---|
604 | |
---|
605 | function _Translate($v) { |
---|
606 | return parent::Translate($v); |
---|
607 | } |
---|
608 | |
---|
609 | function PTranslate($aAngle,$aRad) { |
---|
610 | |
---|
611 | $m = $this->scale[1]; |
---|
612 | $w = $this->graph->img->plotwidth/2; |
---|
613 | $aRad = $aRad/$m*$w; |
---|
614 | |
---|
615 | $a = $aAngle/180 * M_PI; |
---|
616 | if( $this->clockwise ) { |
---|
617 | $a = 2*M_PI-$a; |
---|
618 | } |
---|
619 | |
---|
620 | $x = cos($a) * $aRad; |
---|
621 | $y = sin($a) * $aRad; |
---|
622 | |
---|
623 | $x += $this->_Translate(0); |
---|
624 | |
---|
625 | if( $this->graph->iType == POLAR_360 ) { |
---|
626 | $y = ($this->graph->img->top_margin + $this->graph->img->plotheight/2) - $y; |
---|
627 | } |
---|
628 | else { |
---|
629 | $y = ($this->graph->img->top_margin + $this->graph->img->plotheight) - $y; |
---|
630 | } |
---|
631 | return array($x,$y); |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | class PolarLogScale extends LogScale { |
---|
636 | private $graph; |
---|
637 | public $clockwise=false; |
---|
638 | |
---|
639 | function __construct($aMax,$graph,$aClockwise=false) { |
---|
640 | parent::__construct(0,$aMax,'x'); |
---|
641 | $this->graph = $graph; |
---|
642 | $this->ticks->SetLabelLogType(LOGLABELS_MAGNITUDE); |
---|
643 | $this->clockwise = $aClockwise; |
---|
644 | |
---|
645 | } |
---|
646 | |
---|
647 | function SetClockwise($aFlg) { |
---|
648 | $this->clockwise = $aFlg; |
---|
649 | } |
---|
650 | |
---|
651 | function PTranslate($aAngle,$aRad) { |
---|
652 | |
---|
653 | if( $aRad == 0 ) |
---|
654 | $aRad = 1; |
---|
655 | $aRad = log10($aRad); |
---|
656 | $m = $this->scale[1]; |
---|
657 | $w = $this->graph->img->plotwidth/2; |
---|
658 | $aRad = $aRad/$m*$w; |
---|
659 | |
---|
660 | $a = $aAngle/180 * M_PI; |
---|
661 | if( $this->clockwise ) { |
---|
662 | $a = 2*M_PI-$a; |
---|
663 | } |
---|
664 | |
---|
665 | $x = cos( $a ) * $aRad; |
---|
666 | $y = sin( $a ) * $aRad; |
---|
667 | |
---|
668 | $x += $w+$this->graph->img->left_margin;//$this->_Translate(0); |
---|
669 | if( $this->graph->iType == POLAR_360 ) { |
---|
670 | $y = ($this->graph->img->top_margin + $this->graph->img->plotheight/2) - $y; |
---|
671 | } |
---|
672 | else { |
---|
673 | $y = ($this->graph->img->top_margin + $this->graph->img->plotheight) - $y; |
---|
674 | } |
---|
675 | return array($x,$y); |
---|
676 | } |
---|
677 | } |
---|
678 | |
---|
679 | class PolarGraph extends Graph { |
---|
680 | public $scale; |
---|
681 | public $axis; |
---|
682 | public $iType=POLAR_360; |
---|
683 | private $iClockwise=false; |
---|
684 | |
---|
685 | function __construct($aWidth=300,$aHeight=200,$aCachedName="",$aTimeOut=0,$aInline=true) { |
---|
686 | parent::__construct($aWidth,$aHeight,$aCachedName,$aTimeOut,$aInline) ; |
---|
687 | $this->SetDensity(TICKD_DENSE); |
---|
688 | $this->SetBox(); |
---|
689 | $this->SetMarginColor('white'); |
---|
690 | } |
---|
691 | |
---|
692 | function SetDensity($aDense) { |
---|
693 | $this->SetTickDensity(TICKD_NORMAL,$aDense); |
---|
694 | } |
---|
695 | |
---|
696 | function SetClockwise($aFlg) { |
---|
697 | $this->scale->SetClockwise($aFlg); |
---|
698 | } |
---|
699 | |
---|
700 | function Set90AndMargin($lm=0,$rm=0,$tm=0,$bm=0) { |
---|
701 | $adj = ($this->img->height - $this->img->width)/2; |
---|
702 | $this->SetAngle(90); |
---|
703 | $lm2 = -$adj + ($lm-$rm+$tm+$bm)/2; |
---|
704 | $rm2 = -$adj + (-$lm+$rm+$tm+$bm)/2; |
---|
705 | $tm2 = $adj + ($tm-$bm+$lm+$rm)/2; |
---|
706 | $bm2 = $adj + (-$tm+$bm+$lm+$rm)/2; |
---|
707 | $this->SetMargin($lm2, $rm2, $tm2, $bm2); |
---|
708 | $this->axis->SetLabelAlign('right','center'); |
---|
709 | } |
---|
710 | |
---|
711 | function SetScale($aScale,$rmax=0,$dummy1=1,$dummy2=1,$dummy3=1) { |
---|
712 | if( $aScale == 'lin' ) { |
---|
713 | $this->scale = new PolarScale($rmax,$this,$this->iClockwise); |
---|
714 | } |
---|
715 | elseif( $aScale == 'log' ) { |
---|
716 | $this->scale = new PolarLogScale($rmax,$this,$this->iClockwise); |
---|
717 | } |
---|
718 | else { |
---|
719 | JpGraphError::RaiseL(17004);//('Unknown scale type for polar graph. Must be "lin" or "log"'); |
---|
720 | } |
---|
721 | |
---|
722 | $this->axis = new PolarAxis($this->img,$this->scale); |
---|
723 | $this->SetMargin(40,40,50,40); |
---|
724 | } |
---|
725 | |
---|
726 | function SetType($aType) { |
---|
727 | $this->iType = $aType; |
---|
728 | } |
---|
729 | |
---|
730 | function SetPlotSize($w,$h) { |
---|
731 | $this->SetMargin(($this->img->width-$w)/2,($this->img->width-$w)/2, |
---|
732 | ($this->img->height-$h)/2,($this->img->height-$h)/2); |
---|
733 | } |
---|
734 | |
---|
735 | // Private methods |
---|
736 | function GetPlotsMax() { |
---|
737 | $n = count($this->plots); |
---|
738 | $m = $this->plots[0]->Max(); |
---|
739 | $i=1; |
---|
740 | while($i < $n) { |
---|
741 | $m = max($this->plots[$i]->Max(),$m); |
---|
742 | ++$i; |
---|
743 | } |
---|
744 | return $m; |
---|
745 | } |
---|
746 | |
---|
747 | function Stroke($aStrokeFileName="") { |
---|
748 | |
---|
749 | // Start by adjusting the margin so that potential titles will fit. |
---|
750 | $this->AdjustMarginsForTitles(); |
---|
751 | |
---|
752 | // If the filename is the predefined value = '_csim_special_' |
---|
753 | // we assume that the call to stroke only needs to do enough |
---|
754 | // to correctly generate the CSIM maps. |
---|
755 | // We use this variable to skip things we don't strictly need |
---|
756 | // to do to generate the image map to improve performance |
---|
757 | // a best we can. Therefor you will see a lot of tests !$_csim in the |
---|
758 | // code below. |
---|
759 | $_csim = ($aStrokeFileName===_CSIM_SPECIALFILE); |
---|
760 | |
---|
761 | // We need to know if we have stroked the plot in the |
---|
762 | // GetCSIMareas. Otherwise the CSIM hasn't been generated |
---|
763 | // and in the case of GetCSIM called before stroke to generate |
---|
764 | // CSIM without storing an image to disk GetCSIM must call Stroke. |
---|
765 | $this->iHasStroked = true; |
---|
766 | |
---|
767 | //Check if we should autoscale axis |
---|
768 | if( !$this->scale->IsSpecified() && count($this->plots)>0 ) { |
---|
769 | $max = $this->GetPlotsMax(); |
---|
770 | $t1 = $this->img->plotwidth; |
---|
771 | $this->img->plotwidth /= 2; |
---|
772 | $t2 = $this->img->left_margin; |
---|
773 | $this->img->left_margin += $this->img->plotwidth+1; |
---|
774 | $this->scale->AutoScale($this->img,0,$max, |
---|
775 | $this->img->plotwidth/$this->xtick_factor/2); |
---|
776 | $this->img->plotwidth = $t1; |
---|
777 | $this->img->left_margin = $t2; |
---|
778 | } |
---|
779 | else { |
---|
780 | // The tick calculation will use the user suplied min/max values to determine |
---|
781 | // the ticks. If auto_ticks is false the exact user specifed min and max |
---|
782 | // values will be used for the scale. |
---|
783 | // If auto_ticks is true then the scale might be slightly adjusted |
---|
784 | // so that the min and max values falls on an even major step. |
---|
785 | //$min = 0; |
---|
786 | $max = $this->scale->scale[1]; |
---|
787 | $t1 = $this->img->plotwidth; |
---|
788 | $this->img->plotwidth /= 2; |
---|
789 | $t2 = $this->img->left_margin; |
---|
790 | $this->img->left_margin += $this->img->plotwidth+1; |
---|
791 | $this->scale->AutoScale($this->img,0,$max, |
---|
792 | $this->img->plotwidth/$this->xtick_factor/2); |
---|
793 | $this->img->plotwidth = $t1; |
---|
794 | $this->img->left_margin = $t2; |
---|
795 | } |
---|
796 | |
---|
797 | if( $this->iType == POLAR_180 ) { |
---|
798 | $pos = $this->img->height - $this->img->bottom_margin; |
---|
799 | } |
---|
800 | else { |
---|
801 | $pos = $this->img->plotheight/2 + $this->img->top_margin; |
---|
802 | } |
---|
803 | |
---|
804 | if( !$_csim ) { |
---|
805 | $this->StrokePlotArea(); |
---|
806 | } |
---|
807 | |
---|
808 | $this->iDoClipping = true; |
---|
809 | |
---|
810 | if( $this->iDoClipping ) { |
---|
811 | $oldimage = $this->img->CloneCanvasH(); |
---|
812 | } |
---|
813 | |
---|
814 | if( !$_csim ) { |
---|
815 | $this->axis->StrokeGrid($pos); |
---|
816 | } |
---|
817 | |
---|
818 | // Stroke all plots for Y1 axis |
---|
819 | for($i=0; $i < count($this->plots); ++$i) { |
---|
820 | $this->plots[$i]->Stroke($this->img,$this->scale); |
---|
821 | } |
---|
822 | |
---|
823 | |
---|
824 | if( $this->iDoClipping ) { |
---|
825 | // Clipping only supports graphs at 0 and 90 degrees |
---|
826 | if( $this->img->a == 0 ) { |
---|
827 | $this->img->CopyCanvasH($oldimage,$this->img->img, |
---|
828 | $this->img->left_margin,$this->img->top_margin, |
---|
829 | $this->img->left_margin,$this->img->top_margin, |
---|
830 | $this->img->plotwidth+1,$this->img->plotheight+1); |
---|
831 | } |
---|
832 | elseif( $this->img->a == 90 ) { |
---|
833 | $adj1 = round(($this->img->height - $this->img->width)/2); |
---|
834 | $adj2 = round(($this->img->width - $this->img->height)/2); |
---|
835 | $lm = $this->img->left_margin; |
---|
836 | $rm = $this->img->right_margin; |
---|
837 | $tm = $this->img->top_margin; |
---|
838 | $bm = $this->img->bottom_margin; |
---|
839 | $this->img->CopyCanvasH($oldimage,$this->img->img, |
---|
840 | $adj2 + round(($lm-$rm+$tm+$bm)/2), |
---|
841 | $adj1 + round(($tm-$bm+$lm+$rm)/2), |
---|
842 | $adj2 + round(($lm-$rm+$tm+$bm)/2), |
---|
843 | $adj1 + round(($tm-$bm+$lm+$rm)/2), |
---|
844 | $this->img->plotheight+1, |
---|
845 | $this->img->plotwidth+1); |
---|
846 | } |
---|
847 | $this->img->Destroy(); |
---|
848 | $this->img->SetCanvasH($oldimage); |
---|
849 | } |
---|
850 | |
---|
851 | if( !$_csim ) { |
---|
852 | $this->axis->Stroke($pos); |
---|
853 | $this->axis->StrokeAngleLabels($pos,$this->iType); |
---|
854 | } |
---|
855 | |
---|
856 | if( !$_csim ) { |
---|
857 | $this->StrokePlotBox(); |
---|
858 | $this->footer->Stroke($this->img); |
---|
859 | |
---|
860 | // The titles and legends never gets rotated so make sure |
---|
861 | // that the angle is 0 before stroking them |
---|
862 | $aa = $this->img->SetAngle(0); |
---|
863 | $this->StrokeTitles(); |
---|
864 | } |
---|
865 | |
---|
866 | for($i=0; $i < count($this->plots) ; ++$i ) { |
---|
867 | $this->plots[$i]->Legend($this); |
---|
868 | } |
---|
869 | |
---|
870 | $this->legend->Stroke($this->img); |
---|
871 | |
---|
872 | if( !$_csim ) { |
---|
873 | |
---|
874 | $this->StrokeTexts(); |
---|
875 | $this->img->SetAngle($aa); |
---|
876 | |
---|
877 | // Draw an outline around the image map |
---|
878 | if(_JPG_DEBUG) |
---|
879 | $this->DisplayClientSideaImageMapAreas(); |
---|
880 | |
---|
881 | // If the filename is given as the special "__handle" |
---|
882 | // then the image handler is returned and the image is NOT |
---|
883 | // streamed back |
---|
884 | if( $aStrokeFileName == _IMG_HANDLER ) { |
---|
885 | return $this->img->img; |
---|
886 | } |
---|
887 | else { |
---|
888 | // Finally stream the generated picture |
---|
889 | $this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,$aStrokeFileName); |
---|
890 | } |
---|
891 | } |
---|
892 | } |
---|
893 | } |
---|
894 | |
---|
895 | |
---|
896 | |
---|
897 | ?> |
---|