File: jpgraph_line.php

package info (click to toggle)
libphp-jpgraph 1.5.2-11+u1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 888 kB
  • ctags: 2,689
  • sloc: php: 8,992; makefile: 24
file content (285 lines) | stat: -rw-r--r-- 7,501 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/*=======================================================================
// File: 	JPGRAPH_LINE.PHP
// Description:	Line plot extension for JpGraph
// Created: 	2001-01-08
// Author:	Johan Persson (johanp@aditus.nu)
// Ver:		$Id: jpgraph_line.php,v 1.14 2002/03/01 00:33:31 aditus Exp $
//
// License:	This code is released under GPL 2.0
// Copyright (C) 2001 Johan Persson
//========================================================================
*/

//===================================================
// CLASS Value
// Description: 
//===================================================
class DisplayValue {    
    var $show=false,$format="%d",$angle=0;
    var $ff=FF_FONT1,$fs=FS_NORMAL,$fsize=10;
    var $color="navy",$margin=3;

    function Show($f=true) {
	$this->show=$f;
    }

    function SetColor($color) {
	$this->color = $color;
    }

    function SetFont($ff,$fs=FS_NORMAL,$fsize=10) {
	$this->ff=$ff;
	$this->fs=$fs;
	$this->fsize=$fsize;
    }

    function SetMargin($m) {
	$this->margin = $m;
    }

    function SetFormat($format,$angle=0) {
	$this->format= $format;
	$this->angle = $angle;
    }
}


//===================================================
// CLASS LinePlot
// Description: 
//===================================================
class LinePlot extends Plot{
    var $filled=false;
    var $fill_color;
    var $mark=null;
    var $step_style=false, $center=false;
    var $line_style=1;	// Default to solid
    var $value;

//---------------
// CONSTRUCTOR
    function LinePlot(&$datay,$datax=false) {
	$this->Plot($datay,$datax);
	$this->mark = new PlotMark();
	$this->mark->SetColor($this->color);
	$this->value = new DisplayValue();
    }
//---------------
// PUBLIC METHODS	

    // Set style, filled or open
    function SetFilled($f=true) {
	$this->filled=$f;
    }
	
    function SetStyle($s) {
	$this->line_style=$s;
    }
	
    function SetStepStyle($f=true) {
	$this->step_style = $f;
    }
	
    function SetColor($c) {
	parent::SetColor($c);
	$this->mark->SetColor($this->color);
    }
	
    function SetFillColor($c,$f=true) {
	$this->fill_color=$c;
	$this->filled=$f;
    }
	
    function Legend(&$graph) {
	if( $this->legend!="" ) {
	    if( $this->filled ) {
		$graph->legend->Add($this->legend,
		$this->fill_color,$this->mark);
	    } else {
		$graph->legend->Add($this->legend,
		$this->color,$this->mark,$this->line_style);
	    }
	}	
    }
	
    function SetCenter($c=true) {
	$this->center=$c;
    }	
	
    // Gets called before any axis are stroked
    function PreStrokeAdjust(&$graph) {
	if( $this->center ) {
	    ++$this->numpoints;
	    $a=0.5; $b=0.5;
	} else {
	    $a=0; $b=0;
	}
	$graph->xaxis->scale->ticks->SetXLabelOffset($a);
	$graph->SetTextScaleOff($b);						
	$graph->xaxis->scale->ticks->SupressMinorTickMarks();
    }
	
    function Stroke(&$img,&$xscale,&$yscale) {
	$numpoints=count($this->coords[0]);
	if( isset($this->coords[1]) ) {
	    if( count($this->coords[1])!=$numpoints )
		JpGraphError::Raise("JpGraph Error: Number of X and Y points are not equal.<br>
					Number of X-points:".count($this->coords[1])."<br>
					Number of Y-points:$numpoints");
	    else
		$exist_x = true;
	}
	else 
	    $exist_x = false;

	if( $exist_x )
	    $xs=$this->coords[1][0];
	else
	    $xs=0;

	$img->SetStartPoint($xscale->Translate($xs),
	                    $yscale->Translate($this->coords[0][0]));
		
	if( $this->filled ) {
	    $cord[] = $xscale->Translate($xs);
	    $cord[] = $yscale->Translate($yscale->GetMinVal());
	}
	$cord[] = $xscale->Translate($xs);
	$cord[] = $yscale->Translate($this->coords[0][0]);
	$yt_old = $yscale->Translate($this->coords[0][0]);
	$img->SetColor($this->color);
	$img->SetLineWeight($this->weight);	
	$img->SetLineStyle($this->line_style);
	for( $pnts=1; $pnts<$numpoints; ++$pnts) {
	    if( $exist_x ) $x=$this->coords[1][$pnts];
	    else $x=$pnts;
	    $xt = $xscale->Translate($x);
	    $yt = $yscale->Translate($this->coords[0][$pnts]);
	    $cord[] = $xt;
	    $cord[] = $yt;
	    if( $this->step_style ) {
		$img->StyleLineTo($xt,$yt_old);
		$img->StyleLineTo($xt,$yt);
	    }
	    else {
		$y=$this->coords[0][$pnts];
		if( is_numeric($y) || (is_string($y) && $y != "-") ) { 		 			
		    $tmp1=$this->coords[0][$pnts];
		    $tmp2=$this->coords[0][$pnts-1]; 		 			
		    if( is_numeric($tmp1)  && (is_numeric($tmp2) || $tmp2=="-" ) ) { 
			$img->StyleLineTo($xt,$yt);
		    } 
		    else {
			$img->SetStartPoint($xt,$yt);
		    }
		}
	    }
	    $yt_old = $yt;
	    if( $this->value->show) 
	    {
		$sval=sprintf($this->value->format,$this->coords[0][$pnts]);
		$txt = new Text($sval,$xt,$yt-$this->value->margin);
		$txt->SetFont($this->value->ff,$this->value->fs,$this->value->fsize);
		$txt->Align("center","bottom");
		$txt->SetOrientation($this->value->angle);
		$txt->SetColor($this->value->color);
		$txt->Stroke($img);
	    }
	}	
	if( $this->filled ) {
	    $cord[] = $xt;
	    $cord[] = $yscale->Translate($yscale->GetMinVal());					
	    $img->SetColor($this->fill_color);	
	    $img->FilledPolygon($cord);
	    $img->SetColor($this->color);
	    $img->Polygon($cord);
	}
	$adjust=0;
	if( $this->filled ) $adjust=2;
	for($i=$adjust; $i<count($cord)-$adjust; $i+=2) {
	    if( is_numeric($this->coords[0][($i-$adjust)/2]) )
		$this->mark->Stroke($img,$cord[$i],$cord[$i+1]);
	}
    }
//---------------
// PRIVATE METHODS	
} // Class


//===================================================
// CLASS AccLinePlot
// Description: 
//===================================================
class AccLinePlot extends Plot {
    var $plots=null,$nbrplots=0,$numpoints=0;
//---------------
// CONSTRUCTOR
    function AccLinePlot($plots) {
	$this->plots = $plots;
	$this->nbrplots = count($plots);
	$this->numpoints = $plots[0]->numpoints;		
    }

//---------------
// PUBLIC METHODS	
    function Legend(&$graph) {
	foreach( $this->plots as $p )
	    $p->Legend($graph);
    }
	
    function Max() {
	$accymax=0;
	list($xmax,$dummy) = $this->plots[0]->Max();
	foreach($this->plots as $p) {
	    list($xm,$ym) = $p->Max();
	    $xmax = max($xmax,$xm);
	    $accymax += $ym;
	}
	return array($xmax,$accymax);
    }

    function Min() {
	list($xmin,$ymin)=$this->plots[0]->Min();
	foreach( $this->plots as $p ) {
	    list($xm,$ym)=$p->Min();
	    $xmin=Min($xmin,$xm);
	    $ymin=Min($ymin,$ym);
	}
	return array($xmin,$ymin);	
    }

    // To avoid duplicate of line drawing code here we just
    // change the y-values for each plot and then restore it
    // after we have made the stroke. We must do this copy since
    // it wouldn't be possible to create an acc line plot
    // with the same graphs, i.e AccLinePlot(array($pl,$pl,$pl));
    // since this method would have a side effect.
    function Stroke(&$img,&$xscale,&$yscale) {
	$img->SetLineWeight($this->weight);
	// Allocate array
	$coords[$this->nbrplots][$this->numpoints]=0;
	for($i=0; $i<$this->numpoints; $i++) {
	    $coords[0][$i]=$this->plots[0]->coords[0][$i]; 
	    $accy=$coords[0][$i];
	    for($j=1; $j<$this->nbrplots; ++$j ) {
		$coords[$j][$i] = $this->plots[$j]->coords[0][$i]+$accy; 
		$accy = $coords[$j][$i];
	    }
	}
	for($j=$this->nbrplots-1; $j>=0; --$j) {
	    $p=$this->plots[$j];
	    for( $i=0; $i<$this->numpoints; ++$i) {
		$tmp[$i]=$p->coords[0][$i];
		$p->coords[0][$i]=$coords[$j][$i];
	    }
	    $p->Stroke($img,$xscale,$yscale);
	    for( $i=0; $i<$this->numpoints; ++$i) 
		$p->coords[0][$i]=$tmp[$i];
	    $p->coords[0][]=$tmp;
	}
    }
} // Class

/* EOF */
?>