summaryrefslogtreecommitdiffstats
path: root/Under-Testing/Website/class/pIndicator.class.php
blob: 4f7f23bbfaf80c593095836e1ca1d4e880d37840 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
 /*
     pIndicator - class to draw indicators

     Version     : 2.1.2
     Made by     : Jean-Damien POGOLOTTI
     Last Update : 03/08/11

     This file can be distributed under the license you can find at :

                       http://www.pchart.net/license

     You can find the whole class documentation on the pChart web site.
 */

 define("INDICATOR_CAPTION_DEFAULT"	, 700001);
 define("INDICATOR_CAPTION_EXTENDED"	, 700002);

 define("INDICATOR_CAPTION_INSIDE"	, 700011);
 define("INDICATOR_CAPTION_BOTTOM"	, 700012);

 define("INDICATOR_VALUE_BUBBLE"	, 700021);
 define("INDICATOR_VALUE_LABEL"		, 700022);

 /* pIndicator class definition */
 class pIndicator
  {
   var $pChartObject;

   /* Class creator */
   function pIndicator($pChartObject)
    {
     $this->pChartObject = $pChartObject;
    }

   /* Draw an indicator */
   function draw($X,$Y,$Width,$Height,$Format="")
    {
     $Values			= isset($Format["Values"]) ? $Format["Values"] : VOID;
     $IndicatorSections		= isset($Format["IndicatorSections"]) ? $Format["IndicatorSections"] : NULL;
     $ValueDisplay		= isset($Format["ValueDisplay"]) ? $Format["ValueDisplay"] : INDICATOR_VALUE_BUBBLE;
     $SectionsMargin		= isset($Format["SectionsMargin"]) ? $Format["SectionsMargin"] : 4;
     $DrawLeftHead		= isset($Format["DrawLeftHead"]) ? $Format["DrawLeftHead"] : TRUE;
     $DrawRightHead		= isset($Format["DrawRightHead"]) ? $Format["DrawRightHead"] : TRUE;
     $HeadSize			= isset($Format["HeadSize"]) ? $Format["HeadSize"] : floor($Height/4);
     $TextPadding		= isset($Format["TextPadding"]) ? $Format["TextPadding"] : 4;
     $CaptionLayout		= isset($Format["CaptionLayout"]) ? $Format["CaptionLayout"] : INDICATOR_CAPTION_EXTENDED;
     $CaptionPosition		= isset($Format["CaptionPosition"]) ? $Format["CaptionPosition"] : INDICATOR_CAPTION_INSIDE;
     $CaptionColorFactor	= isset($Format["CaptionColorFactor"]) ? $Format["CaptionColorFactor"] : NULL;
     $CaptionR			= isset($Format["CaptionR"]) ? $Format["CaptionR"] : 255;
     $CaptionG			= isset($Format["CaptionG"]) ? $Format["CaptionG"] : 255;
     $CaptionB			= isset($Format["CaptionB"]) ? $Format["CaptionB"] : 255;
     $CaptionAlpha		= isset($Format["CaptionAlpha"]) ? $Format["CaptionAlpha"] : 100;
     $SubCaptionColorFactor	= isset($Format["SubCaptionColorFactor"]) ? $Format["SubCaptionColorFactor"] : NULL;
     $SubCaptionR		= isset($Format["SubCaptionR"]) ? $Format["SubCaptionR"] : 50;
     $SubCaptionG		= isset($Format["SubCaptionG"]) ? $Format["SubCaptionG"] : 50;
     $SubCaptionB		= isset($Format["SubCaptionB"]) ? $Format["SubCaptionB"] : 50;
     $SubCaptionAlpha		= isset($Format["SubCaptionAlpha"]) ? $Format["SubCaptionAlpha"] : 100;
     $ValueFontName		= isset($Format["ValueFontName"]) ? $Format["ValueFontName"] : $this->pChartObject->FontName;
     $ValueFontSize		= isset($Format["ValueFontSize"]) ? $Format["ValueFontSize"] : $this->pChartObject->FontSize;
     $CaptionFontName		= isset($Format["CaptionFontName"]) ? $Format["CaptionFontName"] : $this->pChartObject->FontName;
     $CaptionFontSize		= isset($Format["CaptionFontSize"]) ? $Format["CaptionFontSize"] : $this->pChartObject->FontSize;
     $Unit			= isset($Format["Unit"]) ? $Format["Unit"] : "";

     /* Convert the Values to display to an array if needed */
     if ( !is_array($Values) ) { $Value = $Values; $Values = ""; $Values[] = $Value; }

     /* No section, let's die */
     if ( $IndicatorSections == NULL ) { return(0); }

     /* Determine indicator visual configuration */
     $OverallMin = $IndicatorSections[0]["End"]; $OverallMax = $IndicatorSections[0]["Start"];
     foreach ($IndicatorSections as $Key => $Settings)
      {
       if ( $Settings["End"] > $OverallMax )   { $OverallMax = $Settings["End"]; }
       if ( $Settings["Start"] < $OverallMin ) { $OverallMin = $Settings["Start"]; }
      }
     $RealWidth = $Width - (count($IndicatorSections)-1)*$SectionsMargin;
     $XScale    = $RealWidth / ($OverallMax-$OverallMin);

     $X1 = $X; $ValuesPos = "";
     foreach ($IndicatorSections as $Key => $Settings)
      {
       $Color      = array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"]);
       $Caption    = $Settings["Caption"];
       $SubCaption = $Settings["Start"]." - ".$Settings["End"];

       $X2 = $X1 + ($Settings["End"] - $Settings["Start"]) * $XScale;

       if ( $Key == 0 && $DrawLeftHead )
        {
         $Poly = ""; $Poly[] = $X1-1; $Poly[] = $Y; $Poly[] = $X1-1; $Poly[] = $Y+$Height; $Poly[] = $X1-1-$HeadSize; $Poly[] = $Y+($Height/2);
         $this->pChartObject->drawPolygon($Poly,$Color);
         $this->pChartObject->drawLine($X1-2,$Y,$X1-2-$HeadSize,$Y+($Height/2),$Color);
         $this->pChartObject->drawLine($X1-2,$Y+$Height,$X1-2-$HeadSize,$Y+($Height/2),$Color);
        }

       /* Determine the position of the breaks */
       $Break = "";
       foreach($Values as $iKey => $Value)
        {
         if ( $Value >= $Settings["Start"] && $Value <= $Settings["End"] )
          {
           $XBreak  = $X1 + ($Value - $Settings["Start"]) * $XScale;
           $ValuesPos[$Value] = $XBreak;
           $Break[] = floor($XBreak);
          }
        }

       if ( $ValueDisplay == INDICATOR_VALUE_LABEL )
        {
         if ( $Break == "" )
          $this->pChartObject->drawFilledRectangle($X1,$Y,$X2,$Y+$Height,$Color);
         else
          {
           sort($Break);
           $Poly = ""; $Poly[] = $X1; $Poly[] = $Y; $LastPointWritten = FALSE;
           foreach($Break as $iKey => $Value)
            {
             if ( $Value-5 >= $X1 )
              { $Poly[] = $Value-5; $Poly[] = $Y; }
             elseif ($X1 - ($Value-5) > 0 )
              {
               $Offset = $X1 - ($Value-5);
               $Poly = ""; $Poly[] = $X1; $Poly[] = $Y + $Offset;
              }

             $Poly[] = $Value;   $Poly[] = $Y+5;

             if ( $Value+5 <= $X2 )
              { $Poly[] = $Value+5; $Poly[] = $Y; }
             elseif (($Value+5) > $X2 )
              {
               $Offset = ($Value+5) - $X2;
               $Poly[] = $X2; $Poly[] = $Y + $Offset;
               $LastPointWritten = TRUE;
              }

            }
           if ( !$LastPointWritten ) { $Poly[] = $X2; $Poly[] = $Y; }
           $Poly[] = $X2; $Poly[] = $Y+$Height;
           $Poly[] = $X1; $Poly[] = $Y+$Height;

           $this->pChartObject->drawPolygon($Poly,$Color);
          }
        }
       else
        $this->pChartObject->drawFilledRectangle($X1,$Y,$X2,$Y+$Height,$Color);

       if ( $Key == count($IndicatorSections)-1 && $DrawRightHead )
        {
         $Poly = ""; $Poly[] = $X2+1; $Poly[] = $Y; $Poly[] = $X2+1; $Poly[] = $Y+$Height; $Poly[] = $X2+1+$HeadSize; $Poly[] = $Y+($Height/2);
         $this->pChartObject->drawPolygon($Poly,$Color);
         $this->pChartObject->drawLine($X2+1,$Y,$X2+1+$HeadSize,$Y+($Height/2),$Color);
         $this->pChartObject->drawLine($X2+1,$Y+$Height,$X2+1+$HeadSize,$Y+($Height/2),$Color);
        }

       if ( $CaptionPosition == INDICATOR_CAPTION_INSIDE )
        {
         $TxtPos  = $this->pChartObject->getTextBox($X1,$Y+$Height+$TextPadding,$CaptionFontName,$CaptionFontSize,0,$Caption);
         $YOffset = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]) + $TextPadding;

         if ( $CaptionLayout == INDICATOR_CAPTION_EXTENDED )
          {
           $TxtPos  = $this->pChartObject->getTextBox($X1,$Y+$Height+$TextPadding,$CaptionFontName,$CaptionFontSize,0,$SubCaption);
           $YOffset = $YOffset + ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]) + $TextPadding*2;
          }

         $XOffset = $TextPadding;
        }
       else
        { $YOffset = 0; $XOffset = 0; }

       if ( $CaptionColorFactor == NULL )
        { $CaptionColor    = array("Align"=>TEXT_ALIGN_TOPLEFT,"FontName"=>$CaptionFontName,"FontSize"=>$CaptionFontSize,"R"=>$CaptionR,"G"=>$CaptionG,"B"=>$CaptionB,"Alpha"=>$CaptionAlpha); }
       else
        { $CaptionColor    = array("Align"=>TEXT_ALIGN_TOPLEFT,"FontName"=>$CaptionFontName,"FontSize"=>$CaptionFontSize,"R"=>$Settings["R"]+$CaptionColorFactor,"G"=>$Settings["G"]+$CaptionColorFactor,"B"=>$Settings["B"]+$CaptionColorFactor); }

       if ( $SubCaptionColorFactor == NULL )
        $SubCaptionColor = array("Align"=>TEXT_ALIGN_TOPLEFT,"FontName"=>$CaptionFontName,"FontSize"=>$CaptionFontSize,"R"=>$SubCaptionR,"G"=>$SubCaptionG,"B"=>$SubCaptionB,"Alpha"=>$SubCaptionAlpha);
       else
        $SubCaptionColor = array("Align"=>TEXT_ALIGN_TOPLEFT,"FontName"=>$CaptionFontName,"FontSize"=>$CaptionFontSize,"R"=>$Settings["R"]+$SubCaptionColorFactor,"G"=>$Settings["G"]+$SubCaptionColorFactor,"B"=>$Settings["B"]+$SubCaptionColorFactor);

       $RestoreShadow = $this->pChartObject->Shadow;
       $this->pChartObject->Shadow = FALSE;

       if ( $CaptionLayout == INDICATOR_CAPTION_DEFAULT )
        $this->pChartObject->drawText($X1,$Y+$Height+$TextPadding,$Caption,$CaptionColor);
       elseif ( $CaptionLayout == INDICATOR_CAPTION_EXTENDED )
        {
         $TxtPos        = $this->pChartObject->getTextBox($X1,$Y+$Height+$TextPadding,$CaptionFontName,$CaptionFontSize,0,$Caption);
         $CaptionHeight = $TxtPos[0]["Y"] - $TxtPos[2]["Y"];

         $this->pChartObject->drawText($X1+$XOffset,$Y+$Height-$YOffset+$TextPadding,$Caption,$CaptionColor);
         $this->pChartObject->drawText($X1+$XOffset,$Y+$Height-$YOffset+$CaptionHeight+$TextPadding*2,$SubCaption,$SubCaptionColor);
        }

       $this->pChartObject->Shadow = $RestoreShadow;

       $X1 = $X2 + $SectionsMargin;
      }

     $RestoreShadow = $this->pChartObject->Shadow;
     $this->pChartObject->Shadow = FALSE;

     foreach($Values as $Key => $Value)
      {
       if ( $Value >= $OverallMin && $Value <= $OverallMax )
        {
         foreach ($IndicatorSections as $Key => $Settings)
          {
           if ( $Value >= $Settings["Start"] && $Value <= $Settings["End"] )
            {
             $X1 = $ValuesPos[$Value]; //$X + $Key*$SectionsMargin + ($Value - $OverallMin) * $XScale;

             if ( $ValueDisplay == INDICATOR_VALUE_BUBBLE )
              {
               $TxtPos = $this->pChartObject->getTextBox($X1,$Y,$ValueFontName,$ValueFontSize,0,$Value.$Unit);
               $Radius = floor(($TxtPos[1]["X"] - $TxtPos[0]["X"] + $TextPadding*4)/2);

               $this->pChartObject->drawFilledCircle($X1,$Y,$Radius+4,array("R"=>$Settings["R"]+20,"G"=>$Settings["G"]+20,"B"=>$Settings["B"]+20));
               $this->pChartObject->drawFilledCircle($X1,$Y,$Radius,array("R"=>255,"G"=>255,"B"=>255));

               $TextSettings = array("Align"=>TEXT_ALIGN_MIDDLEMIDDLE,"FontName"=>$ValueFontName,"FontSize"=>$ValueFontSize);
               $this->pChartObject->drawText($X1-1,$Y-1,$Value.$Unit,$TextSettings);
              }
             elseif( $ValueDisplay == INDICATOR_VALUE_LABEL )
              {
               $Caption = "";
               $Caption[] = array("Format"=>array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"],"Alpha"=>100),"Caption"=>$Value.$Unit);
               $this->pChartObject->drawLabelBox(floor($X1),floor($Y)+2,"Value - ".$Settings["Caption"],$Caption);
              }
            }
           $X1 = $X2 + $SectionsMargin;
          }
        }
      }
     $this->pChartObject->Shadow = $RestoreShadow;
    }
  }
?>