fix value/sum
[contractdashboard.git] / lib / jpgraph / jpgraph_mgraph.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/*=======================================================================
 // File:        JPGRAPH_MGRAPH.PHP
 // Description: Class to handle multiple graphs in the same image
 // Created:     2006-01-15
 // Ver:         $Id: jpgraph_mgraph.php 1770 2009-08-17 06:10:22Z ljp $
 //
 // Copyright (c) Aditus Consulting. All rights reserved.
 //========================================================================
 */
 
//=============================================================================
// CLASS MGraph
// Description: Create a container image that can hold several graph
//=============================================================================
class MGraph {
 
    public $title = null, $subtitle = null, $subsubtitle = null;
 
    protected $img=NULL;
    protected $iCnt=0,$iGraphs = array(); // image_handle, x, y, fx, fy, sizex, sizey
    protected $iFillColor='white', $iCurrentColor=0;
    protected $lm=4,$rm=4,$tm=4,$bm=4;
    protected $iDoFrame = FALSE, $iFrameColor = 'black', $iFrameWeight = 1;
    protected $iLineWeight = 1;
    protected $expired=false;
    protected $cache=null,$cache_name = '',$inline=true;
    protected $image_format='png',$image_quality=75;
    protected $iWidth=NULL,$iHeight=NULL;
    protected $background_image='',$background_image_center=true,
    $backround_image_format='',$background_image_mix=100,
    $background_image_y=NULL, $background_image_x=NULL;
    private $doshadow=false, $shadow_width=4, $shadow_color='gray@0.5';
    public $footer;
 
 
    // Create a new instane of the combined graph
    function __construct($aWidth=NULL,$aHeight=NULL,$aCachedName='',$aTimeOut=0,$aInline=true) {
        $this->iWidth = $aWidth;
        $this->iHeight = $aHeight;
 
        // If the cached version exist just read it directly from the
        // cache, stream it back to browser and exit
        if( $aCachedName!='' && READ_CACHE && $aInline ) {
            $this->cache = new ImgStreamCache();
            $this->cache->SetTimeOut($aTimeOut);
            $image = new Image();
            if( $this->cache->GetAndStream($image,$aCachedName) ) {
                exit();
            }
        }
        $this->inline = $aInline;
        $this->cache_name = $aCachedName;
 
        $this->title = new Text();
        $this->title->ParagraphAlign('center');
        $this->title->SetFont(FF_FONT2,FS_BOLD);
        $this->title->SetMargin(3);
        $this->title->SetAlign('center');
 
        $this->subtitle = new Text();
        $this->subtitle->ParagraphAlign('center');
        $this->subtitle->SetFont(FF_FONT1,FS_BOLD);
        $this->subtitle->SetMargin(3);
        $this->subtitle->SetAlign('center');
 
        $this->subsubtitle = new Text();
        $this->subsubtitle->ParagraphAlign('center');
        $this->subsubtitle->SetFont(FF_FONT1,FS_NORMAL);
        $this->subsubtitle->SetMargin(3);
        $this->subsubtitle->SetAlign('center');
 
        $this->footer = new Footer();
 
    }
 
    // Specify background fill color for the combined graph
    function SetFillColor($aColor) {
        $this->iFillColor = $aColor;
    }
 
    // Add a frame around the combined graph
    function SetFrame($aFlg,$aColor='black',$aWeight=1) {
        $this->iDoFrame = $aFlg;
        $this->iFrameColor = $aColor;
        $this->iFrameWeight = $aWeight;
    }
 
    // Specify a background image blend
    function SetBackgroundImageMix($aMix) {
        $this->background_image_mix = $aMix ;
    }
 
    // Specify a background image
    function SetBackgroundImage($aFileName,$aCenter_aX=NULL,$aY=NULL) {
        // Second argument can be either a boolean value or
        // a numeric
        $aCenter=TRUE;
        $aX=NULL;
 
        if( is_numeric($aCenter_aX) ) {
            $aX=$aCenter_aX;
        }
 
        // Get extension to determine image type
        $e = explode('.',$aFileName);
        if( !$e ) {
            JpGraphError::RaiseL(12002,$aFileName);
            //('Incorrect file name for MGraph::SetBackgroundImage() : '.$aFileName.' Must have a valid image extension (jpg,gif,png) when using autodetection of image type');
        }
 
        $valid_formats = array('png', 'jpg', 'gif');
        $aImgFormat = strtolower($e[count($e)-1]);
        if ($aImgFormat == 'jpeg')  {
            $aImgFormat = 'jpg';
        }
        elseif (!in_array($aImgFormat, $valid_formats) )  {
            JpGraphError::RaiseL(12003,$aImgFormat,$aFileName);
            //('Unknown file extension ($aImgFormat) in MGraph::SetBackgroundImage() for filename: '.$aFileName);
        }
 
        $this->background_image = $aFileName;
        $this->background_image_center=$aCenter;
        $this->background_image_format=$aImgFormat;
        $this->background_image_x = $aX;
        $this->background_image_y = $aY;
    }
 
    function _strokeBackgroundImage() {
        if( $this->background_image == '' ) return;
 
        $bkgimg = Graph::LoadBkgImage('',$this->background_image);
 
        // Background width & Heoght
        $bw = imagesx($bkgimg);
        $bh = imagesy($bkgimg);
 
        // Canvas width and height
        $cw = imagesx($this->img);
        $ch = imagesy($this->img);
 
        if( $this->doshadow ) {
            $cw -= $this->shadow_width;
            $ch -= $this->shadow_width;
        }
 
        if( $this->background_image_x === NULL || $this->background_image_y === NULL ) {
            if( $this->background_image_center ) {
                // Center original image in the plot area
                $x = round($cw/2-$bw/2); $y = round($ch/2-$bh/2);
            }
            else {
                // Just copy the image from left corner, no resizing
                $x=0; $y=0;
            }
        }
        else {
            $x = $this->background_image_x;
            $y = $this->background_image_y;
        }
        imagecopymerge($this->img,$bkgimg,$x,$y,0,0,$bw,$bh,$this->background_image_mix);
    }
 
    function AddMix($aGraph,$x=0,$y=0,$mix=100,$fx=0,$fy=0,$w=0,$h=0) {
        $this->_gdImgHandle($aGraph->Stroke( _IMG_HANDLER),$x,$y,$fx=0,$fy=0,$w,$h,$mix);
    }
 
    function Add($aGraph,$x=0,$y=0,$fx=0,$fy=0,$w=0,$h=0) {
        $this->_gdImgHandle($aGraph->Stroke( _IMG_HANDLER),$x,$y,$fx=0,$fy=0,$w,$h);
    }
 
    function _gdImgHandle($agdCanvas,$x,$y,$fx=0,$fy=0,$w=0,$h=0,$mix=100) {
        if( $w == 0 ) {
            $w = @imagesx($agdCanvas);
        }
        if( $w === NULL ) {
            JpGraphError::RaiseL(12007);
            //('Argument to MGraph::Add() is not a valid GD image handle.');
            return;
        }
        if( $h == 0 ) {
            $h = @imagesy($agdCanvas);
        }
        $this->iGraphs[$this->iCnt++] = array($agdCanvas,$x,$y,$fx,$fy,$w,$h,$mix);
    }
 
    function SetMargin($lm,$rm,$tm,$bm) {
        $this->lm = $lm;
        $this->rm = $rm;
        $this->tm = $tm;
        $this->bm = $bm;
    }
 
    function SetExpired($aFlg=true) {
        $this->expired = $aFlg;
    }
 
    function SetImgFormat($aFormat,$aQuality=75) {
        $this->image_format = $aFormat;
        $this->image_quality = $aQuality;
    }
 
    // Set the shadow around the whole image
    function SetShadow($aShowShadow=true,$aShadowWidth=4,$aShadowColor='gray@0.3') {
        $this->doshadow = $aShowShadow;
        $this->shadow_color = $aShadowColor;
        $this->shadow_width = $aShadowWidth;
        $this->footer->iBottomMargin += $aShadowWidth;
        $this->footer->iRightMargin += $aShadowWidth;
    }
 
    function StrokeTitle($image,$w,$h) {
        // Stroke title
        if( $this->title->t !== '' ) {
 
            $margin = 3;
 
            $y = $this->title->margin;
            if( $this->title->halign == 'center' ) {
                $this->title->Center(0,$w,$y);
            }
            elseif( $this->title->halign == 'left' ) {
                $this->title->SetPos($this->title->margin+2,$y);
            }
            elseif( $this->title->halign == 'right' ) {
                $indent = 0;
                if( $this->doshadow ) {
                    $indent = $this->shadow_width+2;
                }
                $this->title->SetPos($w-$this->title->margin-$indent,$y,'right');
            }
            $this->title->Stroke($image);
 
            // ... and subtitle
            $y += $this->title->GetTextHeight($image) + $margin + $this->subtitle->margin;
            if( $this->subtitle->halign == 'center' ) {
                $this->subtitle->Center(0,$w,$y);
            }
            elseif( $this->subtitle->halign == 'left' ) {
                $this->subtitle->SetPos($this->subtitle->margin+2,$y);
            }
            elseif( $this->subtitle->halign == 'right' ) {
                $indent = 0;
                if( $this->doshadow ) {
                    $indent = $this->shadow_width+2;
                }
                $this->subtitle->SetPos($this->img->width-$this->subtitle->margin-$indent,$y,'right');
            }
            $this->subtitle->Stroke($image);
 
            // ... and subsubtitle
            $y += $this->subtitle->GetTextHeight($image) + $margin + $this->subsubtitle->margin;
            if( $this->subsubtitle->halign == 'center' ) {
                $this->subsubtitle->Center(0,$w,$y);
            }
            elseif( $this->subsubtitle->halign == 'left' ) {
                $this->subsubtitle->SetPos($this->subsubtitle->margin+2,$y);
            }
            elseif( $this->subsubtitle->halign == 'right' ) {
                $indent = 0;
                if( $this->doshadow ) {
                    $indent = $thi