Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 aurelien 1
<?php
2
/*=======================================================================
3
// File:	JPGRAPH_SCATTER.PHP
4
// Description: Scatter (and impuls) plot extension for JpGraph
5
// Created: 	2001-02-11
6
// Ver:		$Id: jpgraph_scatter.php 955 2007-11-17 11:41:42Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
require_once ('jpgraph_plotmark.inc.php');
12
 
13
//===================================================
14
// CLASS FieldArrow
15
// Description: Draw an arrow at (x,y) with angle a
16
//===================================================
17
class FieldArrow {
18
    var $iSize=10;  // Length in pixels for  arrow
19
    var $iArrowSize = 2;
20
    var $iColor='black';
21
    var $isizespec = array(
22
	array(2,1),array(3,2),array(4,3),array(6,4),array(7,4),array(8,5),array(10,6),array(12,7),array(16,8),array(20,10));
23
    function FieldArrow() {
24
    }
25
 
26
    function SetSize($aSize,$aArrowSize=2) {
27
	$this->iSize = $aSize;
28
	$this->iArrowSize = $aArrowSize;
29
    }
30
 
31
    function SetColor($aColor) {
32
	$this->iColor = $aColor;
33
    }
34
 
35
    function Stroke(&$aImg,$x,$y,$a) {
36
	// First rotate the center coordinates
37
	list($x,$y) = $aImg->Rotate($x,$y);
38
 
39
	$old_origin = $aImg->SetCenter($x,$y);
40
	$old_a = $aImg->a;
41
	$aImg->SetAngle(-$a+$old_a);
42
 
43
	$dx = round($this->iSize/2);
44
	$c = array($x-$dx,$y,$x+$dx,$y);
45
	$x += $dx;
46
 
47
	list($dx,$dy) = $this->isizespec[$this->iArrowSize];
48
	$ca = array($x,$y,$x-$dx,$y-$dy,$x-$dx,$y+$dy,$x,$y);
49
 
50
	$aImg->SetColor($this->iColor);
51
	$aImg->Polygon($c);
52
	$aImg->FilledPolygon($ca);
53
 
54
	$aImg->SetCenter($old_origin[0],$old_origin[1]);
55
	$aImg->SetAngle($old_a);
56
    }
57
}
58
 
59
//===================================================
60
// CLASS FieldPlot
61
// Description: Render a field plot
62
//===================================================
63
class FieldPlot extends Plot {
64
    var $iAngles;
65
    var $iCallback='';
66
    function FieldPlot($datay,$datax,$angles) {
67
	if( (count($datax) != count($datay)) )
68
	    JpGraphError::RaiseL(20001);//("Fieldplots must have equal number of X and Y points.");
69
	if( (count($datax) != count($angles)) )
70
	    JpGraphError::RaiseL(20002);//("Fieldplots must have an angle specified for each X and Y points.");
71
 
72
	$this->iAngles = $angles;
73
 
74
	$this->Plot($datay,$datax);
75
	$this->value->SetAlign('center','center');
76
	$this->value->SetMargin(15);
77
 
78
	$this->arrow = new FieldArrow();
79
    }
80
 
81
    function SetCallback($aFunc) {
82
	$this->iCallback = $aFunc;
83
    }
84
 
85
    function Stroke(&$img,&$xscale,&$yscale) {
86
 
87
	// Remeber base color and size
88
	$bc = $this->arrow->iColor;
89
	$bs = $this->arrow->iSize;
90
	$bas = $this->arrow->iArrowSize;
91
 
92
	for( $i=0; $i<$this->numpoints; ++$i ) {
93
	    // Skip null values
94
	    if( $this->coords[0][$i]==="" )
95
		continue;
96
 
97
	    $f = $this->iCallback;
98
	    if( $f != "" ) {
99
		list($cc,$cs,$cas) = call_user_func($f,$this->coords[1][$i],$this->coords[0][$i],$this->iAngles[$i]);
100
		// Fall back on global data if the callback isn't set
101
		if( $cc  == "" ) $cc = $bc;
102
		if( $cs  == "" ) $cs = $bs;
103
		if( $cas == "" ) $cas = $bas;
104
		$this->arrow->SetColor($cc);
105
		$this->arrow->SetSize($cs,$cas);
106
	    }
107
 
108
	    $xt = $xscale->Translate($this->coords[1][$i]);
109
	    $yt = $yscale->Translate($this->coords[0][$i]);
110
 
111
	    $this->arrow->Stroke($img,$xt,$yt,$this->iAngles[$i]);
112
	    $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
113
	}
114
    }
115
 
116
    // Framework function
117
    function Legend(&$aGraph) {
118
	if( $this->legend != "" ) {
119
	    $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
120
				 $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
121
	}
122
    }
123
}
124
 
125
//===================================================
126
// CLASS ScatterPlot
127
// Description: Render X and Y plots
128
//===================================================
129
class ScatterPlot extends Plot {
130
    var $impuls = false;
131
    var $linkpoints = false, $linkpointweight=1, $linkpointcolor="black";
132
//---------------
133
// CONSTRUCTOR
134
    function ScatterPlot($datay,$datax=false) {
135
	if( (count($datax) != count($datay)) && is_array($datax))
136
	    JpGraphError::RaiseL(20003);//("Scatterplot must have equal number of X and Y points.");
137
	$this->Plot($datay,$datax);
138
	$this->mark = new PlotMark();
139
	$this->mark->SetType(MARK_SQUARE);
140
	$this->mark->SetColor($this->color);
141
	$this->value->SetAlign('center','center');
142
	$this->value->SetMargin(0);
143
    }
144
 
145
//---------------
146
// PUBLIC METHODS
147
    function SetImpuls($f=true) {
148
	$this->impuls = $f;
149
    }
150
 
151
    // Combine the scatter plot points with a line
152
    function SetLinkPoints($aFlag=true,$aColor="black",$aWeight=1) {
153
	$this->linkpoints=$aFlag;
154
	$this->linkpointcolor=$aColor;
155
	$this->linkpointweight=$aWeight;
156
    }
157
 
158
    function Stroke(&$img,&$xscale,&$yscale) {
159
 
160
	$ymin=$yscale->scale_abs[0];
161
	if( $yscale->scale[0] < 0 )
162
	    $yzero=$yscale->Translate(0);
163
	else
164
	    $yzero=$yscale->scale_abs[0];
165
 
166
	$this->csimareas = '';
167
	for( $i=0; $i < $this->numpoints; ++$i ) {
168
 
169
	    // Skip null values
170
	    if( $this->coords[0][$i]==="" || $this->coords[0][$i]==='-' || $this->coords[0][$i]==='x')
171
		continue;
172
 
173
	    if( isset($this->coords[1]) )
174
		$xt = $xscale->Translate($this->coords[1][$i]);
175
	    else
176
		$xt = $xscale->Translate($i);
177
	    $yt = $yscale->Translate($this->coords[0][$i]);
178
 
179
 
180
	    if( $this->linkpoints && isset($yt_old) ) {
181
		$img->SetColor($this->linkpointcolor);
182
		$img->SetLineWeight($this->linkpointweight);
183
		$img->Line($xt_old,$yt_old,$xt,$yt);
184
	    }
185
 
186
	    if( $this->impuls ) {
187
		$img->SetColor($this->color);
188
		$img->SetLineWeight($this->weight);
189
		$img->Line($xt,$yzero,$xt,$yt);
190
	    }
191
 
192
	    if( !empty($this->csimtargets[$i]) ) {
193
		if( !empty($this->csimwintargets[$i]) ) {
194
		    $this->mark->SetCSIMTarget($this->csimtargets[$i],$this->csimwintargets[$i]);
195
		}
196
		else {
197
		    $this->mark->SetCSIMTarget($this->csimtargets[$i]);
198
		}
199
	        $this->mark->SetCSIMAlt($this->csimalts[$i]);
200
	    }
201
 
202
	    if( isset($this->coords[1]) ) {
203
		$this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
204
	    }
205
	    else {
206
		$this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
207
	    }
208
 
209
	    $this->mark->Stroke($img,$xt,$yt);
210
 
211
	    $this->csimareas .= $this->mark->GetCSIMAreas();
212
	    $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
213
 
214
	    $xt_old = $xt;
215
	    $yt_old = $yt;
216
	}
217
    }
218
 
219
    // Framework function
220
    function Legend(&$aGraph) {
221
	if( $this->legend != "" ) {
222
	    $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
223
				 $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
224
	}
225
    }
226
} // Class
227
/* EOF */
228
?>