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_PLOTMARK.PHP
4
// Description:	Class file. Handles plotmarks
5
// Created: 	2003-03-21
6
// Ver:		$Id: jpgraph_plotmark.inc.php 955 2007-11-17 11:41:42Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
 
11
 
12
//========================================================================
13
// CLASS ImgData
14
// Description: Base class for all image data classes that contains the
15
// real image data.
16
//========================================================================
17
class ImgData {
18
    var $name = '';		// Each subclass gives a name
19
    var $an = array();		// Data array names
20
    var $colors = array();	// Available colors
21
    var $index  = array();	// Index for colors
22
    var $maxidx = 0 ;		// Max color index
23
    var $anchor_x=0.5, $anchor_y=0.5 ;    // Where is the center of the image
24
    // Create a GD image from the data and return a GD handle
25
    function GetImg($aMark,$aIdx) {
26
	$n = $this->an[$aMark];
27
	if( is_string($aIdx) ) {
28
	    if( !in_array($aIdx,$this->colors) ) {
29
		JpGraphError::RaiseL(23001,$this->name,$aIdx); //('This marker "'.($this->name).'" does not exist in color: '.$aIdx);
30
	    }
31
	    $idx = $this->index[$aIdx];
32
	}
33
	elseif( !is_integer($aIdx) ||
34
		(is_integer($aIdx) && $aIdx > $this->maxidx ) ) {
35
	    JpGraphError::RaiseL(23002,$this->name);
36
//('Mark color index too large for marker "'.($this->name).'"');
37
	}
38
	else
39
	    $idx = $aIdx ;
40
	return Image::CreateFromString(base64_decode($this->{$n}[$idx][1]));
41
    }
42
    function GetAnchor() {
43
	return array($this->anchor_x,$this->anchor_y);
44
    }
45
}
46
 
47
 
48
// Keep a global flag cache to reduce memory usage
49
$_gFlagCache=array(
50
    1 => null,
51
    2 => null,
52
    3 => null,
53
    4 => null,
54
);
55
// Only supposed to b called as statics
56
class FlagCache {
57
    function GetFlagImgByName($aSize,$aName) {
58
	global $_gFlagCache;
59
	require_once('jpgraph_flags.php');
60
	if( $_gFlagCache[$aSize] === null ) {
61
	    $_gFlagCache[$aSize] =& new FlagImages($aSize);
62
	}
63
	$f =& $_gFlagCache[$aSize];
64
	$idx = $f->GetIdxByName($aName,$aFullName);
65
	return $f->GetImgByIdx($idx);
66
    }
67
}
68
 
69
//===================================================
70
// CLASS PlotMark
71
// Description: Handles the plot marks in graphs
72
//===================================================
73
class PlotMark {
74
    var $title, $show=true;
75
    var $type,$weight=1;
76
    var $color="black", $width=4, $fill_color="blue";
77
    var $yvalue,$xvalue='',$csimtarget='',$csimwintarget='',$csimalt='',$csimareas;
78
    var $iFormatCallback="";
79
    var $iFormatCallback2="";
80
    var $markimg='',$iScale=1.0;
81
    var $oldfilename='',$iFileName='';
82
    var $imgdata_balls = null;
83
    var $imgdata_diamonds = null;
84
    var $imgdata_squares = null;
85
    var $imgdata_bevels = null;
86
    var $imgdata_stars = null;
87
    var $imgdata_pushpins = null;
88
 
89
//--------------
90
// CONSTRUCTOR
91
    function PlotMark() {
92
	$this->title = new Text();
93
	$this->title->Hide();
94
	$this->csimareas = '';
95
	$this->type=-1;
96
    }
97
//---------------
98
// PUBLIC METHODS
99
    function SetType($aType,$aFileName='',$aScale=1.0) {
100
	$this->type = $aType;
101
	if( $aType == MARK_IMG && $aFileName=='' ) {
102
	    JpGraphError::RaiseL(23003);//('A filename must be specified if you set the mark type to MARK_IMG.');
103
	}
104
	$this->iFileName = $aFileName;
105
	$this->iScale = $aScale;
106
    }
107
 
108
    function SetCallback($aFunc) {
109
	$this->iFormatCallback = $aFunc;
110
    }
111
 
112
    function SetCallbackYX($aFunc) {
113
	$this->iFormatCallback2 = $aFunc;
114
    }
115
 
116
    function GetType() {
117
	return $this->type;
118
    }
119
 
120
    function SetColor($aColor) {
121
	$this->color=$aColor;
122
    }
123
 
124
    function SetFillColor($aFillColor) {
125
	$this->fill_color = $aFillColor;
126
    }
127
 
128
    function SetWeight($aWeight) {
129
	$this->weight = $aWeight;
130
    }
131
 
132
    // Synonym for SetWidth()
133
    function SetSize($aWidth) {
134
	$this->width=$aWidth;
135
    }
136
 
137
    function SetWidth($aWidth) {
138
	$this->width=$aWidth;
139
    }
140
 
141
    function SetDefaultWidth() {
142
	switch( $this->type ) {
143
	    case MARK_CIRCLE:
144
	    case MARK_FILLEDCIRCLE:
145
		$this->width=4;
146
		break;
147
	    default:
148
		$this->width=7;
149
	}
150
    }
151
 
152
    function GetWidth() {
153
	return $this->width;
154
    }
155
 
156
    function Hide($aHide=true) {
157
	$this->show = !$aHide;
158
    }
159
 
160
    function Show($aShow=true) {
161
	$this->show = $aShow;
162
    }
163
 
164
    function SetCSIMAltVal($aY,$aX='') {
165
        $this->yvalue=$aY;
166
        $this->xvalue=$aX;
167
    }
168
 
169
    function SetCSIMTarget($aTarget,$aWinTarget='') {
170
        $this->csimtarget=$aTarget;
171
        $this->csimwintarget=$aWinTarget;
172
    }
173
 
174
    function SetCSIMAlt($aAlt) {
175
        $this->csimalt=$aAlt;
176
    }
177
 
178
    function GetCSIMAreas(){
179
        return $this->csimareas;
180
    }
181
 
182
    function AddCSIMPoly($aPts) {
183
        $coords = round($aPts[0]).", ".round($aPts[1]);
184
        $n = count($aPts)/2;
185
        for( $i=1; $i < $n; ++$i){
186
            $coords .= ", ".round($aPts[2*$i]).", ".round($aPts[2*$i+1]);
187
        }
188
        $this->csimareas="";
189
        if( !empty($this->csimtarget) ) {
190
	    $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".htmlentities($this->csimtarget)."\"";
191
 
192
	    if( !empty($this->csimwintarget) ) {
193
		$this->csimareas .= " target=\"".$this->csimwintarget."\" ";
194
	    }
195
 
196
	    if( !empty($this->csimalt) ) {
197
		$tmp=sprintf($this->csimalt,$this->yvalue,$this->xvalue);
198
		$this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
199
	    }
200
	    $this->csimareas .= " />\n";
201
	}
202
    }
203
 
204
    function AddCSIMCircle($x,$y,$r) {
205
    	$x = round($x); $y=round($y); $r=round($r);
206
        $this->csimareas="";
207
        if( !empty($this->csimtarget) ) {
208
	    $this->csimareas .= "<area shape=\"circle\" coords=\"$x,$y,$r\" href=\"".htmlentities($this->csimtarget)."\"";
209
 
210
	    if( !empty($this->csimwintarget) ) {
211
		$this->csimareas .= " target=\"".$this->csimwintarget."\" ";
212
	    }
213
 
214
    	    if( !empty($this->csimalt) ) {
215
		$tmp=sprintf($this->csimalt,$this->yvalue,$this->xvalue);
216
		$this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
217
	    }
218
            $this->csimareas .= " />\n";
219
        }
220
    }
221
 
222
    function Stroke(&$img,$x,$y) {
223
	if( !$this->show ) return;
224
 
225
	if( $this->iFormatCallback != '' || $this->iFormatCallback2 != '' ) {
226
 
227
	    if( $this->iFormatCallback != '' ) {
228
		$f = $this->iFormatCallback;
229
		list($width,$color,$fcolor) = call_user_func($f,$this->yvalue);
230
		$filename = $this->iFileName;
231
		$imgscale = $this->iScale;
232
	    }
233
	    else {
234
		$f = $this->iFormatCallback2;
235
		list($width,$color,$fcolor,$filename,$imgscale) = call_user_func($f,$this->yvalue,$this->xvalue);
236
		if( $filename=="" ) $filename = $this->iFileName;
237
		if( $imgscale=="" ) $imgscale = $this->iScale;
238
	    }
239
 
240
	    if( $width=="" ) $width = $this->width;
241
	    if( $color=="" ) $color = $this->color;
242
	    if( $fcolor=="" ) $fcolor = $this->fill_color;
243
 
244
	}
245
	else {
246
	    $fcolor = $this->fill_color;
247
	    $color = $this->color;
248
	    $width = $this->width;
249
	    $filename = $this->iFileName;
250
	    $imgscale = $this->iScale;
251
	}
252
 
253
	if( $this->type == MARK_IMG ||
254
	    ($this->type >= MARK_FLAG1 && $this->type <= MARK_FLAG4 ) ||
255
	    $this->type >= MARK_IMG_PUSHPIN ) {
256
 
257
	    // Note: For the builtin images we use the "filename" parameter
258
	    // to denote the color
259
	    $anchor_x = 0.5;
260
	    $anchor_y = 0.5;
261
	    switch( $this->type ) {
262
		case MARK_FLAG1:
263
		case MARK_FLAG2:
264
		case MARK_FLAG3:
265
		case MARK_FLAG4:
266
		    $this->markimg = FlagCache::GetFlagImgByName($this->type-MARK_FLAG1+1,$filename);
267
		    break;
268
 
269
		case MARK_IMG :
270
		    // Load an image and use that as a marker
271
		    // Small optimization, if we have already read an image don't
272
		    // waste time reading it again.
273
		    if( $this->markimg == '' || !($this->oldfilename === $filename) ) {
274
			$this->markimg = Graph::LoadBkgImage('',$filename);
275
			$this->oldfilename = $filename ;
276
		    }
277
		    break;
278
 
279
		case MARK_IMG_PUSHPIN:
280
		case MARK_IMG_SPUSHPIN:
281
		case MARK_IMG_LPUSHPIN:
282
		    if( $this->imgdata_pushpins == null ) {
283
			require_once 'imgdata_pushpins.inc.php';
284
			$this->imgdata_pushpins = new ImgData_PushPins();
285
		    }
286
		    $this->markimg = $this->imgdata_pushpins->GetImg($this->type,$filename);
287
		    list($anchor_x,$anchor_y) = $this->imgdata_pushpins->GetAnchor();
288
		    break;
289
 
290
		case MARK_IMG_SQUARE:
291
		    if( $this->imgdata_squares == null ) {
292
			require_once 'imgdata_squares.inc.php';
293
			$this->imgdata_squares = new ImgData_Squares();
294
		    }
295
		    $this->markimg = $this->imgdata_squares->GetImg($this->type,$filename);
296
		    list($anchor_x,$anchor_y) = $this->imgdata_squares->GetAnchor();
297
		    break;
298
 
299
		case MARK_IMG_STAR:
300
		    if( $this->imgdata_stars == null ) {
301
			require_once 'imgdata_stars.inc.php';
302
			$this->imgdata_stars = new ImgData_Stars();
303
		    }
304
		    $this->markimg = $this->imgdata_stars->GetImg($this->type,$filename);
305
		    list($anchor_x,$anchor_y) = $this->imgdata_stars->GetAnchor();
306
		    break;
307
 
308
		case MARK_IMG_BEVEL:
309
		    if( $this->imgdata_bevels == null ) {
310
			require_once 'imgdata_bevels.inc.php';
311
			$this->imgdata_bevels = new ImgData_Bevels();
312
		    }
313
		    $this->markimg = $this->imgdata_bevels->GetImg($this->type,$filename);
314
		    list($anchor_x,$anchor_y) = $this->imgdata_bevels->GetAnchor();
315
		    break;
316
 
317
		case MARK_IMG_DIAMOND:
318
		    if( $this->imgdata_diamonds == null ) {
319
			require_once 'imgdata_diamonds.inc.php';
320
			$this->imgdata_diamonds = new ImgData_Diamonds();
321
		    }
322
		    $this->markimg = $this->imgdata_diamonds->GetImg($this->type,$filename);
323
		    list($anchor_x,$anchor_y) = $this->imgdata_diamonds->GetAnchor();
324
		    break;
325
 
326
		case MARK_IMG_BALL:
327
		case MARK_IMG_SBALL:
328
		case MARK_IMG_MBALL:
329
		case MARK_IMG_LBALL:
330
		    if( $this->imgdata_balls == null ) {
331
			require_once 'imgdata_balls.inc.php';
332
			$this->imgdata_balls = new ImgData_Balls();
333
		    }
334
		    $this->markimg = $this->imgdata_balls->GetImg($this->type,$filename);
335
		    list($anchor_x,$anchor_y) = $this->imgdata_balls->GetAnchor();
336
		    break;
337
	    }
338
 
339
	    $w = $img->GetWidth($this->markimg);
340
	    $h = $img->GetHeight($this->markimg);
341
 
342
	    $dw = round($imgscale * $w );
343
	    $dh = round($imgscale * $h );
344
 
345
	    // Do potential rotation
346
	    list($x,$y) = $img->Rotate($x,$y);
347
 
348
	    $dx = round($x-$dw*$anchor_x);
349
	    $dy = round($y-$dh*$anchor_y);
350
 
351
	    $this->width = max($dx,$dy);
352
 
353
	    $img->Copy($this->markimg,$dx,$dy,0,0,$dw,$dh,$w,$h);
354
	    if( !empty($this->csimtarget) ) {
355
		$this->csimareas = "<area shape=\"rect\" coords=\"".
356
		    $dx.','.$dy.','.round($dx+$dw).','.round($dy+$dh).'" '.
357
		    "href=\"".htmlentities($this->csimtarget)."\"";
358
 
359
		if( !empty($this->csimwintarget) ) {
360
		    $this->csimareas .= " target=\"".$this->csimwintarget."\" ";
361
		}
362
 
363
		if( !empty($this->csimalt) ) {
364
		    $tmp=sprintf($this->csimalt,$this->yvalue,$this->xvalue);
365
		    $this->csimareas .= " title=\"$tmp\"  alt=\"$tmp\" ";
366
		}
367
		$this->csimareas .= " />\n";
368
	    }
369
 
370
	    // Stroke title
371
	    $this->title->Align("center","top");
372
	    $this->title->Stroke($img,$x,$y+round($dh/2));
373
	    return;
374
	}
375
 
376
	$weight = $this->weight;
377
	$dx=round($width/2,0);
378
	$dy=round($width/2,0);
379
	$pts=0;
380
 
381
	switch( $this->type ) {
382
	    case MARK_SQUARE:
383
		$c[]=$x-$dx;$c[]=$y-$dy;
384
		$c[]=$x+$dx;$c[]=$y-$dy;
385
		$c[]=$x+$dx;$c[]=$y+$dy;
386
		$c[]=$x-$dx;$c[]=$y+$dy;
387
		$c[]=$x-$dx;$c[]=$y-$dy;
388
		$pts=5;
389
		break;
390
	    case MARK_UTRIANGLE:
391
		++$dx;++$dy;
392
		$c[]=$x-$dx;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
393
		$c[]=$x;$c[]=$y-0.87*$dy;
394
		$c[]=$x+$dx;$c[]=$y+0.87*$dy;
395
		$c[]=$x-$dx;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
396
		$pts=4;
397
		break;
398
	    case MARK_DTRIANGLE:
399
		++$dx;++$dy;
400
		$c[]=$x;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
401
		$c[]=$x-$dx;$c[]=$y-0.87*$dy;
402
		$c[]=$x+$dx;$c[]=$y-0.87*$dy;
403
		$c[]=$x;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
404
		$pts=4;
405
		break;
406
	    case MARK_DIAMOND:
407
		$c[]=$x;$c[]=$y+$dy;
408
		$c[]=$x-$dx;$c[]=$y;
409
		$c[]=$x;$c[]=$y-$dy;
410
		$c[]=$x+$dx;$c[]=$y;
411
		$c[]=$x;$c[]=$y+$dy;
412
		$pts=5;
413
		break;
414
	    case MARK_LEFTTRIANGLE:
415
		$c[]=$x;$c[]=$y;
416
		$c[]=$x;$c[]=$y+2*$dy;
417
		$c[]=$x+$dx*2;$c[]=$y;
418
		$c[]=$x;$c[]=$y;
419
		$pts=4;
420
		break;
421
	    case MARK_RIGHTTRIANGLE:
422
		$c[]=$x-$dx*2;$c[]=$y;
423
		$c[]=$x;$c[]=$y+2*$dy;
424
		$c[]=$x;$c[]=$y;
425
		$c[]=$x-$dx*2;$c[]=$y;
426
		$pts=4;
427
		break;
428
	    case MARK_FLASH:
429
		$dy *= 2;
430
		$c[]=$x+$dx/2; $c[]=$y-$dy;
431
		$c[]=$x-$dx+$dx/2; $c[]=$y+$dy*0.7-$dy;
432
		$c[]=$x+$dx/2; $c[]=$y+$dy*1.3-$dy;
433
		$c[]=$x-$dx+$dx/2; $c[]=$y+2*$dy-$dy;
434
		$img->SetLineWeight($weight);
435
		$img->SetColor($color);
436
		$img->Polygon($c);
437
		$img->SetLineWeight(1);
438
		$this->AddCSIMPoly($c);
439
		break;
440
	}
441
 
442
	if( $pts>0 ) {
443
	    $this->AddCSIMPoly($c);
444
	    $img->SetLineWeight($weight);
445
	    $img->SetColor($fcolor);
446
	    $img->FilledPolygon($c);
447
	    $img->SetColor($color);
448
	    $img->Polygon($c);
449
	    $img->SetLineWeight(1);
450
	}
451
	elseif( $this->type==MARK_CIRCLE ) {
452
	    $img->SetColor($color);
453
	    $img->Circle($x,$y,$width);
454
	    $this->AddCSIMCircle($x,$y,$width);
455
	}
456
	elseif( $this->type==MARK_FILLEDCIRCLE ) {
457
	    $img->SetColor($fcolor);
458
	    $img->FilledCircle($x,$y,$width);
459
	    $img->SetColor($color);
460
	    $img->Circle($x,$y,$width);
461
	    $this->AddCSIMCircle($x,$y,$width);
462
	}
463
	elseif( $this->type==MARK_CROSS ) {
464
	    // Oversize by a pixel to match the X
465
	    $img->SetColor($color);
466
	    $img->SetLineWeight($weight);
467
	    $img->Line($x,$y+$dy+1,$x,$y-$dy-1);
468
	    $img->Line($x-$dx-1,$y,$x+$dx+1,$y);
469
	    $this->AddCSIMCircle($x,$y,$dx);
470
	}
471
	elseif( $this->type==MARK_X ) {
472
	    $img->SetColor($color);
473
	    $img->SetLineWeight($weight);
474
	    $img->Line($x+$dx,$y+$dy,$x-$dx,$y-$dy);
475
	    $img->Line($x-$dx,$y+$dy,$x+$dx,$y-$dy);
476
	    $this->AddCSIMCircle($x,$y,$dx+$dy);
477
	}
478
	elseif( $this->type==MARK_STAR ) {
479
	    $img->SetColor($color);
480
	    $img->SetLineWeight($weight);
481
	    $img->Line($x+$dx,$y+$dy,$x-$dx,$y-$dy);
482
	    $img->Line($x-$dx,$y+$dy,$x+$dx,$y-$dy);
483
	    // Oversize by a pixel to match the X
484
	    $img->Line($x,$y+$dy+1,$x,$y-$dy-1);
485
	    $img->Line($x-$dx-1,$y,$x+$dx+1,$y);
486
	    $this->AddCSIMCircle($x,$y,$dx+$dy);
487
	}
488
 
489
	// Stroke title
490
	$this->title->Align("center","center");
491
	$this->title->Stroke($img,$x,$y);
492
    }
493
} // Class
494
 
495
 
496
?>