1 | <?php
|
---|
2 | /*=======================================================================
|
---|
3 | // File: JPGRAPH_SCATTER.PHP
|
---|
4 | // Description: Scatter (and impuls) plot extension for JpGraph
|
---|
5 | // Created: 2001-02-11
|
---|
6 | // Author: Johan Persson (johanp@aditus.nu)
|
---|
7 | // Ver: $Id: jpgraph_scatter.php,v 1.31 2003/03/09 18:24:06 aditus Exp $
|
---|
8 | //
|
---|
9 | // License: This code is released under QPL
|
---|
10 | // Copyright (C) 2001,2002 Johan Persson
|
---|
11 | //========================================================================
|
---|
12 | */
|
---|
13 |
|
---|
14 |
|
---|
15 | //===================================================
|
---|
16 | // CLASS FieldArrow
|
---|
17 | // Description: Draw an arrow at (x,y) with angle a
|
---|
18 | //===================================================
|
---|
19 | class FieldArrow {
|
---|
20 | var $iSize=10; // Length in pixels for arrow
|
---|
21 | var $iArrowSize = 2;
|
---|
22 | var $iColor='black';
|
---|
23 | var $isizespec = array(
|
---|
24 | 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));
|
---|
25 | function FieldArrow() {
|
---|
26 | }
|
---|
27 |
|
---|
28 | function SetSize($aSize,$aArrowSize=2) {
|
---|
29 | $this->iSize = $aSize;
|
---|
30 | $this->iArrowSize = $aArrowSize;
|
---|
31 | }
|
---|
32 |
|
---|
33 | function SetColor($aColor) {
|
---|
34 | $this->iColor = $aColor;
|
---|
35 | }
|
---|
36 |
|
---|
37 | function Stroke($aImg,$x,$y,$a) {
|
---|
38 | $old_origin = $aImg->SetCenter($x,$y);
|
---|
39 | $old_a = $aImg->SetAngle(-$a);
|
---|
40 |
|
---|
41 | $dx = round($this->iSize/2);
|
---|
42 | $c = array($x-$dx,$y,$x+$dx,$y);
|
---|
43 | $x += $dx;
|
---|
44 |
|
---|
45 | list($dx,$dy) = $this->isizespec[$this->iArrowSize];
|
---|
46 | $ca = array($x,$y,$x-$dx,$y-$dy,$x-$dx,$y+$dy,$x,$y);
|
---|
47 |
|
---|
48 | $aImg->SetColor($this->iColor);
|
---|
49 | $aImg->Polygon($c);
|
---|
50 | $aImg->FilledPolygon($ca);
|
---|
51 |
|
---|
52 | $aImg->SetCenter($old_origin[0],$old_origin[1]);
|
---|
53 | $aImg->SetAngle($old_a);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | //===================================================
|
---|
58 | // CLASS FieldPlot
|
---|
59 | // Description: Render a field plot
|
---|
60 | //===================================================
|
---|
61 | class FieldPlot extends Plot {
|
---|
62 | var $iAngles;
|
---|
63 | var $iCallback='';
|
---|
64 | function FieldPlot($datay,$datax,$angles) {
|
---|
65 | if( (count($datax) != count($datay)) )
|
---|
66 | JpGraphError::Raise("Fieldplots must have equal number of X and Y points.");
|
---|
67 | if( (count($datax) != count($angles)) )
|
---|
68 | JpGraphError::Raise("Fieldplots must have an angle specified for each X and Y points.");
|
---|
69 |
|
---|
70 | $this->iAngles = $angles;
|
---|
71 |
|
---|
72 | $this->Plot($datay,$datax);
|
---|
73 | $this->value->SetAlign('center','center');
|
---|
74 | $this->value->SetMargin(15);
|
---|
75 |
|
---|
76 | $this->arrow = new FieldArrow();
|
---|
77 | }
|
---|
78 |
|
---|
79 | function SetCallback($aFunc) {
|
---|
80 | $this->iCallback = $aFunc;
|
---|
81 | }
|
---|
82 |
|
---|
83 | function Stroke(&$img,&$xscale,&$yscale) {
|
---|
84 |
|
---|
85 | // Remeber base color and size
|
---|
86 | $bc = $this->arrow->iColor;
|
---|
87 | $bs = $this->arrow->iSize;
|
---|
88 | $bas = $this->arrow->iArrowSize;
|
---|
89 |
|
---|
90 | for( $i=0; $i<$this->numpoints; ++$i ) {
|
---|
91 | // Skip null values
|
---|
92 | if( $this->coords[0][$i]==="" )
|
---|
93 | continue;
|
---|
94 |
|
---|
95 | $f = $this->iCallback;
|
---|
96 | if( $f != "" ) {
|
---|
97 | list($cc,$cs,$cas) = $f($this->coords[1][$i],$this->coords[0][$i],
|
---|
98 | $this->iAngles[$i]);
|
---|
99 | // Fall back on global data if the callback isn't set
|
---|
100 | if( $cc == "" ) $cc = $bc;
|
---|
101 | if( $cs == "" ) $cs = $bs;
|
---|
102 | if( $cas == "" ) $cas = $bas;
|
---|
103 | //echo "f=$f, cc=$cc, cs=$cs, cas=$cas<br/>";
|
---|
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);
|
---|
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::Raise("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]==="" )
|
---|
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 | $this->mark->SetCSIMTarget($this->csimtargets[$i]);
|
---|
194 | $this->mark->SetCSIMAlt($this->csimalts[$i]);
|
---|
195 | }
|
---|
196 |
|
---|
197 | if( isset($this->coords[1]) ) {
|
---|
198 | $this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
|
---|
199 | }
|
---|
200 | else {
|
---|
201 | $this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
|
---|
202 | }
|
---|
203 |
|
---|
204 | $this->mark->Stroke($img,$xt,$yt);
|
---|
205 |
|
---|
206 | $this->csimareas .= $this->mark->GetCSIMAreas();
|
---|
207 | $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
|
---|
208 |
|
---|
209 | $xt_old = $xt;
|
---|
210 | $yt_old = $yt;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | // Framework function
|
---|
215 | function Legend(&$aGraph) {
|
---|
216 | if( $this->legend != "" ) {
|
---|
217 | $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
|
---|
218 | $this->legendcsimtarget,$this->legendcsimalt);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | } // Class
|
---|
222 | /* EOF */
|
---|
223 | ?>
|
---|