ccbpgraph.class.php
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php // content="text/plain; charset=utf-8" 
/**
* Class CCBPGraph
* Utility class to create Critical Chain Buffer penetration charts
*/
class CCBPGraph {
    const TickStep = 25;
    const YTitle = '% Buffer used';
    const XTitle = '% CC Completed';
    const NColorMaps = 2;
    private $graph=null;
    private $iWidth,$iHeight;
    private $iPlots=array();
    private $iXMin=-50, $iXMax = 100;
    private $iYMin=-50, $iYMax = 150;
    private $iColorInd = array( 
        array(5,75),   /* Green */
        array(25,85),  /* Yellow */
        array(50,100));/* Red */ 
    private $iColorMap = 0;
    private $iColorSpec = array(
                array('darkgreen:1.0','yellow:1.4','red:0.8','darkred:0.85'),
                array('#c6e9af','#ffeeaa','#ffaaaa','#de8787'));
    private $iMarginColor = array('darkgreen@0.7','darkgreen@0.9');                
    private $iSubTitle='',$iTitle = 'CC Buffer penetration';
    /**
    * Construct a new instance of CCBPGraph 
    * 
    * @param int $aWidth
    * @param int $aHeight
    * @return CCBPGraph
    */
    public function __construct($aWidth, $aHeight) {
        $this->iWidth = $aWidth;
        $this->iHeight = $aHeight;
    }
    /**
    * Set the title and subtitle for the graph
    * 
    * @param string $aTitle
    * @param string $aSubTitle
    */
    public function SetTitle($aTitle, $aSubTitle) {
        $this->iTitle = $aTitle;
        $this->iSubTitle = $aSubTitle;
    }
    /**
    * Set the x-axis min and max values
    * 
    * @param int $aMin
    * @param int $aMax
    */
    public function SetXMinMax($aMin, $aMax) {
        $this->iXMin = floor($aMin/CCBPGraph::TickStep)*CCBPGraph::TickStep;
        $this->iXMax = ceil($aMax/CCBPGraph::TickStep)*CCBPGraph::TickStep;
    }
    /**
    * Specify what color map to use
    * 
    * @param int $aMap
    */
    public function SetColorMap($aMap) {
        $this->iColorMap = $aMap % CCBPGraph::NColorMaps;
    }
    /**
    * Set the y-axis min and max values
    * 
    * @param int $aMin
    * @param int $aMax
    */
    public function SetYMinMax($aMin,$aMax) {
        $this->iYMin = floor($aMin/CCBPGraph::TickStep)*CCBPGraph::TickStep;
        $this->iYMax = ceil($aMax/CCBPGraph::TickStep)*CCBPGraph::TickStep;
    }
    /**
    * Set the specification of the color backgrounds and also the
    * optional exact colors to be used
    * 
    * @param mixed $aSpec  An array of 3 1x2 arrays. Each array specify the 
    * color indication value at x=0 and x=max x in order to determine the slope
    * @param mixed $aColors  An array with four elements specifying the colors
    * of each color indicator
    */
    public function SetColorIndication(array $aSpec,array $aColors=null) {
        if( count($aSpec) !== 3 ) {
            JpgraphError::Raise('Specification of scale values for background indicators must be an array with three elements.');
        }
        $this->iColorInd = $aSpec;    
        if( $aColors !== null ) {
            if( is_array($aColors) && count($aColors) == 4 ) {
                $this->iColorSpec = $aColors;
            }
            else {
                JpGraphError::Raise('Color specification for background indication must have four colors.');
            }
        }        
    }
    /**
    * Construct the graph
    * 
    */
    private function Init() {
 
        // Setup limits for color indications
        $lowx = $this->iXMin; $highx= $this->iXMax;
        $lowy = $this->iYMin; $highy = $this->iYMax;
         $width=$this->iWidth; $height=$this->iHeight;
 
        // Margins
        $lm=50; $rm=40;
        $tm=60; $bm=40;
        
        if( $width <= 300 || $height <= 250 ) {
            $labelsize = 8;
            $lm=25; $rm=25;
            $tm=45; $bm=25;
        }                
        elseif( $width <= 450 || $height <= 300 ) {
            $labelsize = 8;
            $lm=30; $rm=30;
            $tm=50; $bm=30;
        }
        elseif( $width <= 600 || $height <= 400 ) {
            $labelsize = 9;        
        }
        else {
            $labelsize = 11;
        }    
        
        if( $this->iSubTitle == '' ) {
            $tm -= $labelsize+4;
        }     
 
        $graph = new Graph($width,$height);
        $graph->SetScale('intint',$lowy,$highy,$lowx,$highx);
        $graph->SetMargin($lm,$rm,$tm,$bm);
        $graph->SetMarginColor($this->iMarginColor[$this->iColorMap]);
        $graph->SetClipping();
        
        $graph->title->Set($this->iTitle);
        $graph->subtitle->Set($this->iSubTitle);
        
        $graph->title->SetFont(FF_ARIAL,FS_BOLD,$labelsize+4);
        $graph->subtitle->SetFont(FF_ARIAL,FS_BOLD,$labelsize+1);
        
        $graph->SetBox(true,'black@0.3');
 
        $graph->xaxis->SetFont(FF_ARIAL,FS_BOLD,$labelsize);
        $graph->yaxis->SetFont(FF_ARIAL,FS_BOLD,$labelsize);
        
        $graph->xaxis->scale->ticks->Set(CCBPGraph::TickStep,CCBPGraph::TickStep);
        $graph->yaxis->scale->ticks->Set(CCBPGraph::TickStep,CCBPGraph::TickStep);
        
        $graph->xaxis->HideZeroLabel();
        $graph->yaxis->HideZeroLabel();
                
        $graph->xaxis->SetLabelFormatString('%d%%');
        $graph->yaxis->SetLabelFormatString('%d%%');
        
        // For the x-axis we adjust the color so labels on the left of the Y-axis are in black
        $n1 = floor(abs($this->iXMin/25))+1;
        $n2 = floor($this->iXMax/25);
        if( $this->iColorMap == 0 ) {
            $xlcolors=array();
            for( $i = 0; $i < $n1; ++$i ) {
                $xlcolors[$i] = 'black';
            }
            for( $i = 0; $i < $n2; ++$i ) {
                $xlcolors[$n1+$i] = 'lightgray:1.5';
            }
            $graph->xaxis->SetColor('gray',$xlcolors);
            $graph->yaxis->SetColor('gray','lightgray:1.5');
        }
        else {
            $graph->xaxis->SetColor('darkgray','darkgray:0.8');
            $graph->yaxis->SetColor('darkgray','darkgray:0.8');            
        }
        $graph->SetGridDepth(DEPTH_FRONT);
        $graph->ygrid->SetColor('gray@0.6');
        $graph->ygrid->SetLineStyle('dotted');
        
        $graph->ygrid->Show();
        
        $graph->xaxis->SetWeight(1);
        $graph->yaxis->SetWeight(1);
        
        $ytitle = new Text(CCBPGraph::YTitle,floor($lm*.75),($height-$tm-$bm)/2+$tm);
        $ytitle->SetFont(FF_VERA,FS_BOLD,$labelsize+1);
        $ytitle->SetAlign('right','center');
        $ytitle->SetAngle(90);
        $graph->Add($ytitle);
        
        $xtitle = new Text(CCBPGraph::XTitle,($width-$lm-$rm)/2+$lm,$height - 10);
        $xtitle->SetFont(FF_VERA,FS_BOLD,$labelsize);
        $xtitle->SetAlign('center','bottom');
        $graph->Add($xtitle);
        
        $df = 'D j:S M, Y'; 
        if( $width < 400 ) {
            $df = 'D j:S M';
        }
        
        $time = new Text(date($df),$width-10,$height-10);
        $time->SetAlign('right','bottom');
        $time->SetFont(FF_VERA,FS_NORMAL,$labelsize-1);
        $time->SetColor('darkgray');
        $graph->Add($time);
 
        // Use an accumulated fille line graph to create the colored bands
       
        $n = 3;
        for( $i=0; $i < $n; ++$i ) {
            $b = $this->iColorInd[$i][0];
            $k = ($this->iColorInd[$i][1] - $this->iColorInd[$i][0])/$this->iXMax;
            $colarea[$i] = array( array($lowx,$lowx*$k+$b), array($highx,$highx*$k+$b) );
        }
        $colarea[3] = array( array($lowx,$highy), array($highx,$highy) );
        
        
        $cb = array();
        for( $i=0; $i < 4; ++$i ) {
            $cb[$i] = new LinePlot(array($colarea[$i][0][1],$colarea[$i][1][1]),
                                   array($colarea[$i][0][0],$colarea[$i][1][0]));
            $cb[$i]->SetFillColor($this->iColorSpec[$this->iColorMap][$i]);
            $cb[$i]->SetFillFromYMin();                                   
        }
 
        $graph->Add(array_slice(array_reverse($cb),0,4));    
        $this->graph = $graph;
    }
    /**
    * Add a line or scatter plot to the graph
    * 
    * @param mixed $aPlots
    */
    public function Add($aPlots) {
        if( is_array($aPlots) ) {
            $this->iPlots = array_merge($this->iPlots,$aPlots);                
        }
        else {
            $this->iPlots[] = $aPlots;
        }
    }
    /**
    * Stroke the graph back to the client or to a file
    * 
    * @param mixed $aFile
    */
    public function Stroke($aFile='') {
        $this->Init();
        if( count($this->iPlots) > 0 )  {
            $this->graph->Add($this->iPlots);
        }
        $this->graph->Stroke($aFile);
    }
}
?>