Start graph and import refactoring
[contractdashboard.git] / lib / jpgraph_legend.inc.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
//=======================================================================
// File:        JPGRAPH_LEGEND.INC.PHP
// Description: Class to handle the legend box in the graph that gives
//              names on the data series. The number of rows and columns
//              in the legend are user specifyable.
// Created:     2001-01-08 (Refactored to separate file 2008-08-01)
// Ver:         $Id: jpgraph_legend.inc.php 1911 2009-10-09 17:33:14Z ljp $
//
// Copyright (c) Aditus Consulting. All rights reserved.
//========================================================================
 
DEFINE('_DEFAULT_LPM_SIZE',8); // Default Legend Plot Mark size
 
 
//===================================================
// CLASS Legend
// Description: Responsible for drawing the box containing
// all the legend text for the graph
//===================================================
 
class Legend {
    public $txtcol=array();
    public $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
    private $color=array(0,0,0); // Default fram color
    private $fill_color=array(235,235,235); // Default fill color
    private $shadow=true; // Shadow around legend "box"
    private $shadow_color='darkgray';
    private $mark_abs_hsize=_DEFAULT_LPM_SIZE,$mark_abs_vsize=_DEFAULT_LPM_SIZE;
    private $xmargin=10,$ymargin=0,$shadow_width=2;
    private $xlmargin=4;
    private $ylinespacing=2;
    
     // We need a separate margin since the baseline of the last text would coincide with the bottom otherwise
    private $ybottom_margin = 8;
    
    private $xpos=0.05, $ypos=0.15, $xabspos=-1, $yabspos=-1;
    private $halign="right", $valign="top";
    private $font_color='black';
    private $hide=false,$layout_n=1;
    private $weight=1,$frameweight=1;
    private $csimareas='';
    private $reverse = false ;
    private $bkg_gradtype=-1, $bkg_gradfrom='lightgray', $bkg_gradto='gray';
 
    //---------------
    // CONSTRUCTOR
    function __construct() {
        // Empty
    }
    //---------------
    // PUBLIC METHODS
    function Hide($aHide=true) {
        $this->hide=$aHide;
    }
 
    function SetHColMargin($aXMarg) {
        $this->xmargin = $aXMarg;
    }
 
    function SetVColMargin($aSpacing) {
        $this->ylinespacing = $aSpacing ;
    }
 
    function SetLeftMargin($aXMarg) {
        $this->xlmargin = $aXMarg;
    }
 
    // Synonym
    function SetLineSpacing($aSpacing) {
        $this->ylinespacing = $aSpacing ;
    }
 
    function SetShadow($aShow='gray',$aWidth=4) {
        if( is_string($aShow) ) {
            $this->shadow_color = $aShow;
            $this->shadow=true;
        }
        else {
            $this->shadow = $aShow;
        }
        $this->shadow_width = $aWidth;
    }
 
    function SetMarkAbsSize($aSize) {
        $this->mark_abs_vsize = $aSize ;
        $this->mark_abs_hsize = $aSize ;
    }
 
    function SetMarkAbsVSize($aSize) {
        $this->mark_abs_vsize = $aSize ;
    }
 
    function SetMarkAbsHSize($aSize) {
        $this->mark_abs_hsize = $aSize ;
    }
 
    function SetLineWeight($aWeight) {
        $this->weight = $aWeight;
    }
 
    function SetFrameWeight($aWeight) {
        $this->frameweight = $aWeight;
    }
 
    function SetLayout($aDirection=LEGEND_VERT) {
        $this->layout_n = $aDirection==LEGEND_VERT ? 1 : 99 ;
    }
 
    function SetColumns($aCols) {
        $this->layout_n = $aCols ;
    }
 
    function SetReverse($f=true) {
        $this->reverse = $f ;
    }
 
    // Set color on frame around box
    function SetColor($aFontColor,$aColor='black') {
        $this->font_color=$aFontColor;
        $this->color=$aColor;
    }
 
    function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
        $this->font_family = $aFamily;
        $this->font_style = $aStyle;
        $this->font_size = $aSize;
    }
 
    function SetPos($aX,$aY,$aHAlign='right',$aVAlign='top') {
        $this->Pos($aX,$aY,$aHAlign,$aVAlign);
    }
 
    function SetAbsPos($aX,$aY,$aHAlign='right',$aVAlign='top') {
        $this->xabspos=$aX;
        $this->yabspos=$aY;
        $this->halign=$aHAlign;
        $this->valign=$aVAlign;
    }
 
    function Pos($aX,$aY,$aHAlign='right',$aVAlign='top') {
        if( !($aX<1 && $aY<1) ) {
            JpGraphError::RaiseL(25120);//(" Position for legend must be given as percentage in range 0-1");
        }
        $this->xpos=$aX;
        $this->ypos=$aY;
        $this->halign=$aHAlign;
        $this->valign=$aVAlign;
    }
 
    function SetFillColor($aColor) {
        $this->fill_color=$aColor;
    }
 
    function Clear() {
        $this->txtcol = array();
    }
 
    function Add($aTxt,$aColor,$aPlotmark='',$aLinestyle=0,$csimtarget='',$csimalt='',$csimwintarget='') {
        $this->txtcol[]=array($aTxt,$aColor,$aPlotmark,$aLinestyle,$csimtarget,$csimalt,$csimwintarget);
    }
 
    function GetCSIMAreas() {
        return $this->csimareas;
    }
 
    function SetBackgroundGradient($aFrom='navy',$aTo='silver',$aGradType=2) {
        $this->bkg_gradtype=$aGradType;
        $this->bkg_gradfrom = $aFrom;
        $this->bkg_gradto = $aTo;
    }
 
    function Stroke($aImg) {
        // Constant
        $fillBoxFrameWeight=1;
 
        if( $this->hide ) return;
 
        $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
 
        if( $this->reverse ) {
            $this->txtcol = array_reverse($this->txtcol);
        }
 
        $n=count($this->txtcol);
        if( $n == 0 ) return;
 
        // Find out the max width and height of each column to be able
        // to size the legend box.
        $numcolumns = ($n > $this->layout_n ? $this->layout_n : $n);
        for( $i=0; $i < $numcolumns; ++$i ) {
            $colwidth[$i] = $aImg->GetTextWidth($this->txtcol[$i][0]) +
                            2*$this->xmargin + 2*$this->mark_abs_hsize;
            $colheight[$i] = 0;
 
        }
 
        // Find our maximum height in each row
        $rows = 0 ; $rowheight[0] = 0;
        for( $i=0; $i < $n; ++$i ) {
            $h = max($this->mark_abs_vsize,
                     $aImg->GetTextHeight($this->txtcol[$i][0]))+$this->ylinespacing;
            // Makes sure we always have a minimum of 1/4 (1/2 on each side) of the mark as space
            // between two vertical legend entries
            //$h = round(max($h,$this->mark_abs_vsize+$this->ymargin));
            //echo "Textheight #$i: tetxheight=".$aImg->GetTextHeight($this->txtcol[$i][0]).', ';
            //echo "h=$h ({$this->mark_abs_vsize},{$this->ymargin})<br>";
            if( $i % $numcolumns == 0 ) {
                $rows++;
                $rowheight[$rows-1] = 0;
            }
            $rowheight[$rows-1] = max($rowheight[$rows-1],$h);
        }
 
        $abs_height = 0;
        for( $i=0; $i < $rows; ++$i ) {
            $abs_height += $rowheight[$i] ;
        }
 
        // Make sure that the height is at least as high as mark size + ymargin
        $abs_height = max($abs_height,$this->mark_abs_vsize);
        $abs_height += $this->ybottom_margin; 
 
        // Find out the maximum width in each column
        for( $i=$numcolumns; $i < $n; ++$i ) {
            $colwidth[$i % $numcolumns] = max(
            $aImg->GetTextWidth($this->txtcol[$i][0])+2*$this->xmargin+2*$this->mark_abs_hsize,
            $colwidth[$i % $numcolumns]);
        }
 
        // Get the total width
        $mtw = 0;
        for( $i=0; $i < $numcolumns; ++$i ) {
            $mtw += $colwidth[$i] ;
        }
 
        // Find out maximum width we need for legend box
        $abs_width = $mtw+$this->xlmargin+($numcolumns-1)*$this->mark_abs_hsize;
 
        if( $this->xabspos === -1  && $this->yabspos === -1 ) {
            $this->xabspos = $this->xpos*$aImg->width ;
            $this->yabspos = $this->ypos*$aImg->height ;
        }
 
        // Positioning of the legend box
        if( $this->halign == 'left' ) {
                $xp = $this->xabspos;
        }
        elseif( $this->halign == 'center' ) {
                $xp = $this->xabspos - $abs_width/2;
        }
        else {
                $xp = $aImg->width - $this->xabspos - $abs_width;
        }
 
        $yp=$this->yabspos;
        if( $this->valign == 'center' ) {
                $yp-=$abs_height/2;
        }
        elseif( $this->valign == 'bottom' ) {
                $yp-=$abs_height;
        }
 
        // Stroke legend box
        $aImg->SetColor($this->color);
        $aImg->SetLineWeight($this->frameweight);
        $aImg->SetLineStyle('solid');
 
        if( $this->shadow ) {
                $aImg->ShadowRectangle($xp,$yp,
                                   $xp+$abs_width+$this->shadow_width+2,
                                   $yp+$abs_height+$this->shadow_width+2,
                                   $this->fill_color,$this->shadow_width+2,$this->shadow_color);
        }
        else {
            $aImg->SetColor($this->fill_color);
            $aImg->FilledRectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
            $aImg->SetColor($this->color);
            $aImg->Rectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
        }
 
        if( $this->bkg_gradtype >= 0 ) {
            $grad = new Gradient($aImg);
            $grad->FilledRectangle($xp+1, $yp+1,
                                   $xp+$abs_width-3, $yp+$abs_height-3,
                                   $this->bkg_gradfrom, $this->bkg_gradto,
                                   $this->bkg_gradtype);
        }
 
        // x1,y1 is the position for the legend marker + text
        // The vertical position is the baseline position for the text
        // and every marker is adjusted acording to that.
        $x1 = $xp + $this->xlmargin;
        $y1 = $yp + $rowheight[0]; // The ymargin is included in rowheight
 
        $grad = new Gradient($aImg);
        $patternFactory = null;
 
        // Now stroke each legend in turn
        // Each plot has added the following information to  the legend
        // p[0] = Legend text
        // p[1] = Color,
        // p[2] = For markers a reference to the PlotMark object
        // p[3] = For lines the line style, for gradient the negative gradient style
        // p[4] = CSIM target
        // p[5] = CSIM Alt text
        $i = 1 ; $row = 0;
        foreach($this->txtcol as $p) {
 
            // STROKE DEBUG BOX
            if( _JPG_DEBUG ) {
        &nbs