Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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