source: trunk/client/modules/Elezioni/grafici/jpgraph_scatter.php@ 358

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