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.1 2006/07/07 13:37:14 powles Exp $
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
13 require_once('jpgraph_plotmark.inc');
15 class RadarLogTicks
extends Ticks
{
18 function __construct() {
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 if( $this->label_formfunc
!= "" ) {
70 $f=$this->label_formfunc
;
71 $l = call_user_func($f,$nextMajor);
81 if( !$this->supress_minor_tickmarks
) {
82 if( $this->mincolor
!="" ) $aImg->PushColor($this->mincolor
);
83 $aImg->Line($xt+
$dx_min,$yt+
$dy_min,$xt-$dx_min,$yt-$dy_min);
84 if( $this->mincolor
!="" ) $aImg->PopColor();
90 class RadarLinearTicks
extends LinearTicks
{
93 function RadarLinearTicks() {
100 // TODO: Add argument grid
101 function Stroke(&$aImg,&$grid,$aPos,$aAxisAngle,&$aScale,&$aMajPos,&$aMajLabel) {
102 // Prepare to draw linear ticks
103 $maj_step_abs = abs($aScale->scale_factor
*$this->major_step
);
104 $min_step_abs = abs($aScale->scale_factor
*$this->minor_step
);
105 $nbrmaj = floor(($aScale->world_abs_size
)/$maj_step_abs);
106 $nbrmin = floor(($aScale->world_abs_size
)/$min_step_abs);
107 $skip = round($nbrmin/$nbrmaj); // Don't draw minor ontop of major
110 $ticklen2=$this->major_abs_size
;
111 $dx=round(sin($aAxisAngle)*$ticklen2);
112 $dy=round(cos($aAxisAngle)*$ticklen2);
113 $label=$aScale->scale
[0]+
$this->major_step
;
115 $aImg->SetLineWeight($this->weight
);
117 for($i=1; $i<=$nbrmaj; ++
$i) {
118 $xt=round($i*$maj_step_abs*cos($aAxisAngle))+
$aScale->scale_abs
[0];
119 $yt=$aPos-round($i*$maj_step_abs*sin($aAxisAngle));
121 if( $this->label_formfunc
!= "" ) {
122 $f=$this->label_formfunc
;
123 $l = call_user_func($f,$label);
129 $label +
= $this->major_step
;
132 $aMajPos[($i-1)*2]=$xt+
2*$dx;
133 $aMajPos[($i-1)*2+
1]=$yt-$aImg->GetFontheight()/2;
134 if( !$this->supress_tickmarks
) {
135 if( $this->majcolor
!="" ) $aImg->PushColor($this->majcolor
);
136 $aImg->Line($xt+
$dx,$yt+
$dy,$xt-$dx,$yt-$dy);
137 if( $this->majcolor
!="" ) $aImg->PopColor();
142 $ticklen2=$this->minor_abs_size
;
143 $dx=round(sin($aAxisAngle)*$ticklen2);
144 $dy=round(cos($aAxisAngle)*$ticklen2);
145 if( !$this->supress_tickmarks
&& !$this->supress_minor_tickmarks
) {
146 if( $this->mincolor
!="" ) $aImg->PushColor($this->mincolor
);
147 for($i=1; $i<=$nbrmin; ++
$i) {
148 if( ($i %
$skip) == 0 ) continue;
149 $xt=round($i*$min_step_abs*cos($aAxisAngle))+
$aScale->scale_abs
[0];
150 $yt=$aPos-round($i*$min_step_abs*sin($aAxisAngle));
151 $aImg->Line($xt+
$dx,$yt+
$dy,$xt-$dx,$yt-$dy);
153 if( $this->mincolor
!="" ) $aImg->PopColor();
160 //===================================================
162 // Description: Implements axis for the radar graph
163 //===================================================
164 class RadarAxis
extends Axis
{
165 var $title_color="navy";
169 function RadarAxis(&$img,&$aScale,$color=array(0,0,0)) {
170 parent
::Axis($img,$aScale,$color);
171 $this->len
=$img->plotheight
;
172 $this->title
= new Text();
173 $this->title
->SetFont(FF_FONT1
,FS_BOLD
);
174 $this->color
= array(0,0,0);
178 function SetTickLabels($l) {
179 $this->ticks_label
= $l;
184 // $pos = Vertical position of axis
185 // $aAxisAngle = Axis angle
186 // $grid = Returns an array with positions used to draw the grid
187 // $lf = Label flag, TRUE if the axis should have labels
188 function Stroke($pos,$aAxisAngle,&$grid,$title,$lf) {
189 $this->img
->SetColor($this->color
);
191 // Determine end points for the axis
192 $x=round($this->scale
->world_abs_size
*cos($aAxisAngle)+
$this->scale
->scale_abs
[0]);
193 $y=round($pos-$this->scale
->world_abs_size
*sin($aAxisAngle));
196 $this->img
->SetColor($this->color
);
197 $this->img
->SetLineWeight($this->weight
);
199 $this->img
->Line($this->scale
->scale_abs
[0],$pos,$x,$y);
201 $this->scale
->ticks
->Stroke($this->img
,$grid,$pos,$aAxisAngle,$this->scale
,$majpos,$majlabel);
204 if( $lf && !$this->hide
) {
205 $this->img
->SetFont($this->font_family
,$this->font_style
,$this->font_size
);
206 $this->img
->SetTextAlign("left","top");
207 $this->img
->SetColor($this->label_color
);
209 // majpos contains (x,y) coordinates for labels
210 if( ! $this->hide_labels
) {
211 $n = floor(count($majpos)/2);
212 for($i=0; $i < $n; ++
$i) {
213 if( $this->ticks_label
!= null && isset($this->ticks_label
[$i]) )
214 $this->img
->StrokeText($majpos[$i*2],$majpos[$i*2+
1],$this->ticks_label
[$i]);
216 $this->img
->StrokeText($majpos[$i*2],$majpos[$i*2+
1],$majlabel[$i]);
220 $this->_StrokeAxisTitle($pos,$aAxisAngle,$title);
225 function _StrokeAxisTitle($pos,$aAxisAngle,$title) {
226 $this->title
->Set($title);
227 $marg=6+
$this->title
->margin
;
228 $xt=round(($this->scale
->world_abs_size+
$marg)*cos($aAxisAngle)+
$this->scale
->scale_abs
[0]);
229 $yt=round($pos-($this->scale
->world_abs_size+
$marg)*sin($aAxisAngle));
231 // Position the axis title.
232 // dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
233 // that intersects with the extension of the corresponding axis. The code looks a little
234 // bit messy but this is really the only way of having a reasonable position of the
236 if( $this->title
->iWordwrap
> 0 ) {
237 $title = wordwrap($title,$this->title
->iWordwrap
,"\n");
240 $h=$this->img
->GetTextHeight($title)*1.2;
241 $w=$this->img
->GetTextWidth($title)*1.2;
242 while( $aAxisAngle > 2*M_PI
) $aAxisAngle -= 2*M_PI
;
244 if( $aAxisAngle>=7*M_PI
/4 ||
$aAxisAngle <= M_PI
/4 ) $dx=0;
245 if( $aAxisAngle>=M_PI
/4 && $aAxisAngle <= 3*M_PI
/4 ) $dx=($aAxisAngle-M_PI
/4)*2/M_PI
;
246 if( $aAxisAngle>=3*M_PI
/4 && $aAxisAngle <= 5*M_PI
/4 ) $dx=1;
247 if( $aAxisAngle>=5*M_PI
/4 && $aAxisAngle <= 7*M_PI
/4 ) $dx=(1-($aAxisAngle-M_PI
*5/4)*2/M_PI
);
249 if( $aAxisAngle>=7*M_PI
/4 ) $dy=(($aAxisAngle-M_PI
)-3*M_PI
/4)*2/M_PI
;
250 if( $aAxisAngle<=M_PI
/4 ) $dy=(1-$aAxisAngle*2/M_PI
);
251 if( $aAxisAngle>=M_PI
/4 && $aAxisAngle <= 3*M_PI
/4 ) $dy=1;
252 if( $aAxisAngle>=3*M_PI
/4 && $aAxisAngle <= 5*M_PI
/4 ) $dy=(1-($aAxisAngle-3*M_PI
/4)*2/M_PI
);
253 if( $aAxisAngle>=5*M_PI
/4 && $aAxisAngle <= 7*M_PI
/4 ) $dy=0;
256 $this->title
->Stroke($this->img
,$xt-$dx*$w,$yt-$dy*$h,$title);
264 //===================================================
266 // Description: Draws grid for the radar graph
267 //===================================================
268 class RadarGrid
extends Grid
{
271 function RadarGrid() {
276 function Stroke(&$img,&$grid) {
277 if( !$this->show
) return;
278 $nbrticks = count($grid[0])/2;
279 $nbrpnts = count($grid);
280 $img->SetColor($this->grid_color
);
281 $img->SetLineWeight($this->weight
);
282 for($i=0; $i<$nbrticks; ++
$i) {
283 for($j=0; $j<$nbrpnts; ++
$j) {
284 $pnts[$j*2]=$grid[$j][$i*2];
285 $pnts[$j*2+
1]=$grid[$j][$i*2+
1];
287 for($k=0; $k<$nbrpnts; ++
$k ){
289 if( $this->type
== "solid" )
290 $img->Line($pnts[$k*2],$pnts[$k*2+
1],$pnts[$l*2],$pnts[$l*2+
1]);
291 elseif( $this->type
== "dotted" )
292 $img->DashedLine($pnts[$k*2],$pnts[$k*2+
1],$pnts[$l*2],$pnts[$l*2+
1],1,6);
293 elseif( $this->type
== "dashed" )
294 $img->DashedLine($pnts[$k*2],$pnts[$k*2+
1],$pnts[$l*2],$pnts[$l*2+
1],2,4);
295 elseif( $this->type
== "longdashed" )
296 $img->DashedLine($pnts[$k*2],$pnts[$k*2+
1],$pnts[$l*2],$pnts[$l*2+
1],8,6);
304 //===================================================
306 // Description: Plot a radarplot
307 //===================================================
310 var $fill=false, $fill_color=array(200,170,180);
311 var $color=array(0,0,0);
314 var $linestyle='solid';
318 function RadarPlot($data) {
320 $this->mark
= new PlotMark();
326 return Min($this->data
);
330 return Max($this->data
);
333 function SetLegend($legend) {
334 $this->legend
=$legend;
337 function SetLineStyle($aStyle) {
338 $this->linestyle
=$aStyle;
341 function SetLineWeight($w) {
345 function SetFillColor($aColor) {
346 $this->fill_color
= $aColor;
350 function SetFill($f=true) {
354 function SetColor($aColor,$aFillColor=false) {
355 $this->color
= $aColor;
357 $this->SetFillColor($aFillColor);
362 function GetCSIMareas() {
363 JpGraphError
::RaiseL(18001);
364 //("Client side image maps not supported for RadarPlots.");
367 function Stroke(&$img, $pos, &$scale, $startangle) {
368 $nbrpnts = count($this->data
);
369 $astep=2*M_PI
/$nbrpnts;
372 // Rotate each point to the correct axis-angle
373 // TODO: Update for LogScale
374 for($i=0; $i<$nbrpnts; ++
$i) {
375 //$c=$this->data[$i];
376 $cs=$scale->RelTranslate($this->data
[$i]);
377 $x=round($cs*cos($a)+
$scale->scale_abs
[0]);
378 $y=round($pos-$cs*sin($a));
381 $x=round(($c-$scale->scale[0])*$scale->scale_factor*cos($a)+$scale->scale_abs[0]);
382 $y=round($pos-($c-$scale->scale[0])*$scale->scale_factor*sin($a));
389 $img->SetColor($this->fill_color
);
390 $img->FilledPolygon($pnts);
392 $img->SetLineWeight($this->weight
);
393 $img->SetColor($this->color
);
394 $img->SetLineStyle($this->linestyle
);
397 $img->Polygon($pnts);
398 $img->SetLineStyle('solid'); // Reset line style to default
399 // Add plotmarks on top
400 if( $this->mark
->show
) {
401 for($i=0; $i < $nbrpnts; ++
$i) {
402 $this->mark
->Stroke($img,$pnts[$i*2],$pnts[$i*2+
1]);
410 function GetCount() {
411 return count($this->data
);
414 function Legend(&$graph) {
415 if( $this->legend
=="" ) return;
417 $graph->legend
->Add($this->legend
,$this->fill_color
,$this->mark
);
419 $graph->legend
->Add($this->legend
,$this->color
,$this->mark
);
424 //===================================================
426 // Description: Main container for a radar graph
427 //===================================================
428 class RadarGraph
extends Graph
{
432 var $plots=null, $axis_title=null;
433 var $grid,$axis=null;
436 function RadarGraph($width=300,$height=200,$cachedName="",$timeout=0,$inline=1) {
437 $this->Graph($width,$height,$cachedName,$timeout,$inline);
438 $this->posx
=$width/2;
439 $this->posy
=$height/2;
440 $this->len
=min($width,$height)*0.35;
441 $this->SetColor(array(255,255,255));
442 $this->SetTickDensity(TICKD_NORMAL
);
443 $this->SetScale("lin");
444 $this->SetGridDepth(DEPTH_FRONT
);
450 function SupressTickMarks($f=true) {
452 JpGraphError
::RaiseL(18002);
453 //('RadarGraph::SupressTickMarks() is deprecated. Use HideTickMarks() instead.');
454 $this->axis
->scale
->ticks
->SupressTickMarks($f);
457 function HideTickMarks($aFlag=true) {
458 $this->axis
->scale
->ticks
->SupressTickMarks($aFlag);
461 function ShowMinorTickmarks($aFlag=true) {
462 $this->yscale
->ticks
->SupressMinorTickMarks(!$aFlag);
465 function SetScale($axtype,$ymin=1,$ymax=1) {
466 if( $axtype != "lin" && $axtype != "log" ) {
467 JpGraphError
::RaiseL(18003,$axtype);
468 //("Illegal scale for radarplot ($axtype). Must be \"lin\" or \"log\"");
470 if( $axtype=="lin" ) {
471 $this->yscale
= new LinearScale($ymin,$ymax);
472 $this->yscale
->ticks
= new RadarLinearTicks();
473 $this->yscale
->ticks
->SupressMinorTickMarks();
475 elseif( $axtype=="log" ) {
476 $this->yscale
= new LogScale($ymin,$ymax);
477 $this->yscale
->ticks
= new RadarLogTicks();
480 $this->axis
= new RadarAxis($this->img
,$this->yscale
);
481 $this->grid
= new RadarGrid();
484 function SetSize($aSize) {
485 if( $aSize < 0.1 ||
$aSize>1 )
486 JpGraphError
::RaiseL(18004,$aSize);
487 //("Radar Plot size must be between 0.1 and 1. (Your value=$s)");
488 $this->len
=min($this->img
->width
,$this->img
->height
)*$aSize/2;
491 function SetPlotSize($aSize) {
492 $this->SetSize($aSize);
495 function SetTickDensity($densy=TICKD_NORMAL
) {
496 $this->ytick_factor
=25;
499 $this->ytick_factor
=12;
502 $this->ytick_factor
=25;
505 $this->ytick_factor
=40;
507 case TICKD_VERYSPARSE
:
508 $this->ytick_factor
=70;
511 JpGraphError
::RaiseL(18005,$densy);
512 //("RadarPlot Unsupported Tick density: $densy");
516 function SetPos($px,$py=0.5) {
517 $this->SetCenter($px,$py);
520 function SetCenter($px,$py=0.5) {
521 assert($px > 0 && $py > 0 );
522 $this->posx
=$this->img
->width
*$px;
523 $this->posy
=$this->img
->height
*$py;
526 function SetColor($c) {
527 $this->SetMarginColor($c);
530 function SetTitles($title) {
531 $this->axis_title
= $title;
534 function Add(&$splot) {
535 $this->plots
[]=$splot;
538 function GetPlotsYMinMax() {
539 $min=$this->plots
[0]->Min();
540 $max=$this->plots
[0]->Max();
541 foreach( $this->plots
as $p ) {
542 $max=max($max,$p->Max());
543 $min=min($min,$p->Min());
546 JpGraphError
::RaiseL(18006,$min);
547 //("Minimum data $min (Radar plots should only be used when all data points > 0)");
548 return array($min,$max);
551 // Stroke the Radar graph
552 function Stroke($aStrokeFileName="") {
553 $n = count($this->plots
);
555 if( !$this->yscale
->IsSpecified() && count($this->plots
)>0 ) {
556 list($min,$max) = $this->GetPlotsYMinMax();
557 $this->yscale
->AutoScale($this->img
,0,$max,$this->len
/$this->ytick_factor
);
559 // Set start position end length of scale (in absolute pixels)
560 $this->yscale
->SetConstants($this->posx
,$this->len
);
562 // We need as many axis as there are data points
563 $nbrpnts=$this->plots
[0]->GetCount();
565 // If we have no titles just number the axis 1,2,3,...
566 if( $this->axis_title
==null ) {
567 for($i=0; $i < $nbrpnts; ++
$i )
568 $this->axis_title
[$i] = $i+
1;
570 elseif(count($this->axis_title
)<$nbrpnts)
571 JpGraphError
::RaiseL(18007);
572 //("Number of titles does not match number of points in plot.");
573 for($i=0; $i < $n; ++
$i )
574 if( $nbrpnts != $this->plots
[$i]->GetCount() )
575 JpGraphError
::RaiseL(18008);
576 //("Each radar plot must have the same number of data points.");
578 if( $this->background_image
!= "" ) {
579 $this->StrokeFrameBackground();
582 $this->StrokeFrame();
584 $astep=2*M_PI
/$nbrpnts;
587 for($i=0; $i < $n; ++
$i)
588 $this->plots
[$i]->Legend($this);
589 $this->legend
->Stroke($this->img
);
590 $this->footer
->Stroke($this->img
);
592 if( $this->grid_depth
== DEPTH_BACK
) {
593 // Draw axis and grid
594 for( $i=0,$a=M_PI
/2; $i < $nbrpnts; ++
$i, $a +
= $astep ) {
595 $this->axis
->Stroke($this->posy
,$a,$grid[$i],$this->axis_title
[$i],$i==0);
601 for($i=0; $i < $n; ++
$i )
602 $this->plots
[$i]->Stroke($this->img
, $this->posy
, $this->yscale
, $a);
604 if( $this->grid_depth
!= DEPTH_BACK
) {
605 // Draw axis and grid
606 for( $i=0,$a=M_PI
/2; $i < $nbrpnts; ++
$i, $a +
= $astep ) {
607 $this->axis
->Stroke($this->posy
,$a,$grid[$i],$this->axis_title
[$i],$i==0);
610 $this->grid
->Stroke($this->img
,$grid);
611 $this->StrokeTitles();
614 if( $this->texts
!= null ) {
615 foreach( $this->texts
as $t)
616 $t->Stroke($this->img
);
619 // Should we do any final image transformation
620 if( $this->iImgTrans
) {
621 if( !class_exists('ImgTrans') ) {
622 require_once('jpgraph_imgtrans.php');
625 $tform = new ImgTrans($this->img
->img
);
626 $this->img
->img
= $tform->Skew3D($this->iImgTransHorizon
,$this->iImgTransSkewDist
,
627 $this->iImgTransDirection
,$this->iImgTransHighQ
,
628 $this->iImgTransMinSize
,$this->iImgTransFillColor
,
629 $this->iImgTransBorder
);
632 // If the filename is given as the special "__handle"
633 // then the image handler is returned and the image is NOT
635 if( $aStrokeFileName == _IMG_HANDLER
) {
636 return $this->img
->img
;
639 // Finally stream the generated picture
640 $this->cache
->PutAndStream($this->img
,$this->cache_name
,$this->inline
,