Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 aurelien 1
<?php
2
/*=======================================================================
3
// File:	JPGRAPH_BAR.PHP
4
// Description:	Bar plot extension for JpGraph
5
// Created: 	2001-01-08
6
// Ver:		$Id: jpgraph_bar.php 1013 2008-06-27 06:33:43Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
 
12
require_once('jpgraph_plotband.php');
13
 
14
// Pattern for Bars
15
DEFINE('PATTERN_DIAG1',1);
16
DEFINE('PATTERN_DIAG2',2);
17
DEFINE('PATTERN_DIAG3',3);
18
DEFINE('PATTERN_DIAG4',4);
19
DEFINE('PATTERN_CROSS1',5);
20
DEFINE('PATTERN_CROSS2',6);
21
DEFINE('PATTERN_CROSS3',7);
22
DEFINE('PATTERN_CROSS4',8);
23
DEFINE('PATTERN_STRIPE1',9);
24
DEFINE('PATTERN_STRIPE2',10);
25
 
26
//===================================================
27
// CLASS BarPlot
28
// Description: Main code to produce a bar plot
29
//===================================================
30
class BarPlot extends Plot {
31
    var $width=0.4; // in percent of major ticks
32
    var $abswidth=-1; // Width in absolute pixels
33
    var $fill=false,$fill_color="lightblue"; // Default is to fill with light blue
34
    var $ybase=0; // Bars start at 0
35
    var $align="center";
36
    var $grad=false,$grad_style=1;
37
    var $grad_fromcolor=array(50,50,200),$grad_tocolor=array(255,255,255);
38
    var $bar_shadow=false;
39
    var $bar_shadow_color="black";
40
    var $bar_shadow_hsize=3,$bar_shadow_vsize=3;
41
    var $valuepos='top';
42
    var $iPattern=-1,$iPatternDensity=80,$iPatternColor='black';
43
 
44
//---------------
45
// CONSTRUCTOR
46
    function BarPlot(&$datay,$datax=false) {
47
	$this->Plot($datay,$datax);
48
	++$this->numpoints;
49
    }
50
 
51
//---------------
52
// PUBLIC METHODS
53
 
54
    // Set a drop shadow for the bar (or rather an "up-right" shadow)
55
    function SetShadow($color="black",$hsize=3,$vsize=3,$show=true) {
56
	$this->bar_shadow=$show;
57
	$this->bar_shadow_color=$color;
58
	$this->bar_shadow_vsize=$vsize;
59
	$this->bar_shadow_hsize=$hsize;
60
 
61
	// Adjust the value margin to compensate for shadow
62
	$this->value->margin += $vsize;
63
    }
64
 
65
    // DEPRECATED use SetYBase instead
66
    function SetYMin($aYStartValue) {
67
	//die("JpGraph Error: Deprecated function SetYMin. Use SetYBase() instead.");
68
	$this->ybase=$aYStartValue;
69
    }
70
 
71
    // Specify the base value for the bars
72
    function SetYBase($aYStartValue) {
73
	$this->ybase=$aYStartValue;
74
    }
75
 
76
    function Legend(&$graph) {
77
	if( $this->grad && $this->legend!="" && !$this->fill ) {
78
	    $color=array($this->grad_fromcolor,$this->grad_tocolor);
79
	    // In order to differentiate between gradients and cooors specified as an RGB triple
80
	    $graph->legend->Add($this->legend,$color,"",-$this->grad_style,
81
				$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
82
	}
83
	elseif( $this->legend!="" && ($this->iPattern > -1 || is_array($this->iPattern)) ) {
84
	    if( is_array($this->iPattern) ) {
85
		$p1 = $this->iPattern[0];
86
		$p2 = $this->iPatternColor[0];
87
		$p3 = $this->iPatternDensity[0];
88
	    }
89
	    else {
90
		$p1 = $this->iPattern;
91
		$p2 = $this->iPatternColor;
92
		$p3 = $this->iPatternDensity;
93
	    }
94
	    $color = array($p1,$p2,$p3,$this->fill_color);
95
	    // A kludge: Too mark that we add a pattern we use a type value of < 100
96
	    $graph->legend->Add($this->legend,$color,"",-101,
97
				$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
98
	}
99
	elseif( $this->fill_color && $this->legend!="" ) {
100
	    if( is_array($this->fill_color) ) {
101
		$graph->legend->Add($this->legend,$this->fill_color[0],"",0,
102
				    $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
103
	    }
104
	    else {
105
		$graph->legend->Add($this->legend,$this->fill_color,"",0,
106
				    $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
107
	    }
108
	}
109
    }
110
 
111
    // Gets called before any axis are stroked
112
    function PreStrokeAdjust(&$graph) {
113
	parent::PreStrokeAdjust($graph);
114
 
115
	// If we are using a log Y-scale we want the base to be at the
116
	// minimum Y-value unless the user have specifically set some other
117
	// value than the default.
118
	if( substr($graph->axtype,-3,3)=="log" && $this->ybase==0 )
119
	    $this->ybase = $graph->yaxis->scale->GetMinVal();
120
 
121
	// For a "text" X-axis scale we will adjust the
122
	// display of the bars a little bit.
123
	if( substr($graph->axtype,0,3)=="tex" ) {
124
	    // Position the ticks between the bars
125
	    $graph->xaxis->scale->ticks->SetXLabelOffset(0.5,0);
126
 
127
	    // Center the bars
128
	    if( $this->abswidth > -1 ) {
129
		$graph->SetTextScaleAbsCenterOff($this->abswidth);
130
	    }
131
	    else {
132
		if( $this->align == "center" )
133
		    $graph->SetTextScaleOff(0.5-$this->width/2);
134
		elseif( $this->align == "right" )
135
		    $graph->SetTextScaleOff(1-$this->width);
136
	    }
137
 
138
	}
139
	/*
140
	elseif( is_a($this,'AccBarPlot') || is_a($this,'GroupBarPlot') ) {
141
	    // We only set an absolute width for linear and int scale
142
	    // for text scale the width will be set to a fraction of
143
	    // the majstep width.
144
	    if( $this->abswidth == -1 ) {
145
                // Not set
146
		// set width to a visuable sensible default
147
		$this->abswidth = $graph->img->plotwidth/(2*count($this->coords[0]));
148
	    }
149
	}
150
	*/
151
    }
152
 
153
    function Min() {
154
	$m = parent::Min();
155
	if( $m[1] >= $this->ybase )
156
	    $m[1] = $this->ybase;
157
	return $m;
158
    }
159
 
160
    function Max() {
161
	$m = parent::Max();
162
	if( $m[1] <= $this->ybase )
163
	    $m[1] = $this->ybase;
164
	return $m;
165
    }
166
 
167
    // Specify width as fractions of the major stepo size
168
    function SetWidth($aWidth) {
169
	if( $aWidth > 1 ) {
170
	    // Interpret this as absolute width
171
	    $this->abswidth=$aWidth;
172
	}
173
	else
174
	    $this->width=$aWidth;
175
    }
176
 
177
    // Specify width in absolute pixels. If specified this
178
    // overrides SetWidth()
179
    function SetAbsWidth($aWidth) {
180
	$this->abswidth=$aWidth;
181
    }
182
 
183
    function SetAlign($aAlign) {
184
	$this->align=$aAlign;
185
    }
186
 
187
    function SetNoFill() {
188
	$this->grad = false;
189
	$this->fill_color=false;
190
	$this->fill=false;
191
    }
192
 
193
    function SetFillColor($aColor) {
194
	$this->fill = true ;
195
	$this->fill_color = $aColor;
196
    }
197
 
198
    function SetFillGradient($aFromColor,$aToColor=null,$aStyle=null) {
199
	$this->grad = true;
200
	$this->grad_fromcolor = $aFromColor;
201
	$this->grad_tocolor   = $aToColor;
202
	$this->grad_style     = $aStyle;
203
    }
204
 
205
    function SetValuePos($aPos) {
206
	$this->valuepos = $aPos;
207
    }
208
 
209
    function SetPattern($aPattern, $aColor='black'){
210
	if( is_array($aPattern) ) {
211
	    $n = count($aPattern);
212
	    $this->iPattern = array();
213
	    $this->iPatternDensity = array();
214
	    if( is_array($aColor) ) {
215
		$this->iPatternColor = array();
216
		if( count($aColor) != $n ) {
217
		    JpGraphError::RaiseL(2001);//('NUmber of colors is not the same as the number of patterns in BarPlot::SetPattern()');
218
		}
219
	    }
220
	    else
221
		$this->iPatternColor = $aColor;
222
	    for( $i=0; $i < $n; ++$i ) {
223
		$this->_SetPatternHelper($aPattern[$i], $this->iPattern[$i], $this->iPatternDensity[$i]);
224
		if( is_array($aColor) ) {
225
		    $this->iPatternColor[$i] = $aColor[$i];
226
		}
227
	    }
228
	}
229
	else {
230
	    $this->_SetPatternHelper($aPattern, $this->iPattern, $this->iPatternDensity);
231
	    $this->iPatternColor = $aColor;
232
	}
233
    }
234
 
235
    function _SetPatternHelper($aPattern, &$aPatternValue, &$aDensity){
236
	switch( $aPattern ) {
237
	    case PATTERN_DIAG1:
238
		$aPatternValue= 1;
239
		$aDensity = 90;
240
		break;
241
	    case PATTERN_DIAG2:
242
		$aPatternValue= 1;
243
		$aDensity = 75;
244
		break;
245
	    case PATTERN_DIAG3:
246
		$aPatternValue= 2;
247
		$aDensity = 90;
248
		break;
249
	    case PATTERN_DIAG4:
250
		$aPatternValue= 2;
251
		$aDensity = 75;
252
		break;
253
	    case PATTERN_CROSS1:
254
		$aPatternValue= 8;
255
		$aDensity = 90;
256
		break;
257
	    case PATTERN_CROSS2:
258
		$aPatternValue= 8;
259
		$aDensity = 78;
260
		break;
261
	    case PATTERN_CROSS3:
262
		$aPatternValue= 8;
263
		$aDensity = 65;
264
		break;
265
	    case PATTERN_CROSS4:
266
		$aPatternValue= 7;
267
		$aDensity = 90;
268
		break;
269
	    case PATTERN_STRIPE1:
270
		$aPatternValue= 5;
271
		$aDensity = 95;
272
		break;
273
	    case PATTERN_STRIPE2:
274
		$aPatternValue= 5;
275
		$aDensity = 85;
276
		break;
277
	    default:
278
		JpGraphError::RaiseL(2002);//('Unknown pattern specified in call to BarPlot::SetPattern()');
279
	}
280
    }
281
 
282
    function Stroke(&$img,&$xscale,&$yscale) {
283
 
284
	$numpoints = count($this->coords[0]);
285
	if( isset($this->coords[1]) ) {
286
	    if( count($this->coords[1])!=$numpoints )
287
		JpGraphError::RaiseL(2003,count($this->coords[1]),$numpoints);
288
//("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])."Number of Y-points:$numpoints");
289
	    else
290
		$exist_x = true;
291
	}
292
	else
293
	    $exist_x = false;
294
 
295
 
296
	$numbars=count($this->coords[0]);
297
 
298
	// Use GetMinVal() instead of scale[0] directly since in the case
299
	// of log scale we get a correct value. Log scales will have negative
300
	// values for values < 1 while still not representing negative numbers.
301
	if( $yscale->GetMinVal() >= 0 )
302
	    $zp=$yscale->scale_abs[0];
303
	else {
304
	    $zp=$yscale->Translate(0);
305
	}
306
 
307
	if( $this->abswidth > -1 ) {
308
	    $abswidth=$this->abswidth;
309
	}
310
	else
311
	    $abswidth=round($this->width*$xscale->scale_factor,0);
312
 
313
	// Count potential pattern array to avoid doing the count for each iteration
314
	if( is_array($this->iPattern) ) {
315
	    $np = count($this->iPattern);
316
	}
317
 
318
	$grad = null;
319
	for($i=0; $i < $numbars; ++$i) {
320
 
321
 	    // If value is NULL, or 0 then don't draw a bar at all
322
 	    if ($this->coords[0][$i] === null || $this->coords[0][$i] === '' )
323
	        continue;
324
 
325
	    if( $exist_x ) $x=$this->coords[1][$i];
326
	    else $x=$i;
327
 
328
	    $x=$xscale->Translate($x);
329
 
330
// Comment Note: This confuses the positioning when using acc together with
331
// grouped bars. Workaround for fixing #191
332
/*
333
	    if( !$xscale->textscale ) {
334
	    	if($this->align=="center")
335
		    $x -= $abswidth/2;
336
		elseif($this->align=="right")
337
		    $x -= $abswidth;
338
	    }
339
 
340
*/
341
	    // Stroke fill color and fill gradient
342
	    $pts=array(
343
		$x,$zp,
344
		$x,$yscale->Translate($this->coords[0][$i]),
345
		$x+$abswidth,$yscale->Translate($this->coords[0][$i]),
346
		$x+$abswidth,$zp);
347
	    if( $this->grad ) {
348
		if( $grad === null )
349
		    $grad = new Gradient($img);
350
		if( is_array($this->grad_fromcolor) ) {
351
		    // The first argument (grad_fromcolor) can be either an array or a single color. If it is an array
352
		    // then we have two choices. It can either a) be a single color specified as an RGB triple or it can be
353
		    // an array to specify both (from, to style) for each individual bar. The way to know the difference is
354
		    // to investgate the first element. If this element is an integer [0,255] then we assume it is an RGB
355
		    // triple.
356
		    $ng = count($this->grad_fromcolor);
357
		    if( $ng === 3 ) {
358
			if( is_numeric($this->grad_fromcolor[0]) && $this->grad_fromcolor[0] > 0 && $this->grad_fromcolor[0] < 256 ) {
359
			    // RGB Triple
360
			    $fromcolor = $this->grad_fromcolor;
361
			    $tocolor = $this->grad_tocolor;
362
			    $style = $this->grad_style;
363
			}
364
		    }
365
		    else {
366
			$fromcolor = $this->grad_fromcolor[$i % $ng][0];
367
			$tocolor = $this->grad_fromcolor[$i % $ng][1];
368
			$style = $this->grad_fromcolor[$i % $ng][2];
369
		    }
370
		    $grad->FilledRectangle($pts[2],$pts[3],
371
					   $pts[6],$pts[7],
372
					   $fromcolor,$tocolor,$style);
373
		}
374
		else {
375
		    $grad->FilledRectangle($pts[2],$pts[3],
376
					   $pts[6],$pts[7],
377
					   $this->grad_fromcolor,$this->grad_tocolor,$this->grad_style);
378
		}
379
 
380
	    }
381
	    elseif( !empty($this->fill_color) ) {
382
		if(is_array($this->fill_color)) {
383
		    $img->PushColor($this->fill_color[$i % count($this->fill_color)]);
384
		} else {
385
		    $img->PushColor($this->fill_color);
386
		}
387
		$img->FilledPolygon($pts);
388
		$img->PopColor();
389
	    }
390
 
391
 
392
	    // Remember value of this bar
393
	    $val=$this->coords[0][$i];
394
 
395
	    if( !empty($val) && !is_numeric($val) ) {
396
		JpGraphError::RaiseL(2004,$i,$val);
397
//('All values for a barplot must be numeric. You have specified value['.$i.'] == \''.$val.'\'');
398
	    }
399
 
400
	    // Determine the shadow
401
	    if( $this->bar_shadow && $val != 0) {
402
 
403
		$ssh = $this->bar_shadow_hsize;
404
		$ssv = $this->bar_shadow_vsize;
405
		// Create points to create a "upper-right" shadow
406
		if( $val > 0 ) {
407
		    $sp[0]=$pts[6];		$sp[1]=$pts[7];
408
		    $sp[2]=$pts[4];		$sp[3]=$pts[5];
409
		    $sp[4]=$pts[2];		$sp[5]=$pts[3];
410
		    $sp[6]=$pts[2]+$ssh;	$sp[7]=$pts[3]-$ssv;
411
		    $sp[8]=$pts[4]+$ssh;	$sp[9]=$pts[5]-$ssv;
412
		    $sp[10]=$pts[6]+$ssh;	$sp[11]=$pts[7]-$ssv;
413
		}
414
		elseif( $val < 0 ) {
415
		    $sp[0]=$pts[4];		$sp[1]=$pts[5];
416
		    $sp[2]=$pts[6];		$sp[3]=$pts[7];
417
		    $sp[4]=$pts[0];		$sp[5]=$pts[1];
418
		    $sp[6]=$pts[0]+$ssh;	$sp[7]=$pts[1]-$ssv;
419
		    $sp[8]=$pts[6]+$ssh;	$sp[9]=$pts[7]-$ssv;
420
		    $sp[10]=$pts[4]+$ssh;	$sp[11]=$pts[5]-$ssv;
421
		}
422
		if( is_array($this->bar_shadow_color) ) {
423
		    $numcolors = count($this->bar_shadow_color);
424
		    if( $numcolors == 0 ) {
425
			JpGraphError::RaiseL(2005);//('You have specified an empty array for shadow colors in the bar plot.');
426
		    }
427
		    $img->PushColor($this->bar_shadow_color[$i % $numcolors]);
428
		}
429
		else {
430
		    $img->PushColor($this->bar_shadow_color);
431
		}
432
		$img->FilledPolygon($sp);
433
		$img->PopColor();
434
	    }
435
 
436
	    // Stroke the pattern
437
	    if( is_array($this->iPattern) ) {
438
		$f = new RectPatternFactory();
439
		if( is_array($this->iPatternColor) ) {
440
		    $pcolor = $this->iPatternColor[$i % $np];
441
		}
442
		else
443
		    $pcolor = $this->iPatternColor;
444
		$prect = $f->Create($this->iPattern[$i % $np],$pcolor,1);
445
		$prect->SetDensity($this->iPatternDensity[$i % $np]);
446
 
447
		if( $val < 0 ) {
448
		    $rx = $pts[0];
449
		    $ry = $pts[1];
450
		}
451
		else {
452
		    $rx = $pts[2];
453
		    $ry = $pts[3];
454
		}
455
		$width = abs($pts[4]-$pts[0])+1;
456
		$height = abs($pts[1]-$pts[3])+1;
457
		$prect->SetPos(new Rectangle($rx,$ry,$width,$height));
458
		$prect->Stroke($img);
459
	    }
460
	    else {
461
		if( $this->iPattern > -1 ) {
462
		    $f = new RectPatternFactory();
463
		    $prect = $f->Create($this->iPattern,$this->iPatternColor,1);
464
		    $prect->SetDensity($this->iPatternDensity);
465
		    if( $val < 0 ) {
466
			$rx = $pts[0];
467
			$ry = $pts[1];
468
		    }
469
		    else {
470
			$rx = $pts[2];
471
			$ry = $pts[3];
472
		    }
473
		    $width = abs($pts[4]-$pts[0])+1;
474
		    $height = abs($pts[1]-$pts[3])+1;
475
		    $prect->SetPos(new Rectangle($rx,$ry,$width,$height));
476
		    $prect->Stroke($img);
477
		}
478
	    }
479
	    // Stroke the outline of the bar
480
	    if( is_array($this->color) )
481
		$img->SetColor($this->color[$i % count($this->color)]);
482
	    else
483
		$img->SetColor($this->color);
484
 
485
	    $pts[] = $pts[0];
486
	    $pts[] = $pts[1];
487
 
488
	    if( $this->weight > 0 ) {
489
		$img->SetLineWeight($this->weight);
490
		$img->Polygon($pts);
491
	    }
492
 
493
	    // Determine how to best position the values of the individual bars
494
	    $x=$pts[2]+($pts[4]-$pts[2])/2;
495
	    $this->value->SetMargin(5);
496
 
497
	    if( $this->valuepos=='top' ) {
498
		$y=$pts[3];
499
		if( $img->a === 90 ) {
500
		    if( $val < 0 )
501
			$this->value->SetAlign('right','center');
502
		    else
503
			$this->value->SetAlign('left','center');
504
 
505
		}
506
		else {
507
		    if( $val < 0 ) {
508
			$this->value->SetMargin(-5);
509
			$y=$pts[1];
510
			$this->value->SetAlign('center','bottom');
511
		    }
512
		    else {
513
			$this->value->SetAlign('center','bottom');
514
		    }
515
 
516
		}
517
		$this->value->Stroke($img,$val,$x,$y);
518
	    }
519
	    elseif( $this->valuepos=='max' ) {
520
		$y=$pts[3];
521
		if( $img->a === 90 ) {
522
		    if( $val < 0 ) {
523
			$this->value->SetAlign('left','center');
524
		    }
525
		    else {
526
			$this->value->SetAlign('right','center');
527
		    }
528
		}
529
		else {
530
		    if( $val < 0 ) {
531
			$this->value->SetAlign('center','bottom');
532
		    }
533
		    else {
534
			$this->value->SetAlign('center','top');
535
		    }
536
		}
537
		$this->value->SetMargin(-5);
538
		$this->value->Stroke($img,$val,$x,$y);
539
	    }
540
	    elseif( $this->valuepos=='center' ) {
541
		$y = ($pts[3] + $pts[1])/2;
542
		$this->value->SetAlign('center','center');
543
		$this->value->SetMargin(0);
544
		$this->value->Stroke($img,$val,$x,$y);
545
	    }
546
	    elseif( $this->valuepos=='bottom' || $this->valuepos=='min' ) {
547
		$y=$pts[1];
548
		if( $img->a === 90 ) {
549
		    if( $val < 0 )
550
			$this->value->SetAlign('right','center');
551
		    else
552
			$this->value->SetAlign('left','center');
553
		}
554
		$this->value->SetMargin(3);
555
		$this->value->Stroke($img,$val,$x,$y);
556
	    }
557
	    else {
558
		JpGraphError::RaiseL(2006,$this->valuepos);
559
//('Unknown position for values on bars :'.$this->valuepos);
560
	    }
561
	    if( !empty($this->csimtargets[$i]) ) {
562
		// Create the client side image map
563
		$rpts = $img->ArrRotate($pts);
564
		$csimcoord=round($rpts[0]).", ".round($rpts[1]);
565
		for( $j=1; $j < 4; ++$j){
566
		    $csimcoord .= ", ".round($rpts[2*$j]).", ".round($rpts[2*$j+1]);
567
		}
568
		$this->csimareas .= '<area shape="poly" coords="'.$csimcoord.'" ';
569
		$this->csimareas .= " href=\"".htmlentities($this->csimtargets[$i])."\"";
570
 
571
		if( !empty($this->csimwintargets[$i]) ) {
572
		    $this->csimareas .= " target=\"".$this->csimwintargets[$i]."\" ";
573
		}
574
 
575
		$sval='';
576
		if( !empty($this->csimalts[$i]) ) {
577
		    $sval=sprintf($this->csimalts[$i],$this->coords[0][$i]);
578
		    $this->csimareas .= " title=\"$sval\" alt=\"$sval\" ";
579
		}
580
		$this->csimareas .= " />\n";
581
	    }
582
	}
583
	return true;
584
    }
585
} // Class
586
 
587
//===================================================
588
// CLASS GroupBarPlot
589
// Description: Produce grouped bar plots
590
//===================================================
591
class GroupBarPlot extends BarPlot {
592
    var $plots=array(), $nbrplots=0;
593
    var $numpoints;
594
//---------------
595
// CONSTRUCTOR
596
    function GroupBarPlot($plots) {
597
	$this->width=0.5;
598
	$this->plots = $plots;
599
	$this->nbrplots = count($plots);
600
	if( $this->nbrplots < 1 ) {
601
	    JpGraphError::RaiseL(2007);//('Cannot create GroupBarPlot from empty plot array.');
602
	}
603
	for($i=0; $i < $this->nbrplots; ++$i ) {
604
	    if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
605
		JpGraphError::RaiseL(2008,$i);//("Group bar plot element nbr $i is undefined or empty.");
606
	    }
607
	}
608
	$this->numpoints = $plots[0]->numpoints;
609
    }
610
 
611
//---------------
612
// PUBLIC METHODS
613
    function Legend(&$graph) {
614
	$n = count($this->plots);
615
	for($i=0; $i < $n; ++$i) {
616
	    $c = get_class($this->plots[$i]);
617
	    if( !is_a($this->plots[$i],'BarPlot') ) {
618
		JpGraphError::RaiseL(2009,$c);//('One of the objects submitted to GroupBar is not a BarPlot. Make sure that you create the Group Bar plot from an array of BarPlot or AccBarPlot objects. (Class = '.$c.')');
619
	    }
620
	    $this->plots[$i]->DoLegend($graph);
621
	}
622
    }
623
 
624
    function Min() {
625
	list($xmin,$ymin) = $this->plots[0]->Min();
626
	$n = count($this->plots);
627
	for($i=0; $i < $n; ++$i) {
628
	    list($xm,$ym) = $this->plots[$i]->Min();
629
	    $xmin = max($xmin,$xm);
630
	    $ymin = min($ymin,$ym);
631
	}
632
	return array($xmin,$ymin);
633
    }
634
 
635
    function Max() {
636
	list($xmax,$ymax) = $this->plots[0]->Max();
637
	$n = count($this->plots);
638
	for($i=0; $i < $n; ++$i) {
639
	    list($xm,$ym) = $this->plots[$i]->Max();
640
	    $xmax = max($xmax,$xm);
641
	    $ymax = max($ymax,$ym);
642
	}
643
	return array($xmax,$ymax);
644
    }
645
 
646
    function GetCSIMareas() {
647
	$n = count($this->plots);
648
	$csimareas='';
649
	for($i=0; $i < $n; ++$i) {
650
	    $csimareas .= $this->plots[$i]->csimareas;
651
	}
652
	return $csimareas;
653
    }
654
 
655
    // Stroke all the bars next to each other
656
    function Stroke(&$img,&$xscale,&$yscale) {
657
	$tmp=$xscale->off;
658
	$n = count($this->plots);
659
	$subwidth = $this->width/$this->nbrplots ;
660
 
661
	for( $i=0; $i < $n; ++$i ) {
662
	    $this->plots[$i]->ymin=$this->ybase;
663
	    $this->plots[$i]->SetWidth($subwidth);
664
 
665
	    // If the client have used SetTextTickInterval() then
666
	    // major_step will be > 1 and the positioning will fail.
667
	    // If we assume it is always one the positioning will work
668
	    // fine with a text scale but this will not work with
669
	    // arbitrary linear scale
670
	    $xscale->off = $tmp+$i*round($xscale->scale_factor* $subwidth);
671
	    $this->plots[$i]->Stroke($img,$xscale,$yscale);
672
	}
673
	$xscale->off=$tmp;
674
    }
675
} // Class
676
 
677
//===================================================
678
// CLASS AccBarPlot
679
// Description: Produce accumulated bar plots
680
//===================================================
681
class AccBarPlot extends BarPlot {
682
    var $plots=null,$nbrplots=0,$numpoints=0;
683
//---------------
684
// CONSTRUCTOR
685
    function AccBarPlot($plots) {
686
	$this->plots = $plots;
687
	$this->nbrplots = count($plots);
688
	if( $this->nbrplots < 1 ) {
689
	    JpGraphError::RaiseL(2010);//('Cannot create AccBarPlot from empty plot array.');
690
	}
691
	for($i=0; $i < $this->nbrplots; ++$i ) {
692
	    if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
693
		JpGraphError::RaiseL(2011,$i);//("Acc bar plot element nbr $i is undefined or empty.");
694
	    }
695
	}
696
	$this->numpoints = $plots[0]->numpoints;
697
	$this->value = new DisplayValue();
698
    }
699
 
700
//---------------
701
// PUBLIC METHODS
702
    function Legend(&$graph) {
703
	$n = count($this->plots);
704
	for( $i=$n-1; $i >= 0; --$i ) {
705
	    $c = get_class($this->plots[$i]);
706
	    if( !is_a($this->plots[$i],'BarPlot') ) {
707
		JpGraphError::RaiseL(2012,$c);//('One of the objects submitted to AccBar is not a BarPlot. Make sure that you create the AccBar plot from an array of BarPlot objects.(Class='.$c.')');
708
	    }
709
	    $this->plots[$i]->DoLegend($graph);
710
	}
711
    }
712
 
713
    function Max() {
714
	list($xmax) = $this->plots[0]->Max();
715
	$nmax=0;
716
	for($i=0; $i < count($this->plots); ++$i) {
717
	    $n = count($this->plots[$i]->coords[0]);
718
	    $nmax = max($nmax,$n);
719
	    list($x) = $this->plots[$i]->Max();
720
	    $xmax = max($xmax,$x);
721
	}
722
	for( $i = 0; $i < $nmax; $i++ ) {
723
	    // Get y-value for bar $i by adding the
724
	    // individual bars from all the plots added.
725
	    // It would be wrong to just add the
726
	    // individual plots max y-value since that
727
	    // would in most cases give to large y-value.
728
	    $y=0;
729
	    if( !isset($this->plots[0]->coords[0][$i]) ) {
730
		JpGraphError::RaiseL(2014);
731
	    }
732
	    if( $this->plots[0]->coords[0][$i] > 0 )
733
		$y=$this->plots[0]->coords[0][$i];
734
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
735
		if( !isset($this->plots[$j]->coords[0][$i]) ) {
736
		    JpGraphError::RaiseL(2014);
737
		}
738
		if( $this->plots[$j]->coords[0][$i] > 0 )
739
		    $y += $this->plots[$j]->coords[0][$i];
740
	    }
741
	    $ymax[$i] = $y;
742
	}
743
	$ymax = max($ymax);
744
 
745
	// Bar always start at baseline
746
	if( $ymax <= $this->ybase )
747
	    $ymax = $this->ybase;
748
	return array($xmax,$ymax);
749
    }
750
 
751
    function Min() {
752
	$nmax=0;
753
	list($xmin,$ysetmin) = $this->plots[0]->Min();
754
	for($i=0; $i < count($this->plots); ++$i) {
755
	    $n = count($this->plots[$i]->coords[0]);
756
	    $nmax = max($nmax,$n);
757
	    list($x,$y) = $this->plots[$i]->Min();
758
	    $xmin = Min($xmin,$x);
759
	    $ysetmin = Min($y,$ysetmin);
760
	}
761
	for( $i = 0; $i < $nmax; $i++ ) {
762
	    // Get y-value for bar $i by adding the
763
	    // individual bars from all the plots added.
764
	    // It would be wrong to just add the
765
	    // individual plots max y-value since that
766
	    // would in most cases give to large y-value.
767
	    $y=0;
768
	    if( $this->plots[0]->coords[0][$i] < 0 )
769
		$y=$this->plots[0]->coords[0][$i];
770
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
771
		if( $this->plots[$j]->coords[0][$i] < 0 )
772
		    $y += $this->plots[ $j ]->coords[0][$i];
773
	    }
774
	    $ymin[$i] = $y;
775
	}
776
	$ymin = Min($ysetmin,Min($ymin));
777
	// Bar always start at baseline
778
	if( $ymin >= $this->ybase )
779
	    $ymin = $this->ybase;
780
	return array($xmin,$ymin);
781
    }
782
 
783
    // Stroke acc bar plot
784
    function Stroke(&$img,&$xscale,&$yscale) {
785
	$pattern=NULL;
786
	$img->SetLineWeight($this->weight);
787
	for($i=0; $i < $this->numpoints-1; $i++) {
788
	    $accy = 0;
789
	    $accy_neg = 0;
790
	    for($j=0; $j < $this->nbrplots; ++$j ) {
791
		$img->SetColor($this->plots[$j]->color);
792
 
793
		if ( $this->plots[$j]->coords[0][$i] >= 0) {
794
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
795
		    $accyt=$yscale->Translate($accy);
796
		    $accy+=$this->plots[$j]->coords[0][$i];
797
		}
798
		else {
799
		    //if ( $this->plots[$j]->coords[0][$i] < 0 || $accy_neg < 0 ) {
800
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
801
		    $accyt=$yscale->Translate($accy_neg);
802
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
803
		}
804
 
805
		$xt=$xscale->Translate($i);
806
 
807
		if( $this->abswidth > -1 )
808
		    $abswidth=$this->abswidth;
809
		else
810
		    $abswidth=round($this->width*$xscale->scale_factor,0);
811
 
812
		$pts=array($xt,$accyt,$xt,$yt,$xt+$abswidth,$yt,$xt+$abswidth,$accyt);
813
 
814
		if( $this->bar_shadow ) {
815
		    $ssh = $this->bar_shadow_hsize;
816
		    $ssv = $this->bar_shadow_vsize;
817
 
818
		    // We must also differ if we are a positive or negative bar.
819
		    if( $j === 0 ) {
820
			// This gets extra complicated since we have to
821
			// see all plots to see if we are negative. It could
822
			// for example be that all plots are 0 until the very
823
			// last one. We therefore need to save the initial setup
824
			// for both the negative and positive case
825
 
826
			// In case the final bar is positive
827
			$sp[0]=$pts[6]+1; $sp[1]=$pts[7];
828
			$sp[2]=$pts[6]+$ssh; $sp[3]=$pts[7]-$ssv;
829
 
830
			// In case the final bar is negative
831
			$nsp[0]=$pts[0]; $nsp[1]=$pts[1];
832
			$nsp[2]=$pts[0]+$ssh; $nsp[3]=$pts[1]-$ssv;
833
			$nsp[4]=$pts[6]+$ssh; $nsp[5]=$pts[7]-$ssv;
834
			$nsp[10]=$pts[6]+1; $nsp[11]=$pts[7];
835
		    }
836
 
837
		    if( $j === $this->nbrplots-1 ) {
838
			// If this is the last plot of the bar and
839
			// the total value is larger than 0 then we
840
			// add the shadow.
841
			if( is_array($this->bar_shadow_color) ) {
842
			    $numcolors = count($this->bar_shadow_color);
843
			    if( $numcolors == 0 ) {
844
				JpGraphError::RaiseL(2013);//('You have specified an empty array for shadow colors in the bar plot.');
845
			    }
846
			    $img->PushColor($this->bar_shadow_color[$i % $numcolors]);
847
			}
848
			else {
849
			    $img->PushColor($this->bar_shadow_color);
850
			}
851
 
852
			if( $accy > 0 ) {
853
			    $sp[4]=$pts[4]+$ssh; $sp[5]=$pts[5]-$ssv;
854
			    $sp[6]=$pts[2]+$ssh; $sp[7]=$pts[3]-$ssv;
855
			    $sp[8]=$pts[2]; $sp[9]=$pts[3]-1;
856
			    $sp[10]=$pts[4]+1; $sp[11]=$pts[5];
857
			    $img->FilledPolygon($sp,4);
858
			}
859
			elseif( $accy_neg < 0 ) {
860
			    $nsp[6]=$pts[4]+$ssh; $nsp[7]=$pts[5]-$ssv;
861
			    $nsp[8]=$pts[4]+1; $nsp[9]=$pts[5];
862
			    $img->FilledPolygon($nsp,4);
863
			}
864
			$img->PopColor();
865
		    }
866
		}
867
 
868
 
869
		// If value is NULL or 0, then don't draw a bar at all
870
		if ($this->plots[$j]->coords[0][$i] == 0 ) continue;
871
 
872
		if( $this->plots[$j]->grad ) {
873
		    $grad = new Gradient($img);
874
		    $grad->FilledRectangle(
875
			$pts[2],$pts[3],
876
			$pts[6],$pts[7],
877
			$this->plots[$j]->grad_fromcolor,
878
			$this->plots[$j]->grad_tocolor,
879
			$this->plots[$j]->grad_style);
880
		} else {
881
		    if (is_array($this->plots[$j]->fill_color) ) {
882
			$numcolors = count($this->plots[$j]->fill_color);
883
			$fillcolor = $this->plots[$j]->fill_color[$i % $numcolors];
884
			// If the bar is specified to be non filled then the fill color is false
885
			if( $fillcolor !== false )
886
			    $img->SetColor($this->plots[$j]->fill_color[$i % $numcolors]);
887
		    }
888
		    else {
889
			$fillcolor = $this->plots[$j]->fill_color;
890
			if( $fillcolor !== false )
891
			    $img->SetColor($this->plots[$j]->fill_color);
892
		    }
893
		    if( $fillcolor !== false )
894
			$img->FilledPolygon($pts);
895
		    $img->SetColor($this->plots[$j]->color);
896
		}
897
 
898
		// Stroke the pattern
899
		if( $this->plots[$j]->iPattern > -1 ) {
900
		    if( $pattern===NULL )
901
			$pattern = new RectPatternFactory();
902
 
903
		    $prect = $pattern->Create($this->plots[$j]->iPattern,$this->plots[$j]->iPatternColor,1);
904
		    $prect->SetDensity($this->plots[$j]->iPatternDensity);
905
		    if( $this->plots[$j]->coords[0][$i] < 0 ) {
906
			$rx = $pts[0];
907
			$ry = $pts[1];
908
		    }
909
		    else {
910
			$rx = $pts[2];
911
			$ry = $pts[3];
912
		    }
913
		    $width = abs($pts[4]-$pts[0])+1;
914
		    $height = abs($pts[1]-$pts[3])+1;
915
		    $prect->SetPos(new Rectangle($rx,$ry,$width,$height));
916
		    $prect->Stroke($img);
917
		}
918
 
919
 
920
		// CSIM array
921
 
922
		if( $i < count($this->plots[$j]->csimtargets) ) {
923
		    // Create the client side image map
924
		    $rpts = $img->ArrRotate($pts);
925
		    $csimcoord=round($rpts[0]).", ".round($rpts[1]);
926
		    for( $k=1; $k < 4; ++$k){
927
			$csimcoord .= ", ".round($rpts[2*$k]).", ".round($rpts[2*$k+1]);
928
		    }
929
		    if( ! empty($this->plots[$j]->csimtargets[$i]) ) {
930
			$this->csimareas.= '<area shape="poly" coords="'.$csimcoord.'" ';
931
			$this->csimareas.= " href=\"".$this->plots[$j]->csimtargets[$i]."\" ";
932
 
933
			if( ! empty($this->plots[$j]->csimwintargets[$i]) ) {
934
			    $this->csimareas.= " target=\"".$this->plots[$j]->csimwintargets[$i]."\" ";
935
			}
936
 
937
			$sval='';
938
			if( !empty($this->plots[$j]->csimalts[$i]) ) {
939
			    $sval=sprintf($this->plots[$j]->csimalts[$i],$this->plots[$j]->coords[0][$i]);
940
			    $this->csimareas .= " title=\"$sval\" ";
941
			}
942
			$this->csimareas .= " alt=\"$sval\" />\n";
943
		    }
944
		}
945
 
946
		$pts[] = $pts[0];
947
		$pts[] = $pts[1];
948
		$img->SetLineWeight($this->plots[$j]->line_weight);
949
		$img->Polygon($pts);
950
		$img->SetLineWeight(1);
951
	    }
952
 
953
	    // Draw labels for each acc.bar
954
 
955
	    $x=$pts[2]+($pts[4]-$pts[2])/2;
956
	    if($this->bar_shadow) $x += $ssh;
957
 
958
	    // First stroke the accumulated value for the entire bar
959
	    // This value is always placed at the top/bottom of the bars
960
	    if( $accy_neg < 0 ) {
961
		$y=$yscale->Translate($accy_neg);
962
		$this->value->Stroke($img,$accy_neg,$x,$y);
963
	    }
964
	    else {
965
		$y=$yscale->Translate($accy);
966
		$this->value->Stroke($img,$accy,$x,$y);
967
	    }
968
 
969
	    $accy = 0;
970
	    $accy_neg = 0;
971
	    for($j=0; $j < $this->nbrplots; ++$j ) {
972
 
973
		// We don't print 0 values in an accumulated bar plot
974
		if( $this->plots[$j]->coords[0][$i] == 0 ) continue;
975
 
976
		if ($this->plots[$j]->coords[0][$i] > 0) {
977
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
978
		    $accyt=$yscale->Translate($accy);
979
		    if(  $this->plots[$j]->valuepos=='center' ) {
980
			$y = $accyt-($accyt-$yt)/2;
981
		    }
982
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
983
			$y = $accyt;
984
		    }
985
		    else { // top or max
986
			$y = $accyt-($accyt-$yt);
987
		    }
988
		    $accy+=$this->plots[$j]->coords[0][$i];
989
		    if(  $this->plots[$j]->valuepos=='center' ) {
990
			$this->plots[$j]->value->SetAlign("center","center");
991
			$this->plots[$j]->value->SetMargin(0);
992
		    }
993
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
994
			$this->plots[$j]->value->SetAlign('center','bottom');
995
			$this->plots[$j]->value->SetMargin(2);
996
		    }
997
		    else {
998
			$this->plots[$j]->value->SetAlign('center','top');
999
			$this->plots[$j]->value->SetMargin(1);
1000
		    }
1001
		} else {
1002
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
1003
		    $accyt=$yscale->Translate($accy_neg);
1004
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
1005
		    if(  $this->plots[$j]->valuepos=='center' ) {
1006
			$y = $accyt-($accyt-$yt)/2;
1007
		    }
1008
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
1009
			$y = $accyt;
1010
		    }
1011
		    else {
1012
			$y = $accyt-($accyt-$yt);
1013
		    }
1014
		    if(  $this->plots[$j]->valuepos=='center' ) {
1015
			$this->plots[$j]->value->SetAlign("center","center");
1016
			$this->plots[$j]->value->SetMargin(0);
1017
		    }
1018
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
1019
			$this->plots[$j]->value->SetAlign('center',$j==0 ? 'bottom':'top');
1020
			$this->plots[$j]->value->SetMargin(-2);
1021
		    }
1022
		    else {
1023
			$this->plots[$j]->value->SetAlign('center','bottom');
1024
			$this->plots[$j]->value->SetMargin(-1);
1025
		    }
1026
		}
1027
		$this->plots[$j]->value->Stroke($img,$this->plots[$j]->coords[0][$i],$x,$y);
1028
	    }
1029
 
1030
	}
1031
	return true;
1032
    }
1033
} // Class
1034
 
1035
/* EOF */
1036
?>