Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 861 2007-03-23 19:18:17Z 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,$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->csimalt = '';
96
	$this->type=-1;
97
    }
98
//---------------
99
// PUBLIC METHODS
100
    function SetType($aType,$aFileName='',$aScale=1.0) {
101
	$this->type = $aType;
102
	if( $aType == MARK_IMG && $aFileName=='' ) {
103
	    JpGraphError::RaiseL(23003);//('A filename must be specified if you set the mark type to MARK_IMG.');
104
	}
105
	$this->iFileName = $aFileName;
106
	$this->iScale = $aScale;
107
    }
108
 
109
    function SetCallback($aFunc) {
110
	$this->iFormatCallback = $aFunc;
111
    }
112
 
113
    function SetCallbackYX($aFunc) {
114
	$this->iFormatCallback2 = $aFunc;
115
    }
116
 
117
    function GetType() {
118
	return $this->type;
119
    }
120
 
121
    function SetColor($aColor) {
122
	$this->color=$aColor;
123
    }
124
 
125
    function SetFillColor($aFillColor) {
126
	$this->fill_color = $aFillColor;
127
    }
128
 
129
    function SetWeight($aWeight) {
130
	$this->weight = $aWeight;
131
    }
132
 
133
    // Synonym for SetWidth()
134
    function SetSize($aWidth) {
135
	$this->width=$aWidth;
136
    }
137
 
138
    function SetWidth($aWidth) {
139
	$this->width=$aWidth;
140
    }
141
 
142
    function SetDefaultWidth() {
143
	switch( $this->type ) {
144
	    case MARK_CIRCLE:
145
	    case MARK_FILLEDCIRCLE:
146
		$this->width=4;
147
		break;
148
	    default:
149
		$this->width=7;
150
	}
151
    }
152
 
153
    function GetWidth() {
154
	return $this->width;
155
    }
156
 
157
    function Hide($aHide=true) {
158
	$this->show = !$aHide;
159
    }
160
 
161
    function Show($aShow=true) {
162
	$this->show = $aShow;
163
    }
164
 
165
    function SetCSIMAltVal($aY,$aX='') {
166
        $this->yvalue=$aY;
167
        $this->xvalue=$aX;
168
    }
169
 
170
    function SetCSIMTarget($aTarget) {
171
        $this->csimtarget=$aTarget;
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
	    if( !empty($this->csimalt) ) {
192
		$tmp=sprintf($this->csimalt,$this->yvalue,$this->xvalue);
193
		$this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
194
	    }
195
	    $this->csimareas .= " />\n";
196
	}
197
    }
198
 
199
    function AddCSIMCircle($x,$y,$r) {
200
    	$x = round($x); $y=round($y); $r=round($r);
201
        $this->csimareas="";
202
        if( !empty($this->csimtarget) ) {
203
	    $this->csimareas .= "<area shape=\"circle\" coords=\"$x,$y,$r\" href=\"".htmlentities($this->csimtarget)."\"";
204
    	    if( !empty($this->csimalt) ) {
205
		$tmp=sprintf($this->csimalt,$this->yvalue,$this->xvalue);
206
		$this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
207
	    }
208
            $this->csimareas .= " />\n";
209
        }
210
    }
211
 
212
    function Stroke(&$img,$x,$y) {
213
	if( !$this->show ) return;
214
 
215
	if( $this->iFormatCallback != '' || $this->iFormatCallback2 != '' ) {
216
 
217
	    if( $this->iFormatCallback != '' ) {
218
		$f = $this->iFormatCallback;
219
		list($width,$color,$fcolor) = call_user_func($f,$this->yvalue);
220
		$filename = $this->iFileName;
221
		$imgscale = $this->iScale;
222
	    }
223
	    else {
224
		$f = $this->iFormatCallback2;
225
		list($width,$color,$fcolor,$filename,$imgscale) = call_user_func($f,$this->yvalue,$this->xvalue);
226
		if( $filename=="" ) $filename = $this->iFileName;
227
		if( $imgscale=="" ) $imgscale = $this->iScale;
228
	    }
229
 
230
	    if( $width=="" ) $width = $this->width;
231
	    if( $color=="" ) $color = $this->color;
232
	    if( $fcolor=="" ) $fcolor = $this->fill_color;
233
 
234
	}
235
	else {
236
	    $fcolor = $this->fill_color;
237
	    $color = $this->color;
238
	    $width = $this->width;
239
	    $filename = $this->iFileName;
240
	    $imgscale = $this->iScale;
241
	}
242
 
243
	if( $this->type == MARK_IMG ||
244
	    ($this->type >= MARK_FLAG1 && $this->type <= MARK_FLAG4 ) ||
245
	    $this->type >= MARK_IMG_PUSHPIN ) {
246
 
247
	    // Note: For the builtin images we use the "filename" parameter
248
	    // to denote the color
249
	    $anchor_x = 0.5;
250
	    $anchor_y = 0.5;
251
	    switch( $this->type ) {
252
		case MARK_FLAG1:
253
		case MARK_FLAG2:
254
		case MARK_FLAG3:
255
		case MARK_FLAG4:
256
		    $this->markimg = FlagCache::GetFlagImgByName($this->type-MARK_FLAG1+1,$filename);
257
		    break;
258
 
259
		case MARK_IMG :
260
		    // Load an image and use that as a marker
261
		    // Small optimization, if we have already read an image don't
262
		    // waste time reading it again.
263
		    if( $this->markimg == '' || !($this->oldfilename === $filename) ) {
264
			$this->markimg = Graph::LoadBkgImage('',$filename);
265
			$this->oldfilename = $filename ;
266
		    }
267
		    break;
268
 
269
		case MARK_IMG_PUSHPIN:
270
		case MARK_IMG_SPUSHPIN:
271
		case MARK_IMG_LPUSHPIN:
272
		    if( $this->imgdata_pushpins == null ) {
273
			require_once 'imgdata_pushpins.inc.php';
274
			$this->imgdata_pushpins = new ImgData_PushPins();
275
		    }
276
		    $this->markimg = $this->imgdata_pushpins->GetImg($this->type,$filename);
277
		    list($anchor_x,$anchor_y) = $this->imgdata_pushpins->GetAnchor();
278
		    break;
279
 
280
		case MARK_IMG_SQUARE:
281
		    if( $this->imgdata_squares == null ) {
282
			require_once 'imgdata_squares.inc.php';
283
			$this->imgdata_squares = new ImgData_Squares();
284
		    }
285
		    $this->markimg = $this->imgdata_squares->GetImg($this->type,$filename);
286
		    list($anchor_x,$anchor_y) = $this->imgdata_squares->GetAnchor();
287
		    break;
288
 
289
		case MARK_IMG_STAR:
290
		    if( $this->imgdata_stars == null ) {
291
			require_once 'imgdata_stars.inc.php';
292
			$this->imgdata_stars = new ImgData_Stars();
293
		    }
294
		    $this->markimg = $this->imgdata_stars->GetImg($this->type,$filename);
295
		    list($anchor_x,$anchor_y) = $this->imgdata_stars->GetAnchor();
296
		    break;
297
 
298
		case MARK_IMG_BEVEL:
299
		    if( $this->imgdata_bevels == null ) {
300
			require_once 'imgdata_bevels.inc.php';
301
			$this->imgdata_bevels = new ImgData_Bevels();
302
		    }
303
		    $this->markimg = $this->imgdata_bevels->GetImg($this->type,$filename);
304
		    list($anchor_x,$anchor_y) = $this->imgdata_bevels->GetAnchor();
305
		    break;
306
 
307
		case MARK_IMG_DIAMOND:
308
		    if( $this->imgdata_diamonds == null ) {
309
			require_once 'imgdata_diamonds.inc.php';
310
			$this->imgdata_diamonds = new ImgData_Diamonds();
311
		    }
312
		    $this->markimg = $this->imgdata_diamonds->GetImg($this->type,$filename);
313
		    list($anchor_x,$anchor_y) = $this->imgdata_diamonds->GetAnchor();
314
		    break;
315
 
316
		case MARK_IMG_BALL:
317
		case MARK_IMG_SBALL:
318
		case MARK_IMG_MBALL:
319
		case MARK_IMG_LBALL:
320
		    if( $this->imgdata_balls == null ) {
321
			require_once 'imgdata_balls.inc.php';
322
			$this->imgdata_balls = new ImgData_Balls();
323
		    }
324
		    $this->markimg = $this->imgdata_balls->GetImg($this->type,$filename);
325
		    list($anchor_x,$anchor_y) = $this->imgdata_balls->GetAnchor();
326
		    break;
327
	    }
328
 
329
	    $w = $img->GetWidth($this->markimg);
330
	    $h = $img->GetHeight($this->markimg);
331
 
332
	    $dw = round($imgscale * $w );
333
	    $dh = round($imgscale * $h );
334
 
335
	    // Do potential rotation
336
	    list($x,$y) = $img->Rotate($x,$y);
337
 
338
	    $dx = round($x-$dw*$anchor_x);
339
	    $dy = round($y-$dh*$anchor_y);
340
 
341
	    $this->width = max($dx,$dy);
342
 
343
	    $img->Copy($this->markimg,$dx,$dy,0,0,$dw,$dh,$w,$h);
344
	    if( !empty($this->csimtarget) ) {
345
		$this->csimareas = "<area shape=\"rect\" coords=\"".
346
		    $dx.','.$dy.','.round($dx+$dw).','.round($dy+$dh).'" '.
347
		    "href=\"".htmlentities($this->csimtarget)."\"";
348
		if( !empty($this->csimalt) ) {
349
		    $tmp=sprintf($this->csimalt,$this->yvalue,$this->xvalue);
350
		    $this->csimareas .= " title=\"$tmp\"  alt=\"$tmp\" ";
351
		}
352
		$this->csimareas .= " />\n";
353
	    }
354
 
355
	    // Stroke title
356
	    $this->title->Align("center","top");
357
	    $this->title->Stroke($img,$x,$y+round($dh/2));
358
	    return;
359
	}
360
 
361
	$weight = $this->weight;
362
	$dx=round($width/2,0);
363
	$dy=round($width/2,0);
364
	$pts=0;
365
 
366
	switch( $this->type ) {
367
	    case MARK_SQUARE:
368
		$c[]=$x-$dx;$c[]=$y-$dy;
369
		$c[]=$x+$dx;$c[]=$y-$dy;
370
		$c[]=$x+$dx;$c[]=$y+$dy;
371
		$c[]=$x-$dx;$c[]=$y+$dy;
372
		$c[]=$x-$dx;$c[]=$y-$dy;
373
		$pts=5;
374
		break;
375
	    case MARK_UTRIANGLE:
376
		++$dx;++$dy;
377
		$c[]=$x-$dx;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
378
		$c[]=$x;$c[]=$y-0.87*$dy;
379
		$c[]=$x+$dx;$c[]=$y+0.87*$dy;
380
		$c[]=$x-$dx;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
381
		$pts=4;
382
		break;
383
	    case MARK_DTRIANGLE:
384
		++$dx;++$dy;
385
		$c[]=$x;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
386
		$c[]=$x-$dx;$c[]=$y-0.87*$dy;
387
		$c[]=$x+$dx;$c[]=$y-0.87*$dy;
388
		$c[]=$x;$c[]=$y+0.87*$dy;	// tan(60)/2*$dx
389
		$pts=4;
390
		break;
391
	    case MARK_DIAMOND:
392
		$c[]=$x;$c[]=$y+$dy;
393
		$c[]=$x-$dx;$c[]=$y;
394
		$c[]=$x;$c[]=$y-$dy;
395
		$c[]=$x+$dx;$c[]=$y;
396
		$c[]=$x;$c[]=$y+$dy;
397
		$pts=5;
398
		break;
399
	    case MARK_LEFTTRIANGLE:
400
		$c[]=$x;$c[]=$y;
401
		$c[]=$x;$c[]=$y+2*$dy;
402
		$c[]=$x+$dx*2;$c[]=$y;
403
		$c[]=$x;$c[]=$y;
404
		$pts=4;
405
		break;
406
	    case MARK_RIGHTTRIANGLE:
407
		$c[]=$x-$dx*2;$c[]=$y;
408
		$c[]=$x;$c[]=$y+2*$dy;
409
		$c[]=$x;$c[]=$y;
410
		$c[]=$x-$dx*2;$c[]=$y;
411
		$pts=4;
412
		break;
413
	    case MARK_FLASH:
414
		$dy *= 2;
415
		$c[]=$x+$dx/2; $c[]=$y-$dy;
416
		$c[]=$x-$dx+$dx/2; $c[]=$y+$dy*0.7-$dy;
417
		$c[]=$x+$dx/2; $c[]=$y+$dy*1.3-$dy;
418
		$c[]=$x-$dx+$dx/2; $c[]=$y+2*$dy-$dy;
419
		$img->SetLineWeight($weight);
420
		$img->SetColor($color);
421
		$img->Polygon($c);
422
		$img->SetLineWeight(1);
423
		$this->AddCSIMPoly($c);
424
		break;
425
	}
426
 
427
	if( $pts>0 ) {
428
	    $this->AddCSIMPoly($c);
429
	    $img->SetLineWeight($weight);
430
	    $img->SetColor($fcolor);
431
	    $img->FilledPolygon($c);
432
	    $img->SetColor($color);
433
	    $img->Polygon($c);
434
	    $img->SetLineWeight(1);
435
	}
436
	elseif( $this->type==MARK_CIRCLE ) {
437
	    $img->SetColor($color);
438
	    $img->Circle($x,$y,$width);
439
	    $this->AddCSIMCircle($x,$y,$width);
440
	}
441
	elseif( $this->type==MARK_FILLEDCIRCLE ) {
442
	    $img->SetColor($fcolor);
443
	    $img->FilledCircle($x,$y,$width);
444
	    $img->SetColor($color);
445
	    $img->Circle($x,$y,$width);
446
	    $this->AddCSIMCircle($x,$y,$width);
447
	}
448
	elseif( $this->type==MARK_CROSS ) {
449
	    // Oversize by a pixel to match the X
450
	    $img->SetColor($color);
451
	    $img->SetLineWeight($weight);
452
	    $img->Line($x,$y+$dy+1,$x,$y-$dy-1);
453
	    $img->Line($x-$dx-1,$y,$x+$dx+1,$y);
454
	    $this->AddCSIMCircle($x,$y,$dx);
455
	}
456
	elseif( $this->type==MARK_X ) {
457
	    $img->SetColor($color);
458
	    $img->SetLineWeight($weight);
459
	    $img->Line($x+$dx,$y+$dy,$x-$dx,$y-$dy);
460
	    $img->Line($x-$dx,$y+$dy,$x+$dx,$y-$dy);
461
	    $this->AddCSIMCircle($x,$y,$dx+$dy);
462
	}
463
	elseif( $this->type==MARK_STAR ) {
464
	    $img->SetColor($color);
465
	    $img->SetLineWeight($weight);
466
	    $img->Line($x+$dx,$y+$dy,$x-$dx,$y-$dy);
467
	    $img->Line($x-$dx,$y+$dy,$x+$dx,$y-$dy);
468
	    // Oversize by a pixel to match the X
469
	    $img->Line($x,$y+$dy+1,$x,$y-$dy-1);
470
	    $img->Line($x-$dx-1,$y,$x+$dx+1,$y);
471
	    $this->AddCSIMCircle($x,$y,$dx+$dy);
472
	}
473
 
474
	// Stroke title
475
	$this->title->Align("center","center");
476
	$this->title->Stroke($img,$x,$y);
477
    }
478
} // Class
479
 
480
 
481
?>