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_UTILS.INC
4
// Description: Collection of non-essential "nice to have" utilities
5
// Created: 	2005-11-20
6
// Ver:		$Id: jpgraph_utils.inc.php 859 2007-03-23 19:12:08Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
 
12
//===================================================
13
// CLASS FuncGenerator
14
// Description: Utility class to help generate data for function plots.
15
// The class supports both parametric and regular functions.
16
//===================================================
17
class FuncGenerator {
18
    var $iFunc='',$iXFunc='',$iMin,$iMax,$iStepSize;
19
 
20
    function FuncGenerator($aFunc,$aXFunc='') {
21
	$this->iFunc = $aFunc;
22
	$this->iXFunc = $aXFunc;
23
    }
24
 
25
    function E($aXMin,$aXMax,$aSteps=50) {
26
	$this->iMin = $aXMin;
27
	$this->iMax = $aXMax;
28
	$this->iStepSize = ($aXMax-$aXMin)/$aSteps;
29
 
30
	if( $this->iXFunc != '' )
31
	    $t = 'for($i='.$aXMin.'; $i<='.$aXMax.'; $i += '.$this->iStepSize.') {$ya[]='.$this->iFunc.';$xa[]='.$this->iXFunc.';}';
32
	elseif( $this->iFunc != '' )
33
	    $t = 'for($x='.$aXMin.'; $x<='.$aXMax.'; $x += '.$this->iStepSize.') {$ya[]='.$this->iFunc.';$xa[]=$x;} $x='.$aXMax.';$ya[]='.$this->iFunc.';$xa[]=$x;';
34
	else
35
	    JpGraphError::RaiseL(24001);//('FuncGenerator : No function specified. ');
36
 
37
	@eval($t);
38
 
39
	// If there is an error in the function specifcation this is the only
40
	// way we can discover that.
41
	if( empty($xa) || empty($ya) )
42
	    JpGraphError::RaiseL(24002);//('FuncGenerator : Syntax error in function specification ');
43
 
44
	return array($xa,$ya);
45
    }
46
}
47
 
48
//=============================================================================
49
// CLASS SymChar
50
// Description: Code values for some commonly used characters that
51
//              normally isn't available directly on the keyboard, for example
52
//              mathematical and greek symbols.
53
//=============================================================================
54
class  SymChar {
55
    function Get($aSymb,$aCapital=FALSE) {
56
        static $iSymbols = array(
57
    /* Greek */
58
	array('alpha','03B1','0391'),
59
	array('beta','03B2','0392'),
60
	array('gamma','03B3','0393'),
61
	array('delta','03B4','0394'),
62
	array('epsilon','03B5','0395'),
63
	array('zeta','03B6','0396'),
64
	array('ny','03B7','0397'),
65
	array('eta','03B8','0398'),
66
	array('theta','03B8','0398'),
67
	array('iota','03B9','0399'),
68
	array('kappa','03BA','039A'),
69
	array('lambda','03BB','039B'),
70
	array('mu','03BC','039C'),
71
	array('nu','03BD','039D'),
72
	array('xi','03BE','039E'),
73
	array('omicron','03BF','039F'),
74
	array('pi','03C0','03A0'),
75
	array('rho','03C1','03A1'),
76
	array('sigma','03C3','03A3'),
77
	array('tau','03C4','03A4'),
78
	array('upsilon','03C5','03A5'),
79
	array('phi','03C6','03A6'),
80
	array('chi','03C7','03A7'),
81
	array('psi','03C8','03A8'),
82
	array('omega','03C9','03A9'),
83
    /* Money */
84
	array('euro','20AC'),
85
	array('yen','00A5'),
86
	array('pound','20A4'),
87
    /* Math */
88
	array('approx','2248'),
89
	array('neq','2260'),
90
	array('not','2310'),
91
	array('def','2261'),
92
	array('inf','221E'),
93
	array('sqrt','221A'),
94
	array('int','222B'),
95
    /* Misc */
96
	array('copy','00A9'),
97
	array('para','00A7'));
98
 
99
	$n = count($iSymbols);
100
	$i=0;
101
	$found = false;
102
	$aSymb = strtolower($aSymb);
103
	while( $i < $n && !$found ) {
104
	    $found = $aSymb === $iSymbols[$i++][0];
105
	}
106
	if( $found ) {
107
	    $ca = $iSymbols[--$i];
108
	    if( $aCapital && count($ca)==3 )
109
		$s = $ca[2];
110
	    else
111
		$s = $ca[1];
112
	    return sprintf('&#%04d;',hexdec($s));
113
	}
114
	else
115
	    return '';
116
    }
117
}
118
 
119
 
120
//=============================================================================
121
// CLASS DateScaleUtils
122
// Description: Help to create a manual date scale
123
//=============================================================================
124
DEFINE('DSUTILS_MONTH',1); // Major and minor ticks on a monthly basis
125
DEFINE('DSUTILS_MONTH1',1); // Major and minor ticks on a monthly basis
126
DEFINE('DSUTILS_MONTH2',2); // Major ticks on a bi-monthly basis
127
DEFINE('DSUTILS_MONTH3',3); // Major icks on a tri-monthly basis
128
DEFINE('DSUTILS_MONTH6',4); // Major on a six-monthly basis
129
DEFINE('DSUTILS_WEEK1',5); // Major ticks on a weekly basis
130
DEFINE('DSUTILS_WEEK2',6); // Major ticks on a bi-weekly basis
131
DEFINE('DSUTILS_WEEK4',7); // Major ticks on a quod-weekly basis
132
DEFINE('DSUTILS_DAY1',8); // Major ticks on a daily basis
133
DEFINE('DSUTILS_DAY2',9); // Major ticks on a bi-daily basis
134
DEFINE('DSUTILS_DAY4',10); // Major ticks on a qoud-daily basis
135
DEFINE('DSUTILS_YEAR1',11); // Major ticks on a yearly basis
136
DEFINE('DSUTILS_YEAR2',12); // Major ticks on a bi-yearly basis
137
DEFINE('DSUTILS_YEAR5',13); // Major ticks on a five-yearly basis
138
 
139
 
140
class DateScaleUtils {
141
    var $starthour,$startmonth, $startday, $startyear;
142
    var $endmonth, $endyear, $endday;
143
    var $tickPositions=array(),$minTickPositions=array();
144
    var $iUseWeeks = true;
145
 
146
    function UseWeekFormat($aFlg) {
147
	$this->iUseWeeks = $aFlg;
148
    }
149
 
150
    function doYearly($aType,$aMinor=false) {
151
	$i=0; $j=0;
152
	$m = $this->startmonth;
153
	$y = $this->startyear;
154
 
155
	if( $this->startday == 1 ) {
156
	    $this->tickPositions[$i++] = mktime(0,0,0,$m,1,$y);
157
	}
158
	++$m;
159
 
160
	switch( $aType ) {
161
	    case DSUTILS_YEAR1:
162
		for($y=$this->startyear; $y <= $this->endyear; ++$y ) {
163
		    if( $aMinor ) {
164
			while( $m <= 12 ) {
165
			    if( !($y == $this->endyear && $m > $this->endmonth) ) {
166
				$this->minTickPositions[$j++] = mktime(0,0,0,$m,1,$y);
167
			    }
168
			    ++$m;
169
			}
170
			$m=1;
171
		    }
172
		    $this->tickPositions[$i++] = mktime(0,0,0,1,1,$y);
173
		}
174
		break;
175
	    case DSUTILS_YEAR2:
176
		$y=$this->startyear;
177
		while( $y <= $this->endyear ) {
178
		    $this->tickPositions[$i++] = mktime(0,0,0,1,1,$y);
179
		    for($k=0; $k < 1; ++$k ) {
180
			++$y;
181
			if( $aMinor ) {
182
			    $this->minTickPositions[$j++] = mktime(0,0,0,1,1,$y);
183
			}
184
		    }
185
		    ++$y;
186
		}
187
		break;
188
	    case DSUTILS_YEAR5:
189
		$y=$this->startyear;
190
		while( $y <= $this->endyear ) {
191
		    $this->tickPositions[$i++] = mktime(0,0,0,1,1,$y);
192
		    for($k=0; $k < 4; ++$k ) {
193
			++$y;
194
			if( $aMinor ) {
195
			    $this->minTickPositions[$j++] = mktime(0,0,0,1,1,$y);
196
			}
197
		    }
198
		    ++$y;
199
		}
200
		break;
201
	}
202
    }
203
 
204
    function doDaily($aType,$aMinor=false) {
205
	$m = $this->startmonth;
206
	$y = $this->startyear;
207
	$d = $this->startday;
208
	$h = $this->starthour;
209
	$i=0;$j=0;
210
 
211
	if( $h == 0 ) {
212
	    $this->tickPositions[$i++] = mktime(0,0,0,$m,$d,$y);
213
	    $t = mktime(0,0,0,$m,$d,$y);
214
	}
215
	else {
216
	    $t = mktime(0,0,0,$m,$d,$y);
217
	}
218
	switch($aType) {
219
	    case DSUTILS_DAY1:
220
		while( $t <= $this->iMax ) {
221
		    $t += 3600*24;
222
		    $this->tickPositions[$i++] = $t;
223
		}
224
		break;
225
	    case DSUTILS_DAY2:
226
		while( $t <= $this->iMax ) {
227
		    $t += 3600*24;
228
		    if( $aMinor ) {
229
			$this->minTickPositions[$j++] = $t;
230
		    }
231
		    $t += 3600*24;
232
		    $this->tickPositions[$i++] = $t;
233
		}
234
		break;
235
	    case DSUTILS_DAY4:
236
		while( $t <= $this->iMax ) {
237
		    for($k=0; $k < 3; ++$k ) {
238
			$t += 3600*24;
239
			if( $aMinor ) {
240
			    $this->minTickPositions[$j++] = $t;
241
			}
242
		    }
243
		    $t += 3600*24;
244
		    $this->tickPositions[$i++] = $t;
245
		}
246
		break;
247
	}
248
    }
249
 
250
    function doWeekly($aType,$aMinor=false) {
251
	$hpd = 3600*24;
252
	$hpw = 3600*24*7;
253
	// Find out week number of min date
254
	$thursday = $this->iMin + $hpd * (3 - (date('w', $this->iMin) + 6) % 7);
255
	$week = 1 + (date('z', $thursday) - (11 - date('w', mktime(0, 0, 0, 1, 1, date('Y', $thursday)))) % 7) / 7;
256
	$daynumber = date('w',$this->iMin);
257
	if( $daynumber == 0 ) $daynumber = 7;
258
	$m = $this->startmonth;
259
	$y = $this->startyear;
260
	$d = $this->startday;
261
	$i=0;$j=0;
262
	// The assumption is that the weeks start on Monday. If the first day
263
	// is later in the week then the first week tick has to be on the following
264
	// week.
265
	if( $daynumber == 1 ) {
266
	    $this->tickPositions[$i++] = mktime(0,0,0,$m,$d,$y);
267
	    $t = mktime(0,0,0,$m,$d,$y) + $hpw;
268
	}
269
	else {
270
	    $t = mktime(0,0,0,$m,$d,$y) + $hpd*(8-$daynumber);
271
	}
272
 
273
	switch($aType) {
274
	    case DSUTILS_WEEK1:
275
		$cnt=0;
276
		break;
277
	    case DSUTILS_WEEK2:
278
		$cnt=1;
279
		break;
280
	    case DSUTILS_WEEK4:
281
		$cnt=3;
282
		break;
283
	}
284
	while( $t <= $this->iMax ) {
285
	    $this->tickPositions[$i++] = $t;
286
	    for($k=0; $k < $cnt; ++$k ) {
287
		$t += $hpw;
288
		if( $aMinor ) {
289
		    $this->minTickPositions[$j++] = $t;
290
		}
291
	    }
292
	    $t += $hpw;
293
	}
294
    }
295
 
296
    function doMonthly($aType,$aMinor=false) {
297
	$monthcount=0;
298
	$m = $this->startmonth;
299
	$y = $this->startyear;
300
	$i=0; $j=0;
301
 
302
	// Skip the first month label if it is before the startdate
303
	if( $this->startday == 1 ) {
304
	    $this->tickPositions[$i++] = mktime(0,0,0,$m,1,$y);
305
	    $monthcount=1;
306
	}
307
	if( $aType == 1 ) {
308
	    if( $this->startday < 15 ) {
309
		$this->minTickPositions[$j++] = mktime(0,0,0,$m,15,$y);
310
	    }
311
	}
312
	++$m;
313
 
314
	// Loop through all the years included in the scale
315
	for($y=$this->startyear; $y <= $this->endyear; ++$y ) {
316
	    // Loop through all the months. There are three cases to consider:
317
	    // 1. We are in the first year and must start with the startmonth
318
	    // 2. We are in the end year and we must stop at last month of the scale
319
	    // 3. A year in between where we run through all the 12 months
320
	    $stopmonth = $y == $this->endyear ? $this->endmonth : 12;
321
	    while( $m <= $stopmonth ) {
322
		switch( $aType ) {
323
		    case DSUTILS_MONTH1:
324
			// Set minor tick at the middle of the month
325
			if( $aMinor ) {
326
			    if( $m <= $stopmonth ) {
327
				if( !($y==$this->endyear && $m==$stopmonth && $this->endday < 15) )
328
				$this->minTickPositions[$j++] = mktime(0,0,0,$m,15,$y);
329
			    }
330
			}
331
			// Major at month
332
			// Get timestamp of first hour of first day in each month
333
			$this->tickPositions[$i++] = mktime(0,0,0,$m,1,$y);
334
 
335
			break;
336
		    case DSUTILS_MONTH2:
337
			if( $aMinor ) {
338
			    // Set minor tick at start of each month
339
			    $this->minTickPositions[$j++] = mktime(0,0,0,$m,1,$y);
340
			}
341
 
342
			// Major at every second month
343
			// Get timestamp of first hour of first day in each month
344
			if( $monthcount % 2 == 0 ) {
345
			    $this->tickPositions[$i++] = mktime(0,0,0,$m,1,$y);
346
			}
347
			break;
348
		    case DSUTILS_MONTH3:
349
			if( $aMinor ) {
350
			    // Set minor tick at start of each month
351
			    $this->minTickPositions[$j++] = mktime(0,0,0,$m,1,$y);
352
			}
353
			    // Major at every third month
354
			// Get timestamp of first hour of first day in each month
355
			if( $monthcount % 3 == 0 ) {
356
			    $this->tickPositions[$i++] = mktime(0,0,0,$m,1,$y);
357
			}
358
			break;
359
		    case DSUTILS_MONTH6:
360
			if( $aMinor ) {
361
			    // Set minor tick at start of each month
362
			    $this->minTickPositions[$j++] = mktime(0,0,0,$m,1,$y);
363
			}
364
			// Major at every third month
365
			// Get timestamp of first hour of first day in each month
366
			if( $monthcount % 6 == 0 ) {
367
			    $this->tickPositions[$i++] = mktime(0,0,0,$m,1,$y);
368
			}
369
			break;
370
		}
371
		++$m;
372
		++$monthcount;
373
	    }
374
	    $m=1;
375
	}
376
 
377
	// For the case where all dates are within the same month
378
	// we want to make sure we have at least two ticks on the scale
379
	// since the scale want work properly otherwise
380
	if($this->startmonth == $this->endmonth && $this->startyear == $this->endyear && $aType==1 ) {
381
	    $this->tickPositions[$i++] = mktime(0 ,0 ,0, $this->startmonth + 1, 1, $this->startyear);
382
	}
383
 
384
	return array($this->tickPositions,$this->minTickPositions);
385
    }
386
 
387
    function GetTicks($aData,$aType=1,$aMinor=false,$aEndPoints=false) {
388
	$n = count($aData);
389
	return $this->GetTicksFromMinMax($aData[0],$aData[$n-1],$aType,$aMinor,$aEndPoints);
390
    }
391
 
392
    function GetAutoTicks($aMin,$aMax,$aMaxTicks=10,$aMinor=false) {
393
	$diff = $aMax - $aMin;
394
	$spd = 3600*24;
395
	$spw = $spd*7;
396
	$spm = $spd*30;
397
	$spy = $spd*352;
398
 
399
	if( $this->iUseWeeks )
400
	    $w = 'W';
401
	else
402
	    $w = 'd M';
403
 
404
	// Decision table for suitable scales
405
	// First value: Main decision point
406
	// Second value: Array of formatting depending on divisor for wanted max number of ticks. <divisor><formatting><format-string>,..
407
	$tt = array(
408
	    array($spw, array(1,DSUTILS_DAY1,'d M',2,DSUTILS_DAY2,'d M',-1,DSUTILS_DAY4,'d M')),
409
	    array($spm, array(1,DSUTILS_DAY1,'d M',2,DSUTILS_DAY2,'d M',4,DSUTILS_DAY4,'d M',
410
			      7,DSUTILS_WEEK1,$w,-1,DSUTILS_WEEK2,$w)),
411
	    array($spy, array(1,DSUTILS_DAY1,'d M',2,DSUTILS_DAY2,'d M',4,DSUTILS_DAY4,'d M',
412
			      7,DSUTILS_WEEK1,$w,14,DSUTILS_WEEK2,$w,
413
			      30,DSUTILS_MONTH1,'M',60,DSUTILS_MONTH2,'M',-1,DSUTILS_MONTH3,'M')),
414
	    array(-1, array(30,DSUTILS_MONTH1,'M-Y',60,DSUTILS_MONTH2,'M-Y',90,DSUTILS_MONTH3,'M-Y',
415
			    180,DSUTILS_MONTH6,'M-Y',352,DSUTILS_YEAR1,'Y',704,DSUTILS_YEAR2,'Y',-1,DSUTILS_YEAR5,'Y')));
416
 
417
	$ntt = count($tt);
418
	$nd = floor($diff/$spd);
419
	for($i=0; $i < $ntt; ++$i ) {
420
	    if( $diff <= $tt[$i][0] || $i==$ntt-1) {
421
		$t = $tt[$i][1];
422
		$n = count($t)/3;
423
		for( $j=0; $j < $n; ++$j ) {
424
		    if( $nd/$t[3*$j] <= $aMaxTicks || $j==$n-1) {
425
			$type = $t[3*$j+1];
426
			$fs = $t[3*$j+2];
427
			list($tickPositions,$minTickPositions) = $this->GetTicksFromMinMax($aMin,$aMax,$type,$aMinor);
428
			return array($fs,$tickPositions,$minTickPositions,$type);
429
		    }
430
		}
431
	    }
432
	}
433
    }
434
 
435
    function GetTicksFromMinMax($aMin,$aMax,$aType,$aMinor=false,$aEndPoints=false) {
436
	$this->starthour = date('G',$aMin);
437
	$this->startmonth = date('n',$aMin);
438
	$this->startday = date('j',$aMin);
439
	$this->startyear = date('Y',$aMin);
440
	$this->endmonth = date('n',$aMax);
441
	$this->endyear = date('Y',$aMax);
442
	$this->endday = date('j',$aMax);
443
	$this->iMin = $aMin;
444
	$this->iMax = $aMax;
445
 
446
	if( $aType <= DSUTILS_MONTH6 ) {
447
	    $this->doMonthly($aType,$aMinor);
448
	}
449
	elseif( $aType <= DSUTILS_WEEK4 ) {
450
	    $this->doWeekly($aType,$aMinor);
451
	}
452
	elseif( $aType <= DSUTILS_DAY4 ) {
453
	    $this->doDaily($aType,$aMinor);
454
	}
455
	elseif( $aType <= DSUTILS_YEAR5 ) {
456
	    $this->doYearly($aType,$aMinor);
457
	}
458
	else {
459
	    JpGraphError::RaiseL(24003);
460
	}
461
	// put a label at the very left data pos
462
	if( $aEndPoints ) {
463
	    $tickPositions[$i++] = $aData[0];
464
	}
465
 
466
	// put a label at the very right data pos
467
	if( $aEndPoints ) {
468
	    $tickPositions[$i] = $aData[$n-1];
469
	}
470
 
471
	return array($this->tickPositions,$this->minTickPositions);
472
    }
473
 
474
}
475
 
476
//=============================================================================
477
// Class ReadFileData
478
//=============================================================================
479
Class ReadFileData {
480
 
481
    //----------------------------------------------------------------------------
482
    // Desciption:
483
    // Read numeric data from a file.
484
    // Each value should be separated by either a new line or by a specified
485
    // separator character (default is ',').
486
    // Before returning the data each value is converted to a proper float
487
    // value. The routine is robust in the sense that non numeric data in the
488
    // file will be discarded.
489
    //
490
    // Returns:
491
    // The number of data values read on success, FALSE on failure
492
    //----------------------------------------------------------------------------
493
    function FromCSV($aFile,&$aData,$aSepChar=',',$aMaxLineLength=1024) {
494
	$rh = fopen($aFile,'r');
495
	if( $rh === false )
496
	    return false;
497
	$tmp = array();
498
	$lineofdata = fgetcsv($rh, 1000, ',');
499
	while ( $lineofdata !== FALSE) {
500
	    $tmp = array_merge($tmp,$lineofdata);
501
	    $lineofdata = fgetcsv($rh, $aMaxLineLength, $aSepChar);
502
	}
503
	fclose($rh);
504
 
505
	// Now make sure that all data is numeric. By default
506
	// all data is read as strings
507
	$n = count($tmp);
508
	$aData = array();
509
	$cnt=0;
510
	for($i=0; $i < $n; ++$i) {
511
	    if( $tmp[$i] !== "" ) {
512
		$aData[$cnt++] = floatval($tmp[$i]);
513
	    }
514
	}
515
	return $cnt;
516
    }
517
}
518
 
519
?>