1 |
efrain |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
pStock - class to draw stock charts
|
|
|
4 |
|
|
|
5 |
Version : 2.1.4
|
|
|
6 |
Made by : Jean-Damien POGOLOTTI
|
|
|
7 |
Last Update : 19/01/2014
|
|
|
8 |
|
|
|
9 |
This file can be distributed under the license you can find at :
|
|
|
10 |
|
|
|
11 |
http://www.pchart.net/license
|
|
|
12 |
|
|
|
13 |
You can find the whole class documentation on the pChart web site.
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
define("STOCK_MISSING_SERIE", 180001);
|
|
|
17 |
|
|
|
18 |
/* pStock class definition */
|
|
|
19 |
class pStock
|
|
|
20 |
{
|
|
|
21 |
var $pChartObject;
|
|
|
22 |
var $pDataObject;
|
|
|
23 |
|
|
|
24 |
/* Class creator */
|
|
|
25 |
function __construct($pChartObject, $pDataObject)
|
|
|
26 |
{
|
|
|
27 |
$this->pChartObject = $pChartObject;
|
|
|
28 |
$this->pDataObject = $pDataObject;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
/* Draw a stock chart */
|
|
|
32 |
function drawStockChart(array $Format = [])
|
|
|
33 |
{
|
|
|
34 |
$SerieOpen = "Open";
|
|
|
35 |
$SerieClose = "Close";
|
|
|
36 |
$SerieMin = "Min";
|
|
|
37 |
$SerieMax = "Max";
|
|
|
38 |
$SerieMedian = NULL;
|
|
|
39 |
$LineWidth = 1;
|
|
|
40 |
$LineR = 0;
|
|
|
41 |
$LineG = 0;
|
|
|
42 |
$LineB = 0;
|
|
|
43 |
$LineAlpha = 100;
|
|
|
44 |
$ExtremityWidth = 1;
|
|
|
45 |
$ExtremityLength = 3;
|
|
|
46 |
$ExtremityR = 0;
|
|
|
47 |
$ExtremityG = 0;
|
|
|
48 |
$ExtremityB = 0;
|
|
|
49 |
$ExtremityAlpha = 100;
|
|
|
50 |
$BoxWidth = 8;
|
|
|
51 |
$BoxUpR = isset($Format["BoxUpR"]) ? $Format["BoxUpR"] : 188;
|
|
|
52 |
$BoxUpG = isset($Format["BoxUpG"]) ? $Format["BoxUpG"] : 224;
|
|
|
53 |
$BoxUpB = isset($Format["BoxUpB"]) ? $Format["BoxUpB"] : 46;
|
|
|
54 |
$BoxUpAlpha = 100;
|
|
|
55 |
$BoxUpSurrounding = NULL;
|
|
|
56 |
$BoxUpBorderR = $BoxUpR - 20;
|
|
|
57 |
$BoxUpBorderG = $BoxUpG - 20;
|
|
|
58 |
$BoxUpBorderB = $BoxUpB - 20;
|
|
|
59 |
$BoxUpBorderAlpha = 100;
|
|
|
60 |
$BoxDownR = isset($Format["BoxDownR"]) ? $Format["BoxDownR"] : 224;
|
|
|
61 |
$BoxDownG = isset($Format["BoxDownG"]) ? $Format["BoxDownG"] : 100;
|
|
|
62 |
$BoxDownB = isset($Format["BoxDownB"]) ? $Format["BoxDownB"] : 46;
|
|
|
63 |
$BoxDownAlpha = 100;
|
|
|
64 |
$BoxDownSurrounding = NULL;
|
|
|
65 |
$BoxDownBorderR = $BoxDownR - 20;
|
|
|
66 |
$BoxDownBorderG = $BoxDownG - 20;
|
|
|
67 |
$BoxDownBorderB = $BoxDownB - 20;
|
|
|
68 |
$BoxDownBorderAlpha = 100;
|
|
|
69 |
$ShadowOnBoxesOnly = TRUE;
|
|
|
70 |
$MedianR = 255;
|
|
|
71 |
$MedianG = 0;
|
|
|
72 |
$MedianB = 0;
|
|
|
73 |
$MedianAlpha = 100;
|
|
|
74 |
$RecordImageMap = FALSE;
|
|
|
75 |
$ImageMapTitle = "Stock Chart";
|
|
|
76 |
|
|
|
77 |
/* Override defaults */
|
|
|
78 |
extract($Format);
|
|
|
79 |
|
|
|
80 |
/* Data Processing */
|
|
|
81 |
$Data = $this->pDataObject->getData();
|
|
|
82 |
$Palette = $this->pDataObject->getPalette();
|
|
|
83 |
|
|
|
84 |
if ($BoxUpSurrounding != NULL) {
|
|
|
85 |
$BoxUpBorderR = $BoxUpR + $BoxUpSurrounding;
|
|
|
86 |
$BoxUpBorderG = $BoxUpG + $BoxUpSurrounding;
|
|
|
87 |
$BoxUpBorderB = $BoxUpB + $BoxUpSurrounding;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if ($BoxDownSurrounding != NULL) {
|
|
|
91 |
$BoxDownBorderR = $BoxDownR + $BoxDownSurrounding;
|
|
|
92 |
$BoxDownBorderG = $BoxDownG + $BoxDownSurrounding;
|
|
|
93 |
$BoxDownBorderB = $BoxDownB + $BoxDownSurrounding;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if ($LineWidth != 1) {
|
|
|
97 |
$LineOffset = $LineWidth / 2;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$BoxOffset = $BoxWidth / 2;
|
|
|
101 |
$Data = $this->pChartObject->DataSet->getData();
|
|
|
102 |
list($XMargin, $XDivs) = $this->pChartObject->scaleGetXSettings();
|
|
|
103 |
if (!isset($Data["Series"][$SerieOpen]) || !isset($Data["Series"][$SerieClose]) || !isset($Data["Series"][$SerieMin]) || !isset($Data["Series"][$SerieMax])) {
|
|
|
104 |
return STOCK_MISSING_SERIE;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
$Plots = [];
|
|
|
108 |
foreach($Data["Series"][$SerieOpen]["Data"] as $Key => $Value) {
|
|
|
109 |
$Point = [];
|
|
|
110 |
if (isset($Data["Series"][$SerieClose]["Data"][$Key]) || isset($Data["Series"][$SerieMin]["Data"][$Key]) || isset($Data["Series"][$SerieMax]["Data"][$Key])) {
|
|
|
111 |
$Point = array($Value,$Data["Series"][$SerieClose]["Data"][$Key],$Data["Series"][$SerieMin]["Data"][$Key],$Data["Series"][$SerieMax]["Data"][$Key]);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
if ($SerieMedian != NULL && isset($Data["Series"][$SerieMedian]["Data"][$Key])) {
|
|
|
115 |
$Point[] = $Data["Series"][$SerieMedian]["Data"][$Key];
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
$Plots[] = $Point;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$AxisID = $Data["Series"][$SerieOpen]["Axis"];
|
|
|
122 |
$Mode = $Data["Axis"][$AxisID]["Display"];
|
|
|
123 |
$Format = $Data["Axis"][$AxisID]["Format"];
|
|
|
124 |
$Unit = $Data["Axis"][$AxisID]["Unit"];
|
|
|
125 |
$YZero = $this->pChartObject->scaleComputeY(0, ["AxisID" => $AxisID]);
|
|
|
126 |
$XStep = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1 - $XMargin * 2) / $XDivs;
|
|
|
127 |
$X = $this->pChartObject->GraphAreaX1 + $XMargin;
|
|
|
128 |
$Y = $this->pChartObject->GraphAreaY1 + $XMargin;
|
|
|
129 |
$LineSettings = ["R" => $LineR,"G" => $LineG,"B" => $LineB,"Alpha" => $LineAlpha];
|
|
|
130 |
$ExtremitySettings = ["R" => $ExtremityR,"G" => $ExtremityG,"B" => $ExtremityB, "Alpha" => $ExtremityAlpha];
|
|
|
131 |
$BoxUpSettings = ["R" => $BoxUpR,"G" => $BoxUpG,"B" => $BoxUpB,"Alpha" => $BoxUpAlpha,"BorderR" => $BoxUpBorderR,"BorderG" => $BoxUpBorderG,"BorderB" => $BoxUpBorderB,"BorderAlpha" => $BoxUpBorderAlpha];
|
|
|
132 |
$BoxDownSettings = ["R" => $BoxDownR,"G" => $BoxDownG,"B" => $BoxDownB,"Alpha" => $BoxDownAlpha,"BorderR" => $BoxDownBorderR,"BorderG" => $BoxDownBorderG,"BorderB" => $BoxDownBorderB,"BorderAlpha" => $BoxDownBorderAlpha];
|
|
|
133 |
$MedianSettings = ["R" => $MedianR,"G" => $MedianG,"B" => $MedianB,"Alpha" => $MedianAlpha];
|
|
|
134 |
|
|
|
135 |
foreach($Plots as $Key => $Points) {
|
|
|
136 |
|
|
|
137 |
$PosArray = $this->pChartObject->scaleComputeY($Points, ["AxisID" => $AxisID]);
|
|
|
138 |
$Values = "Open :" . $Data["Series"][$SerieOpen]["Data"][$Key] . "<br />Close : " . $Data["Series"][$SerieClose]["Data"][$Key] . "<br />Min : " . $Data["Series"][$SerieMin]["Data"][$Key] . "<br />Max : " . $Data["Series"][$SerieMax]["Data"][$Key] . "<br />";
|
|
|
139 |
|
|
|
140 |
if ($SerieMedian != NULL) {
|
|
|
141 |
$Values = $Values . "Median : " . $Data["Series"][$SerieMedian]["Data"][$Key] . "<br />";
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
if ($PosArray[0] > $PosArray[1]) {
|
|
|
145 |
$ImageMapColor = $this->pChartObject->toHTMLColor($BoxUpR, $BoxUpG, $BoxUpB);
|
|
|
146 |
} else {
|
|
|
147 |
$ImageMapColor = $this->pChartObject->toHTMLColor($BoxDownR, $BoxDownG, $BoxDownB);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if ($Data["Orientation"] == SCALE_POS_LEFTRIGHT) {
|
|
|
151 |
if ($YZero > $this->pChartObject->GraphAreaY2 - 1) {
|
|
|
152 |
$YZero = $this->pChartObject->GraphAreaY2 - 1;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if ($YZero < $this->pChartObject->GraphAreaY1 + 1) {
|
|
|
156 |
$YZero = $this->pChartObject->GraphAreaY1 + 1;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
if ($XDivs == 0) {
|
|
|
160 |
$XStep = 0;
|
|
|
161 |
} else {
|
|
|
162 |
$XStep = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1 - $XMargin * 2) / $XDivs;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if ($ShadowOnBoxesOnly) {
|
|
|
166 |
$RestoreShadow = $this->pChartObject->Shadow;
|
|
|
167 |
$this->pChartObject->Shadow = FALSE;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
if ($LineWidth == 1) {
|
|
|
171 |
$this->pChartObject->drawLine($X, $PosArray[2], $X, $PosArray[3], $LineSettings);
|
|
|
172 |
} else {
|
|
|
173 |
$this->pChartObject->drawFilledRectangle($X - $LineOffset, $PosArray[2], $X + $LineOffset, $PosArray[3], $LineSettings);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if ($ExtremityWidth == 1) {
|
|
|
177 |
$this->pChartObject->drawLine($X - $ExtremityLength, $PosArray[2], $X + $ExtremityLength, $PosArray[2], $ExtremitySettings);
|
|
|
178 |
$this->pChartObject->drawLine($X - $ExtremityLength, $PosArray[3], $X + $ExtremityLength, $PosArray[3], $ExtremitySettings);
|
|
|
179 |
if ($RecordImageMap) {
|
|
|
180 |
$this->pChartObject->addToImageMap("RECT", floor($X - $ExtremityLength) . "," . floor($PosArray[2]) . "," . floor($X + $ExtremityLength) . "," . floor($PosArray[3]) , $ImageMapColor, $ImageMapTitle, $Values);
|
|
|
181 |
}
|
|
|
182 |
} else {
|
|
|
183 |
$this->pChartObject->drawFilledRectangle($X - $ExtremityLength, $PosArray[2], $X + $ExtremityLength, $PosArray[2] - $ExtremityWidth, $ExtremitySettings);
|
|
|
184 |
$this->pChartObject->drawFilledRectangle($X - $ExtremityLength, $PosArray[3], $X + $ExtremityLength, $PosArray[3] + $ExtremityWidth, $ExtremitySettings);
|
|
|
185 |
if ($RecordImageMap) {
|
|
|
186 |
$this->pChartObject->addToImageMap("RECT", floor($X - $ExtremityLength) . "," . floor($PosArray[2] - $ExtremityWidth) . "," . floor($X + $ExtremityLength) . "," . floor($PosArray[3] + $ExtremityWidth) , $ImageMapColor, $ImageMapTitle, $Values);
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
if ($ShadowOnBoxesOnly) {
|
|
|
191 |
$this->pChartObject->Shadow = $RestoreShadow;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
if ($PosArray[0] > $PosArray[1]) {
|
|
|
195 |
$this->pChartObject->drawFilledRectangle($X - $BoxOffset, $PosArray[0], $X + $BoxOffset, $PosArray[1], $BoxUpSettings);
|
|
|
196 |
} else {
|
|
|
197 |
$this->pChartObject->drawFilledRectangle($X - $BoxOffset, $PosArray[0], $X + $BoxOffset, $PosArray[1], $BoxDownSettings);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if (isset($PosArray[4])) {
|
|
|
201 |
$this->pChartObject->drawLine($X - $ExtremityLength, $PosArray[4], $X + $ExtremityLength, $PosArray[4], $MedianSettings);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
$X = $X + $XStep;
|
|
|
205 |
|
|
|
206 |
} elseif ($Data["Orientation"] == SCALE_POS_TOPBOTTOM) {
|
|
|
207 |
|
|
|
208 |
if ($YZero > $this->pChartObject->GraphAreaX2 - 1) {
|
|
|
209 |
$YZero = $this->pChartObject->GraphAreaX2 - 1;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
if ($YZero < $this->pChartObject->GraphAreaX1 + 1) {
|
|
|
213 |
$YZero = $this->pChartObject->GraphAreaX1 + 1;
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
if ($XDivs == 0) {
|
|
|
217 |
$XStep = 0;
|
|
|
218 |
} else {
|
|
|
219 |
$XStep = ($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1 - $XMargin * 2) / $XDivs;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
if ($LineWidth == 1) {
|
|
|
223 |
$this->pChartObject->drawLine($PosArray[2], $Y, $PosArray[3], $Y, $LineSettings);
|
|
|
224 |
} else {
|
|
|
225 |
$this->pChartObject->drawFilledRectangle($PosArray[2], $Y - $LineOffset, $PosArray[3], $Y + $LineOffset, $LineSettings);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
if ($ShadowOnBoxesOnly) {
|
|
|
229 |
$RestoreShadow = $this->pChartObject->Shadow;
|
|
|
230 |
$this->pChartObject->Shadow = FALSE;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
if ($ExtremityWidth == 1) {
|
|
|
234 |
$this->pChartObject->drawLine($PosArray[2], $Y - $ExtremityLength, $PosArray[2], $Y + $ExtremityLength, $ExtremitySettings);
|
|
|
235 |
$this->pChartObject->drawLine($PosArray[3], $Y - $ExtremityLength, $PosArray[3], $Y + $ExtremityLength, $ExtremitySettings);
|
|
|
236 |
if ($RecordImageMap) {
|
|
|
237 |
$this->pChartObject->addToImageMap("RECT", floor($PosArray[2]) . "," . floor($Y - $ExtremityLength) . "," . floor($PosArray[3]) . "," . floor($Y + $ExtremityLength) , $ImageMapColor, $ImageMapTitle, $Values);
|
|
|
238 |
}
|
|
|
239 |
} else {
|
|
|
240 |
$this->pChartObject->drawFilledRectangle($PosArray[2], $Y - $ExtremityLength, $PosArray[2] - $ExtremityWidth, $Y + $ExtremityLength, $ExtremitySettings);
|
|
|
241 |
$this->pChartObject->drawFilledRectangle($PosArray[3], $Y - $ExtremityLength, $PosArray[3] + $ExtremityWidth, $Y + $ExtremityLength, $ExtremitySettings);
|
|
|
242 |
if ($RecordImageMap) {
|
|
|
243 |
$this->pChartObject->addToImageMap("RECT", floor($PosArray[2] - $ExtremityWidth) . "," . floor($Y - $ExtremityLength) . "," . floor($PosArray[3] + $ExtremityWidth) . "," . floor($Y + $ExtremityLength) , $ImageMapColor, $ImageMapTitle, $Values);
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
if ($ShadowOnBoxesOnly) {
|
|
|
248 |
$this->pChartObject->Shadow = $RestoreShadow;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
if ($PosArray[0] < $PosArray[1]) {
|
|
|
252 |
$this->pChartObject->drawFilledRectangle($PosArray[0], $Y - $BoxOffset, $PosArray[1], $Y + $BoxOffset, $BoxUpSettings);
|
|
|
253 |
} else {
|
|
|
254 |
$this->pChartObject->drawFilledRectangle($PosArray[0], $Y - $BoxOffset, $PosArray[1], $Y + $BoxOffset, $BoxDownSettings);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
if (isset($PosArray[4])) {
|
|
|
258 |
$this->pChartObject->drawLine($PosArray[4], $Y - $ExtremityLength, $PosArray[4], $Y + $ExtremityLength, $MedianSettings);
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
$Y = $Y + $XStep;
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
?>
|