1 |
efrain |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
pIndicator - class to draw indicators
|
|
|
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("INDICATOR_CAPTION_DEFAULT", 700001);
|
|
|
17 |
define("INDICATOR_CAPTION_EXTENDED", 700002);
|
|
|
18 |
define("INDICATOR_CAPTION_INSIDE", 700011);
|
|
|
19 |
define("INDICATOR_CAPTION_BOTTOM", 700012);
|
|
|
20 |
define("INDICATOR_VALUE_BUBBLE", 700021);
|
|
|
21 |
define("INDICATOR_VALUE_LABEL", 700022);
|
|
|
22 |
|
|
|
23 |
/* pIndicator class definition */
|
|
|
24 |
class pIndicator
|
|
|
25 |
{
|
|
|
26 |
var $pChartObject;
|
|
|
27 |
/* Class creator */
|
|
|
28 |
function __construct($pChartObject)
|
|
|
29 |
{
|
|
|
30 |
$this->pChartObject = $pChartObject;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/* Draw an indicator */
|
|
|
34 |
function draw($X, $Y, $Width, $Height, array $Format = [])
|
|
|
35 |
{
|
|
|
36 |
|
|
|
37 |
/* No section, let's die */
|
|
|
38 |
if (isset($Format["IndicatorSections"])){
|
|
|
39 |
$IndicatorSections = $Format["IndicatorSections"];
|
|
|
40 |
} else {
|
|
|
41 |
return 0;
|
|
|
42 |
}
|
|
|
43 |
$Values = VOID;
|
|
|
44 |
$ValueDisplay = INDICATOR_VALUE_BUBBLE;
|
|
|
45 |
$SectionsMargin = 4;
|
|
|
46 |
$DrawLeftHead = TRUE;
|
|
|
47 |
$DrawRightHead = TRUE;
|
|
|
48 |
$HeadSize = floor($Height / 4);
|
|
|
49 |
$TextPadding = 4;
|
|
|
50 |
$CaptionLayout = INDICATOR_CAPTION_EXTENDED;
|
|
|
51 |
$CaptionPosition = INDICATOR_CAPTION_INSIDE;
|
|
|
52 |
$CaptionColorFactor = NULL;
|
|
|
53 |
$CaptionR = 255;
|
|
|
54 |
$CaptionG = 255;
|
|
|
55 |
$CaptionB = 255;
|
|
|
56 |
$CaptionAlpha = 100;
|
|
|
57 |
$SubCaptionColorFactor = NULL;
|
|
|
58 |
$SubCaptionR = 50;
|
|
|
59 |
$SubCaptionG = 50;
|
|
|
60 |
$SubCaptionB = 50;
|
|
|
61 |
$SubCaptionAlpha = 100;
|
|
|
62 |
$ValueFontName = $this->pChartObject->FontName;
|
|
|
63 |
$ValueFontSize = $this->pChartObject->FontSize;
|
|
|
64 |
$CaptionFontName = $this->pChartObject->FontName;
|
|
|
65 |
$CaptionFontSize = $this->pChartObject->FontSize;
|
|
|
66 |
$Unit = "";
|
|
|
67 |
|
|
|
68 |
/* Override defaults */
|
|
|
69 |
extract($Format);
|
|
|
70 |
|
|
|
71 |
/* Convert the Values to display to an array if needed */
|
|
|
72 |
(!is_array($Values)) AND $Values = [$Values];
|
|
|
73 |
|
|
|
74 |
/* Determine indicator visual configuration */
|
|
|
75 |
$OverallMin = $IndicatorSections[0]["End"];
|
|
|
76 |
$OverallMax = $IndicatorSections[0]["Start"];
|
|
|
77 |
foreach($IndicatorSections as $Key => $Settings) {
|
|
|
78 |
($Settings["End"] > $OverallMax) AND $OverallMax = $Settings["End"];
|
|
|
79 |
($Settings["Start"] < $OverallMin) AND $OverallMin = $Settings["Start"];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$RealWidth = $Width - (count($IndicatorSections) - 1) * $SectionsMargin;
|
|
|
83 |
$XScale = $RealWidth / ($OverallMax - $OverallMin);
|
|
|
84 |
$X1 = $X;
|
|
|
85 |
$ValuesPos = [];
|
|
|
86 |
foreach($IndicatorSections as $Key => $Settings) {
|
|
|
87 |
$Color = ["R" => $Settings["R"],"G" => $Settings["G"],"B" => $Settings["B"]];
|
|
|
88 |
$Caption = $Settings["Caption"];
|
|
|
89 |
$SubCaption = $Settings["Start"] . " - " . $Settings["End"];
|
|
|
90 |
$X2 = $X1 + ($Settings["End"] - $Settings["Start"]) * $XScale;
|
|
|
91 |
if ($Key == 0 && $DrawLeftHead) {
|
|
|
92 |
$Poly = [$X1 - 1, $Y, $X1 - 1, $Y + $Height, $X1 - 1 - $HeadSize, $Y + ($Height / 2) ];
|
|
|
93 |
$this->pChartObject->drawPolygon($Poly, $Color);
|
|
|
94 |
$this->pChartObject->drawLine($X1 - 2, $Y, $X1 - 2 - $HeadSize, $Y + ($Height / 2) , $Color);
|
|
|
95 |
$this->pChartObject->drawLine($X1 - 2, $Y + $Height, $X1 - 2 - $HeadSize, $Y + ($Height / 2) , $Color);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/* Determine the position of the breaks */
|
|
|
99 |
$Break = [];
|
|
|
100 |
foreach($Values as $iKey => $Value) {
|
|
|
101 |
if ($Value >= $Settings["Start"] && $Value <= $Settings["End"]) {
|
|
|
102 |
$XBreak = $X1 + ($Value - $Settings["Start"]) * $XScale;
|
|
|
103 |
$ValuesPos[$Value] = $XBreak;
|
|
|
104 |
$Break[] = floor($XBreak);
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
if ($ValueDisplay == INDICATOR_VALUE_LABEL) {
|
|
|
109 |
if (count($Break) == 0){
|
|
|
110 |
$this->pChartObject->drawFilledRectangle($X1, $Y, $X2, $Y + $Height, $Color);
|
|
|
111 |
} else {
|
|
|
112 |
sort($Break);
|
|
|
113 |
$Poly = [$X1, $Y];
|
|
|
114 |
$LastPointWritten = FALSE;
|
|
|
115 |
foreach($Break as $iKey => $Value) {
|
|
|
116 |
|
|
|
117 |
if ($Value - 5 >= $X1) {
|
|
|
118 |
$Poly[] = $Value - 5;
|
|
|
119 |
$Poly[] = $Y;
|
|
|
120 |
} elseif ($X1 - ($Value - 5) > 0) {
|
|
|
121 |
$Offset = $X1 - ($Value - 5);
|
|
|
122 |
$Poly = [$X1, $Y + $Offset];
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
$Poly[] = $Value;
|
|
|
126 |
$Poly[] = $Y + 5;
|
|
|
127 |
|
|
|
128 |
if ($Value + 5 <= $X2) {
|
|
|
129 |
$Poly[] = $Value + 5;
|
|
|
130 |
$Poly[] = $Y;
|
|
|
131 |
} elseif (($Value + 5) > $X2) {
|
|
|
132 |
$Offset = ($Value + 5) - $X2;
|
|
|
133 |
$Poly[] = $X2;
|
|
|
134 |
$Poly[] = $Y + $Offset;
|
|
|
135 |
$LastPointWritten = TRUE;
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
if (!$LastPointWritten) {
|
|
|
140 |
$Poly[] = $X2;
|
|
|
141 |
$Poly[] = $Y;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
$Poly[] = $X2;
|
|
|
145 |
$Poly[] = $Y + $Height;
|
|
|
146 |
$Poly[] = $X1;
|
|
|
147 |
$Poly[] = $Y + $Height;
|
|
|
148 |
$this->pChartObject->drawPolygon($Poly, $Color);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
else {
|
|
|
152 |
$this->pChartObject->drawFilledRectangle($X1, $Y, $X2, $Y + $Height, $Color);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if ($Key == count($IndicatorSections) - 1 && $DrawRightHead) {
|
|
|
156 |
$Poly = [$X2 + 1, $Y, $X2 + 1, $Y + $Height, $X2 + 1 + $HeadSize, $Y + ($Height / 2) ];
|
|
|
157 |
$this->pChartObject->drawPolygon($Poly, $Color);
|
|
|
158 |
$this->pChartObject->drawLine($X2 + 1, $Y, $X2 + 1 + $HeadSize, $Y + ($Height / 2) , $Color);
|
|
|
159 |
$this->pChartObject->drawLine($X2 + 1, $Y + $Height, $X2 + 1 + $HeadSize, $Y + ($Height / 2) , $Color);
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
if ($CaptionPosition == INDICATOR_CAPTION_INSIDE) {
|
|
|
163 |
$TxtPos = $this->pChartObject->getTextBox($X1, $Y + $Height + $TextPadding, $CaptionFontName, $CaptionFontSize, 0, $Caption);
|
|
|
164 |
$YOffset = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]) + $TextPadding;
|
|
|
165 |
if ($CaptionLayout == INDICATOR_CAPTION_EXTENDED) {
|
|
|
166 |
$TxtPos = $this->pChartObject->getTextBox($X1, $Y + $Height + $TextPadding, $CaptionFontName, $CaptionFontSize, 0, $SubCaption);
|
|
|
167 |
$YOffset = $YOffset + ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]) + $TextPadding * 2;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
$XOffset = $TextPadding;
|
|
|
171 |
} else {
|
|
|
172 |
$YOffset = 0;
|
|
|
173 |
$XOffset = 0;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if ($CaptionColorFactor == NULL) {
|
|
|
177 |
$CaptionColor = ["Align" => TEXT_ALIGN_TOPLEFT,"FontName" => $CaptionFontName,"FontSize" => $CaptionFontSize,"R" => $CaptionR,"G" => $CaptionG,"B" => $CaptionB,"Alpha" => $CaptionAlpha];
|
|
|
178 |
} else {
|
|
|
179 |
$CaptionColor = ["Align" => TEXT_ALIGN_TOPLEFT,"FontName" => $CaptionFontName,"FontSize" => $CaptionFontSize,"R" => $Settings["R"] + $CaptionColorFactor,"G" => $Settings["G"] + $CaptionColorFactor,"B" => $Settings["B"] + $CaptionColorFactor];
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
if ($SubCaptionColorFactor == NULL) {
|
|
|
183 |
$SubCaptionColor = ["Align" => TEXT_ALIGN_TOPLEFT,"FontName" => $CaptionFontName,"FontSize" => $CaptionFontSize,"R" => $SubCaptionR,"G" => $SubCaptionG,"B" => $SubCaptionB,"Alpha" => $SubCaptionAlpha];
|
|
|
184 |
} else {
|
|
|
185 |
$SubCaptionColor = ["Align" => TEXT_ALIGN_TOPLEFT,"FontName" => $CaptionFontName,"FontSize" => $CaptionFontSize,"R" => $Settings["R"] + $SubCaptionColorFactor,"G" => $Settings["G"] + $SubCaptionColorFactor,"B" => $Settings["B"] + $SubCaptionColorFactor];
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
$RestoreShadow = $this->pChartObject->Shadow;
|
|
|
189 |
$this->pChartObject->Shadow = FALSE;
|
|
|
190 |
if ($CaptionLayout == INDICATOR_CAPTION_DEFAULT) {
|
|
|
191 |
$this->pChartObject->drawText($X1, $Y + $Height + $TextPadding, $Caption, $CaptionColor);
|
|
|
192 |
} elseif ($CaptionLayout == INDICATOR_CAPTION_EXTENDED) {
|
|
|
193 |
$TxtPos = $this->pChartObject->getTextBox($X1, $Y + $Height + $TextPadding, $CaptionFontName, $CaptionFontSize, 0, $Caption);
|
|
|
194 |
$CaptionHeight = $TxtPos[0]["Y"] - $TxtPos[2]["Y"];
|
|
|
195 |
$this->pChartObject->drawText($X1 + $XOffset, $Y + $Height - $YOffset + $TextPadding, $Caption, $CaptionColor);
|
|
|
196 |
$this->pChartObject->drawText($X1 + $XOffset, $Y + $Height - $YOffset + $CaptionHeight + $TextPadding * 2, $SubCaption, $SubCaptionColor);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
$this->pChartObject->Shadow = $RestoreShadow;
|
|
|
200 |
$X1 = $X2 + $SectionsMargin;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
$RestoreShadow = $this->pChartObject->Shadow;
|
|
|
204 |
$this->pChartObject->Shadow = FALSE;
|
|
|
205 |
foreach($Values as $Key => $Value) {
|
|
|
206 |
if ($Value >= $OverallMin && $Value <= $OverallMax) {
|
|
|
207 |
foreach($IndicatorSections as $Key => $Settings) {
|
|
|
208 |
if ($Value >= $Settings["Start"] && $Value <= $Settings["End"]) {
|
|
|
209 |
$X1 = $ValuesPos[$Value]; //$X + $Key*$SectionsMargin + ($Value - $OverallMin) * $XScale;
|
|
|
210 |
if ($ValueDisplay == INDICATOR_VALUE_BUBBLE) {
|
|
|
211 |
$TxtPos = $this->pChartObject->getTextBox($X1, $Y, $ValueFontName, $ValueFontSize, 0, $Value . $Unit);
|
|
|
212 |
$Radius = floor(($TxtPos[1]["X"] - $TxtPos[0]["X"] + $TextPadding * 4) / 2);
|
|
|
213 |
$this->pChartObject->drawFilledCircle($X1, $Y, $Radius + 4, ["R" => $Settings["R"] + 20,"G" => $Settings["G"] + 20,"B" => $Settings["B"] + 20]);
|
|
|
214 |
$this->pChartObject->drawFilledCircle($X1, $Y, $Radius, ["R" => 255,"G" => 255,"B" => 255]);
|
|
|
215 |
$this->pChartObject->drawText($X1 - 1, $Y - 1, $Value . $Unit, ["Align" => TEXT_ALIGN_MIDDLEMIDDLE,"FontName" => $ValueFontName,"FontSize" => $ValueFontSize]);
|
|
|
216 |
} elseif ($ValueDisplay == INDICATOR_VALUE_LABEL) {
|
|
|
217 |
$Caption = array(
|
|
|
218 |
"Format" => ["R" => $Settings["R"],"G" => $Settings["G"],"B" => $Settings["B"],"Alpha" => 100],
|
|
|
219 |
"Caption" => $Value . $Unit
|
|
|
220 |
);
|
|
|
221 |
$this->pChartObject->drawLabelBox(floor($X1) , floor($Y) + 2, "Value - " . $Settings["Caption"], $Caption);
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
$X1 = $X2 + $SectionsMargin;
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
$this->pChartObject->Shadow = $RestoreShadow;
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
?>
|