2 /*=======================================================================
3 // File: JPGRAPH_RADAR.PHP
4 // Description: Radar plot extension for JpGraph
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_radar.php,v 1.7 2003/03/20 19:57:22 aditus Exp $
9 // License: This code is released under QPL
10 // Copyright (C) 2001,2002 Johan Persson
11 //========================================================================
15 class RadarLogTicks extends Ticks {
18 function RadarLogTicks() {
23 // TODO: Add Argument grid
24 function Stroke(&$aImg,&$grid,$aPos,$aAxisAngle,&$aScale,&$aMajPos,&$aMajLabel) {
25 $start = $aScale->GetMinVal();
26 $limit = $aScale->GetMaxVal();
27 $nextMajor = 10*$start;
28 $step = $nextMajor / 10.0;
32 $dx_maj=round(sin($aAxisAngle)*$ticklen_maj);
33 $dy_maj=round(cos($aAxisAngle)*$ticklen_maj);
35 $dx_min=round(sin($aAxisAngle)*$ticklen_min);
36 $dy_min=round(cos($aAxisAngle)*$ticklen_min);
41 if( $this->supress_first )
45 $yr=$aScale->RelTranslate($start);
46 $xt=round($yr*cos($aAxisAngle))+$aScale->scale_abs[0];
47 $yt=$aPos-round($yr*sin($aAxisAngle));
48 $aMajPos[]=$xt+2*$dx_maj;
49 $aMajPos[]=$yt-$aImg->GetFontheight()/2;
53 $aImg->SetLineWeight($this->weight);
55 for($y=$start; $y<=$limit; $y+=$step,++$count ) {
56 $yr=$aScale->RelTranslate($y);
57 $xt=round($yr*cos($aAxisAngle))+$aScale->scale_abs[0];
58 $yt=$aPos-round($yr*sin($aAxisAngle));
59 if( $count % 10 == 0 ) {
62 $aMajPos[]=$xt+2*$dx_maj;
63 $aMajPos[]=$yt-$aImg->GetFontheight()/2;
64 if( !$this->supress_tickmarks ) {
65 if( $this->majcolor!="" ) $aImg->PushColor($this->majcolor);
66 $aImg->Line($xt+$dx_maj,$yt+$dy_maj,$xt-$dx_maj,$yt-$dy_maj);
67 if( $this->majcolor!="" ) $aImg->PopColor();
69 $aMajLabel[]=$nextMajor;
75 if( !$this->supress_minor_tickmarks ) {
76 if( $this->mincolor!="" ) $aImg->PushColor($this->mincolor);
77 $aImg->Line($xt+$dx_min,$yt+$dy_min,$xt-$dx_min,$yt-$dy_min);
78 if( $this->mincolor!="" ) $aImg->PopColor();
84 class RadarLinearTicks extends LinearTicks {
87 function RadarLinearTicks() {
94 // TODO: Add argument grid
95 function Stroke(&$aImg,&$grid,$aPos,$aAxisAngle,&$aScale,&$aMajPos,&$aMajLabel) {
96 // Prepare to draw linear ticks
97 $maj_step_abs = abs($aScale->scale_factor*$this->major_step);
98 $min_step_abs = abs($aScale->scale_factor*$this->minor_step);
99 $nbrmaj = floor(($aScale->world_abs_size)/$maj_step_abs);
100 $nbrmin = floor(($aScale->world_abs_size)/$min_step_abs);
101 $skip = round($nbrmin/$nbrmaj); // Don't draw minor ontop of major
104 $ticklen2=$this->major_abs_size;
105 $dx=round(sin($aAxisAngle)*$ticklen2);
106 $dy=round(cos($aAxisAngle)*$ticklen2);
107 $label=$aScale->scale[0]+$this->major_step;
109 $aImg->SetLineWeight($this->weight);
111 for($i=1; $i<=$nbrmaj; ++$i) {
112 $xt=round($i*$maj_step_abs*cos($aAxisAngle))+$aScale->scale_abs[0];
113 $yt=$aPos-round($i*$maj_step_abs*sin($aAxisAngle));
115 $label += $this->major_step;
118 $aMajPos[($i-1)*2]=$xt+2*$dx;
119 $aMajPos[($i-1)*2+1]=$yt-$aImg->GetFontheight()/2;
120 if( !$this->supress_tickmarks ) {
121 if( $this->majcolor!="" ) $aImg->PushColor($this->majcolor);
122 $aImg->Line($xt+$dx,$yt+$dy,$xt-$dx,$yt-$dy);
123 if( $this->majcolor!="" ) $aImg->PopColor();
128 $ticklen2=$this->minor_abs_size;
129 $dx=round(sin($aAxisAngle)*$ticklen2);
130 $dy=round(cos($aAxisAngle)*$ticklen2);
131 if( !$this->supress_tickmarks && !$this->supress_minor_tickmarks) {
132 if( $this->mincolor!="" ) $aImg->PushColor($this->mincolor);
133 for($i=1; $i<=$nbrmin; ++$i) {
134 if( ($i % $skip) == 0 ) continue;
135 $xt=round($i*$min_step_abs*cos($aAxisAngle))+$aScale->scale_abs[0];
136 $yt=$aPos-round($i*$min_step_abs*sin($aAxisAngle));
137 $aImg->Line($xt+$dx,$yt+$dy,$xt-$dx,$yt-$dy);
139 if( $this->mincolor!="" ) $aImg->PopColor();
146 //===================================================
148 // Description: Implements axis for the spider graph
149 //===================================================
150 class RadarAxis extends Axis {
151 var $title_color="navy";
155 function RadarAxis(&$img,&$aScale,$color=array(0,0,0)) {
156 parent::Axis($img,$aScale,$color);
157 $this->len=$img->plotheight;
158 $this->title = new Text();
159 $this->title->SetFont(FF_FONT1,FS_BOLD);
160 $this->color = array(0,0,0);
164 function SetTickLabels($l) {
165 $this->ticks_label = $l;
170 // $pos = Vertical position of axis
171 // $aAxisAngle = Axis angle
172 // $grid = Returns an array with positions used to draw the grid
173 // $lf = Label flag, TRUE if the axis should have labels
174 function Stroke($pos,$aAxisAngle,&$grid,$title,$lf) {
175 $this->img->SetColor($this->color);
177 // Determine end points for the axis
178 $x=round($this->scale->world_abs_size*cos($aAxisAngle)+$this->scale->scale_abs[0]);
179 $y=round($pos-$this->scale->world_abs_size*sin($aAxisAngle));
182 $this->img->SetColor($this->color);
183 $this->img->SetLineWeight($this->weight);
185 $this->img->Line($this->scale->scale_abs[0],$pos,$x,$y);
187 $this->scale->ticks->Stroke($this->img,$grid,$pos,$aAxisAngle,$this->scale,$majpos,$majlabel);
190 if( $lf && !$this->hide ) {
191 $this->img->SetFont($this->font_family,$this->font_style,$this->font_size);
192 $this->img->SetTextAlign("left","top");
193 $this->img->SetColor($this->color);
195 // majpos contsins (x,y) coordinates for labels
196 for($i=0; $i<count($majpos)/2; ++$i) {
197 if( $this->ticks_label != null )
198 $this->img->StrokeText($majpos[$i*2],$majpos[$i*2+1],$this->ticks_label[$i]);
200 $this->img->StrokeText($majpos[$i*2],$majpos[$i*2+1],$majlabel[$i]);
203 $this->_StrokeAxisTitle($pos,$aAxisAngle,$title);
208 function _StrokeAxisTitle($pos,$aAxisAngle,$title) {
209 $this->title->Set($title);
210 $marg=6+$this->title->margin;
211 $xt=round(($this->scale->world_abs_size+$marg)*cos($aAxisAngle)+$this->scale->scale_abs[0]);
212 $yt=round($pos-($this->scale->world_abs_size+$marg)*sin($aAxisAngle));
214 // Position the axis title.
215 // dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
216 // that intersects with the extension of the corresponding axis. The code looks a little
217 // bit messy but this is really the only way of having a reasonable position of the
219 $h=$this->img->GetFontHeight();
220 $w=$this->img->GetTextWidth($title);
221 while( $aAxisAngle > 2*M_PI ) $aAxisAngle -= 2*M_PI;
222 if( $aAxisAngle>=7*M_PI/4 || $aAxisAngle <= M_PI/4 ) $dx=0;
223 if( $aAxisAngle>=M_PI/4 && $aAxisAngle <= 3*M_PI/4 ) $dx=($aAxisAngle-M_PI/4)*2/M_PI;
224 if( $aAxisAngle>=3*M_PI/4 && $aAxisAngle <= 5*M_PI/4 ) $dx=1;
225 if( $aAxisAngle>=5*M_PI/4 && $aAxisAngle <= 7*M_PI/4 ) $dx=(1-($aAxisAngle-M_PI*5/4)*2/M_PI);
227 if( $aAxisAngle>=7*M_PI/4 ) $dy=(($aAxisAngle-M_PI)-3*M_PI/4)*2/M_PI;
228 if( $aAxisAngle<=M_PI/4 ) $dy=(1-$aAxisAngle*2/M_PI);
229 if( $aAxisAngle>=M_PI/4 && $aAxisAngle <= 3*M_PI/4 ) $dy=1;
230 if( $aAxisAngle>=3*M_PI/4 && $aAxisAngle <= 5*M_PI/4 ) $dy=(1-($aAxisAngle-3*M_PI/4)*2/M_PI);
231 if( $aAxisAngle>=5*M_PI/4 && $aAxisAngle <= 7*M_PI/4 ) $dy=0;
234 $this->title->Stroke($this->img,$xt-$dx*$w,$yt-$dy*$h,$title);
241 //===================================================
243 // Description: Draws grid for the spider graph
244 //===================================================
245 class RadarGrid extends Grid {
248 function RadarGrid() {
253 function Stroke(&$img,&$grid) {
254 if( !$this->show ) return;
255 $nbrticks = count($grid[0])/2;
256 $nbrpnts = count($grid);
257 $img->SetColor($this->grid_color);
258 $img->SetLineWeight($this->weight);
259 for($i=0; $i<$nbrticks; ++$i) {
260 for($j=0; $j<$nbrpnts; ++$j) {
261 $pnts[$j*2]=$grid[$j][$i*2];
262 $pnts[$j*2+1]=$grid[$j][$i*2+1];
264 for($k=0; $k<$nbrpnts; ++$k ){
266 if( $this->type == "solid" )
267 $img->Line($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1]);
268 elseif( $this->type == "dotted" )
269 $img->DashedLine($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1],1,6);
270 elseif( $this->type == "dashed" )
271 $img->DashedLine($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1],2,4);
272 elseif( $this->type == "longdashed" )
273 $img->DashedLine($pnts[$k*2],$pnts[$k*2+1],$pnts[$l*2],$pnts[$l*2+1],8,6);
281 //===================================================
283 // Description: Plot a spiderplot
284 //===================================================
287 var $fill=false, $fill_color=array(200,170,180);
288 var $color=array(0,0,0);
293 function RadarPlot($data) {
300 return Min($this->data);
304 return Max($this->data);
307 function SetLegend($legend) {
308 $this->legend=$legend;
312 function SetLineWeight($w) {
316 function SetFillColor($aColor) {
317 $this->fill_color = $aColor;
321 function SetFill($f=true) {
325 function SetColor($aColor,$aFillColor=false) {
326 $this->color = $aColor;
328 $this->SetFillColor($aFillColor);
333 function GetCSIMareas() {
334 JpGraphError::Raise("Client side image maps not supported for RadarPlots.");
337 function Stroke(&$img, $pos, &$scale, $startangle) {
338 $nbrpnts = count($this->data);
339 $astep=2*M_PI/$nbrpnts;
342 // Rotate each point to the correct axis-angle
343 // TODO: Update for LogScale
344 for($i=0; $i<$nbrpnts; ++$i) {
345 //$c=$this->data[$i];
346 $cs=$scale->RelTranslate($this->data[$i]);
347 $x=round($cs*cos($a)+$scale->scale_abs[0]);
348 $y=round($pos-$cs*sin($a));
351 $x=round(($c-$scale->scale[0])*$scale->scale_factor*cos($a)+$scale->scale_abs[0]);
352 $y=round($pos-($c-$scale->scale[0])*$scale->scale_factor*sin($a));
359 $img->SetColor($this->fill_color);
360 $img->FilledPolygon($pnts);
362 $img->SetLineWeight($this->weight);
363 $img->SetColor($this->color);
366 $img->Polygon($pnts);
371 function GetCount() {
372 return count($this->data);
375 function Legend(&$graph) {
376 if( $this->legend=="" ) return;
378 $graph->legend->Add($this->legend,$this->fill_color);
380 $graph->legend->Add($this->legend,$this->color);
385 //===================================================
387 // Description: Main container for a spider graph
388 //===================================================
389 class RadarGraph extends Graph {
393 var $plots=null, $axis_title=null;
394 var $grid,$axis=null;
397 function RadarGraph($width=300,$height=200,$cachedName="",$timeout=0,$inline=1) {
398 $this->Graph($width,$height,$cachedName,$timeout,$inline);
399 $this->posx=$width/2;
400 $this->posy=$height/2;
401 $this->len=min($width,$height)*0.35;
402 $this->SetColor(array(255,255,255));
403 $this->SetTickDensity(TICKD_NORMAL);
404 $this->SetScale("lin");
409 function SupressTickMarks($f=true) {
411 JpGraphError::Raise('RadarGraph::SupressTickMarks() is deprecated. Use HideTickMarks() instead.');
412 $this->axis->scale->ticks->SupressTickMarks($f);
415 function HideTickMarks($aFlag=true) {
416 $this->axis->scale->ticks->SupressTickMarks($aFlag);
419 function ShowMinorTickmarks($aFlag=true) {
420 $this->yscale->ticks->SupressMinorTickMarks(!$aFlag);
423 function SetScale($axtype,$ymin=1,$ymax=1) {
424 if( $axtype != "lin" && $axtype != "log" ) {
425 JpGraphError::Raise("Illegal scale for spiderplot ($axtype). Must be \"lin\" or \"log\"");
427 if( $axtype=="lin" ) {
428 $this->yscale = & new LinearScale($ymin,$ymax);
429 $this->yscale->ticks = & new RadarLinearTicks();
430 $this->yscale->ticks->SupressMinorTickMarks();
432 elseif( $axtype=="log" ) {
433 $this->yscale = & new LogScale($ymin,$ymax);
434 $this->yscale->ticks = & new RadarLogTicks();
437 $this->axis = & new RadarAxis($this->img,$this->yscale);
438 $this->grid = & new RadarGrid();
441 function SetSize($aSize) {
442 if( $aSize<0.1 || $aSize>1 )
443 JpGraphError::Raise("Radar Plot size must be between 0.1 and 1. (Your value=$s)");
444 $this->len=min($this->img->width,$this->img->height)*$aSize/2;
447 function SetPlotSize($aSize) {
448 $this->SetSize($aSize);
451 function SetTickDensity($densy=TICKD_NORMAL) {
452 $this->ytick_factor=25;
455 $this->ytick_factor=12;
458 $this->ytick_factor=25;
461 $this->ytick_factor=40;
463 case TICKD_VERYSPARSE:
464 $this->ytick_factor=70;
467 JpGraphError::Raise("RadarPlot Unsupported Tick density: $densy");
471 function SetPos($px,$py=0.5) {
472 $this->SetCenter($px,$py);
475 function SetCenter($px,$py=0.5) {
476 assert($px > 0 && $py > 0 );
477 $this->posx=$this->img->width*$px;
478 $this->posy=$this->img->height*$py;
481 function SetColor($c) {
482 $this->SetMarginColor($c);
485 function SetTitles($title) {
486 $this->axis_title = $title;
489 function Add(&$splot) {
490 $this->plots[]=$splot;
493 function GetPlotsYMinMax() {
494 $min=$this->plots[0]->Min();
495 $max=$this->plots[0]->Max();
496 foreach( $this->plots as $p ) {
497 $max=max($max,$p->Max());
498 $min=min($min,$p->Min());
501 JpGraphError::Raise("Minimum data $min (Radar plots only makes sence to use when all data points > 0)");
502 return array($min,$max);
505 // Stroke the Radar graph
506 function Stroke($aStrokeFileName="") {
508 if( !$this->yscale->IsSpecified() && count($this->plots)>0 ) {
509 list($min,$max) = $this->GetPlotsYMinMax();
510 $this->yscale->AutoScale($this->img,0,$max,$this->len/$this->ytick_factor);
512 // Set start position end length of scale (in absolute pixels)
513 $this->yscale->SetConstants($this->posx,$this->len);
515 // We need as many axis as there are data points
516 $nbrpnts=$this->plots[0]->GetCount();
518 // If we have no titles just number the axis 1,2,3,...
519 if( $this->axis_title==null ) {
520 for($i=0; $i<$nbrpnts; ++$i )
521 $this->axis_title[$i] = $i+1;
523 elseif(count($this->axis_title)<$nbrpnts)
524 JpGraphError::Raise("Number of titles does not match number of points in plot.");
525 for($i=0; $i<count($this->plots); ++$i )
526 if( $nbrpnts != $this->plots[$i]->GetCount() )
527 JpGraphError::Raise("Each spider plot must have the same number of data points.");
529 if( $this->background_image != "" ) {
530 $this->StrokeFrameBackground();
533 $this->StrokeFrame();
535 $astep=2*M_PI/$nbrpnts;
538 for($i=0; $i<count($this->plots); ++$i)
539 $this->plots[$i]->Legend($this);
540 $this->legend->Stroke($this->img);
541 $this->footer->Stroke($this->img);
545 for($i=0; $i<count($this->plots); ++$i )
546 $this->plots[$i]->Stroke($this->img, $this->posy, $this->yscale, $a);
548 // Draw axis and grid
549 for( $i=0,$a=M_PI/2; $i<$nbrpnts; ++$i, $a+=$astep ) {
550 $this->axis->Stroke($this->posy,$a,$grid[$i],$this->axis_title[$i],$i==0);
552 $this->grid->Stroke($this->img,$grid);
553 $this->StrokeTitles();
556 if( $this->texts != null )
557 foreach( $this->texts as $t)
558 $t->Stroke($this->img);
561 // If the filename is given as the special "__handle"
562 // then the image handler is returned and the image is NOT
564 if( $aStrokeFileName == _IMG_HANDLER ) {
565 return $this->img->img;
568 // Finally stream the generated picture
569 $this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,