1
|
|
|
<?php |
2
|
|
|
namespace Amenadiel\JpGraph\Plot; |
3
|
|
|
|
4
|
|
|
use Amenadiel\JpGraph\Graph; |
5
|
|
|
use Amenadiel\JpGraph\Text; |
6
|
|
|
|
7
|
|
|
define('WINDROSE_TYPE4', 1); |
8
|
|
|
define('WINDROSE_TYPE8', 2); |
9
|
|
|
define('WINDROSE_TYPE16', 3); |
10
|
|
|
define('WINDROSE_TYPEFREE', 4); |
11
|
|
|
|
12
|
|
|
//------------------------------------------------------------------------ |
13
|
|
|
// How should the labels for the circular grids be aligned |
14
|
|
|
//------------------------------------------------------------------------ |
15
|
|
|
define('LBLALIGN_CENTER', 1); |
16
|
|
|
define('LBLALIGN_TOP', 2); |
17
|
|
|
|
18
|
|
|
//------------------------------------------------------------------------ |
19
|
|
|
// How should the labels around the plot be align |
20
|
|
|
//------------------------------------------------------------------------ |
21
|
|
|
define('LBLPOSITION_CENTER', 1); |
22
|
|
|
define('LBLPOSITION_EDGE', 2); |
23
|
|
|
|
24
|
|
|
//------------------------------------------------------------------------ |
25
|
|
|
// Interpretation of ordinal values in the data |
26
|
|
|
//------------------------------------------------------------------------ |
27
|
|
|
define('KEYENCODING_CLOCKWISE', 1); |
28
|
|
|
define('KEYENCODING_ANTICLOCKWISE', 2); |
29
|
|
|
|
30
|
|
|
// Internal debug flag |
31
|
|
|
define('__DEBUG', false); |
32
|
|
|
define('RANGE_OVERLAPPING', 0); |
33
|
|
|
define('RANGE_DISCRETE', 1); |
34
|
|
|
|
35
|
|
|
//=================================================== |
36
|
|
|
// CLASS WindrosePlot |
37
|
|
|
//=================================================== |
38
|
|
|
class WindrosePlot |
39
|
|
|
{ |
40
|
|
|
private $iAntiAlias = true; |
41
|
|
|
private $iData = []; |
42
|
|
|
public $iX = 0.5; |
43
|
|
|
public $iY = 0.5; |
44
|
|
|
public $iSize = 0.55; |
45
|
|
|
private $iGridColor1 = 'gray'; |
46
|
|
|
private $iGridColor2 = 'darkgreen'; |
47
|
|
|
private $iRadialColorArray = []; |
48
|
|
|
private $iRadialWeightArray = []; |
49
|
|
|
private $iRadialStyleArray = []; |
50
|
|
|
private $iRanges = [1, 2, 3, 5, 6, 10, 13.5, 99.0]; |
51
|
|
|
private $iRangeStyle = RANGE_OVERLAPPING; |
52
|
|
|
public $iCenterSize = 60; |
53
|
|
|
private $iType = WINDROSE_TYPE16; |
54
|
|
|
public $iFontFamily = FF_VERDANA; |
55
|
|
|
public $iFontStyle = FS_NORMAL; |
56
|
|
|
public $iFontSize = 10; |
57
|
|
|
public $iFontColor = 'darkgray'; |
58
|
|
|
private $iRadialGridStyle = 'longdashed'; |
59
|
|
|
private $iAllDirectionLabels = ['E', 'ENE', 'NE', 'NNE', 'N', 'NNW', 'NW', 'WNW', 'W', 'WSW', 'SW', 'SSW', 'S', 'SSE', 'SE', 'ESE']; |
60
|
|
|
private $iStandardDirections = []; |
61
|
|
|
private $iCircGridWeight = 3; |
62
|
|
|
private $iRadialGridWeight = 1; |
63
|
|
|
private $iLabelMargin = 12; |
64
|
|
|
private $iLegweights = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; |
65
|
|
|
private $iLegColors = ['orange', 'black', 'blue', 'red', 'green', 'purple', 'navy', 'yellow', 'brown']; |
66
|
|
|
private $iLabelFormatString = ''; |
67
|
|
|
private $iLabels = []; |
68
|
|
|
private $iLabelPositioning = LBLPOSITION_EDGE; |
69
|
|
|
private $iColor = 'white'; |
70
|
|
|
private $iShowBox = false; |
71
|
|
|
private $iBoxColor = 'black'; |
72
|
|
|
private $iBoxWeight = 1; |
73
|
|
|
private $iBoxStyle = 'solid'; |
74
|
|
|
private $iOrdinalEncoding = KEYENCODING_ANTICLOCKWISE; |
75
|
|
|
public $legend = null; |
76
|
|
|
|
77
|
|
|
public function __construct($aData) |
78
|
|
|
{ |
79
|
|
|
$this->iData = $aData; |
80
|
|
|
$this->legend = new LegendStyle(); |
81
|
|
|
|
82
|
|
|
// Setup the scale |
83
|
|
|
$this->scale = new Graph\WindrosePlotScale($this->iData); |
|
|
|
|
84
|
|
|
|
85
|
|
|
// default label for free type i agle and a degree sign |
86
|
|
|
$this->iLabelFormatString = '%.1f' . Graph\SymChar::Get('degree'); |
87
|
|
|
|
88
|
|
|
$delta = 2 * M_PI / 16; |
|
|
|
|
89
|
|
View Code Duplication |
for ($i = 0, $a = 0; $i < 16; ++$i, $a += $delta) { |
|
|
|
|
90
|
|
|
$this->iStandardDirections[$this->iAllDirectionLabels[$i]] = $a; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Dummy method to make window plots have the same signature as the |
95
|
|
|
// layout classes since windrose plots are "leaf" classes in the hierarchy |
96
|
|
|
public function LayoutSize() |
97
|
|
|
{ |
98
|
|
|
return 1; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function SetSize($aSize) |
102
|
|
|
{ |
103
|
|
|
$this->iSize = $aSize; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function SetDataKeyEncoding($aEncoding) |
107
|
|
|
{ |
108
|
|
|
$this->iOrdinalEncoding = $aEncoding; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function SetColor($aColor) |
112
|
|
|
{ |
113
|
|
|
$this->iColor = $aColor; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function SetRadialColors($aColors) |
117
|
|
|
{ |
118
|
|
|
$this->iRadialColorArray = $aColors; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function SetRadialWeights($aWeights) |
122
|
|
|
{ |
123
|
|
|
$this->iRadialWeightArray = $aWeights; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function SetRadialStyles($aStyles) |
127
|
|
|
{ |
128
|
|
|
$this->iRadialStyleArray = $aStyles; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function SetBox($aColor = 'black', $aWeight = 1, $aStyle = 'solid', $aShow = true) |
132
|
|
|
{ |
133
|
|
|
$this->iShowBox = $aShow; |
134
|
|
|
$this->iBoxColor = $aColor; |
135
|
|
|
$this->iBoxWeight = $aWeight; |
136
|
|
|
$this->iBoxStyle = $aStyle; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function SetLabels($aLabels) |
140
|
|
|
{ |
141
|
|
|
$this->iLabels = $aLabels; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function SetLabelMargin($aMarg) |
145
|
|
|
{ |
146
|
|
|
$this->iLabelMargin = $aMarg; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function SetLabelFormat($aLblFormat) |
150
|
|
|
{ |
151
|
|
|
$this->iLabelFormatString = $aLblFormat; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function SetCompassLabels($aLabels) |
155
|
|
|
{ |
156
|
|
|
if (count($aLabels) != 16) { |
157
|
|
|
Util\JpGraphError::RaiseL(22004); //('Label specification for windrose directions must have 16 values (one for each compass direction).'); |
158
|
|
|
} |
159
|
|
|
$this->iAllDirectionLabels = $aLabels; |
160
|
|
|
|
161
|
|
|
$delta = 2 * M_PI / 16; |
|
|
|
|
162
|
|
View Code Duplication |
for ($i = 0, $a = 0; $i < 16; ++$i, $a += $delta) { |
|
|
|
|
163
|
|
|
$this->iStandardDirections[$this->iAllDirectionLabels[$i]] = $a; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function SetCenterSize($aSize) |
168
|
|
|
{ |
169
|
|
|
$this->iCenterSize = $aSize; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Alias for SetCenterSize |
173
|
|
|
public function SetZCircleSize($aSize) |
174
|
|
|
{ |
175
|
|
|
$this->iCenterSize = $aSize; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function SetFont($aFFam, $aFStyle = FS_NORMAL, $aFSize = 10) |
179
|
|
|
{ |
180
|
|
|
$this->iFontFamily = $aFFam; |
181
|
|
|
$this->iFontStyle = $aFStyle; |
182
|
|
|
$this->iFontSize = $aFSize; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function SetFontColor($aColor) |
186
|
|
|
{ |
187
|
|
|
$this->iFontColor = $aColor; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function SetGridColor($aColor1, $aColor2) |
191
|
|
|
{ |
192
|
|
|
$this->iGridColor1 = $aColor1; |
193
|
|
|
$this->iGridColor2 = $aColor2; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function SetGridWeight($aGrid1 = 1, $aGrid2 = 2) |
197
|
|
|
{ |
198
|
|
|
$this->iCircGridWeight = $aGrid1; |
199
|
|
|
$this->iRadialGridWeight = $aGrid2; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function SetRadialGridStyle($aStyle) |
203
|
|
|
{ |
204
|
|
|
$aStyle = strtolower($aStyle); |
205
|
|
|
if (!in_array($aStyle, ['solid', 'dotted', 'dashed', 'longdashed'])) { |
206
|
|
|
Util\JpGraphError::RaiseL(22005); //("Line style for radial lines must be on of ('solid','dotted','dashed','longdashed') "); |
207
|
|
|
} |
208
|
|
|
$this->iRadialGridStyle = $aStyle; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function SetRanges($aRanges) |
212
|
|
|
{ |
213
|
|
|
$this->iRanges = $aRanges; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function SetRangeStyle($aStyle) |
217
|
|
|
{ |
218
|
|
|
$this->iRangeStyle = $aStyle; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function SetRangeColors($aLegColors) |
222
|
|
|
{ |
223
|
|
|
$this->iLegColors = $aLegColors; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function SetRangeWeights($aWeights) |
227
|
|
|
{ |
228
|
|
|
$n = count($aWeights); |
229
|
|
|
for ($i = 0; $i < $n; ++$i) { |
230
|
|
|
$aWeights[$i] = floor($aWeights[$i] / 2); |
231
|
|
|
} |
232
|
|
|
$this->iLegweights = $aWeights; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function SetType($aType) |
236
|
|
|
{ |
237
|
|
|
if ($aType < WINDROSE_TYPE4 || $aType > WINDROSE_TYPEFREE) { |
238
|
|
|
Util\JpGraphError::RaiseL(22006); //('Illegal windrose type specified.'); |
239
|
|
|
} |
240
|
|
|
$this->iType = $aType; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// Alias for SetPos() |
|
|
|
|
244
|
|
|
public function SetCenterPos($aX, $aY) |
245
|
|
|
{ |
246
|
|
|
$this->iX = $aX; |
247
|
|
|
$this->iY = $aY; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function SetPos($aX, $aY) |
251
|
|
|
{ |
252
|
|
|
$this->iX = $aX; |
253
|
|
|
$this->iY = $aY; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function SetAntiAlias($aFlag) |
257
|
|
|
{ |
258
|
|
|
$this->iAntiAlias = $aFlag; |
259
|
|
|
if (!$aFlag) { |
260
|
|
|
$this->iCircGridWeight = 1; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function _ThickCircle($aImg, $aXC, $aYC, $aRad, $aWeight = 2, $aColor) |
265
|
|
|
{ |
266
|
|
|
$aImg->SetColor($aColor); |
267
|
|
|
$aRad *= 2; |
268
|
|
|
$aImg->Ellipse($aXC, $aYC, $aRad, $aRad); |
269
|
|
|
if ($aWeight > 1) { |
270
|
|
|
$aImg->Ellipse($aXC, $aYC, $aRad + 1, $aRad + 1); |
271
|
|
|
$aImg->Ellipse($aXC, $aYC, $aRad + 2, $aRad + 2); |
272
|
|
|
if ($aWeight > 2) { |
273
|
|
|
$aImg->Ellipse($aXC, $aYC, $aRad + 3, $aRad + 3); |
274
|
|
|
$aImg->Ellipse($aXC, $aYC, $aRad + 3, $aRad + 4); |
275
|
|
|
$aImg->Ellipse($aXC, $aYC, $aRad + 4, $aRad + 3); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function _StrokeWindLeg($aImg, $xc, $yc, $a, $ri, $r, $weight, $color) |
281
|
|
|
{ |
282
|
|
|
|
283
|
|
|
// If less than 1 px long then we assume this has been caused by rounding problems |
284
|
|
|
// and should not be stroked |
285
|
|
|
if ($r < 1) { |
286
|
|
|
return; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$xt = $xc + cos($a) * $ri; |
290
|
|
|
$yt = $yc - sin($a) * $ri; |
291
|
|
|
$xxt = $xc + cos($a) * ($ri + $r); |
292
|
|
|
$yyt = $yc - sin($a) * ($ri + $r); |
293
|
|
|
|
294
|
|
|
$x1 = $xt - $weight * sin($a); |
295
|
|
|
$y1 = $yt - $weight * cos($a); |
296
|
|
|
$x2 = $xxt - $weight * sin($a); |
297
|
|
|
$y2 = $yyt - $weight * cos($a); |
298
|
|
|
|
299
|
|
|
$x3 = $xxt + $weight * sin($a); |
300
|
|
|
$y3 = $yyt + $weight * cos($a); |
301
|
|
|
$x4 = $xt + $weight * sin($a); |
302
|
|
|
$y4 = $yt + $weight * cos($a); |
303
|
|
|
|
304
|
|
|
$pts = [$x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4]; |
305
|
|
|
$aImg->SetColor($color); |
306
|
|
|
$aImg->FilledPolygon($pts); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function _StrokeLegend($aImg, $x, $y, $scaling = 1, $aReturnWidth = false) |
310
|
|
|
{ |
311
|
|
|
if (!$this->legend->iShow) { |
312
|
|
|
return 0; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$nlc = count($this->iLegColors); |
316
|
|
|
$nlw = count($this->iLegweights); |
317
|
|
|
|
318
|
|
|
// Setup font for ranges |
319
|
|
|
$value = new Text\Text(); |
320
|
|
|
$value->SetAlign('center', 'bottom'); |
321
|
|
|
$value->SetFont($this->legend->iLblFontFamily, |
322
|
|
|
$this->legend->iLblFontStyle, |
323
|
|
|
$this->legend->iLblFontSize * $scaling); |
324
|
|
|
$value->SetColor($this->legend->iLblFontColor); |
325
|
|
|
|
326
|
|
|
// Remember x-center |
327
|
|
|
$xcenter = $x; |
328
|
|
|
|
329
|
|
|
// Construct format string |
330
|
|
|
$fmt = $this->legend->iFormatString . '-' . $this->legend->iFormatString; |
331
|
|
|
|
332
|
|
|
// Make sure that the length of each range is enough to cover the |
333
|
|
|
// size of the labels |
334
|
|
|
$tst = sprintf($fmt, $this->iRanges[0], $this->iRanges[1]); |
335
|
|
|
$value->Set($tst); |
336
|
|
|
$w = $value->GetWidth($aImg); |
337
|
|
|
$l = round(max($this->legend->iLength * $scaling, $w * 1.5)); |
338
|
|
|
|
339
|
|
|
$r = $this->legend->iCircleRadius * $scaling; |
340
|
|
|
$len = 2 * $r + $this->scale->iMaxNum * $l; |
341
|
|
|
|
342
|
|
|
// We are called just to find out the width |
343
|
|
|
if ($aReturnWidth) { |
344
|
|
|
return $len; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$x -= round($len / 2); |
348
|
|
|
$x += $r; |
349
|
|
|
|
350
|
|
|
// 4 pixels extra vertical margin since the circle sometimes is +/- 1 pixel of the |
351
|
|
|
// theorethical radius due to imperfection in the GD library |
352
|
|
|
//$y -= round(max($r,$scaling*$this->iLegweights[($this->scale->iMaxNum-1) % $nlw])+4*$scaling); |
|
|
|
|
353
|
|
|
$y -= ($this->legend->iCircleRadius + 2) * $scaling + $this->legend->iBottomMargin * $scaling; |
354
|
|
|
|
355
|
|
|
// Adjust for bottom text |
356
|
|
|
if ($this->legend->iTxt != '') { |
357
|
|
|
// Setup font for text |
358
|
|
|
$value->Set($this->legend->iTxt); |
359
|
|
|
$y -= /*$this->legend->iTxtMargin + */$value->GetHeight($aImg); |
|
|
|
|
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// Stroke 0-circle |
363
|
|
|
$this->_ThickCircle($aImg, $x, $y, $r, $this->legend->iCircleWeight, |
364
|
|
|
$this->legend->iCircleColor); |
365
|
|
|
|
366
|
|
|
// Remember the center of the circe |
367
|
|
|
$xc = $x; |
368
|
|
|
$yc = $y; |
369
|
|
|
|
370
|
|
|
$value->SetAlign('center', 'bottom'); |
371
|
|
|
$x += $r + 1; |
372
|
|
|
|
373
|
|
|
// Stroke all used ranges |
374
|
|
|
$txty = $y - |
375
|
|
|
round($this->iLegweights[($this->scale->iMaxNum - 1) % $nlw] * $scaling) - 4 * $scaling; |
376
|
|
|
if ($this->scale->iMaxNum >= count($this->iRanges)) { |
377
|
|
|
Util\JpGraphError::RaiseL(22007); //('To few values for the range legend.'); |
378
|
|
|
} |
379
|
|
|
$i = 0; |
380
|
|
|
$idx = 0; |
381
|
|
|
while ($i < $this->scale->iMaxNum) { |
382
|
|
|
$y1 = $y - round($this->iLegweights[$i % $nlw] * $scaling); |
383
|
|
|
$y2 = $y + round($this->iLegweights[$i % $nlw] * $scaling); |
384
|
|
|
$x2 = $x + $l; |
385
|
|
|
$aImg->SetColor($this->iLegColors[$i % $nlc]); |
386
|
|
|
$aImg->FilledRectangle($x, $y1, $x2, $y2); |
387
|
|
|
if ($this->iRangeStyle == RANGE_OVERLAPPING) { |
388
|
|
|
$lbl = sprintf($fmt, $this->iRanges[$idx], $this->iRanges[$idx + 1]); |
389
|
|
|
} else { |
390
|
|
|
$lbl = sprintf($fmt, $this->iRanges[$idx], $this->iRanges[$idx + 1]); |
391
|
|
|
++$idx; |
392
|
|
|
} |
393
|
|
|
$value->Set($lbl); |
394
|
|
|
$value->Stroke($aImg, $x + $l / 2, $txty); |
395
|
|
|
$x = $x2; |
396
|
|
|
++$i; |
397
|
|
|
++$idx; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// Setup circle font |
401
|
|
|
$value->SetFont($this->legend->iCircleFontFamily, |
402
|
|
|
$this->legend->iCircleFontStyle, |
403
|
|
|
$this->legend->iCircleFontSize * $scaling); |
404
|
|
|
$value->SetColor($this->legend->iCircleFontColor); |
405
|
|
|
|
406
|
|
|
// Stroke 0-circle text |
407
|
|
|
$value->Set($this->legend->iZCircleTxt); |
408
|
|
|
$value->SetAlign('center', 'center'); |
409
|
|
|
$value->ParagraphAlign('center'); |
410
|
|
|
$value->Stroke($aImg, $xc, $yc); |
411
|
|
|
|
412
|
|
|
// Setup circle font |
413
|
|
|
$value->SetFont($this->legend->iTxtFontFamily, |
414
|
|
|
$this->legend->iTxtFontStyle, |
415
|
|
|
$this->legend->iTxtFontSize * $scaling); |
416
|
|
|
$value->SetColor($this->legend->iTxtFontColor); |
417
|
|
|
|
418
|
|
|
// Draw the text under the legend |
419
|
|
|
$value->Set($this->legend->iTxt); |
420
|
|
|
$value->SetAlign('center', 'top'); |
421
|
|
|
$value->SetParagraphAlign('center'); |
422
|
|
|
$value->Stroke($aImg, $xcenter, $y2 + $this->legend->iTxtMargin * $scaling); |
|
|
|
|
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function SetAutoScaleAngle($aIsRegRose = true) |
426
|
|
|
{ |
427
|
|
|
|
428
|
|
|
// If the user already has manually set an angle don't |
429
|
|
|
// trye to find a position |
430
|
|
|
if (is_numeric($this->scale->iAngle)) { |
431
|
|
|
return; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
if ($aIsRegRose) { |
435
|
|
|
|
436
|
|
|
// Create a complete data for all directions |
437
|
|
|
// and translate string directions to ordinal values. |
438
|
|
|
// This will much simplify the logic below |
439
|
|
|
for ($i = 0; $i < 16; ++$i) { |
440
|
|
|
$dtxt = $this->iAllDirectionLabels[$i]; |
441
|
|
|
if (!empty($this->iData[$dtxt])) { |
442
|
|
|
$data[$i] = $this->iData[$dtxt]; |
|
|
|
|
443
|
|
|
} elseif (!empty($this->iData[strtolower($dtxt)])) { |
444
|
|
|
$data[$i] = $this->iData[strtolower($dtxt)]; |
|
|
|
|
445
|
|
|
} elseif (!empty($this->iData[$i])) { |
446
|
|
|
$data[$i] = $this->iData[$i]; |
447
|
|
|
} else { |
448
|
|
|
$data[$i] = []; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
// Find the leg which has the lowest weighted sum of number of data around it |
453
|
|
|
$c0 = array_sum($data[0]); |
454
|
|
|
$c1 = array_sum($data[1]); |
455
|
|
|
$found = 1; |
456
|
|
|
$min = $c0 + $c1 * 100; // Initialize to a high value |
457
|
|
|
for ($i = 1; $i < 15; ++$i) { |
458
|
|
|
$c2 = array_sum($data[$i + 1]); |
459
|
|
|
|
460
|
|
|
// Weight the leg we will use more to give preference |
461
|
|
|
// to a short middle leg even if the 3 way sum is similair |
462
|
|
|
$w = $c0 + 3 * $c1 + $c2; |
463
|
|
|
if ($w < $min) { |
464
|
|
|
$min = $w; |
465
|
|
|
$found = $i; |
466
|
|
|
} |
467
|
|
|
$c0 = $c1; |
468
|
|
|
$c1 = $c2; |
469
|
|
|
} |
470
|
|
|
$this->scale->iAngle = $found * 22.5; |
471
|
|
|
} else { |
472
|
|
|
$n = count($this->iData); |
|
|
|
|
473
|
|
|
foreach ($this->iData as $dir => $leg) { |
474
|
|
|
if (!is_numeric($dir)) { |
475
|
|
|
$pos = array_search(strtoupper($dir), $this->iAllDirectionLabels); |
476
|
|
|
if ($pos !== false) { |
477
|
|
|
$dir = $pos * 22.5; |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
$data[round($dir)] = $leg; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
// Get all the angles for the data and sort it |
484
|
|
|
$keys = array_keys($data); |
485
|
|
|
sort($keys, SORT_NUMERIC); |
486
|
|
|
|
487
|
|
|
$n = count($data); |
488
|
|
|
$found = false; |
489
|
|
|
$max = 0; |
490
|
|
|
for ($i = 0; $i < 15; ++$i) { |
491
|
|
|
$try_a = round(22.5 * $i); |
492
|
|
|
|
493
|
|
|
if ($try_a > $keys[$n - 1]) { |
494
|
|
|
break; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
if (in_array($try_a, $keys)) { |
498
|
|
|
continue; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
// Find the angle just lower than this |
502
|
|
|
$j = 0; |
503
|
|
|
while ($j < $n && $keys[$j] <= $try_a) { |
504
|
|
|
++$j; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
View Code Duplication |
if ($j == 0) { |
|
|
|
|
508
|
|
|
$kj = 0; |
509
|
|
|
$keys[$n - 1]; |
510
|
|
|
$d1 = 0; |
511
|
|
|
abs($kj - $try_a); |
512
|
|
|
} else { |
513
|
|
|
--$j; |
514
|
|
|
$kj = $keys[$j]; |
515
|
|
|
$d1 = abs($kj - $try_a); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
// Find the angle just larger than this |
519
|
|
|
$l = $n - 1; |
520
|
|
|
while ($l >= 0 && $keys[$l] >= $try_a) { |
521
|
|
|
--$l; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
View Code Duplication |
if ($l == $n - 1) { |
|
|
|
|
525
|
|
|
$kl = $keys[0]; |
526
|
|
|
$d2 = abs($kl - $try_a); |
527
|
|
|
} else { |
528
|
|
|
++$l; |
529
|
|
|
$kl = $keys[$l]; |
530
|
|
|
$d2 = abs($kl - $try_a); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
// Weight the distance so that legs with large spread |
534
|
|
|
// gets a better weight |
535
|
|
|
$w = $d1 + $d2; |
536
|
|
|
if ($i == 0) { |
537
|
|
|
$w = round(1.4 * $w); |
538
|
|
|
} |
539
|
|
|
$diff = abs($d1 - $d2); |
540
|
|
|
$w *= (360 - $diff); |
541
|
|
|
if ($w > $max) { |
542
|
|
|
$found = $i; |
543
|
|
|
$max = $w; |
544
|
|
|
} |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
$a = $found * 22.5; |
548
|
|
|
|
549
|
|
|
// Some heuristics to have some preferred positions |
550
|
|
|
if ($keys[$n - 1] < 25) { |
551
|
|
|
$a = 45; |
552
|
|
|
} elseif ($keys[0] > 60) { |
553
|
|
|
$a = 45; |
554
|
|
|
} elseif ($keys[0] > 25 && $keys[$n - 1] < 340) { |
555
|
|
|
$a = 0; |
556
|
|
|
} elseif ($keys[$n - 1] < 75) { |
557
|
|
|
$a = 90; |
558
|
|
|
} elseif ($keys[$n - 1] < 120) { |
559
|
|
|
$a = 135; |
560
|
|
|
} elseif ($keys[$n - 1] < 160) { |
561
|
|
|
$a = 180; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
$this->scale->iAngle = $a; |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
public function NormAngle($a) |
569
|
|
|
{ |
570
|
|
|
while ($a > 360) { |
571
|
|
|
$a -= 360; |
572
|
|
|
} |
573
|
|
|
return $a; |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
public function SetLabelPosition($aPos) |
577
|
|
|
{ |
578
|
|
|
$this->iLabelPositioning = $aPos; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
public function _StrokeFreeRose($dblImg, $value, $scaling, $xc, $yc, $r, $ri) |
582
|
|
|
{ |
583
|
|
|
|
584
|
|
|
// Plot radial grid lines and remember the end position |
585
|
|
|
// and the angle for later use when plotting the labels |
586
|
|
|
if ($this->iType != WINDROSE_TYPEFREE) { |
587
|
|
|
Util\JpGraphError::RaiseL(22008); //('Internal error: Trying to plot free Windrose even though type is not a free windorose'); |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
// Check if we should auto-position the angle for the |
591
|
|
|
// labels. Basically we try to find a firection with smallest |
592
|
|
|
// (or none) data. |
593
|
|
|
$this->SetAutoScaleAngle(false); |
594
|
|
|
|
595
|
|
|
$nlc = count($this->iLegColors); |
596
|
|
|
$nlw = count($this->iLegweights); |
597
|
|
|
|
598
|
|
|
// Stroke grid lines for directions and remember the |
599
|
|
|
// position for the labels |
600
|
|
|
$txtpos = []; |
601
|
|
|
$num = count($this->iData); |
602
|
|
|
|
603
|
|
|
$keys = array_keys($this->iData); |
604
|
|
|
|
605
|
|
|
foreach ($this->iData as $dir => $legdata) { |
606
|
|
|
if (in_array($dir, $this->iAllDirectionLabels, true) === true) { |
607
|
|
|
$a = $this->iStandardDirections[strtoupper($dir)]; |
608
|
|
|
if (in_array($a * 180 / M_PI, $keys)) { |
609
|
|
|
Util\JpGraphError::RaiseL(22009, round($a * 180 / M_PI)); |
610
|
|
|
//('You have specified the same direction twice, once with an angle and once with a compass direction ('.$a*180/M_PI.' degrees.)'); |
|
|
|
|
611
|
|
|
} |
612
|
|
|
} elseif (is_numeric($dir)) { |
613
|
|
|
$this->NormAngle($dir); |
|
|
|
|
614
|
|
|
|
615
|
|
|
if ($this->iOrdinalEncoding == KEYENCODING_CLOCKWISE) { |
616
|
|
|
$dir = 360 - $dir; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
$a = $dir * M_PI / 180; |
620
|
|
|
} else { |
621
|
|
|
Util\JpGraphError::RaiseL(22010); //('Direction must either be a numeric value or one of the 16 compass directions'); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
$xxc = round($xc + cos($a) * $ri); |
|
|
|
|
625
|
|
|
$yyc = round($yc - sin($a) * $ri); |
626
|
|
|
$x = round($xc + cos($a) * $r); |
627
|
|
|
$y = round($yc - sin($a) * $r); |
628
|
|
View Code Duplication |
if (empty($this->iRadialColorArray[$dir])) { |
|
|
|
|
629
|
|
|
$dblImg->SetColor($this->iGridColor2); |
630
|
|
|
} else { |
631
|
|
|
$dblImg->SetColor($this->iRadialColorArray[$dir]); |
632
|
|
|
} |
633
|
|
View Code Duplication |
if (empty($this->iRadialWeightArray[$dir])) { |
|
|
|
|
634
|
|
|
$dblImg->SetLineWeight($this->iRadialGridWeight); |
635
|
|
|
} else { |
636
|
|
|
$dblImg->SetLineWeight($this->iRadialWeightArray[$dir]); |
637
|
|
|
} |
638
|
|
View Code Duplication |
if (empty($this->iRadialStyleArray[$dir])) { |
|
|
|
|
639
|
|
|
$dblImg->SetLineStyle($this->iRadialGridStyle); |
640
|
|
|
} else { |
641
|
|
|
$dblImg->SetLineStyle($this->iRadialStyleArray[$dir]); |
642
|
|
|
} |
643
|
|
|
$dblImg->StyleLine($xxc, $yyc, $x, $y); |
644
|
|
|
$txtpos[] = [$x, $y, $a]; |
645
|
|
|
} |
646
|
|
|
$dblImg->SetLineWeight(1); |
647
|
|
|
|
648
|
|
|
// Setup labels |
649
|
|
|
$lr = $scaling * $this->iLabelMargin; |
650
|
|
|
|
651
|
|
View Code Duplication |
if ($this->iLabelPositioning == LBLPOSITION_EDGE) { |
|
|
|
|
652
|
|
|
$value->SetAlign('left', 'top'); |
653
|
|
|
} else { |
654
|
|
|
$value->SetAlign('center', 'center'); |
655
|
|
|
$value->SetMargin(0); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
for ($i = 0; $i < $num; ++$i) { |
659
|
|
|
list($x, $y, $a) = $txtpos[$i]; |
660
|
|
|
|
661
|
|
|
// Determine the label |
662
|
|
|
|
663
|
|
|
$da = $a * 180 / M_PI; |
664
|
|
|
if ($this->iOrdinalEncoding == KEYENCODING_CLOCKWISE) { |
665
|
|
|
$da = 360 - $da; |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
//$da = 360-$da; |
|
|
|
|
669
|
|
|
|
670
|
|
|
if (!empty($this->iLabels[$keys[$i]])) { |
671
|
|
|
$lbl = $this->iLabels[$keys[$i]]; |
672
|
|
|
} else { |
673
|
|
|
$lbl = sprintf($this->iLabelFormatString, $da); |
674
|
|
|
} |
675
|
|
|
|
676
|
|
View Code Duplication |
if ($this->iLabelPositioning == LBLPOSITION_CENTER) { |
|
|
|
|
677
|
|
|
$dx = $dy = 0; |
678
|
|
|
} else { |
679
|
|
|
// LBLPOSIITON_EDGE |
680
|
|
|
if ($a >= 7 * M_PI / 4 || $a <= M_PI / 4) { |
681
|
|
|
$dx = 0; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
if ($a >= M_PI / 4 && $a <= 3 * M_PI / 4) { |
685
|
|
|
$dx = ($a - M_PI / 4) * 2 / M_PI; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
if ($a >= 3 * M_PI / 4 && $a <= 5 * M_PI / 4) { |
689
|
|
|
$dx = 1; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
if ($a >= 5 * M_PI / 4 && $a <= 7 * M_PI / 4) { |
693
|
|
|
$dx = (1 - ($a - M_PI * 5 / 4) * 2 / M_PI); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
if ($a >= 7 * M_PI / 4) { |
697
|
|
|
$dy = (($a - M_PI) - 3 * M_PI / 4) * 2 / M_PI; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
if ($a <= M_PI / 4) { |
701
|
|
|
$dy = (0.5 + $a * 2 / M_PI); |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
if ($a >= M_PI / 4 && $a <= 3 * M_PI / 4) { |
705
|
|
|
$dy = 1; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
if ($a >= 3 * M_PI / 4 && $a <= 5 * M_PI / 4) { |
709
|
|
|
$dy = (1 - ($a - 3 * M_PI / 4) * 2 / M_PI); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
if ($a >= 5 * M_PI / 4 && $a <= 7 * M_PI / 4) { |
713
|
|
|
$dy = 0; |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
$value->Set($lbl); |
718
|
|
|
$th = $value->GetHeight($dblImg); |
719
|
|
|
$tw = $value->GetWidth($dblImg); |
720
|
|
|
$xt = round($lr * cos($a) + $x) - $dx * $tw; |
|
|
|
|
721
|
|
|
$yt = round($y - $lr * sin($a)) - $dy * $th; |
|
|
|
|
722
|
|
|
|
723
|
|
|
$value->Stroke($dblImg, $xt, $yt); |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
if (__DEBUG) { |
727
|
|
|
$dblImg->SetColor('red'); |
728
|
|
|
$dblImg->Circle($xc, $yc, $lr + $r); |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
// Stroke all the legs |
732
|
|
|
reset($this->iData); |
733
|
|
|
$i = 0; |
734
|
|
|
foreach ($this->iData as $dir => $legdata) { |
735
|
|
|
$legdata = array_slice($legdata, 1); |
736
|
|
|
$nn = count($legdata); |
737
|
|
|
|
738
|
|
|
$a = $txtpos[$i][2]; |
739
|
|
|
$rri = $ri / $scaling; |
740
|
|
View Code Duplication |
for ($j = 0; $j < $nn; ++$j) { |
|
|
|
|
741
|
|
|
// We want the non scaled original radius |
742
|
|
|
$legr = $this->scale->RelTranslate($legdata[$j], $r / $scaling, $ri / $scaling); |
743
|
|
|
$this->_StrokeWindLeg($dblImg, $xc, $yc, $a, |
744
|
|
|
$rri * $scaling, |
745
|
|
|
$legr * $scaling, |
746
|
|
|
$this->iLegweights[$j % $nlw] * $scaling, |
747
|
|
|
$this->iLegColors[$j % $nlc]); |
748
|
|
|
$rri += $legr; |
749
|
|
|
} |
750
|
|
|
++$i; |
751
|
|
|
} |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
// Translate potential string specified compass labels to their |
755
|
|
|
// corresponding index. |
756
|
|
|
public function FixupIndexes($aDataArray, $num) |
757
|
|
|
{ |
758
|
|
|
$ret = []; |
759
|
|
|
$keys = array_keys($aDataArray); |
760
|
|
|
foreach ($aDataArray as $idx => $data) { |
761
|
|
|
if (is_string($idx)) { |
762
|
|
|
$idx = strtoupper($idx); |
763
|
|
|
$res = array_search($idx, $this->iAllDirectionLabels); |
764
|
|
|
if ($res === false) { |
765
|
|
|
Util\JpGraphError::RaiseL(22011, $idx); //('Windrose index must be numeric or direction label. You have specified index='.$idx); |
|
|
|
|
766
|
|
|
} |
767
|
|
|
$idx = $res; |
768
|
|
|
if ($idx % (16 / $num) !== 0) { |
769
|
|
|
Util\JpGraphError::RaiseL(22012); //('Windrose radial axis specification contains a direction which is not enabled.'); |
770
|
|
|
} |
771
|
|
|
$idx /= (16 / $num); |
772
|
|
|
|
773
|
|
|
if (in_array($idx, $keys, 1)) { |
774
|
|
|
Util\JpGraphError::RaiseL(22013, $idx); //('You have specified the look&feel for the same compass direction twice, once with text and once with index (Index='.$idx.')'); |
|
|
|
|
775
|
|
|
} |
776
|
|
|
} |
777
|
|
|
if ($idx < 0 || $idx > 15) { |
778
|
|
|
Util\JpGraphError::RaiseL(22014); //('Index for copmass direction must be between 0 and 15.'); |
779
|
|
|
} |
780
|
|
|
$ret[$idx] = $data; |
781
|
|
|
} |
782
|
|
|
return $ret; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
public function _StrokeRegularRose($dblImg, $value, $scaling, $xc, $yc, $r, $ri) |
786
|
|
|
{ |
787
|
|
|
// _StrokeRegularRose($dblImg,$xc,$yc,$r,$ri) |
|
|
|
|
788
|
|
|
// Plot radial grid lines and remember the end position |
789
|
|
|
// and the angle for later use when plotting the labels |
790
|
|
|
switch ($this->iType) { |
791
|
|
|
case WINDROSE_TYPE4: |
792
|
|
|
$num = 4; |
793
|
|
|
break; |
794
|
|
|
case WINDROSE_TYPE8: |
795
|
|
|
$num = 8; |
796
|
|
|
break; |
797
|
|
|
case WINDROSE_TYPE16: |
798
|
|
|
$num = 16; |
799
|
|
|
break; |
800
|
|
|
default: |
801
|
|
|
Util\JpGraphError::RaiseL(22015); //('You have specified an undefined Windrose plot type.'); |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
// Check if we should auto-position the angle for the |
805
|
|
|
// labels. Basically we try to find a firection with smallest |
806
|
|
|
// (or none) data. |
807
|
|
|
$this->SetAutoScaleAngle(true); |
808
|
|
|
|
809
|
|
|
$nlc = count($this->iLegColors); |
810
|
|
|
$nlw = count($this->iLegweights); |
811
|
|
|
|
812
|
|
|
$this->iRadialColorArray = $this->FixupIndexes($this->iRadialColorArray, $num); |
|
|
|
|
813
|
|
|
$this->iRadialWeightArray = $this->FixupIndexes($this->iRadialWeightArray, $num); |
814
|
|
|
$this->iRadialStyleArray = $this->FixupIndexes($this->iRadialStyleArray, $num); |
815
|
|
|
|
816
|
|
|
$txtpos = []; |
817
|
|
|
$a = 2 * M_PI / $num; |
818
|
|
|
$dblImg->SetColor($this->iGridColor2); |
819
|
|
|
$dblImg->SetLineStyle($this->iRadialGridStyle); |
820
|
|
|
$dblImg->SetLineWeight($this->iRadialGridWeight); |
821
|
|
|
|
822
|
|
|
// Translate any name specified directions to the index |
823
|
|
|
// so we can easily use it in the loop below |
824
|
|
|
for ($i = 0; $i < $num; ++$i) { |
825
|
|
|
$xxc = round($xc + cos($a * $i) * $ri); |
826
|
|
|
$yyc = round($yc - sin($a * $i) * $ri); |
827
|
|
|
$x = round($xc + cos($a * $i) * $r); |
828
|
|
|
$y = round($yc - sin($a * $i) * $r); |
829
|
|
View Code Duplication |
if (empty($this->iRadialColorArray[$i])) { |
|
|
|
|
830
|
|
|
$dblImg->SetColor($this->iGridColor2); |
831
|
|
|
} else { |
832
|
|
|
$dblImg->SetColor($this->iRadialColorArray[$i]); |
833
|
|
|
} |
834
|
|
View Code Duplication |
if (empty($this->iRadialWeightArray[$i])) { |
|
|
|
|
835
|
|
|
$dblImg->SetLineWeight($this->iRadialGridWeight); |
836
|
|
|
} else { |
837
|
|
|
$dblImg->SetLineWeight($this->iRadialWeightArray[$i]); |
838
|
|
|
} |
839
|
|
View Code Duplication |
if (empty($this->iRadialStyleArray[$i])) { |
|
|
|
|
840
|
|
|
$dblImg->SetLineStyle($this->iRadialGridStyle); |
841
|
|
|
} else { |
842
|
|
|
$dblImg->SetLineStyle($this->iRadialStyleArray[$i]); |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
$dblImg->StyleLine($xxc, $yyc, $x, $y); |
846
|
|
|
$txtpos[] = [$x, $y, $a * $i]; |
847
|
|
|
} |
848
|
|
|
$dblImg->SetLineWeight(1); |
849
|
|
|
|
850
|
|
|
$lr = $scaling * $this->iLabelMargin; |
851
|
|
View Code Duplication |
if ($this->iLabelPositioning == LBLPOSITION_CENTER) { |
|
|
|
|
852
|
|
|
$value->SetAlign('center', 'center'); |
853
|
|
|
} else { |
854
|
|
|
$value->SetAlign('left', 'top'); |
855
|
|
|
$value->SetMargin(0); |
856
|
|
|
$lr /= 2; |
857
|
|
|
} |
858
|
|
|
|
859
|
|
|
for ($i = 0; $i < $num; ++$i) { |
860
|
|
|
list($x, $y, $a) = $txtpos[$i]; |
861
|
|
|
|
862
|
|
|
// Set the position of the label |
863
|
|
View Code Duplication |
if ($this->iLabelPositioning == LBLPOSITION_CENTER) { |
|
|
|
|
864
|
|
|
$dx = $dy = 0; |
865
|
|
|
} else { |
866
|
|
|
// LBLPOSIITON_EDGE |
867
|
|
|
if ($a >= 7 * M_PI / 4 || $a <= M_PI / 4) { |
868
|
|
|
$dx = 0; |
869
|
|
|
} |
870
|
|
|
|
871
|
|
|
if ($a >= M_PI / 4 && $a <= 3 * M_PI / 4) { |
872
|
|
|
$dx = ($a - M_PI / 4) * 2 / M_PI; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
if ($a >= 3 * M_PI / 4 && $a <= 5 * M_PI / 4) { |
876
|
|
|
$dx = 1; |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
if ($a >= 5 * M_PI / 4 && $a <= 7 * M_PI / 4) { |
880
|
|
|
$dx = (1 - ($a - M_PI * 5 / 4) * 2 / M_PI); |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
if ($a >= 7 * M_PI / 4) { |
884
|
|
|
$dy = (($a - M_PI) - 3 * M_PI / 4) * 2 / M_PI; |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
if ($a <= M_PI / 4) { |
888
|
|
|
$dy = (0.5 + $a * 2 / M_PI); |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
if ($a >= M_PI / 4 && $a <= 3 * M_PI / 4) { |
892
|
|
|
$dy = 1; |
893
|
|
|
} |
894
|
|
|
|
895
|
|
|
if ($a >= 3 * M_PI / 4 && $a <= 5 * M_PI / 4) { |
896
|
|
|
$dy = (1 - ($a - 3 * M_PI / 4) * 2 / M_PI); |
897
|
|
|
} |
898
|
|
|
|
899
|
|
|
if ($a >= 5 * M_PI / 4 && $a <= 7 * M_PI / 4) { |
900
|
|
|
$dy = 0; |
901
|
|
|
} |
902
|
|
|
} |
903
|
|
|
|
904
|
|
|
$value->Set($this->iAllDirectionLabels[$i * (16 / $num)]); |
905
|
|
|
$th = $value->GetHeight($dblImg); |
906
|
|
|
$tw = $value->GetWidth($dblImg); |
907
|
|
|
$xt = round($lr * cos($a) + $x) - $dx * $tw; |
|
|
|
|
908
|
|
|
$yt = round($y - $lr * sin($a)) - $dy * $th; |
|
|
|
|
909
|
|
|
|
910
|
|
|
$value->Stroke($dblImg, $xt, $yt); |
911
|
|
|
} |
912
|
|
|
|
913
|
|
|
if (__DEBUG) { |
914
|
|
|
$dblImg->SetColor("red"); |
915
|
|
|
$dblImg->Circle($xc, $yc, $lr + $r); |
916
|
|
|
} |
917
|
|
|
|
918
|
|
|
// Stroke all the legs |
919
|
|
|
reset($this->iData); |
920
|
|
|
$keys = array_keys($this->iData); |
921
|
|
|
foreach ($this->iData as $idx => $legdata) { |
922
|
|
|
$legdata = array_slice($legdata, 1); |
923
|
|
|
$nn = count($legdata); |
924
|
|
|
if (is_string($idx)) { |
925
|
|
|
$idx = strtoupper($idx); |
926
|
|
|
$idx = array_search($idx, $this->iAllDirectionLabels); |
927
|
|
|
if ($idx === false) { |
928
|
|
|
Util\JpGraphError::RaiseL(22016); //('Windrose leg index must be numeric or direction label.'); |
929
|
|
|
} |
930
|
|
|
if ($idx % (16 / $num) !== 0) { |
931
|
|
|
Util\JpGraphError::RaiseL(22017); //('Windrose data contains a direction which is not enabled. Please adjust what labels are displayed.'); |
932
|
|
|
} |
933
|
|
|
$idx /= (16 / $num); |
934
|
|
|
|
935
|
|
|
if (in_array($idx, $keys, 1)) { |
936
|
|
|
Util\JpGraphError::RaiseL(22018, $idx); //('You have specified data for the same compass direction twice, once with text and once with index (Index='.$idx.')'); |
|
|
|
|
937
|
|
|
} |
938
|
|
|
} |
939
|
|
|
if ($idx < 0 || $idx > 15) { |
940
|
|
|
Util\JpGraphError::RaiseL(22019); //('Index for direction must be between 0 and 15. You can\'t specify angles for a Regular Windplot, only index and compass directions.'); |
941
|
|
|
} |
942
|
|
|
$a = $idx * (360 / $num); |
943
|
|
|
$a *= M_PI / 180.0; |
944
|
|
|
$rri = $ri / $scaling; |
945
|
|
View Code Duplication |
for ($j = 0; $j < $nn; ++$j) { |
|
|
|
|
946
|
|
|
// We want the non scaled original radius |
947
|
|
|
$legr = $this->scale->RelTranslate($legdata[$j], $r / $scaling, $ri / $scaling); |
948
|
|
|
$this->_StrokeWindLeg($dblImg, $xc, $yc, $a, |
949
|
|
|
$rri * $scaling, |
950
|
|
|
$legr * $scaling, |
951
|
|
|
$this->iLegweights[$j % $nlw] * $scaling, |
952
|
|
|
$this->iLegColors[$j % $nlc]); |
953
|
|
|
$rri += $legr; |
954
|
|
|
} |
955
|
|
|
} |
956
|
|
|
} |
957
|
|
|
|
958
|
|
|
public function getWidth($aImg) |
959
|
|
|
{ |
960
|
|
|
$scaling = 1; //$this->iAntiAlias ? 2 : 1 ; |
|
|
|
|
961
|
|
View Code Duplication |
if ($this->iSize > 0 && $this->iSize < 1) { |
|
|
|
|
962
|
|
|
$this->iSize *= min($aImg->width, $aImg->height); |
963
|
|
|
} |
964
|
|
|
|
965
|
|
|
$value = new Text\Text(); |
966
|
|
|
$value->SetFont($this->iFontFamily, $this->iFontStyle, $this->iFontSize * $scaling); |
967
|
|
|
$value->SetColor($this->iFontColor); |
968
|
|
|
// Setup extra size around the graph needed so that the labels |
969
|
|
|
// doesn't get cut. For this we need to find the largest label. |
970
|
|
|
// The code below gives a possible a little to large margin. The |
971
|
|
|
// really, really proper way would be to account for what angle |
972
|
|
|
// the label are at |
973
|
|
|
$n = count($this->iLabels); |
974
|
|
View Code Duplication |
if ($n > 0) { |
|
|
|
|
975
|
|
|
$maxh = 0; |
|
|
|
|
976
|
|
|
$maxw = 0; |
977
|
|
|
foreach ($this->iLabels as $key => $lbl) { |
978
|
|
|
$value->Set($lbl); |
979
|
|
|
$maxw = max($maxw, $value->GetWidth($aImg)); |
980
|
|
|
} |
981
|
|
|
} else { |
982
|
|
|
$value->Set('888.888'); // Dummy value to get width/height |
983
|
|
|
$maxw = $value->GetWidth($aImg); |
984
|
|
|
} |
985
|
|
|
// Add an extra margin of 50% the font size |
986
|
|
|
$maxw += round($this->iFontSize * $scaling * 0.4); |
987
|
|
|
|
988
|
|
|
$valxmarg = 1.5 * $maxw + 2 * $this->iLabelMargin * $scaling; |
989
|
|
|
$w = round($this->iSize * $scaling + $valxmarg); |
990
|
|
|
|
991
|
|
|
// Make sure that the width of the legend fits |
992
|
|
|
$legendwidth = $this->_StrokeLegend($aImg, 0, 0, $scaling, true) + 10 * $scaling; |
993
|
|
|
$w = max($w, $legendwidth); |
994
|
|
|
|
995
|
|
|
return $w; |
996
|
|
|
} |
997
|
|
|
|
998
|
|
|
public function getHeight($aImg) |
999
|
|
|
{ |
1000
|
|
|
$scaling = 1; //$this->iAntiAlias ? 2 : 1 ; |
|
|
|
|
1001
|
|
View Code Duplication |
if ($this->iSize > 0 && $this->iSize < 1) { |
|
|
|
|
1002
|
|
|
$this->iSize *= min($aImg->width, $aImg->height); |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
$value = new Text\Text(); |
1006
|
|
|
$value->SetFont($this->iFontFamily, $this->iFontStyle, $this->iFontSize * $scaling); |
1007
|
|
|
$value->SetColor($this->iFontColor); |
1008
|
|
|
// Setup extra size around the graph needed so that the labels |
1009
|
|
|
// doesn't get cut. For this we need to find the largest label. |
1010
|
|
|
// The code below gives a possible a little to large margin. The |
1011
|
|
|
// really, really proper way would be to account for what angle |
1012
|
|
|
// the label are at |
1013
|
|
|
$n = count($this->iLabels); |
1014
|
|
View Code Duplication |
if ($n > 0) { |
|
|
|
|
1015
|
|
|
$maxh = 0; |
1016
|
|
|
$maxw = 0; |
|
|
|
|
1017
|
|
|
foreach ($this->iLabels as $key => $lbl) { |
1018
|
|
|
$value->Set($lbl); |
1019
|
|
|
$maxh = max($maxh, $value->GetHeight($aImg)); |
1020
|
|
|
} |
1021
|
|
|
} else { |
1022
|
|
|
$value->Set('180.8'); // Dummy value to get width/height |
1023
|
|
|
$maxh = $value->GetHeight($aImg); |
1024
|
|
|
} |
1025
|
|
|
// Add an extra margin of 50% the font size |
1026
|
|
|
//$maxh += round($this->iFontSize*$scaling * 0.5) ; |
|
|
|
|
1027
|
|
|
$valymarg = 2 * $maxh + 2 * $this->iLabelMargin * $scaling; |
1028
|
|
|
|
1029
|
|
|
$legendheight = round($this->legend->iShow ? 1 : 0); |
1030
|
|
|
$legendheight *= max($this->legend->iCircleRadius * 2, $this->legend->iTxtFontSize * 2) + |
1031
|
|
|
$this->legend->iMargin + $this->legend->iBottomMargin + 2; |
1032
|
|
|
$legendheight *= $scaling; |
1033
|
|
|
$h = round($this->iSize * $scaling + $valymarg) + $legendheight; |
1034
|
|
|
|
1035
|
|
|
return $h; |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
|
|
public function Stroke($aGraph) |
1039
|
|
|
{ |
1040
|
|
|
$aImg = $aGraph->img; |
1041
|
|
|
|
1042
|
|
|
if ($this->iX > 0 && $this->iX < 1) { |
1043
|
|
|
$this->iX = round($aImg->width * $this->iX); |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
if ($this->iY > 0 && $this->iY < 1) { |
1047
|
|
|
$this->iY = round($aImg->height * $this->iY); |
1048
|
|
|
} |
1049
|
|
|
|
1050
|
|
View Code Duplication |
if ($this->iSize > 0 && $this->iSize < 1) { |
|
|
|
|
1051
|
|
|
$this->iSize *= min($aImg->width, $aImg->height); |
1052
|
|
|
} |
1053
|
|
|
|
1054
|
|
|
if ($this->iCenterSize > 0 && $this->iCenterSize < 1) { |
1055
|
|
|
$this->iCenterSize *= $this->iSize; |
1056
|
|
|
} |
1057
|
|
|
|
1058
|
|
|
$this->scale->AutoScale(($this->iSize - $this->iCenterSize) / 2, round(2.5 * $this->scale->iFontSize)); |
1059
|
|
|
|
1060
|
|
|
$scaling = $this->iAntiAlias ? 2 : 1; |
1061
|
|
|
|
1062
|
|
|
$value = new Text\Text(); |
1063
|
|
|
$value->SetFont($this->iFontFamily, $this->iFontStyle, $this->iFontSize * $scaling); |
1064
|
|
|
$value->SetColor($this->iFontColor); |
1065
|
|
|
|
1066
|
|
|
$legendheight = round($this->legend->iShow ? 1 : 0); |
1067
|
|
|
$legendheight *= max($this->legend->iCircleRadius * 2, $this->legend->iTxtFontSize * 2) + |
1068
|
|
|
$this->legend->iMargin + $this->legend->iBottomMargin + 2; |
1069
|
|
|
$legendheight *= $scaling; |
1070
|
|
|
|
1071
|
|
|
$w = $scaling * $this->getWidth($aImg); |
1072
|
|
|
$h = $scaling * $this->getHeight($aImg); |
1073
|
|
|
|
1074
|
|
|
// Copy back the double buffered image to the proper canvas |
1075
|
|
|
$ww = $w / $scaling; |
1076
|
|
|
$hh = $h / $scaling; |
1077
|
|
|
|
1078
|
|
|
// Create the double buffer |
1079
|
|
|
if ($this->iAntiAlias) { |
1080
|
|
|
$dblImg = new RotImage($w, $h); |
1081
|
|
|
// Set the background color |
1082
|
|
|
$dblImg->SetColor($this->iColor); |
1083
|
|
|
$dblImg->FilledRectangle(0, 0, $w, $h); |
1084
|
|
|
} else { |
1085
|
|
|
$dblImg = $aImg; |
1086
|
|
|
// Make sure the ix and it coordinates correpond to the new top left center |
1087
|
|
|
$dblImg->SetTranslation($this->iX - $w / 2, $this->iY - $h / 2); |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
|
|
if (__DEBUG) { |
1091
|
|
|
$dblImg->SetColor('red'); |
1092
|
|
|
$dblImg->Rectangle(0, 0, $w - 1, $h - 1); |
1093
|
|
|
} |
1094
|
|
|
|
1095
|
|
|
$dblImg->SetColor('black'); |
1096
|
|
|
|
1097
|
|
|
if ($this->iShowBox) { |
1098
|
|
|
$dblImg->SetColor($this->iBoxColor); |
1099
|
|
|
$old = $dblImg->SetLineWeight($this->iBoxWeight); |
1100
|
|
|
$dblImg->SetLineStyle($this->iBoxStyle); |
1101
|
|
|
$dblImg->Rectangle(0, 0, $w - 1, $h - 1); |
1102
|
|
|
$dblImg->SetLineWeight($old); |
1103
|
|
|
} |
1104
|
|
|
|
1105
|
|
|
$xc = round($w / 2); |
1106
|
|
|
$yc = round(($h - $legendheight) / 2); |
1107
|
|
|
|
1108
|
|
|
if (__DEBUG) { |
1109
|
|
|
$dblImg->SetColor('red'); |
1110
|
|
|
$old = $dblImg->SetLineWeight(2); |
1111
|
|
|
$dblImg->Line($xc - 5, $yc - 5, $xc + 5, $yc + 5); |
1112
|
|
|
$dblImg->Line($xc + 5, $yc - 5, $xc - 5, $yc + 5); |
1113
|
|
|
$dblImg->SetLineWeight($old); |
1114
|
|
|
} |
1115
|
|
|
|
1116
|
|
|
$this->iSize *= $scaling; |
1117
|
|
|
|
1118
|
|
|
// Inner circle size |
1119
|
|
|
$ri = $this->iCenterSize / 2; |
1120
|
|
|
|
1121
|
|
|
// Full circle radius |
1122
|
|
|
$r = round($this->iSize / 2); |
1123
|
|
|
|
1124
|
|
|
// Get number of grid circles |
1125
|
|
|
$n = $this->scale->GetNumCirc(); |
1126
|
|
|
|
1127
|
|
|
// Plot circle grids |
1128
|
|
|
$ri *= $scaling; |
1129
|
|
|
$rr = round(($r - $ri) / $n); |
1130
|
|
|
for ($i = 1; $i <= $n; ++$i) { |
1131
|
|
|
$this->_ThickCircle($dblImg, $xc, $yc, $rr * $i + $ri, |
1132
|
|
|
$this->iCircGridWeight, $this->iGridColor1); |
1133
|
|
|
} |
1134
|
|
|
|
1135
|
|
|
$num = 0; |
|
|
|
|
1136
|
|
|
|
1137
|
|
|
if ($this->iType == WINDROSE_TYPEFREE) { |
1138
|
|
|
$this->_StrokeFreeRose($dblImg, $value, $scaling, $xc, $yc, $r, $ri); |
1139
|
|
|
} else { |
1140
|
|
|
// Check if we need to re-code the interpretation of the ordinal |
1141
|
|
|
// number in the data. Internally ordinal value 0 is East and then |
1142
|
|
|
// counted anti-clockwise. The user might choose an encoding |
1143
|
|
|
// that have 0 being the first axis to the right of the "N" axis and then |
1144
|
|
|
// counted clock-wise |
1145
|
|
|
if ($this->iOrdinalEncoding == KEYENCODING_CLOCKWISE) { |
1146
|
|
|
if ($this->iType == WINDROSE_TYPE16) { |
1147
|
|
|
$const1 = 19; |
1148
|
|
|
$const2 = 16; |
1149
|
|
|
} elseif ($this->iType == WINDROSE_TYPE8) { |
1150
|
|
|
$const1 = 9; |
1151
|
|
|
$const2 = 8; |
1152
|
|
|
} else { |
1153
|
|
|
$const1 = 4; |
1154
|
|
|
$const2 = 4; |
1155
|
|
|
} |
1156
|
|
|
$tmp = []; |
1157
|
|
|
$n = count($this->iData); |
|
|
|
|
1158
|
|
|
foreach ($this->iData as $key => $val) { |
1159
|
|
|
if (is_numeric($key)) { |
1160
|
|
|
$key = ($const1 - $key) % $const2; |
1161
|
|
|
} |
1162
|
|
|
$tmp[$key] = $val; |
1163
|
|
|
} |
1164
|
|
|
$this->iData = $tmp; |
1165
|
|
|
} |
1166
|
|
|
$this->_StrokeRegularRose($dblImg, $value, $scaling, $xc, $yc, $r, $ri); |
1167
|
|
|
} |
1168
|
|
|
|
1169
|
|
|
// Stroke the labels |
1170
|
|
|
$this->scale->iFontSize *= $scaling; |
1171
|
|
|
$this->scale->iZFontSize *= $scaling; |
1172
|
|
|
$this->scale->StrokeLabels($dblImg, $xc, $yc, $ri, $rr); |
1173
|
|
|
|
1174
|
|
|
// Stroke the inner circle again since the legs |
1175
|
|
|
// might have written over it |
1176
|
|
|
$this->_ThickCircle($dblImg, $xc, $yc, $ri, $this->iCircGridWeight, $this->iGridColor1); |
1177
|
|
|
|
1178
|
|
|
if ($ww > $aImg->width) { |
1179
|
|
|
Util\JpGraphError::RaiseL(22020); |
1180
|
|
|
//('Windrose plot is too large to fit the specified Graph size. Please use WindrosePlot::SetSize() to make the plot smaller or increase the size of the Graph in the initial WindroseGraph() call.'); |
1181
|
|
|
} |
1182
|
|
|
|
1183
|
|
|
$x = $xc; |
1184
|
|
|
$y = $h; |
1185
|
|
|
$this->_StrokeLegend($dblImg, $x, $y, $scaling); |
1186
|
|
|
|
1187
|
|
|
if ($this->iAntiAlias) { |
1188
|
|
|
$aImg->Copy($dblImg->img, $this->iX - $ww / 2, $this->iY - $hh / 2, 0, 0, $ww, $hh, $w, $h); |
1189
|
|
|
} |
1190
|
|
|
|
1191
|
|
|
// We need to restore the translation matrix |
1192
|
|
|
$aImg->SetTranslation(0, 0); |
1193
|
|
|
} |
1194
|
|
|
} |
1195
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: