Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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