Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 david 1
<?php
2
/*=======================================================================
3
// File:	JPGRAPH_RADAR.PHP
4
// Description: Radar plot extension for JpGraph
5
// Created: 	2001-02-04
6
// Author:	Johan Persson (johanp@aditus.nu)
7
// Ver:		$Id: jpgraph_radar.php,v 1.1 2004/06/15 10:13:19 jpm Exp $
8
//
9
// License:	This code is released under QPL
10
// Copyright (C) 2001,2002 Johan Persson
11
//========================================================================
12
*/
13
 
14
 
15
class RadarLogTicks extends Ticks {
16
//---------------
17
// CONSTRUCTOR
18
    function RadarLogTicks() {
19
    }
20
//---------------
21
// PUBLIC METHODS
22
 
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;
29
	$count=1;
30
 
31
	$ticklen_maj=5;
32
	$dx_maj=round(sin($aAxisAngle)*$ticklen_maj);
33
	$dy_maj=round(cos($aAxisAngle)*$ticklen_maj);
34
	$ticklen_min=3;
35
	$dx_min=round(sin($aAxisAngle)*$ticklen_min);
36
	$dy_min=round(cos($aAxisAngle)*$ticklen_min);
37
 
38
	$aMajPos=array();
39
	$aMajLabel=array();
40
 
41
	if( $this->supress_first )
42
	    $aMajLabel[]="";
43
	else
44
	    $aMajLabel[]=$start;
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;
50
	$grid[]=$xt;
51
	$grid[]=$yt;
52
 
53
	$aImg->SetLineWeight($this->weight);
54
 
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 ) {
60
		$grid[]=$xt;
61
		$grid[]=$yt;
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();
68
		}
69
		$aMajLabel[]=$nextMajor;
70
		$nextMajor *= 10;
71
		$step *= 10;
72
		$count=1;
73
	    }
74
	    else
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();
79
		}
80
	}
81
    }
82
}
83
 
84
class RadarLinearTicks extends LinearTicks {
85
//---------------
86
// CONSTRUCTOR
87
    function RadarLinearTicks() {
88
	// Empty
89
    }
90
 
91
//---------------
92
// PUBLIC METHODS
93
 
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
102
 
103
	// Draw major ticks
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;
108
 
109
	$aImg->SetLineWeight($this->weight);
110
 
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));
114
	    $aMajLabel[]=$label;
115
	    $label += $this->major_step;
116
	    $grid[]=$xt;
117
	    $grid[]=$yt;
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();
124
	    }
125
	}
126
 
127
	// Draw minor ticks
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);
138
	    }
139
	    if( $this->mincolor!="" ) $aImg->PopColor();
140
	}
141
    }
142
}
143
 
144
 
145
 
146
//===================================================
147
// CLASS RadarAxis
148
// Description: Implements axis for the spider graph
149
//===================================================
150
class RadarAxis extends Axis {
151
    var $title_color="navy";
152
    var $title=null;
153
//---------------
154
// CONSTRUCTOR
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);
161
    }
162
//---------------
163
// PUBLIC METHODS
164
    function SetTickLabels($l) {
165
	$this->ticks_label = $l;
166
    }
167
 
168
 
169
    // Stroke the axis
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);
176
 
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));
180
 
181
	// Draw axis
182
	$this->img->SetColor($this->color);
183
	$this->img->SetLineWeight($this->weight);
184
	if( !$this->hide )
185
	    $this->img->Line($this->scale->scale_abs[0],$pos,$x,$y);
186
 
187
	$this->scale->ticks->Stroke($this->img,$grid,$pos,$aAxisAngle,$this->scale,$majpos,$majlabel);
188
 
189
	// Draw labels
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);
194
 
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]);
199
		else
200
		    $this->img->StrokeText($majpos[$i*2],$majpos[$i*2+1],$majlabel[$i]);
201
	    }
202
	}
203
	$this->_StrokeAxisTitle($pos,$aAxisAngle,$title);
204
    }
205
//---------------
206
// PRIVATE METHODS
207
 
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));
213
 
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
218
	// axis titles.
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);
226
 
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;
232
 
233
	if( !$this->hide )
234
		$this->title->Stroke($this->img,$xt-$dx*$w,$yt-$dy*$h,$title);
235
    }
236
 
237
 
238
} // Class
239
 
240
 
241
//===================================================
242
// CLASS RadarGrid
243
// Description: Draws grid for the spider graph
244
//===================================================
245
class RadarGrid extends Grid {
246
//------------
247
// CONSTRUCTOR
248
    function RadarGrid() {
249
    }
250
 
251
//----------------
252
// PRIVATE METHODS
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];
263
	    }
264
	    for($k=0; $k<$nbrpnts; ++$k ){
265
		$l=($k+1)%$nbrpnts;
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);
274
	    }
275
	    $pnts=array();
276
	}
277
    }
278
} // Class
279
 
280
 
281
//===================================================
282
// CLASS RadarPlot
283
// Description: Plot a spiderplot
284
//===================================================
285
class RadarPlot {
286
    var $data=array();
287
    var $fill=false, $fill_color=array(200,170,180);
288
    var $color=array(0,0,0);
289
    var $legend="";
290
    var $weight=1;
291
//---------------
292
// CONSTRUCTOR
293
    function RadarPlot($data) {
294
	$this->data = $data;
295
    }
296
 
297
//---------------
298
// PUBLIC METHODS
299
    function Min() {
300
	return Min($this->data);
301
    }
302
 
303
    function Max() {
304
	return Max($this->data);
305
    }
306
 
307
    function SetLegend($legend) {
308
	$this->legend=$legend;
309
    }
310
 
311
 
312
    function SetLineWeight($w) {
313
	$this->weight=$w;
314
    }
315
 
316
	function SetFillColor($aColor) {
317
		$this->fill_color = $aColor;
318
		$this->fill = true;
319
	}
320
 
321
    function SetFill($f=true) {
322
	$this->fill = $f;
323
    }
324
 
325
    function SetColor($aColor,$aFillColor=false) {
326
	$this->color = $aColor;
327
	if( $aFillColor ) {
328
	    $this->SetFillColor($aFillColor);
329
	    $this->fill = true;
330
	}
331
    }
332
 
333
    function GetCSIMareas() {
334
	JpGraphError::Raise("Client side image maps not supported for RadarPlots.");
335
    }
336
 
337
    function Stroke(&$img, $pos, &$scale, $startangle) {
338
	$nbrpnts = count($this->data);
339
	$astep=2*M_PI/$nbrpnts;
340
	$a=$startangle;
341
 
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));
349
	    /*
350
	      $c=log10($c);
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));
353
	    */
354
	    $pnts[$i*2]=$x;
355
	    $pnts[$i*2+1]=$y;
356
	    $a += $astep;
357
	}
358
	if( $this->fill ) {
359
	    $img->SetColor($this->fill_color);
360
	    $img->FilledPolygon($pnts);
361
	}
362
	$img->SetLineWeight($this->weight);
363
	$img->SetColor($this->color);
364
	$pnts[]=$pnts[0];
365
	$pnts[]=$pnts[1];
366
	$img->Polygon($pnts);
367
    }
368
 
369
//---------------
370
// PRIVATE METHODS
371
    function GetCount() {
372
	return count($this->data);
373
    }
374
 
375
    function Legend(&$graph) {
376
	if( $this->legend=="" ) return;
377
	if( $this->fill )
378
	    $graph->legend->Add($this->legend,$this->fill_color);
379
	else
380
	    $graph->legend->Add($this->legend,$this->color);
381
    }
382
 
383
} // Class
384
 
385
//===================================================
386
// CLASS RadarGraph
387
// Description: Main container for a spider graph
388
//===================================================
389
class RadarGraph extends Graph {
390
    var $posx;
391
    var $posy;
392
    var $len;
393
    var $plots=null, $axis_title=null;
394
    var $grid,$axis=null;
395
//---------------
396
// CONSTRUCTOR
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");
405
    }
406
 
407
//---------------
408
// PUBLIC METHODS
409
    function SupressTickMarks($f=true) {
410
    	if( ERR_DEPRECATED )
411
    		JpGraphError::Raise('RadarGraph::SupressTickMarks() is deprecated. Use HideTickMarks() instead.');
412
		$this->axis->scale->ticks->SupressTickMarks($f);
413
    }
414
 
415
    function HideTickMarks($aFlag=true) {
416
		$this->axis->scale->ticks->SupressTickMarks($aFlag);
417
    }
418
 
419
    function ShowMinorTickmarks($aFlag=true) {
420
    	$this->yscale->ticks->SupressMinorTickMarks(!$aFlag);
421
    }
422
 
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\"");
426
	}
427
	if( $axtype=="lin" ) {
428
	    $this->yscale = & new LinearScale($ymin,$ymax);
429
	    $this->yscale->ticks = & new RadarLinearTicks();
430
	    $this->yscale->ticks->SupressMinorTickMarks();
431
	}
432
	elseif( $axtype=="log" ) {
433
	    $this->yscale = & new LogScale($ymin,$ymax);
434
	    $this->yscale->ticks = & new RadarLogTicks();
435
	}
436
 
437
	$this->axis = & new RadarAxis($this->img,$this->yscale);
438
	$this->grid = & new RadarGrid();
439
    }
440
 
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;
445
    }
446
 
447
    function SetPlotSize($aSize) {
448
	$this->SetSize($aSize);
449
    }
450
 
451
    function SetTickDensity($densy=TICKD_NORMAL) {
452
	$this->ytick_factor=25;
453
	switch( $densy ) {
454
	    case TICKD_DENSE:
455
		$this->ytick_factor=12;
456
	    break;
457
	    case TICKD_NORMAL:
458
		$this->ytick_factor=25;
459
	    break;
460
	    case TICKD_SPARSE:
461
		$this->ytick_factor=40;
462
	    break;
463
	    case TICKD_VERYSPARSE:
464
		$this->ytick_factor=70;
465
	    break;
466
	    default:
467
		JpGraphError::Raise("RadarPlot Unsupported Tick density: $densy");
468
	}
469
    }
470
 
471
    function SetPos($px,$py=0.5) {
472
	$this->SetCenter($px,$py);
473
    }
474
 
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;
479
    }
480
 
481
    function SetColor($c) {
482
	$this->SetMarginColor($c);
483
    }
484
 
485
    function SetTitles($title) {
486
	$this->axis_title = $title;
487
    }
488
 
489
    function Add(&$splot) {
490
	$this->plots[]=$splot;
491
    }
492
 
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());
499
	}
500
	if( $min < 0 )
501
	    JpGraphError::Raise("Minimum data $min (Radar plots only makes sence to use when all data points > 0)");
502
	return array($min,$max);
503
    }
504
 
505
    // Stroke the Radar graph
506
    function Stroke($aStrokeFileName="") {
507
	// Set Y-scale
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);
511
	}
512
	// Set start position end length of scale (in absolute pixels)
513
	$this->yscale->SetConstants($this->posx,$this->len);
514
 
515
	// We need as many axis as there are data points
516
	$nbrpnts=$this->plots[0]->GetCount();
517
 
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;
522
	}
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.");
528
 
529
	if( $this->background_image != "" ) {
530
	    $this->StrokeFrameBackground();
531
	}
532
	else {
533
	    $this->StrokeFrame();
534
	}
535
	$astep=2*M_PI/$nbrpnts;
536
 
537
	// Prepare legends
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);
542
 
543
	// Plot points
544
	$a=M_PI/2;
545
	for($i=0; $i<count($this->plots); ++$i )
546
	    $this->plots[$i]->Stroke($this->img, $this->posy, $this->yscale, $a);
547
 
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);
551
	}
552
	$this->grid->Stroke($this->img,$grid);
553
	$this->StrokeTitles();
554
 
555
	// Stroke texts
556
	if( $this->texts != null )
557
	    foreach( $this->texts as $t)
558
		$t->Stroke($this->img);
559
 
560
 
561
	// If the filename is given as the special "__handle"
562
	// then the image handler is returned and the image is NOT
563
	// streamed back
564
	if( $aStrokeFileName == _IMG_HANDLER ) {
565
	    return $this->img->img;
566
	    }
567
	else {
568
	    // Finally stream the generated picture
569
	    $this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,
570
				       $aStrokeFileName);
571
	}
572
    }
573
} // Class
574
 
575
/* EOF */
576
?>