2 /*=======================================================================
3 // File: JPGRAPH_POLAR.PHP
4 // Description: Polar plot extension for JpGraph
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_polar.php,v 1.2.2.1 2003/08/07 19:31:34 aditus Exp $
9 // License: This code is released under QPL
10 // Copyright (C) 2003 Johan Persson
11 //========================================================================
13 require_once "jpgraph_log.php";
16 DEFINE('POLAR_360',1);
17 DEFINE('POLAR_180',2);
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
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.
31 //--------------------------------------------------------------------------
33 //--------------------------------------------------------------------------
36 var $iColor='navy',$iFillColor='';
39 var $legendcsimtarget='';
40 var $legendcsimalt='';
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;
47 function PolarPlot($aData) {
50 JpGraphError::Raise('Polar plots must have an even number of data point. Each data point is a tuple (angle,radius).');
52 $this->numpoints = $n/2;
53 $this->coord = $aData;
54 $this->mark = new PlotMark();
57 function SetWeight($aWeight) {
58 $this->iLineWeight = $aWeight;
61 function SetColor($aColor){
62 $this->iColor = $aColor;
65 function SetFillColor($aColor){
66 $this->iFillColor = $aColor;
72 while( $i < $this->numpoints ) {
73 $m = max($m,$this->coord[2*$i+1]);
78 // Set href targets for CSIM
79 function SetCSIMTargets($aTargets,$aAlts=null) {
80 $this->csimtargets=$aTargets;
81 $this->csimalts=$aAlts;
84 // Get all created areas
85 function GetCSIMareas() {
86 return $this->csimareas;
89 function SetLegend($aLegend,$aCSIM="",$aCSIMAlt="") {
90 $this->legend = $aLegend;
91 $this->legendcsimtarget = $aCSIM;
92 $this->legendcsimalt = $aCSIMAlt;
97 function Legend(&$aGraph) {
98 $color = $this->iColor ;
99 if( $this->legend != "" ) {
100 if( $this->iFillColor!='' ) {
101 $color = $this->iFillColor;
102 $aGraph->legend->Add($this->legend,$color,$this->mark,0,
103 $this->legendcsimtarget,$this->legendcsimalt);
106 $aGraph->legend->Add($this->legend,$color,$this->mark,$this->line_style,
107 $this->legendcsimtarget,$this->legendcsimalt);
112 function Stroke($img,$scale) {
117 while($i < $this->numpoints) {
118 list($x1,$y1) = $scale->PTranslate($this->coord[2*$i],$this->coord[2*$i+1]);
122 if( isset($this->csimtargets[$i]) ) {
123 $this->mark->SetCSIMTarget($this->csimtargets[$i]);
124 $this->mark->SetCSIMAlt($this->csimalts[$i]);
125 $this->mark->SetCSIMAltVal($this->coord[2*$i], $this->coord[2*$i+1]);
126 $this->mark->Stroke($img,$x1,$y1);
127 $this->csimareas .= $this->mark->GetCSIMAreas();
130 $this->mark->Stroke($img,$x1,$y1);
135 if( $this->iFillColor != '' ) {
136 $img->SetColor($this->iFillColor);
137 $img->FilledPolygon($p);
139 $img->SetLineWeight($this->iLineWeight);
140 $img->SetColor($this->iColor);
141 $img->Polygon($p,$this->iFillColor!='');
145 //--------------------------------------------------------------------------
147 //--------------------------------------------------------------------------
148 class PolarAxis extends Axis {
149 var $angle_step=15,$angle_color='lightgray',$angle_label_color='black';
150 var $angle_fontfam=FF_FONT1,$angle_fontstyle=FS_NORMAL,$angle_fontsize=10;
151 var $angle_fontcolor = 'navy';
152 var $gridminor_color='lightgray',$gridmajor_color='lightgray';
153 var $show_minor_grid = false, $show_major_grid = true ;
154 var $show_angle_mark=true, $show_angle_grid=true, $show_angle_label=true;
155 var $angle_tick_len=3, $angle_tick_len2=3, $angle_tick_color='black';
156 var $show_angle_tick=true;
157 var $radius_tick_color='black';
159 function PolarAxis(&$img,&$aScale) {
160 parent::Axis($img,$aScale);
163 function ShowAngleDegreeMark($aFlg=true) {
164 $this->show_angle_mark = $aFlg;
167 function SetAngleStep($aStep) {
168 $this->angle_step=$aStep;
171 function HideTicks($aFlg=true,$aAngleFlg=true) {
172 parent::HideTicks($aFlg,$aFlg);
173 $this->show_angle_tick = !$aAngleFlg;
176 function ShowAngleLabel($aFlg=true) {
177 $this->show_angle_label = $aFlg;
180 function ShowGrid($aMajor=true,$aMinor=false,$aAngle=true) {
181 $this->show_minor_grid = $aMinor;
182 $this->show_major_grid = $aMajor;
183 $this->show_angle_grid = $aAngle ;
186 function SetAngleFont($aFontFam,$aFontStyle=FS_NORMAL,$aFontSize=10) {
187 $this->angle_fontfam = $aFontFam;
188 $this->angle_fontstyle = $aFontStyle;
189 $this->angle_fontsize = $aFontSize;
192 function SetColor($aColor,$aRadColor='',$aAngleColor='') {
193 if( $aAngleColor == '' )
194 $aAngleColor=$aColor;
195 parent::SetColor($aColor,$aRadColor);
196 $this->angle_fontcolor = $aAngleColor;
199 function SetGridColor($aMajorColor,$aMinorColor='',$aAngleColor='') {
200 if( $aMinorColor == '' )
201 $aMinorColor = $aMajorColor;
202 if( $aAngleColor == '' )
203 $aAngleColor = $aMajorColor;
205 $this->gridminor_color = $aMinorColor;
206 $this->gridmajor_color = $aMajorColor;
207 $this->angle_color = $aAngleColor;
210 function SetTickColors($aRadColor,$aAngleColor='') {
211 $this->radius_tick_color = $aRadColor;
212 $this->angle_tick_color = $aAngleColor;
216 function StrokeGrid($pos) {
217 $x = round($this->img->left_margin + $this->img->plotwidth/2);
218 $this->scale->ticks->Stroke($this->img,$this->scale,$pos);
220 // Stroke the minor arcs
222 $p = $this->scale->ticks->ticks_pos;
225 $this->img->SetColor($this->gridminor_color);
229 if( $this->show_minor_grid ) {
230 $this->img->Circle($x,$pos,$r);
235 $limit = max($this->img->plotwidth,$this->img->plotheight)*1.4 ;
236 while( $r < $limit ) {
239 $r = $off + round($p[$i]-$x+1);
240 while( $r < $limit && $i < $n ) {
243 if( $this->show_minor_grid ) {
244 $this->img->Circle($x,$pos,$r);
250 // Stroke the major arcs
251 if( $this->show_major_grid ) {
252 // First determine how many minor step on
253 // every major step. We have recorded the minor radius
254 // in pmin and use these values. This is done in order
255 // to avoid rounding errors if we were to recalculate the
256 // different major radius.
257 $pmaj = $this->scale->ticks->maj_ticks_pos;
258 $p = $this->scale->ticks->ticks_pos;
259 if( $this->scale->name == 'lin' ) {
260 $step=round(($pmaj[1] - $pmaj[0])/($p[1] - $p[0]));
265 $n = round(count($pmin)/$step);
267 $this->img->SetColor($this->gridmajor_color);
268 $limit = max($this->img->plotwidth,$this->img->plotheight)*1.4 ;
271 $r = $pmin[$i*$step];
272 while( $r < $limit && $i < $n ) {
273 $r = $pmin[$i*$step];
274 $this->img->Circle($x,$pos,$r);
280 if( $this->show_angle_grid ) {
281 $this->img->SetColor($this->angle_color);
282 $d = max($this->img->plotheight,$this->img->plotwidth)*1.4 ;
284 $p = $this->scale->ticks->ticks_pos;
285 $start_radius = $p[1]-$x;
287 if( $a == 90 || $a == 270 ) {
288 // Make sure there are no rounding problem with
289 // exactly vertical lines
290 $this->img->Line($x+$start_radius*cos($a/180*M_PI)+1,
291 $pos-$start_radius*sin($a/180*M_PI),
292 $x+$start_radius*cos($a/180*M_PI)+1,
293 $pos-$d*sin($a/180*M_PI));
297 $this->img->Line($x+$start_radius*cos($a/180*M_PI)+1,
298 $pos-$start_radius*sin($a/180*M_PI),
299 $x+$d*cos($a/180*M_PI),
300 $pos-$d*sin($a/180*M_PI));
302 $a += $this->angle_step;
307 function StrokeAngleLabels($pos,$type) {
309 if( !$this->show_angle_label )
312 $x0 = round($this->img->left_margin+$this->img->plotwidth/2)+1;
314 $d = max($this->img->plotwidth,$this->img->plotheight)*1.42;
315 $a = $this->angle_step;
317 $t->SetColor($this->angle_fontcolor);
318 $t->SetFont($this->angle_fontfam,$this->angle_fontstyle,$this->angle_fontsize);
319 $xright = $this->img->width - $this->img->right_margin;
320 $ytop = $this->img->top_margin;
321 $xleft = $this->img->left_margin;
322 $ybottom = $this->img->height - $this->img->bottom_margin;
325 $w = $this->img->plotwidth/2;
326 $h = $this->img->plotheight/2;
327 $xt = $x0; $yt = $pos;
330 $tl = $this->angle_tick_len ; // Outer len
331 $tl2 = $this->angle_tick_len2 ; // Interior len
333 $this->img->SetColor($this->angle_tick_color);
334 $rot90 = $this->img->a == 90 ;
336 if( $type == POLAR_360 ) {
337 $ca1 = atan($h/$w)/M_PI*180;
343 $ca = cos($a/180*M_PI);
344 $sa = sin($a/180*M_PI);
348 if( $a <= $ca1 || $a >= $ca4 ) {
349 $yt = $pos - $w * $y/$x;
350 $xt = $xright + $margin;
359 $x1=$xright-$tl2; $x2=$xright+$tl;
362 elseif( $a > $ca1 && $a < $ca2 ) {
363 $xt = $x0 + $h * $x/$y;
364 $yt = $ytop - $margin;
373 $y1=$ytop+$tl2;$y2=$ytop-$tl;
376 elseif( $a >= $ca2 && $a <= $ca3 ) {
377 $yt = $pos + $w * $y/$x;
378 $xt = $xleft - $margin;
387 $x1=$xleft+$tl2;$x2=$xleft-$tl;
391 $xt = $x0 - $h * $x/$y;
392 $yt = $ybottom + $margin;
401 $y1=$ybottom-$tl2;$y2=$ybottom+$tl;
404 if( $a != 0 && $a != 180 ) {
406 if( $this->show_angle_mark )
409 $t->Stroke($this->img,$xt,$yt);
410 if( $this->show_angle_tick )
411 $this->img->Line($x1,$y1,$x2,$y2);
413 $a += $this->angle_step;
418 $ca1 = atan($h/$w*2)/M_PI*180;
422 $ca = cos($a/180*M_PI);
423 $sa = sin($a/180*M_PI);
427 $yt = $pos - $w * $y/$x;
428 $xt = $xright + $margin;
437 $x1=$xright-$tl2; $x2=$xright+$tl;
440 elseif( $a > $ca1 && $a < $ca2 ) {
441 $xt = $x0 + 2*$h * $x/$y;
442 $yt = $ytop - $margin;
451 $y1=$ytop+$tl2;$y2=$ytop-$tl;
454 elseif( $a >= $ca2 ) {
455 $yt = $pos + $w * $y/$x;
456 $xt = $xleft - $margin;
465 $x1=$xleft+$tl2;$x2=$xleft-$tl;
469 if( $this->show_angle_mark )
472 $t->Stroke($this->img,$xt,$yt);
473 if( $this->show_angle_tick )
474 $this->img->Line($x1,$y1,$x2,$y2);
475 $a += $this->angle_step;
480 function Stroke($pos) {
482 $this->img->SetLineWeight($this->weight);
483 $this->img->SetColor($this->color);
484 $this->img->SetFont($this->font_family,$this->font_style,$this->font_size);
485 if( !$this->hide_line )
486 $this->img->FilledRectangle($this->img->left_margin,$pos,
487 $this->img->width-$this->img->right_margin,$pos+$this->weight-1);
488 $y=$pos+$this->img->GetFontHeight()+$this->title_margin+$this->title->margin;
489 if( $this->title_adjust=="high" )
490 $this->title->Pos($this->img->width-$this->img->right_margin,$y,"right","top");
491 elseif( $this->title_adjust=="middle" || $this->title_adjust=="center" )
492 $this->title->Pos(($this->img->width-$this->img->left_margin-
493 $this->img->right_margin)/2+$this->img->left_margin,
495 elseif($this->title_adjust=="low")
496 $this->title->Pos($this->img->left_margin,$y,"left","top");
498 JpGraphError::Raise('Unknown alignment specified for X-axis title. ('.
499 $this->title_adjust.')');
502 $this->StrokeLabels($pos,false);
503 $this->img->SetColor($this->radius_tick_color);
504 $this->scale->ticks->Stroke($this->img,$this->scale,$pos);
507 // Mirror the positions for the left side of the scale
509 $mid = 2*($this->img->left_margin+$this->img->plotwidth/2);
510 $n = count($this->scale->ticks->ticks_pos);
513 $this->scale->ticks->ticks_pos[$i] =
514 $mid-$this->scale->ticks->ticks_pos[$i] ;
518 $n = count($this->scale->ticks->maj_ticks_pos);
521 $this->scale->ticks->maj_ticks_pos[$i] =
522 $mid-$this->scale->ticks->maj_ticks_pos[$i] ;
526 $n = count($this->scale->ticks->maj_ticklabels_pos);
529 $this->scale->ticks->maj_ticklabels_pos[$i] =
530 $mid-$this->scale->ticks->maj_ticklabels_pos[$i] ;
534 // Draw the left side of the scale
535 $n = count($this->scale->ticks->ticks_pos);
536 $yu = $pos - $this->scale->ticks->direction*$this->scale->ticks->GetMinTickAbsSize();
542 $x = round($this->scale->ticks->ticks_pos[$i]) ;
543 $this->img->Line($x,$pos,$x,$yu);
547 $n = count($this->scale->ticks->maj_ticks_pos);
548 $yu = $pos - $this->scale->ticks->direction*$this->scale->ticks->GetMajTickAbsSize();
554 $x = round($this->scale->ticks->maj_ticks_pos[$i]) ;
555 $this->img->Line($x,$pos,$x,$yu);
559 $this->StrokeLabels($pos,false);
560 $this->title->Stroke($this->img);
564 class PolarScale extends LinearScale {
566 function PolarScale($aMax=0,&$graph) {
567 parent::LinearScale(0,$aMax,'x');
568 $this->graph = &$graph;
571 function _Translate($v) {
572 return parent::Translate($v);
575 function PTranslate($aAngle,$aRad) {
577 $m = $this->scale[1];
578 $w = $this->graph->img->plotwidth/2;
581 $x = cos( $aAngle/180 * M_PI ) * $aRad;
582 $y = sin( $aAngle/180 * M_PI ) * $aRad;
584 $x += $this->_Translate(0);
586 if( $this->graph->iType == POLAR_360 ) {
587 $y = ($this->graph->img->top_margin + $this->graph->img->plotheight/2) - $y;
590 $y = ($this->graph->img->top_margin + $this->graph->img->plotheight) - $y;
596 class PolarLogScale extends LogScale {
598 function PolarLogScale($aMax=1,&$graph) {
599 parent::LogScale(0,$aMax,'x');
600 $this->graph = &$graph;
601 $this->ticks->SetLabelLogType(LOGLABELS_MAGNITUDE);
605 function PTranslate($aAngle,$aRad) {
609 $aRad = log10($aRad);
610 $m = $this->scale[1];
611 $w = $this->graph->img->plotwidth/2;
614 $x = cos( $aAngle/180 * M_PI ) * $aRad;
615 $y = sin( $aAngle/180 * M_PI ) * $aRad;
617 $x += $w+$this->graph->img->left_margin;//$this->_Translate(0);
618 if( $this->graph->iType == POLAR_360 ) {
619 $y = ($this->graph->img->top_margin + $this->graph->img->plotheight/2) - $y;
622 $y = ($this->graph->img->top_margin + $this->graph->img->plotheight) - $y;
628 class PolarGraph extends Graph {
630 var $iType=POLAR_360;
633 function PolarGraph($aWidth=300,$aHeight=200,$aCachedName="",$aTimeOut=0,$aInline=true) {
634 parent::Graph($aWidth,$aHeight,$aCachedName,$aTimeOut,$aInline) ;
635 $this->SetDensity(TICKD_DENSE);
637 $this->SetMarginColor('white');
640 function SetDensity($aDense) {
641 $this->SetTickDensity(TICKD_NORMAL,$aDense);
644 function Set90AndMargin($lm=0,$rm=0,$tm=0,$bm=0) {
645 $adj = ($this->img->height - $this->img->width)/2;
647 $this->img->SetMargin($lm-$adj,$rm-$adj,$tm+$adj,$bm+$adj);
648 $this->img->SetCenter(floor($this->img->width/2),floor($this->img->height/2));
649 $this->axis->SetLabelAlign('right','center');
650 //JpGraphError::Raise('Set90AndMargin() is not supported for polar graphs.');
653 function SetScale($aScale,$rmax=0) {
654 if( $aScale == 'lin' )
655 $this->scale = new PolarScale($rmax,$this);
656 elseif( $aScale == 'log' ) {
657 $this->scale = new PolarLogScale($rmax,$this);
660 JpGraphError::Raise('Unknown scale type for polar graph. Must be "lin" or "log"');
663 $this->scale->Init($this->img);
664 $this->axis = new PolarAxis($this->img,$this->scale);
665 $this->SetMargin(40,40,50,40);
668 function SetType($aType) {
669 $this->iType = $aType;
672 function SetPlotSize($w,$h) {
673 $this->SetMargin(($this->img->width-$w)/2,($this->img->width-$w)/2,
674 ($this->img->height-$h)/2,($this->img->height-$h)/2);
678 function GetPlotsMax() {
679 $n = count($this->plots);
680 $m = $this->plots[0]->Max();
683 $m = max($this->plots[$i]->Max());
689 function Stroke($aStrokeFileName="") {
691 // Start by adjusting the margin so that potential titles will fit.
692 $this->AdjustMarginsForTitles();
694 // If the filename is the predefined value = '_csim_special_'
695 // we assume that the call to stroke only needs to do enough
696 // to correctly generate the CSIM maps.
697 // We use this variable to skip things we don't strictly need
698 // to do to generate the image map to improve performance
699 // a best we can. Therefor you will see a lot of tests !$_csim in the
701 $_csim = ($aStrokeFileName===_CSIM_SPECIALFILE);
703 // We need to know if we have stroked the plot in the
704 // GetCSIMareas. Otherwise the CSIM hasn't been generated
705 // and in the case of GetCSIM called before stroke to generate
706 // CSIM without storing an image to disk GetCSIM must call Stroke.
707 $this->iHasStroked = true;
709 //Check if we should autoscale axis
710 if( !$this->scale->IsSpecified() && count($this->plots)>0 ) {
711 $max = $this->GetPlotsMax();
712 $t1 = $this->img->plotwidth;
713 $this->img->plotwidth /= 2;
714 $t2 = $this->img->left_margin;
715 $this->img->left_margin += $this->img->plotwidth+1;
716 $this->scale->AutoScale($this->img,0,$max,
717 $this->img->plotwidth/$this->xtick_factor/2);
718 $this->img->plotwidth = $t1;
719 $this->img->left_margin = $t2;
722 // The tick calculation will use the user suplied min/max values to determine
723 // the ticks. If auto_ticks is false the exact user specifed min and max
724 // values will be used for the scale.
725 // If auto_ticks is true then the scale might be slightly adjusted
726 // so that the min and max values falls on an even major step.
728 $max = $this->scale->scale[1];
729 $t1 = $this->img->plotwidth;
730 $this->img->plotwidth /= 2;
731 $t2 = $this->img->left_margin;
732 $this->img->left_margin += $this->img->plotwidth+1;
733 $this->scale->AutoScale($this->img,0,$max,
734 $this->img->plotwidth/$this->xtick_factor/2);
735 $this->img->plotwidth = $t1;
736 $this->img->left_margin = $t2;
739 if( $this->iType == POLAR_180 )
740 $pos = $this->img->height - $this->img->bottom_margin;
742 $pos = $this->img->plotheight/2 + $this->img->top_margin;
746 $this->StrokePlotArea();
749 $this->iDoClipping = true;
751 if( $this->iDoClipping ) {
752 $oldimage = $this->img->CloneCanvasH();
756 $this->axis->StrokeGrid($pos);
759 // Stroke all plots for Y1 axis
760 for($i=0; $i < count($this->plots); ++$i) {
761 $this->plots[$i]->Stroke($this->img,$this->scale);
765 if( $this->iDoClipping ) {
766 // Clipping only supports graphs at 0 and 90 degrees
767 if( $this->img->a == 0 ) {
768 $this->img->CopyCanvasH($oldimage,$this->img->img,
769 $this->img->left_margin,$this->img->top_margin,
770 $this->img->left_margin,$this->img->top_margin,
771 $this->img->plotwidth+1,$this->img->plotheight+1);
773 elseif( $this->img->a == 90 ) {
774 $adj = round(($this->img->height - $this->img->width)/2);
775 $this->img->CopyCanvasH($oldimage,$this->img->img,
776 $this->img->bottom_margin-$adj,$this->img->left_margin+$adj,
777 $this->img->bottom_margin-$adj,$this->img->left_margin+$adj,
778 $this->img->plotheight,$this->img->plotwidth);
780 $this->img->Destroy();
781 $this->img->SetCanvasH($oldimage);
785 $this->axis->Stroke($pos);
786 $this->axis->StrokeAngleLabels($pos,$this->iType);
790 $this->StrokePlotBox();
791 $this->footer->Stroke($this->img);
793 // The titles and legends never gets rotated so make sure
794 // that the angle is 0 before stroking them
795 $aa = $this->img->SetAngle(0);
796 $this->StrokeTitles();
799 for($i=0; $i < count($this->plots) ; ++$i ) {
800 $this->plots[$i]->Legend($this);
803 $this->legend->Stroke($this->img);
807 $this->StrokeTexts();
808 $this->img->SetAngle($aa);
810 // Draw an outline around the image map
812 $this->DisplayClientSideaImageMapAreas();
814 // Adjust the appearance of the image
815 $this->AdjustSaturationBrightnessContrast();
817 // If the filename is given as the special "__handle"
818 // then the image handler is returned and the image is NOT
820 if( $aStrokeFileName == _IMG_HANDLER ) {
821 return $this->img->img;
824 // Finally stream the generated picture
825 $this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,