Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 866 2007-03-24 11:17:27Z 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);
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);
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);
103
	    }
104
	    else {
105
		$graph->legend->Add($this->legend,$this->fill_color,"",0,
106
				    $this->legendcsimtarget,$this->legendcsimalt);
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
	    if( $this->valuepos=='top' ) {
496
		$y=$pts[3];
497
		if( $img->a === 90 ) {
498
		    if( $val < 0 )
499
			$this->value->SetAlign('right','center');
500
		    else
501
			$this->value->SetAlign('left','center');
502
 
503
		}
504
		$this->value->Stroke($img,$val,$x,$y);
505
	    }
506
	    elseif( $this->valuepos=='max' ) {
507
		$y=$pts[3];
508
		if( $img->a === 90 ) {
509
		    if( $val < 0 )
510
			$this->value->SetAlign('left','center');
511
		    else
512
			$this->value->SetAlign('right','center');
513
		}
514
		else {
515
		    $this->value->SetAlign('center','top');
516
		}
517
		$this->value->SetMargin(-3);
518
		$this->value->Stroke($img,$val,$x,$y);
519
	    }
520
	    elseif( $this->valuepos=='center' ) {
521
		$y = ($pts[3] + $pts[1])/2;
522
		$this->value->SetAlign('center','center');
523
		$this->value->SetMargin(0);
524
		$this->value->Stroke($img,$val,$x,$y);
525
	    }
526
	    elseif( $this->valuepos=='bottom' || $this->valuepos=='min' ) {
527
		$y=$pts[1];
528
		if( $img->a === 90 ) {
529
		    if( $val < 0 )
530
			$this->value->SetAlign('right','center');
531
		    else
532
			$this->value->SetAlign('left','center');
533
		}
534
		$this->value->SetMargin(3);
535
		$this->value->Stroke($img,$val,$x,$y);
536
	    }
537
	    else {
538
		JpGraphError::RaiseL(2006,$this->valuepos);
539
//('Unknown position for values on bars :'.$this->valuepos);
540
	    }
541
	    if( !empty($this->csimtargets[$i]) ) {
542
		// Create the client side image map
543
		$rpts = $img->ArrRotate($pts);
544
		$csimcoord=round($rpts[0]).", ".round($rpts[1]);
545
		for( $j=1; $j < 4; ++$j){
546
		    $csimcoord .= ", ".round($rpts[2*$j]).", ".round($rpts[2*$j+1]);
547
		}
548
		$this->csimareas .= '<area shape="poly" coords="'.$csimcoord.'" ';
549
		$this->csimareas .= " href=\"".htmlentities($this->csimtargets[$i])."\"";
550
		$sval='';
551
		if( !empty($this->csimalts[$i]) ) {
552
		    $sval=sprintf($this->csimalts[$i],$this->coords[0][$i]);
553
		    $this->csimareas .= " title=\"$sval\" ";
554
		}
555
		$this->csimareas .= " alt=\"$sval\" />\n";
556
	    }
557
	}
558
	return true;
559
    }
560
} // Class
561
 
562
//===================================================
563
// CLASS GroupBarPlot
564
// Description: Produce grouped bar plots
565
//===================================================
566
class GroupBarPlot extends BarPlot {
567
    var $plots=array(), $nbrplots=0;
568
    var $numpoints;
569
//---------------
570
// CONSTRUCTOR
571
    function GroupBarPlot($plots) {
572
	$this->width=0.5;
573
	$this->plots = $plots;
574
	$this->nbrplots = count($plots);
575
	if( $this->nbrplots < 1 ) {
576
	    JpGraphError::RaiseL(2007);//('Cannot create GroupBarPlot from empty plot array.');
577
	}
578
	for($i=0; $i < $this->nbrplots; ++$i ) {
579
	    if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
580
		JpGraphError::RaiseL(2008,$i);//("Group bar plot element nbr $i is undefined or empty.");
581
	    }
582
	}
583
	$this->numpoints = $plots[0]->numpoints;
584
    }
585
 
586
//---------------
587
// PUBLIC METHODS
588
    function Legend(&$graph) {
589
	$n = count($this->plots);
590
	for($i=0; $i < $n; ++$i) {
591
	    $c = get_class($this->plots[$i]);
592
	    if( !is_a($this->plots[$i],'BarPlot') ) {
593
		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.')');
594
	    }
595
	    $this->plots[$i]->DoLegend($graph);
596
	}
597
    }
598
 
599
    function Min() {
600
	list($xmin,$ymin) = $this->plots[0]->Min();
601
	$n = count($this->plots);
602
	for($i=0; $i < $n; ++$i) {
603
	    list($xm,$ym) = $this->plots[$i]->Min();
604
	    $xmin = max($xmin,$xm);
605
	    $ymin = min($ymin,$ym);
606
	}
607
	return array($xmin,$ymin);
608
    }
609
 
610
    function Max() {
611
	list($xmax,$ymax) = $this->plots[0]->Max();
612
	$n = count($this->plots);
613
	for($i=0; $i < $n; ++$i) {
614
	    list($xm,$ym) = $this->plots[$i]->Max();
615
	    $xmax = max($xmax,$xm);
616
	    $ymax = max($ymax,$ym);
617
	}
618
	return array($xmax,$ymax);
619
    }
620
 
621
    function GetCSIMareas() {
622
	$n = count($this->plots);
623
	$csimareas='';
624
	for($i=0; $i < $n; ++$i) {
625
	    $csimareas .= $this->plots[$i]->csimareas;
626
	}
627
	return $csimareas;
628
    }
629
 
630
    // Stroke all the bars next to each other
631
    function Stroke(&$img,&$xscale,&$yscale) {
632
	$tmp=$xscale->off;
633
	$n = count($this->plots);
634
	$subwidth = $this->width/$this->nbrplots ;
635
 
636
	for( $i=0; $i < $n; ++$i ) {
637
	    $this->plots[$i]->ymin=$this->ybase;
638
	    $this->plots[$i]->SetWidth($subwidth);
639
 
640
	    // If the client have used SetTextTickInterval() then
641
	    // major_step will be > 1 and the positioning will fail.
642
	    // If we assume it is always one the positioning will work
643
	    // fine with a text scale but this will not work with
644
	    // arbitrary linear scale
645
	    $xscale->off = $tmp+$i*round($xscale->scale_factor* $subwidth);
646
	    $this->plots[$i]->Stroke($img,$xscale,$yscale);
647
	}
648
	$xscale->off=$tmp;
649
    }
650
} // Class
651
 
652
//===================================================
653
// CLASS AccBarPlot
654
// Description: Produce accumulated bar plots
655
//===================================================
656
class AccBarPlot extends BarPlot {
657
    var $plots=null,$nbrplots=0,$numpoints=0;
658
//---------------
659
// CONSTRUCTOR
660
    function AccBarPlot($plots) {
661
	$this->plots = $plots;
662
	$this->nbrplots = count($plots);
663
	if( $this->nbrplots < 1 ) {
664
	    JpGraphError::RaiseL(2010);//('Cannot create AccBarPlot from empty plot array.');
665
	}
666
	for($i=0; $i < $this->nbrplots; ++$i ) {
667
	    if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
668
		JpGraphError::RaiseL(2011,$i);//("Acc bar plot element nbr $i is undefined or empty.");
669
	    }
670
	}
671
	$this->numpoints = $plots[0]->numpoints;
672
	$this->value = new DisplayValue();
673
    }
674
 
675
//---------------
676
// PUBLIC METHODS
677
    function Legend(&$graph) {
678
	$n = count($this->plots);
679
	for( $i=$n-1; $i >= 0; --$i ) {
680
	    $c = get_class($this->plots[$i]);
681
	    if( !is_a($this->plots[$i],'BarPlot') ) {
682
		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.')');
683
	    }
684
	    $this->plots[$i]->DoLegend($graph);
685
	}
686
    }
687
 
688
    function Max() {
689
	list($xmax) = $this->plots[0]->Max();
690
	$nmax=0;
691
	for($i=0; $i < count($this->plots); ++$i) {
692
	    $n = count($this->plots[$i]->coords[0]);
693
	    $nmax = max($nmax,$n);
694
	    list($x) = $this->plots[$i]->Max();
695
	    $xmax = max($xmax,$x);
696
	}
697
	for( $i = 0; $i < $nmax; $i++ ) {
698
	    // Get y-value for bar $i by adding the
699
	    // individual bars from all the plots added.
700
	    // It would be wrong to just add the
701
	    // individual plots max y-value since that
702
	    // would in most cases give to large y-value.
703
	    $y=0;
704
	    if( !isset($this->plots[0]->coords[0][$i]) ) {
705
		JpGraphError::RaiseL(2014);
706
	    }
707
	    if( $this->plots[0]->coords[0][$i] > 0 )
708
		$y=$this->plots[0]->coords[0][$i];
709
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
710
		if( !isset($this->plots[$j]->coords[0][$i]) ) {
711
		    JpGraphError::RaiseL(2014);
712
		}
713
		if( $this->plots[$j]->coords[0][$i] > 0 )
714
		    $y += $this->plots[$j]->coords[0][$i];
715
	    }
716
	    $ymax[$i] = $y;
717
	}
718
	$ymax = max($ymax);
719
 
720
	// Bar always start at baseline
721
	if( $ymax <= $this->ybase )
722
	    $ymax = $this->ybase;
723
	return array($xmax,$ymax);
724
    }
725
 
726
    function Min() {
727
	$nmax=0;
728
	list($xmin,$ysetmin) = $this->plots[0]->Min();
729
	for($i=0; $i < count($this->plots); ++$i) {
730
	    $n = count($this->plots[$i]->coords[0]);
731
	    $nmax = max($nmax,$n);
732
	    list($x,$y) = $this->plots[$i]->Min();
733
	    $xmin = Min($xmin,$x);
734
	    $ysetmin = Min($y,$ysetmin);
735
	}
736
	for( $i = 0; $i < $nmax; $i++ ) {
737
	    // Get y-value for bar $i by adding the
738
	    // individual bars from all the plots added.
739
	    // It would be wrong to just add the
740
	    // individual plots max y-value since that
741
	    // would in most cases give to large y-value.
742
	    $y=0;
743
	    if( $this->plots[0]->coords[0][$i] < 0 )
744
		$y=$this->plots[0]->coords[0][$i];
745
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
746
		if( $this->plots[$j]->coords[0][$i] < 0 )
747
		    $y += $this->plots[ $j ]->coords[0][$i];
748
	    }
749
	    $ymin[$i] = $y;
750
	}
751
	$ymin = Min($ysetmin,Min($ymin));
752
	// Bar always start at baseline
753
	if( $ymin >= $this->ybase )
754
	    $ymin = $this->ybase;
755
	return array($xmin,$ymin);
756
    }
757
 
758
    // Stroke acc bar plot
759
    function Stroke(&$img,&$xscale,&$yscale) {
760
	$pattern=NULL;
761
	$img->SetLineWeight($this->weight);
762
	for($i=0; $i < $this->numpoints-1; $i++) {
763
	    $accy = 0;
764
	    $accy_neg = 0;
765
	    for($j=0; $j < $this->nbrplots; ++$j ) {
766
		$img->SetColor($this->plots[$j]->color);
767
 
768
		if ( $this->plots[$j]->coords[0][$i] >= 0) {
769
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
770
		    $accyt=$yscale->Translate($accy);
771
		    $accy+=$this->plots[$j]->coords[0][$i];
772
		}
773
		else {
774
		    //if ( $this->plots[$j]->coords[0][$i] < 0 || $accy_neg < 0 ) {
775
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
776
		    $accyt=$yscale->Translate($accy_neg);
777
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
778
		}
779
 
780
		$xt=$xscale->Translate($i);
781
 
782
		if( $this->abswidth > -1 )
783
		    $abswidth=$this->abswidth;
784
		else
785
		    $abswidth=round($this->width*$xscale->scale_factor,0);
786
 
787
		$pts=array($xt,$accyt,$xt,$yt,$xt+$abswidth,$yt,$xt+$abswidth,$accyt);
788
 
789
		if( $this->bar_shadow ) {
790
		    $ssh = $this->bar_shadow_hsize;
791
		    $ssv = $this->bar_shadow_vsize;
792
 
793
		    // We must also differ if we are a positive or negative bar.
794
		    if( $j === 0 ) {
795
			// This gets extra complicated since we have to
796
			// see all plots to see if we are negative. It could
797
			// for example be that all plots are 0 until the very
798
			// last one. We therefore need to save the initial setup
799
			// for both the negative and positive case
800
 
801
			// In case the final bar is positive
802
			$sp[0]=$pts[6]+1; $sp[1]=$pts[7];
803
			$sp[2]=$pts[6]+$ssh; $sp[3]=$pts[7]-$ssv;
804
 
805
			// In case the final bar is negative
806
			$nsp[0]=$pts[0]; $nsp[1]=$pts[1];
807
			$nsp[2]=$pts[0]+$ssh; $nsp[3]=$pts[1]-$ssv;
808
			$nsp[4]=$pts[6]+$ssh; $nsp[5]=$pts[7]-$ssv;
809
			$nsp[10]=$pts[6]+1; $nsp[11]=$pts[7];
810
		    }
811
 
812
		    if( $j === $this->nbrplots-1 ) {
813
			// If this is the last plot of the bar and
814
			// the total value is larger than 0 then we
815
			// add the shadow.
816
			if( is_array($this->bar_shadow_color) ) {
817
			    $numcolors = count($this->bar_shadow_color);
818
			    if( $numcolors == 0 ) {
819
				JpGraphError::RaiseL(2013);//('You have specified an empty array for shadow colors in the bar plot.');
820
			    }
821
			    $img->PushColor($this->bar_shadow_color[$i % $numcolors]);
822
			}
823
			else {
824
			    $img->PushColor($this->bar_shadow_color);
825
			}
826
 
827
			if( $accy > 0 ) {
828
			    $sp[4]=$pts[4]+$ssh; $sp[5]=$pts[5]-$ssv;
829
			    $sp[6]=$pts[2]+$ssh; $sp[7]=$pts[3]-$ssv;
830
			    $sp[8]=$pts[2]; $sp[9]=$pts[3]-1;
831
			    $sp[10]=$pts[4]+1; $sp[11]=$pts[5];
832
			    $img->FilledPolygon($sp,4);
833
			}
834
			elseif( $accy_neg < 0 ) {
835
			    $nsp[6]=$pts[4]+$ssh; $nsp[7]=$pts[5]-$ssv;
836
			    $nsp[8]=$pts[4]+1; $nsp[9]=$pts[5];
837
			    $img->FilledPolygon($nsp,4);
838
			}
839
			$img->PopColor();
840
		    }
841
		}
842
 
843
 
844
		// If value is NULL or 0, then don't draw a bar at all
845
		if ($this->plots[$j]->coords[0][$i] == 0 ) continue;
846
 
847
		if( $this->plots[$j]->grad ) {
848
		    $grad = new Gradient($img);
849
		    $grad->FilledRectangle(
850
			$pts[2],$pts[3],
851
			$pts[6],$pts[7],
852
			$this->plots[$j]->grad_fromcolor,
853
			$this->plots[$j]->grad_tocolor,
854
			$this->plots[$j]->grad_style);
855
		} else {
856
		    if (is_array($this->plots[$j]->fill_color) ) {
857
			$numcolors = count($this->plots[$j]->fill_color);
858
			$fillcolor = $this->plots[$j]->fill_color[$i % $numcolors];
859
			// If the bar is specified to be non filled then the fill color is false
860
			if( $fillcolor !== false )
861
			    $img->SetColor($this->plots[$j]->fill_color[$i % $numcolors]);
862
		    }
863
		    else {
864
			$fillcolor = $this->plots[$j]->fill_color;
865
			if( $fillcolor !== false )
866
			    $img->SetColor($this->plots[$j]->fill_color);
867
		    }
868
		    if( $fillcolor !== false )
869
			$img->FilledPolygon($pts);
870
		    $img->SetColor($this->plots[$j]->color);
871
		}
872
 
873
		// Stroke the pattern
874
		if( $this->plots[$j]->iPattern > -1 ) {
875
		    if( $pattern===NULL )
876
			$pattern = new RectPatternFactory();
877
 
878
		    $prect = $pattern->Create($this->plots[$j]->iPattern,$this->plots[$j]->iPatternColor,1);
879
		    $prect->SetDensity($this->plots[$j]->iPatternDensity);
880
		    if( $this->plots[$j]->coords[0][$i] < 0 ) {
881
			$rx = $pts[0];
882
			$ry = $pts[1];
883
		    }
884
		    else {
885
			$rx = $pts[2];
886
			$ry = $pts[3];
887
		    }
888
		    $width = abs($pts[4]-$pts[0])+1;
889
		    $height = abs($pts[1]-$pts[3])+1;
890
		    $prect->SetPos(new Rectangle($rx,$ry,$width,$height));
891
		    $prect->Stroke($img);
892
		}
893
 
894
 
895
		// CSIM array
896
 
897
		if( $i < count($this->plots[$j]->csimtargets) ) {
898
		    // Create the client side image map
899
		    $rpts = $img->ArrRotate($pts);
900
		    $csimcoord=round($rpts[0]).", ".round($rpts[1]);
901
		    for( $k=1; $k < 4; ++$k){
902
			$csimcoord .= ", ".round($rpts[2*$k]).", ".round($rpts[2*$k+1]);
903
		    }
904
		    if( ! empty($this->plots[$j]->csimtargets[$i]) ) {
905
			$this->csimareas.= '<area shape="poly" coords="'.$csimcoord.'" ';
906
			$this->csimareas.= " href=\"".$this->plots[$j]->csimtargets[$i]."\"";
907
			$sval='';
908
			if( !empty($this->plots[$j]->csimalts[$i]) ) {
909
			    $sval=sprintf($this->plots[$j]->csimalts[$i],$this->plots[$j]->coords[0][$i]);
910
			    $this->csimareas .= " title=\"$sval\" ";
911
			}
912
			$this->csimareas .= " alt=\"$sval\" />\n";
913
		    }
914
		}
915
 
916
		$pts[] = $pts[0];
917
		$pts[] = $pts[1];
918
		$img->SetLineWeight($this->plots[$j]->line_weight);
919
		$img->Polygon($pts);
920
		$img->SetLineWeight(1);
921
	    }
922
 
923
	    // Draw labels for each acc.bar
924
 
925
	    $x=$pts[2]+($pts[4]-$pts[2])/2;
926
	    if($this->bar_shadow) $x += $ssh;
927
 
928
	    // First stroke the accumulated value for the entire bar
929
	    // This value is always placed at the top/bottom of the bars
930
	    if( $accy_neg < 0 ) {
931
		$y=$yscale->Translate($accy_neg);
932
		$this->value->Stroke($img,$accy_neg,$x,$y);
933
	    }
934
	    else {
935
		$y=$yscale->Translate($accy);
936
		$this->value->Stroke($img,$accy,$x,$y);
937
	    }
938
 
939
	    $accy = 0;
940
	    $accy_neg = 0;
941
	    for($j=0; $j < $this->nbrplots; ++$j ) {
942
 
943
		// We don't print 0 values in an accumulated bar plot
944
		if( $this->plots[$j]->coords[0][$i] == 0 ) continue;
945
 
946
		if ($this->plots[$j]->coords[0][$i] > 0) {
947
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
948
		    $accyt=$yscale->Translate($accy);
949
		    if(  $this->plots[$j]->valuepos=='center' ) {
950
			$y = $accyt-($accyt-$yt)/2;
951
		    }
952
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
953
			$y = $accyt;
954
		    }
955
		    else { // top or max
956
			$y = $accyt-($accyt-$yt);
957
		    }
958
		    $accy+=$this->plots[$j]->coords[0][$i];
959
		    if(  $this->plots[$j]->valuepos=='center' ) {
960
			$this->plots[$j]->value->SetAlign("center","center");
961
			$this->plots[$j]->value->SetMargin(0);
962
		    }
963
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
964
			$this->plots[$j]->value->SetAlign('center','bottom');
965
			$this->plots[$j]->value->SetMargin(2);
966
		    }
967
		    else {
968
			$this->plots[$j]->value->SetAlign('center','top');
969
			$this->plots[$j]->value->SetMargin(1);
970
		    }
971
		} else {
972
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
973
		    $accyt=$yscale->Translate($accy_neg);
974
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
975
		    if(  $this->plots[$j]->valuepos=='center' ) {
976
			$y = $accyt-($accyt-$yt)/2;
977
		    }
978
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
979
			$y = $accyt;
980
		    }
981
		    else {
982
			$y = $accyt-($accyt-$yt);
983
		    }
984
		    if(  $this->plots[$j]->valuepos=='center' ) {
985
			$this->plots[$j]->value->SetAlign("center","center");
986
			$this->plots[$j]->value->SetMargin(0);
987
		    }
988
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
989
			$this->plots[$j]->value->SetAlign('center',$j==0 ? 'bottom':'top');
990
			$this->plots[$j]->value->SetMargin(-2);
991
		    }
992
		    else {
993
			$this->plots[$j]->value->SetAlign('center','bottom');
994
			$this->plots[$j]->value->SetMargin(-1);
995
		    }
996
		}
997
		$this->plots[$j]->value->Stroke($img,$this->plots[$j]->coords[0][$i],$x,$y);
998
	    }
999
 
1000
	}
1001
	return true;
1002
    }
1003
} // Class
1004
 
1005
/* EOF */
1006
?>