source: trunk/client/modules/Elezioni/grafici/jpgraph_legend.inc.php@ 113

Last change on this file since 113 was 2, checked in by root, 15 years ago

importo il progetto

File size: 11.5 KB
RevLine 
[2]1<?php
2//=======================================================================
3// File: JPGRAPH_LEGEND.INC.PHP
4// Description: Class to handle the legend box in the graph that gives
5// names on the data series. The number of rows and columns
6// in the legend are user specifyable.
7// Created: 2001-01-08 (Refactored to separate file 2008-08-01)
8// Ver: $Id: jpgraph_legend.inc.php 1048 2008-08-01 19:56:46Z ljp $
9//
10// Copyright (c) Aditus Consulting. All rights reserved.
11//========================================================================
12
13DEFINE('_DEFAULT_LPM_SIZE',8); // Default Legend Plot Mark size
14
15
16//===================================================
17// CLASS Legend
18// Description: Responsible for drawing the box containing
19// all the legend text for the graph
20//===================================================
21
22class Legend {
23 public $txtcol=array();
24 private $color=array(0,0,0); // Default fram color
25 private $fill_color=array(235,235,235); // Default fill color
26 private $shadow=true; // Shadow around legend "box"
27 private $shadow_color='darkgray@0.5';
28 private $mark_abs_hsize=_DEFAULT_LPM_SIZE,$mark_abs_vsize=_DEFAULT_LPM_SIZE;
29 private $xmargin=10,$ymargin=3,$shadow_width=2;
30 private $xlmargin=2, $ylmargin='';
31 private $xpos=0.05, $ypos=0.15, $xabspos=-1, $yabspos=-1;
32 private $halign="right", $valign="top";
33 private $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
34 private $font_color='black';
35 private $hide=false,$layout_n=1;
36 private $weight=1,$frameweight=1;
37 private $csimareas='';
38 private $reverse = false ;
39
40//---------------
41// CONSTRUCTOR
42 function Legend() {
43 // Empty
44 }
45//---------------
46// PUBLIC METHODS
47 function Hide($aHide=true) {
48 $this->hide=$aHide;
49 }
50
51 function SetHColMargin($aXMarg) {
52 $this->xmargin = $aXMarg;
53 }
54
55 function SetVColMargin($aSpacing) {
56 $this->ymargin = $aSpacing ;
57 }
58
59 function SetLeftMargin($aXMarg) {
60 $this->xlmargin = $aXMarg;
61 }
62
63 // Synonym
64 function SetLineSpacing($aSpacing) {
65 $this->ymargin = $aSpacing ;
66 }
67
68 function SetShadow($aShow='gray',$aWidth=2) {
69 if( is_string($aShow) ) {
70 $this->shadow_color = $aShow;
71 $this->shadow=true;
72 }
73 else
74 $this->shadow=$aShow;
75 $this->shadow_width=$aWidth;
76 }
77
78 function SetMarkAbsSize($aSize) {
79 $this->mark_abs_vsize = $aSize ;
80 $this->mark_abs_hsize = $aSize ;
81 }
82
83 function SetMarkAbsVSize($aSize) {
84 $this->mark_abs_vsize = $aSize ;
85 }
86
87 function SetMarkAbsHSize($aSize) {
88 $this->mark_abs_hsize = $aSize ;
89 }
90
91 function SetLineWeight($aWeight) {
92 $this->weight = $aWeight;
93 }
94
95 function SetFrameWeight($aWeight) {
96 $this->frameweight = $aWeight;
97 }
98
99 function SetLayout($aDirection=LEGEND_VERT) {
100 $this->layout_n = $aDirection==LEGEND_VERT ? 1 : 99 ;
101 }
102
103 function SetColumns($aCols) {
104 $this->layout_n = $aCols ;
105 }
106
107 function SetReverse($f=true) {
108 $this->reverse = $f ;
109 }
110
111 // Set color on frame around box
112 function SetColor($aFontColor,$aColor='black') {
113 $this->font_color=$aFontColor;
114 $this->color=$aColor;
115 }
116
117 function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
118 $this->font_family = $aFamily;
119 $this->font_style = $aStyle;
120 $this->font_size = $aSize;
121 }
122
123 function SetPos($aX,$aY,$aHAlign="right",$aVAlign="top") {
124 $this->Pos($aX,$aY,$aHAlign,$aVAlign);
125 }
126
127 function SetAbsPos($aX,$aY,$aHAlign="right",$aVAlign="top") {
128 $this->xabspos=$aX;
129 $this->yabspos=$aY;
130 $this->halign=$aHAlign;
131 $this->valign=$aVAlign;
132 }
133
134
135 function Pos($aX,$aY,$aHAlign="right",$aVAlign="top") {
136 if( !($aX<1 && $aY<1) )
137 JpGraphError::RaiseL(25120);//(" Position for legend must be given as percentage in range 0-1");
138 $this->xpos=$aX;
139 $this->ypos=$aY;
140 $this->halign=$aHAlign;
141 $this->valign=$aVAlign;
142 }
143
144 function SetFillColor($aColor) {
145 $this->fill_color=$aColor;
146 }
147
148 function Add($aTxt,$aColor,$aPlotmark='',$aLinestyle=0,$csimtarget='',$csimalt='',$csimwintarget='') {
149 $this->txtcol[]=array($aTxt,$aColor,$aPlotmark,$aLinestyle,$csimtarget,$csimalt,$csimwintarget);
150 }
151
152 function GetCSIMAreas() {
153 return $this->csimareas;
154 }
155
156 function Stroke(&$aImg) {
157 // Constant
158 $fillBoxFrameWeight=1;
159
160 if( $this->hide ) return;
161
162 $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
163
164 if( $this->reverse ) {
165 $this->txtcol = array_reverse($this->txtcol);
166 }
167
168 $n=count($this->txtcol);
169 if( $n == 0 ) return;
170
171 // Find out the max width and height of each column to be able
172 // to size the legend box.
173 $numcolumns = ($n > $this->layout_n ? $this->layout_n : $n);
174 for( $i=0; $i < $numcolumns; ++$i ) {
175 $colwidth[$i] = $aImg->GetTextWidth($this->txtcol[$i][0]) +
176 2*$this->xmargin + 2*$this->mark_abs_hsize;
177 $colheight[$i] = 0;
178 }
179
180 // Find our maximum height in each row
181 $rows = 0 ; $rowheight[0] = 0;
182 for( $i=0; $i < $n; ++$i ) {
183 $h = max($this->mark_abs_vsize,$aImg->GetTextHeight($this->txtcol[$i][0]))+$this->ymargin;
184 if( $i % $numcolumns == 0 ) {
185 $rows++;
186 $rowheight[$rows-1] = 0;
187 }
188 $rowheight[$rows-1] = max($rowheight[$rows-1],$h);
189 }
190
191 $abs_height = 0;
192 for( $i=0; $i < $rows; ++$i ) {
193 $abs_height += $rowheight[$i] ;
194 }
195
196 // Make sure that the height is at least as high as mark size + ymargin
197 $abs_height = max($abs_height,$this->mark_abs_vsize);
198
199 // We add 3 extra pixels height to compensate for the difficult in
200 // calculating font height
201 $abs_height += $this->ymargin+3;
202
203 // Find out the maximum width in each column
204 for( $i=$numcolumns; $i < $n; ++$i ) {
205 $colwidth[$i % $numcolumns] = max(
206 $aImg->GetTextWidth($this->txtcol[$i][0])+2*$this->xmargin+2*$this->mark_abs_hsize,$colwidth[$i % $numcolumns]);
207 }
208
209 // Get the total width
210 $mtw = 0;
211 for( $i=0; $i < $numcolumns; ++$i ) {
212 $mtw += $colwidth[$i] ;
213 }
214
215 // Find out maximum width we need for legend box
216 $abs_width = $mtw+$this->xlmargin;
217
218 if( $this->xabspos === -1 && $this->yabspos === -1 ) {
219 $this->xabspos = $this->xpos*$aImg->width ;
220 $this->yabspos = $this->ypos*$aImg->height ;
221 }
222
223 // Positioning of the legend box
224 if( $this->halign == 'left' )
225 $xp = $this->xabspos;
226 elseif( $this->halign == 'center' )
227 $xp = $this->xabspos - $abs_width/2;
228 else
229 $xp = $aImg->width - $this->xabspos - $abs_width;
230
231 $yp=$this->yabspos;
232 if( $this->valign == 'center' )
233 $yp-=$abs_height/2;
234 elseif( $this->valign == 'bottom' )
235 $yp-=$abs_height;
236
237 // Stroke legend box
238 $aImg->SetColor($this->color);
239 $aImg->SetLineWeight($this->frameweight);
240 $aImg->SetLineStyle('solid');
241
242 if( $this->shadow )
243 $aImg->ShadowRectangle($xp,$yp,$xp+$abs_width+$this->shadow_width,
244 $yp+$abs_height+$this->shadow_width,
245 $this->fill_color,$this->shadow_width,$this->shadow_color);
246 else {
247 $aImg->SetColor($this->fill_color);
248 $aImg->FilledRectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
249 $aImg->SetColor($this->color);
250 $aImg->Rectangle($xp,$yp,$xp+$abs_width,$yp+$abs_height);
251 }
252
253 // x1,y1 is the position for the legend mark
254 $x1=$xp+$this->mark_abs_hsize+$this->xlmargin;
255 $y1=$yp + $this->ymargin;
256
257 $f2 = round($aImg->GetTextHeight('X')/2);
258
259 $grad = new Gradient($aImg);
260 $patternFactory = null;
261
262 // Now stroke each legend in turn
263 // Each plot has added the following information to the legend
264 // p[0] = Legend text
265 // p[1] = Color,
266 // p[2] = For markers a reference to the PlotMark object
267 // p[3] = For lines the line style, for gradient the negative gradient style
268 // p[4] = CSIM target
269 // p[5] = CSIM Alt text
270 $i = 1 ; $row = 0;
271 foreach($this->txtcol as $p) {
272
273 // STROKE DEBUG BOX
274 if( _JPG_DEBUG ) {
275 $aImg->SetLineWeight(1);
276 $aImg->SetColor('red');
277 $aImg->SetLineStyle('solid');
278 $aImg->Rectangle($xp,$y1,$xp+$abs_width,$y1+$rowheight[$row]);
279 }
280
281 $aImg->SetLineWeight($this->weight);
282 $x1 = round($x1); $y1=round($y1);
283 if ( !empty($p[2]) && $p[2]->GetType() > -1 ) {
284 // Make a plot mark legend
285 $aImg->SetColor($p[1]);
286 if( is_string($p[3]) || $p[3]>0 ) {
287 $aImg->SetLineStyle($p[3]);
288 $aImg->StyleLine($x1-$this->mark_abs_hsize,$y1+$f2,$x1+$this->mark_abs_hsize,$y1+$f2);
289 }
290 // Stroke a mark with the standard size
291 // (As long as it is not an image mark )
292 if( $p[2]->GetType() != MARK_IMG ) {
293
294 // Clear any user callbacks since we ont want them called for
295 // the legend marks
296 $p[2]->iFormatCallback = '';
297 $p[2]->iFormatCallback2 = '';
298
299 // Since size for circles is specified as the radius
300 // this means that we must half the size to make the total
301 // width behave as the other marks
302 if( $p[2]->GetType() == MARK_FILLEDCIRCLE || $p[2]->GetType() == MARK_CIRCLE ) {
303 $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize)/2);
304 $p[2]->Stroke($aImg,$x1,$y1+$f2);
305 }
306 else {
307 $p[2]->SetSize(min($this->mark_abs_vsize,$this->mark_abs_hsize));
308 $p[2]->Stroke($aImg,$x1,$y1+$f2);
309 }
310 }
311 }
312 elseif ( !empty($p[2]) && (is_string($p[3]) || $p[3]>0 ) ) {
313 // Draw a styled line
314 $aImg->SetColor($p[1]);
315 $aImg->SetLineStyle($p[3]);
316 $aImg->StyleLine($x1-1,$y1+$f2,$x1+$this->mark_abs_hsize,$y1+$f2);
317 $aImg->StyleLine($x1-1,$y1+$f2+1,$x1+$this->mark_abs_hsize,$y1+$f2+1);
318 }
319 else {
320 // Draw a colored box
321 $color = $p[1] ;
322 // We make boxes slightly larger to better show
323 $boxsize = min($this->mark_abs_vsize,$this->mark_abs_hsize) + 2 ;
324 $ym = round($y1 + $f2 - $boxsize/2);
325 // We either need to plot a gradient or a
326 // pattern. To differentiate we use a kludge.
327 // Patterns have a p[3] value of < -100
328 if( $p[3] < -100 ) {
329 // p[1][0] == iPattern, p[1][1] == iPatternColor, p[1][2] == iPatternDensity
330 if( $patternFactory == null ) {
331 $patternFactory = new RectPatternFactory();
332 }
333 $prect = $patternFactory->Create($p[1][0],$p[1][1],1);
334 $prect->SetBackground($p[1][3]);
335 $prect->SetDensity($p[1][2]+1);
336 $prect->SetPos(new Rectangle($x1,$ym,$boxsize,$boxsize));
337 $prect->Stroke($aImg);
338 $prect=null;
339 }
340 else {
341 if( is_array($color) && count($color)==2 ) {
342 // The client want a gradient color
343 $grad->FilledRectangle($x1,$ym,
344 $x1+$boxsize,$ym+$boxsize,
345 $color[0],$color[1],-$p[3]);
346 }
347 else {
348 $aImg->SetColor($p[1]);
349 $aImg->FilledRectangle($x1,$ym,$x1+$boxsize,$ym+$boxsize);
350 }
351 $aImg->SetColor($this->color);
352 $aImg->SetLineWeight($fillBoxFrameWeight);
353 $aImg->Rectangle($x1,$ym,$x1+$boxsize,$ym+$boxsize);
354 }
355 }
356 $aImg->SetColor($this->font_color);
357 $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
358 $aImg->SetTextAlign("left","top");
359 $aImg->StrokeText(round($x1+$this->mark_abs_hsize+$this->xmargin),$y1,$p[0]);
360
361 // Add CSIM for Legend if defined
362 if( !empty($p[4]) ) {
363
364 $xe = $x1 + $this->xmargin+$this->mark_abs_hsize+$aImg->GetTextWidth($p[0]);
365 $ye = $y1 + max($this->mark_abs_vsize,$aImg->GetTextHeight($p[0]));
366 $coords = "$x1,$y1,$xe,$y1,$xe,$ye,$x1,$ye";
367 if( ! empty($p[4]) ) {
368 $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".htmlentities($p[4])."\"";
369
370 if( !empty($p[6]) ) {
371 $this->csimareas .= " target=\"".$p[6]."\"";
372 }
373
374 if( !empty($p[5]) ) {
375 $tmp=sprintf($p[5],$p[0]);
376 $this->csimareas .= " title=\"$tmp\" alt=\"$tmp\" ";
377 }
378 $this->csimareas .= " />\n";
379 }
380 }
381 if( $i >= $this->layout_n ) {
382 $x1 = $xp+$this->mark_abs_hsize+$this->xlmargin;
383 $y1 += $rowheight[$row++];
384 $i = 1;
385 }
386 else {
387 $x1 += $colwidth[($i-1) % $numcolumns] ;
388 ++$i;
389 }
390 }
391 }
392} // Class
393
394?>
Note: See TracBrowser for help on using the repository browser.