Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 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 781 2006-10-08 08:07:47Z 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
    public $iColor='black';
19
    public $iSize=10;  // Length in pixels for  arrow
20
    public $iArrowSize = 2;
21
    private $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
    private $iAngles;
65
    private $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
		//echo "f=$f, cc=$cc, cs=$cs, cas=$cas<br>";
105
		$this->arrow->SetColor($cc);
106
		$this->arrow->SetSize($cs,$cas);
107
	    }
108
 
109
	    $xt = $xscale->Translate($this->coords[1][$i]);
110
	    $yt = $yscale->Translate($this->coords[0][$i]);
111
 
112
	    $this->arrow->Stroke($img,$xt,$yt,$this->iAngles[$i]);
113
	    $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
114
	}
115
    }
116
 
117
    // Framework function
118
    function Legend($aGraph) {
119
	if( $this->legend != "" ) {
120
	    $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
121
				 $this->legendcsimtarget,$this->legendcsimalt);
122
	}
123
    }
124
}
125
 
126
//===================================================
127
// CLASS ScatterPlot
128
// Description: Render X and Y plots
129
//===================================================
130
class ScatterPlot extends Plot {
131
    private $impuls = false;
132
    private $linkpoints = false, $linkpointweight=1, $linkpointcolor="black";
133
//---------------
134
// CONSTRUCTOR
135
    function ScatterPlot($datay,$datax=false) {
136
	if( (count($datax) != count($datay)) && is_array($datax))
137
	    JpGraphError::RaiseL(20003);//("Scatterplot must have equal number of X and Y points.");
138
	$this->Plot($datay,$datax);
139
	$this->mark = new PlotMark();
140
	$this->mark->SetType(MARK_SQUARE);
141
	$this->mark->SetColor($this->color);
142
	$this->value->SetAlign('center','center');
143
	$this->value->SetMargin(0);
144
    }
145
 
146
//---------------
147
// PUBLIC METHODS
148
    function SetImpuls($f=true) {
149
	$this->impuls = $f;
150
    }
151
 
152
    // Combine the scatter plot points with a line
153
    function SetLinkPoints($aFlag=true,$aColor="black",$aWeight=1) {
154
	$this->linkpoints=$aFlag;
155
	$this->linkpointcolor=$aColor;
156
	$this->linkpointweight=$aWeight;
157
    }
158
 
159
    function Stroke($img,$xscale,$yscale) {
160
 
161
	$ymin=$yscale->scale_abs[0];
162
	if( $yscale->scale[0] < 0 )
163
	    $yzero=$yscale->Translate(0);
164
	else
165
	    $yzero=$yscale->scale_abs[0];
166
 
167
	$this->csimareas = '';
168
	for( $i=0; $i<$this->numpoints; ++$i ) {
169
 
170
	    // Skip null values
171
	    if( $this->coords[0][$i]==='' || $this->coords[0][$i]==='-' || $this->coords[0][$i]==='x')
172
		continue;
173
 
174
	    if( isset($this->coords[1]) )
175
		$xt = $xscale->Translate($this->coords[1][$i]);
176
	    else
177
		$xt = $xscale->Translate($i);
178
	    $yt = $yscale->Translate($this->coords[0][$i]);
179
 
180
 
181
	    if( $this->linkpoints && isset($yt_old) ) {
182
		$img->SetColor($this->linkpointcolor);
183
		$img->SetLineWeight($this->linkpointweight);
184
		$img->Line($xt_old,$yt_old,$xt,$yt);
185
	    }
186
 
187
	    if( $this->impuls ) {
188
		$img->SetColor($this->color);
189
		$img->SetLineWeight($this->weight);
190
		$img->Line($xt,$yzero,$xt,$yt);
191
	    }
192
 
193
	    if( !empty($this->csimtargets[$i]) ) {
194
	        $this->mark->SetCSIMTarget($this->csimtargets[$i]);
195
	        $this->mark->SetCSIMAlt($this->csimalts[$i]);
196
	    }
197
 
198
	    if( isset($this->coords[1]) ) {
199
		$this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
200
	    }
201
	    else {
202
		$this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
203
	    }
204
 
205
	    $this->mark->Stroke($img,$xt,$yt);
206
 
207
	    $this->csimareas .= $this->mark->GetCSIMAreas();
208
	    $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);
209
 
210
	    $xt_old = $xt;
211
	    $yt_old = $yt;
212
	}
213
    }
214
 
215
    // Framework function
216
    function Legend($aGraph) {
217
	if( $this->legend != "" ) {
218
	    $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
219
				 $this->legendcsimtarget,$this->legendcsimalt);
220
	}
221
    }
222
} // Class
223
/* EOF */
224
?>