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_LINE.PHP
4
// Description:	Line plot extension for JpGraph
5
// Created: 	2001-01-08
6
// Ver:		$Id: jpgraph_line.php 857 2007-03-23 19:03:13Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
 
12
require_once ('jpgraph_plotmark.inc.php');
13
 
14
// constants for the (filled) area
15
DEFINE("LP_AREA_FILLED", true);
16
DEFINE("LP_AREA_NOT_FILLED", false);
17
DEFINE("LP_AREA_BORDER",false);
18
DEFINE("LP_AREA_NO_BORDER",true);
19
 
20
//===================================================
21
// CLASS LinePlot
22
// Description:
23
//===================================================
24
class LinePlot extends Plot{
25
    var $filled=false;
26
    var $fill_color='blue';
27
    var $mark=null;
28
    var $step_style=false, $center=false;
29
    var $line_style=1;	// Default to solid
30
    var $filledAreas = array(); // array of arrays(with min,max,col,filled in them)
31
    var $barcenter=false;  // When we mix line and bar. Should we center the line in the bar.
32
    var $fillFromMin = false ;
33
    var $fillgrad=false,$fillgrad_fromcolor='navy',$fillgrad_tocolor='silver',$fillgrad_numcolors=100;
34
    var $iFastStroke=false;
35
 
36
//---------------
37
// CONSTRUCTOR
38
    function LinePlot(&$datay,$datax=false) {
39
	$this->Plot($datay,$datax);
40
	$this->mark = new PlotMark();
41
    }
42
//---------------
43
// PUBLIC METHODS
44
 
45
    // Set style, filled or open
46
    function SetFilled($aFlag=true) {
47
    	JpGraphError::RaiseL(10001);//('LinePlot::SetFilled() is deprecated. Use SetFillColor()');
48
    }
49
 
50
    function SetBarCenter($aFlag=true) {
51
	$this->barcenter=$aFlag;
52
    }
53
 
54
    function SetStyle($aStyle) {
55
	$this->line_style=$aStyle;
56
    }
57
 
58
    function SetStepStyle($aFlag=true) {
59
	$this->step_style = $aFlag;
60
    }
61
 
62
    function SetColor($aColor) {
63
	parent::SetColor($aColor);
64
    }
65
 
66
    function SetFillFromYMin($f=true) {
67
	$this->fillFromMin = $f ;
68
    }
69
 
70
    function SetFillColor($aColor,$aFilled=true) {
71
	$this->fill_color=$aColor;
72
	$this->filled=$aFilled;
73
    }
74
 
75
    function SetFillGradient($aFromColor,$aToColor,$aNumColors=100,$aFilled=true) {
76
	$this->fillgrad_fromcolor = $aFromColor;
77
	$this->fillgrad_tocolor   = $aToColor;
78
	$this->fillgrad_numcolors = $aNumColors;
79
	$this->filled = $aFilled;
80
	$this->fillgrad = true;
81
    }
82
 
83
    function Legend(&$graph) {
84
	if( $this->legend!="" ) {
85
	    if( $this->filled && !$this->fillgrad ) {
86
		$graph->legend->Add($this->legend,
87
				    $this->fill_color,$this->mark,0,
88
				    $this->legendcsimtarget,$this->legendcsimalt);
89
	    }
90
	    elseif( $this->fillgrad ) {
91
		$color=array($this->fillgrad_fromcolor,$this->fillgrad_tocolor);
92
		// In order to differentiate between gradients and cooors specified as an RGB triple
93
		$graph->legend->Add($this->legend,$color,"",-2 /* -GRAD_HOR */,
94
				    $this->legendcsimtarget,$this->legendcsimalt);
95
	    }
96
	    else {
97
		$graph->legend->Add($this->legend,
98
				    $this->color,$this->mark,$this->line_style,
99
				    $this->legendcsimtarget,$this->legendcsimalt);
100
	    }
101
	}
102
    }
103
 
104
    function AddArea($aMin=0,$aMax=0,$aFilled=LP_AREA_NOT_FILLED,$aColor="gray9",$aBorder=LP_AREA_BORDER) {
105
	if($aMin > $aMax) {
106
	    // swap
107
	    $tmp = $aMin;
108
	    $aMin = $aMax;
109
	    $aMax = $tmp;
110
	}
111
	$this->filledAreas[] = array($aMin,$aMax,$aColor,$aFilled,$aBorder);
112
    }
113
 
114
    // Gets called before any axis are stroked
115
    function PreStrokeAdjust(&$graph) {
116
 
117
	// If another plot type have already adjusted the
118
	// offset we don't touch it.
119
	// (We check for empty in case the scale is  a log scale
120
	// and hence doesn't contain any xlabel_offset)
121
	if( empty($graph->xaxis->scale->ticks->xlabel_offset) ||
122
	    $graph->xaxis->scale->ticks->xlabel_offset == 0 ) {
123
	    if( $this->center ) {
124
		++$this->numpoints;
125
		$a=0.5; $b=0.5;
126
	    } else {
127
		$a=0; $b=0;
128
	    }
129
	    $graph->xaxis->scale->ticks->SetXLabelOffset($a);
130
	    $graph->SetTextScaleOff($b);
131
	    //$graph->xaxis->scale->ticks->SupressMinorTickMarks();
132
	}
133
    }
134
 
135
    function SetFastStroke($aFlg=true) {
136
	$this->iFastStroke = $aFlg;
137
    }
138
 
139
    function FastStroke(&$img,&$xscale,&$yscale,$aStartPoint=0,$exist_x=true) {
140
	// An optimized stroke for many data points with no extra
141
	// features but 60% faster. You can't have values or line styles, or null
142
	// values in plots.
143
	$numpoints=count($this->coords[0]);
144
	if( $this->barcenter )
145
	    $textadj = 0.5-$xscale->text_scale_off;
146
	else
147
	    $textadj = 0;
148
 
149
	$img->SetColor($this->color);
150
	$img->SetLineWeight($this->weight);
151
	$pnts=$aStartPoint;
152
	while( $pnts < $numpoints ) {
153
	    if( $exist_x ) $x=$this->coords[1][$pnts];
154
	    else $x=$pnts+$textadj;
155
	    $xt = $xscale->Translate($x);
156
	    $y=$this->coords[0][$pnts];
157
	    $yt = $yscale->Translate($y);
158
	    if( is_numeric($y) ) {
159
		$cord[] = $xt;
160
		$cord[] = $yt;
161
	    }
162
	    elseif( $y == '-' && $pnts > 0 ) {
163
		// Just ignore
164
	    }
165
	    else {
166
		JpGraphError::RaiseL(10002);//('Plot too complicated for fast line Stroke. Use standard Stroke()');
167
		return;
168
	    }
169
	    ++$pnts;
170
	} // WHILE
171
 
172
	$img->Polygon($cord,false,true);
173
 
174
    }
175
 
176
    function Stroke(&$img,&$xscale,&$yscale) {
177
	$idx=0;
178
	$numpoints=count($this->coords[0]);
179
	if( isset($this->coords[1]) ) {
180
	    if( count($this->coords[1])!=$numpoints )
181
		JpGraphError::RaiseL(2003,count($this->coords[1]),$numpoints);
182
//("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])." Number of Y-points:$numpoints");
183
	    else
184
		$exist_x = true;
185
	}
186
	else
187
	    $exist_x = false;
188
 
189
	if( $this->barcenter )
190
	    $textadj = 0.5-$xscale->text_scale_off;
191
	else
192
	    $textadj = 0;
193
 
194
	// Find the first numeric data point
195
	$startpoint=0;
196
	while( $startpoint < $numpoints && !is_numeric($this->coords[0][$startpoint]) )
197
	    ++$startpoint;
198
 
199
	// Bail out if no data points
200
	if( $startpoint == $numpoints )
201
	    return;
202
 
203
	if( $this->iFastStroke ) {
204
	    $this->FastStroke($img,$xscale,$yscale,$startpoint,$exist_x);
205
	    return;
206
	}
207
 
208
	if( $exist_x )
209
	    $xs=$this->coords[1][$startpoint];
210
	else
211
	    $xs= $textadj+$startpoint;
212
 
213
	$img->SetStartPoint($xscale->Translate($xs),
214
			    $yscale->Translate($this->coords[0][$startpoint]));
215
 
216
	if( $this->filled ) {
217
	    $min = $yscale->GetMinVal();
218
	    if( $min > 0 || $this->fillFromMin )
219
		$fillmin = $yscale->scale_abs[0];//Translate($min);
220
	    else
221
		$fillmin = $yscale->Translate(0);
222
 
223
	    $cord[$idx++] = $xscale->Translate($xs);
224
	    $cord[$idx++] = $fillmin;
225
	}
226
	$xt = $xscale->Translate($xs);
227
	$yt = $yscale->Translate($this->coords[0][$startpoint]);
228
	$cord[$idx++] = $xt;
229
	$cord[$idx++] = $yt;
230
	$yt_old = $yt;
231
	$xt_old = $xt;
232
	$y_old = $this->coords[0][$startpoint];
233
 
234
	$this->value->Stroke($img,$this->coords[0][$startpoint],$xt,$yt);
235
 
236
	$img->SetColor($this->color);
237
	$img->SetLineWeight($this->weight);
238
	$img->SetLineStyle($this->line_style);
239
	$pnts=$startpoint+1;
240
	$firstnonumeric = false;
241
	while( $pnts < $numpoints ) {
242
 
243
	    if( $exist_x ) $x=$this->coords[1][$pnts];
244
	    else $x=$pnts+$textadj;
245
	    $xt = $xscale->Translate($x);
246
	    $yt = $yscale->Translate($this->coords[0][$pnts]);
247
 
248
	    $y=$this->coords[0][$pnts];
249
	    if( $this->step_style ) {
250
		// To handle null values within step style we need to record the
251
		// first non numeric value so we know from where to start if the
252
		// non value is '-'.
253
		if( is_numeric($y) ) {
254
		    $firstnonumeric = false;
255
		    if( is_numeric($y_old) ) {
256
			$img->StyleLine($xt_old,$yt_old,$xt,$yt_old);
257
			$img->StyleLine($xt,$yt_old,$xt,$yt);
258
		    }
259
		    elseif( $y_old == '-' ) {
260
			$img->StyleLine($xt_first,$yt_first,$xt,$yt_first);
261
			$img->StyleLine($xt,$yt_first,$xt,$yt);
262
		    }
263
		    else {
264
			$yt_old = $yt;
265
			$xt_old = $xt;
266
		    }
267
		    $cord[$idx++] = $xt;
268
		    $cord[$idx++] = $yt_old;
269
		    $cord[$idx++] = $xt;
270
		    $cord[$idx++] = $yt;
271
		}
272
		elseif( $firstnonumeric==false ) {
273
		    $firstnonumeric = true;
274
		    $yt_first = $yt_old;
275
		    $xt_first = $xt_old;
276
		}
277
	    }
278
	    else {
279
		$tmp1=$y;
280
		$prev=$this->coords[0][$pnts-1];
281
		if( $tmp1==='' || $tmp1===NULL || $tmp1==='X' ) $tmp1 = 'x';
282
		if( $prev==='' || $prev===null || $prev==='X' ) $prev = 'x';
283
 
284
		if( is_numeric($y) || (is_string($y) && $y != '-') ) {
285
		    if( is_numeric($y) && (is_numeric($prev) || $prev === '-' ) ) {
286
			$img->StyleLineTo($xt,$yt);
287
		    }
288
		    else {
289
			$img->SetStartPoint($xt,$yt);
290
		    }
291
		}
292
		if( $this->filled && $tmp1 !== '-' ) {
293
		    if( $tmp1 === 'x' ) {
294
			$cord[$idx++] = $cord[$idx-3];
295
			$cord[$idx++] = $fillmin;
296
		    }
297
		    elseif( $prev === 'x' ) {
298
			$cord[$idx++] = $xt;
299
			$cord[$idx++] = $fillmin;
300
			$cord[$idx++] = $xt;
301
			$cord[$idx++] = $yt;
302
		    }
303
		    else {
304
			$cord[$idx++] = $xt;
305
			$cord[$idx++] = $yt;
306
		    }
307
		}
308
		else {
309
		    if( is_numeric($tmp1)  && (is_numeric($prev) || $prev === '-' ) ) {
310
			$cord[$idx++] = $xt;
311
			$cord[$idx++] = $yt;
312
		    }
313
		}
314
	    }
315
	    $yt_old = $yt;
316
	    $xt_old = $xt;
317
	    $y_old = $y;
318
 
319
	    $this->StrokeDataValue($img,$this->coords[0][$pnts],$xt,$yt);
320
 
321
	    ++$pnts;
322
	}
323
 
324
	if( $this->filled  ) {
325
	    $cord[$idx++] = $xt;
326
	    if( $min > 0 || $this->fillFromMin )
327
		$cord[$idx++] = $yscale->Translate($min);
328
	    else
329
		$cord[$idx++] = $yscale->Translate(0);
330
	    if( $this->fillgrad ) {
331
		$img->SetLineWeight(1);
332
		$grad = new Gradient($img);
333
		$grad->SetNumColors($this->fillgrad_numcolors);
334
		$grad->FilledFlatPolygon($cord,$this->fillgrad_fromcolor,$this->fillgrad_tocolor);
335
		$img->SetLineWeight($this->weight);
336
	    }
337
	    else {
338
		$img->SetColor($this->fill_color);
339
		$img->FilledPolygon($cord);
340
	    }
341
	    if( $this->line_weight > 0 ) {
342
		$img->SetColor($this->color);
343
		$img->Polygon($cord);
344
	    }
345
	}
346
 
347
	if(!empty($this->filledAreas)) {
348
 
349
	    $minY = $yscale->Translate($yscale->GetMinVal());
350
	    $factor = ($this->step_style ? 4 : 2);
351
 
352
	    for($i = 0; $i < sizeof($this->filledAreas); ++$i) {
353
		// go through all filled area elements ordered by insertion
354
		// fill polygon array
355
		$areaCoords[] = $cord[$this->filledAreas[$i][0] * $factor];
356
		$areaCoords[] = $minY;
357
 
358
		$areaCoords =
359
		    array_merge($areaCoords,
360
				array_slice($cord,
361
					    $this->filledAreas[$i][0] * $factor,
362
					    ($this->filledAreas[$i][1] - $this->filledAreas[$i][0] + ($this->step_style ? 0 : 1))  * $factor));
363
		$areaCoords[] = $areaCoords[sizeof($areaCoords)-2]; // last x
364
		$areaCoords[] = $minY; // last y
365
 
366
		if($this->filledAreas[$i][3]) {
367
		    $img->SetColor($this->filledAreas[$i][2]);
368
		    $img->FilledPolygon($areaCoords);
369
		    $img->SetColor($this->color);
370
		}
371
		// Check if we should draw the frame.
372
		// If not we still re-draw the line since it might have been
373
		// partially overwritten by the filled area and it doesn't look
374
		// very good.
375
		// TODO: The behaviour is undefined if the line does not have
376
		// any line at the position of the area.
377
		if( $this->filledAreas[$i][4] )
378
		    $img->Polygon($areaCoords);
379
		else
380
	    	    $img->Polygon($cord);
381
 
382
		$areaCoords = array();
383
	    }
384
	}
385
 
386
	if( $this->mark->type == -1 || $this->mark->show == false )
387
	    return;
388
 
389
	for( $pnts=0; $pnts<$numpoints; ++$pnts) {
390
 
391
	    if( $exist_x ) $x=$this->coords[1][$pnts];
392
	    else $x=$pnts+$textadj;
393
	    $xt = $xscale->Translate($x);
394
	    $yt = $yscale->Translate($this->coords[0][$pnts]);
395
 
396
	    if( is_numeric($this->coords[0][$pnts]) ) {
397
		if( !empty($this->csimtargets[$pnts]) ) {
398
		    $this->mark->SetCSIMTarget($this->csimtargets[$pnts]);
399
		    $this->mark->SetCSIMAlt($this->csimalts[$pnts]);
400
		}
401
		if( $exist_x )
402
		    $x=$this->coords[1][$pnts];
403
		else
404
		    $x=$pnts;
405
		$this->mark->SetCSIMAltVal($this->coords[0][$pnts],$x);
406
		$this->mark->Stroke($img,$xt,$yt);
407
		$this->csimareas .= $this->mark->GetCSIMAreas();
408
		$this->StrokeDataValue($img,$this->coords[0][$pnts],$xt,$yt);
409
	    }
410
	}
411
 
412
 
413
    }
414
} // Class
415
 
416
 
417
//===================================================
418
// CLASS AccLinePlot
419
// Description:
420
//===================================================
421
class AccLinePlot extends Plot {
422
    var $plots=null,$nbrplots=0,$numpoints=0;
423
    var $iStartEndZero=true;
424
//---------------
425
// CONSTRUCTOR
426
    function AccLinePlot($plots) {
427
        $this->plots = $plots;
428
	$this->nbrplots = count($plots);
429
	$this->numpoints = $plots[0]->numpoints;
430
 
431
	for($i=0; $i < $this->nbrplots; ++$i ) {
432
	    $this->LineInterpolate($this->plots[$i]->coords[0]);
433
	}
434
    }
435
 
436
//---------------
437
// PUBLIC METHODS
438
    function Legend(&$graph) {
439
	$n=count($this->plots);
440
	for($i=0; $i < $n; ++$i )
441
	    $this->plots[$i]->DoLegend($graph);
442
    }
443
 
444
    function Max() {
445
	list($xmax) = $this->plots[0]->Max();
446
	$nmax=0;
447
	$n = count($this->plots);
448
	for($i=0; $i < $n; ++$i) {
449
	    $nc = count($this->plots[$i]->coords[0]);
450
	    $nmax = max($nmax,$nc);
451
	    list($x) = $this->plots[$i]->Max();
452
	    $xmax = Max($xmax,$x);
453
	}
454
	for( $i = 0; $i < $nmax; $i++ ) {
455
	    // Get y-value for line $i by adding the
456
	    // individual bars from all the plots added.
457
	    // It would be wrong to just add the
458
	    // individual plots max y-value since that
459
	    // would in most cases give to large y-value.
460
	    $y=$this->plots[0]->coords[0][$i];
461
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
462
		$y += $this->plots[ $j ]->coords[0][$i];
463
	    }
464
	    $ymax[$i] = $y;
465
	}
466
	$ymax = max($ymax);
467
	return array($xmax,$ymax);
468
    }
469
 
470
    function Min() {
471
	$nmax=0;
472
	list($xmin,$ysetmin) = $this->plots[0]->Min();
473
	$n = count($this->plots);
474
	for($i=0; $i < $n; ++$i) {
475
	    $nc = count($this->plots[$i]->coords[0]);
476
	    $nmax = max($nmax,$nc);
477
	    list($x,$y) = $this->plots[$i]->Min();
478
	    $xmin = Min($xmin,$x);
479
	    $ysetmin = Min($y,$ysetmin);
480
	}
481
	for( $i = 0; $i < $nmax; $i++ ) {
482
	    // Get y-value for line $i by adding the
483
	    // individual bars from all the plots added.
484
	    // It would be wrong to just add the
485
	    // individual plots min y-value since that
486
	    // would in most cases give to small y-value.
487
	    $y=$this->plots[0]->coords[0][$i];
488
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
489
		$y += $this->plots[ $j ]->coords[0][$i];
490
	    }
491
	    $ymin[$i] = $y;
492
	}
493
	$ymin = Min($ysetmin,Min($ymin));
494
	return array($xmin,$ymin);
495
    }
496
 
497
    // Gets called before any axis are stroked
498
    function PreStrokeAdjust(&$graph) {
499
 
500
	// If another plot type have already adjusted the
501
	// offset we don't touch it.
502
	// (We check for empty in case the scale is  a log scale
503
	// and hence doesn't contain any xlabel_offset)
504
 
505
	if( empty($graph->xaxis->scale->ticks->xlabel_offset) ||
506
	    $graph->xaxis->scale->ticks->xlabel_offset == 0 ) {
507
	    if( $this->center ) {
508
		++$this->numpoints;
509
		$a=0.5; $b=0.5;
510
	    } else {
511
		$a=0; $b=0;
512
	    }
513
	    $graph->xaxis->scale->ticks->SetXLabelOffset($a);
514
	    $graph->SetTextScaleOff($b);
515
	    $graph->xaxis->scale->ticks->SupressMinorTickMarks();
516
	}
517
 
518
    }
519
 
520
    function SetInterpolateMode($aIntMode) {
521
	$this->iStartEndZero=$aIntMode;
522
    }
523
 
524
    // Replace all '-' with an interpolated value. We use straightforward
525
    // linear interpolation. If the data starts with one or several '-' they
526
    // will be replaced by the the first valid data point
527
    function LineInterpolate(&$aData) {
528
 
529
	$n=count($aData);
530
	$i=0;
531
 
532
	// If first point is undefined we will set it to the same as the first
533
	// valid data
534
	if( $aData[$i]==='-' ) {
535
	    // Find the first valid data
536
	    while( $i < $n && $aData[$i]==='-' ) {
537
		++$i;
538
	    }
539
	    if( $i < $n ) {
540
		for($j=0; $j < $i; ++$j ) {
541
		    if( $this->iStartEndZero )
542
			$aData[$i] = 0;
543
		    else
544
			$aData[$j] = $aData[$i];
545
		}
546
	    }
547
	    else {
548
		// All '-' => Error
549
		return false;
550
	    }
551
	}
552
 
553
	while($i < $n) {
554
	    while( $i < $n && $aData[$i] !== '-' ) {
555
		++$i;
556
	    }
557
	    if( $i < $n ) {
558
		$pstart=$i-1;
559
 
560
		// Now see how long this segment of '-' are
561
		while( $i < $n && $aData[$i] === '-' )
562
		    ++$i;
563
		if( $i < $n ) {
564
		    $pend=$i;
565
		    $size=$pend-$pstart;
566
		    $k=($aData[$pend]-$aData[$pstart])/$size;
567
		    // Replace the segment of '-' with a linear interpolated value.
568
		    for($j=1; $j < $size; ++$j ) {
569
			$aData[$pstart+$j] = $aData[$pstart] + $j*$k ;
570
		    }
571
		}
572
		else {
573
		    // There are no valid end point. The '-' goes all the way to the end
574
		    // In that case we just set all the remaining values the the same as the
575
		    // last valid data point.
576
		    for( $j=$pstart+1; $j < $n; ++$j )
577
			if( $this->iStartEndZero )
578
			    $aData[$j] = 0;
579
			else
580
			    $aData[$j] = $aData[$pstart] ;
581
		}
582
	    }
583
	}
584
	return true;
585
    }
586
 
587
 
588
 
589
    // To avoid duplicate of line drawing code here we just
590
    // change the y-values for each plot and then restore it
591
    // after we have made the stroke. We must do this copy since
592
    // it wouldn't be possible to create an acc line plot
593
    // with the same graphs, i.e AccLinePlot(array($pl,$pl,$pl));
594
    // since this method would have a side effect.
595
    function Stroke(&$img,&$xscale,&$yscale) {
596
	$img->SetLineWeight($this->weight);
597
	$this->numpoints = count($this->plots[0]->coords[0]);
598
	// Allocate array
599
	$coords[$this->nbrplots][$this->numpoints]=0;
600
	for($i=0; $i<$this->numpoints; $i++) {
601
	    $coords[0][$i]=$this->plots[0]->coords[0][$i];
602
	    $accy=$coords[0][$i];
603
	    for($j=1; $j<$this->nbrplots; ++$j ) {
604
		$coords[$j][$i] = $this->plots[$j]->coords[0][$i]+$accy;
605
		$accy = $coords[$j][$i];
606
	    }
607
	}
608
	for($j=$this->nbrplots-1; $j>=0; --$j) {
609
	    $p=$this->plots[$j];
610
	    for( $i=0; $i<$this->numpoints; ++$i) {
611
		$tmp[$i]=$p->coords[0][$i];
612
		$p->coords[0][$i]=$coords[$j][$i];
613
	    }
614
	    $p->Stroke($img,$xscale,$yscale);
615
	    for( $i=0; $i<$this->numpoints; ++$i)
616
		$p->coords[0][$i]=$tmp[$i];
617
	    $p->coords[0][]=$tmp;
618
	}
619
    }
620
} // Class
621
 
622
 
623
/* EOF */
624
?>