2 /*=======================================================================
3 // File: JPGRAPH_SCATTER.PHP
4 // Description: Scatter (and impuls) plot extension for JpGraph
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_scatter.php,v 1.31.2.1 2003/08/15 15:27:40 aditus Exp $
9 // License: This code is released under QPL
10 // Copyright (C) 2001,2002 Johan Persson
11 //========================================================================
15 //===================================================
17 // Description: Draw an arrow at (x,y) with angle a
18 //===================================================
20 var $iSize=10; // Length in pixels for arrow
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() {
28 function SetSize($aSize,$aArrowSize=2) {
29 $this->iSize = $aSize;
30 $this->iArrowSize = $aArrowSize;
33 function SetColor($aColor) {
34 $this->iColor = $aColor;
37 function Stroke($aImg,$x,$y,$a) {
38 $old_origin = $aImg->SetCenter($x,$y);
39 $old_a = $aImg->SetAngle(-$a);
41 $dx = round($this->iSize/2);
42 $c = array($x-$dx,$y,$x+$dx,$y);
45 list($dx,$dy) = $this->isizespec[$this->iArrowSize];
46 $ca = array($x,$y,$x-$dx,$y-$dy,$x-$dx,$y+$dy,$x,$y);
48 $aImg->SetColor($this->iColor);
50 $aImg->FilledPolygon($ca);
52 $aImg->SetCenter($old_origin[0],$old_origin[1]);
53 $aImg->SetAngle($old_a);
57 //===================================================
59 // Description: Render a field plot
60 //===================================================
61 class FieldPlot extends Plot {
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.");
70 $this->iAngles = $angles;
72 $this->Plot($datay,$datax);
73 $this->value->SetAlign('center','center');
74 $this->value->SetMargin(15);
76 $this->arrow = new FieldArrow();
79 function SetCallback($aFunc) {
80 $this->iCallback = $aFunc;
83 function Stroke(&$img,&$xscale,&$yscale) {
85 // Remeber base color and size
86 $bc = $this->arrow->iColor;
87 $bs = $this->arrow->iSize;
88 $bas = $this->arrow->iArrowSize;
90 for( $i=0; $i<$this->numpoints; ++$i ) {
92 if( $this->coords[0][$i]==="" )
95 $f = $this->iCallback;
97 list($cc,$cs,$cas) = call_user_func($f,$this->coords[1][$i],$this->coords[0][$i],$this->iAngles[$i]);
98 // Fall back on global data if the callback isn't set
99 if( $cc == "" ) $cc = $bc;
100 if( $cs == "" ) $cs = $bs;
101 if( $cas == "" ) $cas = $bas;
102 //echo "f=$f, cc=$cc, cs=$cs, cas=$cas<br>";
103 $this->arrow->SetColor($cc);
104 $this->arrow->SetSize($cs,$cas);
107 $xt = $xscale->Translate($this->coords[1][$i]);
108 $yt = $yscale->Translate($this->coords[0][$i]);
110 $this->arrow->Stroke($img,$xt,$yt,$this->iAngles[$i]);
111 $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
115 // Framework function
116 function Legend(&$aGraph) {
117 if( $this->legend != "" ) {
118 $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
119 $this->legendcsimtarget,$this->legendcsimalt);
124 //===================================================
126 // Description: Render X and Y plots
127 //===================================================
128 class ScatterPlot extends Plot {
130 var $linkpoints = false, $linkpointweight=1, $linkpointcolor="black";
133 function ScatterPlot($datay,$datax=false) {
134 if( (count($datax) != count($datay)) && is_array($datax))
135 JpGraphError::Raise("Scatterplot must have equal number of X and Y points.");
136 $this->Plot($datay,$datax);
137 $this->mark = new PlotMark();
138 $this->mark->SetType(MARK_SQUARE);
139 $this->mark->SetColor($this->color);
140 $this->value->SetAlign('center','center');
141 $this->value->SetMargin(0);
146 function SetImpuls($f=true) {
150 // Combine the scatter plot points with a line
151 function SetLinkPoints($aFlag=true,$aColor="black",$aWeight=1) {
152 $this->linkpoints=$aFlag;
153 $this->linkpointcolor=$aColor;
154 $this->linkpointweight=$aWeight;
157 function Stroke(&$img,&$xscale,&$yscale) {
159 $ymin=$yscale->scale_abs[0];
160 if( $yscale->scale[0] < 0 )
161 $yzero=$yscale->Translate(0);
163 $yzero=$yscale->scale_abs[0];
165 $this->csimareas = '';
166 for( $i=0; $i<$this->numpoints; ++$i ) {
169 if( $this->coords[0][$i]==="" )
172 if( isset($this->coords[1]) )
173 $xt = $xscale->Translate($this->coords[1][$i]);
175 $xt = $xscale->Translate($i);
176 $yt = $yscale->Translate($this->coords[0][$i]);
179 if( $this->linkpoints && isset($yt_old) ) {
180 $img->SetColor($this->linkpointcolor);
181 $img->SetLineWeight($this->linkpointweight);
182 $img->Line($xt_old,$yt_old,$xt,$yt);
185 if( $this->impuls ) {
186 $img->SetColor($this->color);
187 $img->SetLineWeight($this->weight);
188 $img->Line($xt,$yzero,$xt,$yt);
191 if( !empty($this->csimtargets[$i]) ) {
192 $this->mark->SetCSIMTarget($this->csimtargets[$i]);
193 $this->mark->SetCSIMAlt($this->csimalts[$i]);
196 if( isset($this->coords[1]) ) {
197 $this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
200 $this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
203 $this->mark->Stroke($img,$xt,$yt);
205 $this->csimareas .= $this->mark->GetCSIMAreas();
206 $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
213 // Framework function
214 function Legend(&$aGraph) {
215 if( $this->legend != "" ) {
216 $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
217 $this->legendcsimtarget,$this->legendcsimalt);