Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 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 781 2006-10-08 08:07:47Z 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
    public $fill=false,$fill_color="lightblue"; // Default is to fill with light blue
32
    public $iPattern=-1,$iPatternDensity=80,$iPatternColor='black';
33
    public $valuepos='top';
34
    public $grad=false,$grad_style=1;
35
    public $grad_fromcolor=array(50,50,200),$grad_tocolor=array(255,255,255);
36
    protected $width=0.4; // in percent of major ticks
37
    protected $abswidth=-1; // Width in absolute pixels
38
    protected $ybase=0; // Bars start at 0
39
    protected $align="center";
40
    protected $bar_shadow=false;
41
    protected $bar_shadow_color="black";
42
    protected $bar_shadow_hsize=3,$bar_shadow_vsize=3;
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
	elseif( ($this instanceof AccBarPlot) || ($this instanceof GroupBarPlot) ) {
139
	    // We only set an absolute width for linear and int scale
140
	    // for text scale the width will be set to a fraction of
141
	    // the majstep width.
142
	    if( $this->abswidth == -1 ) {
143
                // Not set
144
		// set width to a visuable sensible default
145
		$this->abswidth = $graph->img->plotwidth/(2*count($this->coords[0]));
146
	    }
147
	}
148
    }
149
 
150
    function Min() {
151
	$m = parent::Min();
152
	if( $m[1] >= $this->ybase )
153
	    $m[1] = $this->ybase;
154
	return $m;
155
    }
156
 
157
    function Max() {
158
	$m = parent::Max();
159
	if( $m[1] <= $this->ybase )
160
	    $m[1] = $this->ybase;
161
	return $m;
162
    }
163
 
164
    // Specify width as fractions of the major stepo size
165
    function SetWidth($aWidth) {
166
	if( $aWidth > 1 ) {
167
	    // Interpret this as absolute width
168
	    $this->abswidth=$aWidth;
169
	}
170
	else
171
	    $this->width=$aWidth;
172
    }
173
 
174
    // Specify width in absolute pixels. If specified this
175
    // overrides SetWidth()
176
    function SetAbsWidth($aWidth) {
177
	$this->abswidth=$aWidth;
178
    }
179
 
180
    function SetAlign($aAlign) {
181
	$this->align=$aAlign;
182
    }
183
 
184
    function SetNoFill() {
185
	$this->grad = false;
186
	$this->fill_color=false;
187
	$this->fill=false;
188
    }
189
 
190
    function SetFillColor($aColor) {
191
	$this->fill = true ;
192
	$this->fill_color=$aColor;
193
    }
194
 
195
    function SetFillGradient($from_color,$to_color,$style) {
196
	$this->grad=true;
197
	$this->grad_fromcolor=$from_color;
198
	$this->grad_tocolor=$to_color;
199
	$this->grad_style=$style;
200
    }
201
 
202
    function SetValuePos($aPos) {
203
	$this->valuepos = $aPos;
204
    }
205
 
206
    function SetPattern($aPattern, $aColor='black'){
207
	if( is_array($aPattern) ) {
208
	    $n = count($aPattern);
209
	    $this->iPattern = array();
210
	    $this->iPatternDensity = array();
211
	    if( is_array($aColor) ) {
212
		$this->iPatternColor = array();
213
		if( count($aColor) != $n ) {
214
		    JpGraphError::Raise('NUmber of colors is not the same as the number of patterns in BarPlot::SetPattern()');
215
		}
216
	    }
217
	    else
218
		$this->iPatternColor = $aColor;
219
	    for( $i=0; $i < $n; ++$i ) {
220
		$this->_SetPatternHelper($aPattern[$i], $this->iPattern[$i], $this->iPatternDensity[$i]);
221
		if( is_array($aColor) ) {
222
		    $this->iPatternColor[$i] = $aColor[$i];
223
		}
224
	    }
225
	}
226
	else {
227
	    $this->_SetPatternHelper($aPattern, $this->iPattern, $this->iPatternDensity);
228
	    $this->iPatternColor = $aColor;
229
	}
230
    }
231
 
232
    function _SetPatternHelper($aPattern, &$aPatternValue, &$aDensity){
233
	switch( $aPattern ) {
234
	    case PATTERN_DIAG1:
235
		$aPatternValue= 1;
236
		$aDensity = 90;
237
		break;
238
	    case PATTERN_DIAG2:
239
		$aPatternValue= 1;
240
		$aDensity = 75;
241
		break;
242
	    case PATTERN_DIAG3:
243
		$aPatternValue= 2;
244
		$aDensity = 90;
245
		break;
246
	    case PATTERN_DIAG4:
247
		$aPatternValue= 2;
248
		$aDensity = 75;
249
		break;
250
	    case PATTERN_CROSS1:
251
		$aPatternValue= 8;
252
		$aDensity = 90;
253
		break;
254
	    case PATTERN_CROSS2:
255
		$aPatternValue= 8;
256
		$aDensity = 78;
257
		break;
258
	    case PATTERN_CROSS3:
259
		$aPatternValue= 8;
260
		$aDensity = 65;
261
		break;
262
	    case PATTERN_CROSS4:
263
		$aPatternValue= 7;
264
		$aDensity = 90;
265
		break;
266
	    case PATTERN_STRIPE1:
267
		$aPatternValue= 5;
268
		$aDensity = 90;
269
		break;
270
	    case PATTERN_STRIPE2:
271
		$aPatternValue= 5;
272
		$aDensity = 75;
273
		break;
274
	    default:
275
		JpGraphError::Raise('Unknown pattern specified in call to BarPlot::SetPattern()');
276
	}
277
    }
278
 
279
    function Stroke($img,$xscale,$yscale) {
280
 
281
	$numpoints = count($this->coords[0]);
282
	if( isset($this->coords[1]) ) {
283
	    if( count($this->coords[1])!=$numpoints )
284
		JpGraphError::Raise("Number of X and Y points are not equal. Number of X-points:".count($this->coords[1])."Number of Y-points:$numpoints");
285
	    else
286
		$exist_x = true;
287
	}
288
	else
289
	    $exist_x = false;
290
 
291
 
292
	$numbars=count($this->coords[0]);
293
 
294
	// Use GetMinVal() instead of scale[0] directly since in the case
295
	// of log scale we get a correct value. Log scales will have negative
296
	// values for values < 1 while still not representing negative numbers.
297
	if( $yscale->GetMinVal() >= 0 )
298
	    $zp=$yscale->scale_abs[0];
299
	else {
300
	    $zp=$yscale->Translate(0);
301
	}
302
 
303
	if( $this->abswidth > -1 ) {
304
	    $abswidth=$this->abswidth;
305
	}
306
	else
307
	    $abswidth=round($this->width*$xscale->scale_factor,0);
308
 
309
	// Count pontetial pattern array to avoid doing the count for each iteration
310
	if( is_array($this->iPattern) ) {
311
	    $np = count($this->iPattern);
312
	}
313
 
314
	for($i=0; $i < $numbars; ++$i) {
315
 
316
 	    // If value is NULL, or 0 then don't draw a bar at all
317
 	    if ($this->coords[0][$i] === null || $this->coords[0][$i] === '' )
318
		continue;
319
 
320
	    if( $exist_x ) $x=$this->coords[1][$i];
321
	    else $x=$i;
322
 
323
	    $x=$xscale->Translate($x);
324
 
325
// Comment Note: This confuses the positioning when using acc together with
326
// grouped bars. Workaround for fixing #191
327
/*
328
	    if( !$xscale->textscale ) {
329
	    	if($this->align=="center")
330
		    $x -= $abswidth/2;
331
		elseif($this->align=="right")
332
		    $x -= $abswidth;
333
	    }
334
*/
335
	    // Stroke fill color and fill gradient
336
	    $pts=array(
337
		$x,$zp,
338
		$x,$yscale->Translate($this->coords[0][$i]),
339
		$x+$abswidth,$yscale->Translate($this->coords[0][$i]),
340
		$x+$abswidth,$zp);
341
	    if( $this->grad ) {
342
		$grad = new Gradient($img);
343
		$grad->FilledRectangle($pts[2],$pts[3],
344
				       $pts[6],$pts[7],
345
				       $this->grad_fromcolor,$this->grad_tocolor,$this->grad_style);
346
	    }
347
	    elseif( !empty($this->fill_color) ) {
348
		if(is_array($this->fill_color)) {
349
		    $img->PushColor($this->fill_color[$i % count($this->fill_color)]);
350
		} else {
351
		    $img->PushColor($this->fill_color);
352
		}
353
		$img->FilledPolygon($pts);
354
		$img->PopColor();
355
	    }
356
 
357
 
358
	    // Remember value of this bar
359
	    $val=$this->coords[0][$i];
360
 
361
	    if( !empty($val) && !is_numeric($val) ) {
362
		JpGraphError::Raise('All values for a barplot must be numeric. You have specified value['.$i.'] == \''.$val.'\'');
363
	    }
364
 
365
	    // Determine the shadow
366
	    if( $this->bar_shadow && $val != 0) {
367
 
368
		$ssh = $this->bar_shadow_hsize;
369
		$ssv = $this->bar_shadow_vsize;
370
		// Create points to create a "upper-right" shadow
371
		if( $val > 0 ) {
372
		    $sp[0]=$pts[6];		$sp[1]=$pts[7];
373
		    $sp[2]=$pts[4];		$sp[3]=$pts[5];
374
		    $sp[4]=$pts[2];		$sp[5]=$pts[3];
375
		    $sp[6]=$pts[2]+$ssh;	$sp[7]=$pts[3]-$ssv;
376
		    $sp[8]=$pts[4]+$ssh;	$sp[9]=$pts[5]-$ssv;
377
		    $sp[10]=$pts[6]+$ssh;	$sp[11]=$pts[7]-$ssv;
378
		}
379
		elseif( $val < 0 ) {
380
		    $sp[0]=$pts[4];		$sp[1]=$pts[5];
381
		    $sp[2]=$pts[6];		$sp[3]=$pts[7];
382
		    $sp[4]=$pts[0];		$sp[5]=$pts[1];
383
		    $sp[6]=$pts[0]+$ssh;	$sp[7]=$pts[1]-$ssv;
384
		    $sp[8]=$pts[6]+$ssh;	$sp[9]=$pts[7]-$ssv;
385
		    $sp[10]=$pts[4]+$ssh;	$sp[11]=$pts[5]-$ssv;
386
		}
387
		if( is_array($this->bar_shadow_color) ) {
388
		    $numcolors = count($this->bar_shadow_color);
389
		    if( $numcolors == 0 ) {
390
			JpGraphError::Raise('You have specified an empty array for shadow colors in the bar plot.');
391
		    }
392
		    $img->PushColor($this->bar_shadow_color[$i % $numcolors]);
393
		}
394
		else {
395
		    $img->PushColor($this->bar_shadow_color);
396
		}
397
		$img->FilledPolygon($sp);
398
		$img->PopColor();
399
	    }
400
 
401
	    // Stroke the pattern
402
	    if( is_array($this->iPattern) ) {
403
		$f = new RectPatternFactory();
404
		if( is_array($this->iPatternColor) ) {
405
		    $pcolor = $this->iPatternColor[$i % $np];
406
		}
407
		else
408
		    $pcolor = $this->iPatternColor;
409
		$prect = $f->Create($this->iPattern[$i % $np],$pcolor,1);
410
		$prect->SetDensity($this->iPatternDensity[$i % $np]);
411
 
412
		if( $val < 0 ) {
413
		    $rx = $pts[0];
414
		    $ry = $pts[1];
415
		}
416
		else {
417
		    $rx = $pts[2];
418
		    $ry = $pts[3];
419
		}
420
		$width = abs($pts[4]-$pts[0])+1;
421
		$height = abs($pts[1]-$pts[3])+1;
422
		$prect->SetPos(new Rectangle($rx,$ry,$width,$height));
423
		$prect->Stroke($img);
424
	    }
425
	    else {
426
		if( $this->iPattern > -1 ) {
427
		    $f = new RectPatternFactory();
428
		    $prect = $f->Create($this->iPattern,$this->iPatternColor,1);
429
		    $prect->SetDensity($this->iPatternDensity);
430
		    if( $val < 0 ) {
431
			$rx = $pts[0];
432
			$ry = $pts[1];
433
		    }
434
		    else {
435
			$rx = $pts[2];
436
			$ry = $pts[3];
437
		    }
438
		    $width = abs($pts[4]-$pts[0])+1;
439
		    $height = abs($pts[1]-$pts[3])+1;
440
		    $prect->SetPos(new Rectangle($rx,$ry,$width,$height));
441
		    $prect->Stroke($img);
442
		}
443
	    }
444
	    // Stroke the outline of the bar
445
	    if( is_array($this->color) )
446
		$img->SetColor($this->color[$i % count($this->color)]);
447
	    else
448
		$img->SetColor($this->color);
449
 
450
	    $pts[] = $pts[0];
451
	    $pts[] = $pts[1];
452
 
453
	    if( $this->weight > 0 ) {
454
		$img->SetLineWeight($this->weight);
455
		$img->Polygon($pts);
456
	    }
457
 
458
	    // Determine how to best position the values of the individual bars
459
	    $x=$pts[2]+($pts[4]-$pts[2])/2;
460
	    if( $this->valuepos=='top' ) {
461
		$y=$pts[3];
462
		if( $img->a === 90 ) {
463
		    if( $val < 0 )
464
			$this->value->SetAlign('right','center');
465
		    else
466
			$this->value->SetAlign('left','center');
467
 
468
		}
469
		$this->value->Stroke($img,$val,$x,$y);
470
	    }
471
	    elseif( $this->valuepos=='max' ) {
472
		$y=$pts[3];
473
		if( $img->a === 90 ) {
474
		    if( $val < 0 )
475
			$this->value->SetAlign('left','center');
476
		    else
477
			$this->value->SetAlign('right','center');
478
		}
479
		else {
480
		    $this->value->SetAlign('center','top');
481
		}
482
		$this->value->SetMargin(-3);
483
		$this->value->Stroke($img,$val,$x,$y);
484
	    }
485
	    elseif( $this->valuepos=='center' ) {
486
		$y = ($pts[3] + $pts[1])/2;
487
		$this->value->SetAlign('center','center');
488
		$this->value->SetMargin(0);
489
		$this->value->Stroke($img,$val,$x,$y);
490
	    }
491
	    elseif( $this->valuepos=='bottom' || $this->valuepos=='min' ) {
492
		$y=$pts[1];
493
		if( $img->a === 90 ) {
494
		    if( $val < 0 )
495
			$this->value->SetAlign('right','center');
496
		    else
497
			$this->value->SetAlign('left','center');
498
		}
499
		$this->value->SetMargin(3);
500
		$this->value->Stroke($img,$val,$x,$y);
501
	    }
502
	    else {
503
		JpGraphError::Raise('Unknown position for values on bars :'.$this->valuepos);
504
	    }
505
	    // Create the client side image map
506
	    $rpts = $img->ArrRotate($pts);
507
	    $csimcoord=round($rpts[0]).", ".round($rpts[1]);
508
	    for( $j=1; $j < 4; ++$j){
509
		$csimcoord .= ", ".round($rpts[2*$j]).", ".round($rpts[2*$j+1]);
510
	    }
511
	    if( !empty($this->csimtargets[$i]) ) {
512
		$this->csimareas .= '<area shape="poly" coords="'.$csimcoord.'" ';
513
		$this->csimareas .= " href=\"".htmlentities($this->csimtargets[$i])."\"";
514
		$sval='';
515
		if( !empty($this->csimalts[$i]) ) {
516
		    $sval=sprintf($this->csimalts[$i],$this->coords[0][$i]);
517
		    $this->csimareas .= " title=\"$sval\" ";
518
		}
519
		$this->csimareas .= " alt=\"$sval\" />\n";
520
	    }
521
	}
522
	return true;
523
    }
524
} // Class
525
 
526
//===================================================
527
// CLASS GroupBarPlot
528
// Description: Produce grouped bar plots
529
//===================================================
530
class GroupBarPlot extends BarPlot {
531
    private $plots, $nbrplots=0;
532
//---------------
533
// CONSTRUCTOR
534
    function GroupBarPlot($plots) {
535
	$this->width=0.7;
536
	$this->plots = $plots;
537
	$this->nbrplots = count($plots);
538
	if( $this->nbrplots < 1 ) {
539
	    JpGraphError::Raise('Cannot create GroupBarPlot from empty plot array.');
540
	}
541
	for($i=0; $i < $this->nbrplots; ++$i ) {
542
	    if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
543
		JpGraphError::Raise("Group bar plot element nbr $i is undefined or empty.");
544
	    }
545
	}
546
	$this->numpoints = $plots[0]->numpoints;
547
	$this->width=0.7;
548
    }
549
 
550
//---------------
551
// PUBLIC METHODS
552
    function Legend($graph) {
553
	$n = count($this->plots);
554
	for($i=0; $i < $n; ++$i) {
555
	    $c = get_class($this->plots[$i]);
556
	    if( !($this->plots[$i] instanceof BarPlot) ) {
557
		JpGraphError::Raise('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.')');
558
	    }
559
	    $this->plots[$i]->DoLegend($graph);
560
	}
561
    }
562
 
563
    function Min() {
564
	list($xmin,$ymin) = $this->plots[0]->Min();
565
	$n = count($this->plots);
566
	for($i=0; $i < $n; ++$i) {
567
	    list($xm,$ym) = $this->plots[$i]->Min();
568
	    $xmin = max($xmin,$xm);
569
	    $ymin = min($ymin,$ym);
570
	}
571
	return array($xmin,$ymin);
572
    }
573
 
574
    function Max() {
575
	list($xmax,$ymax) = $this->plots[0]->Max();
576
	$n = count($this->plots);
577
	for($i=0; $i < $n; ++$i) {
578
	    list($xm,$ym) = $this->plots[$i]->Max();
579
	    $xmax = max($xmax,$xm);
580
	    $ymax = max($ymax,$ym);
581
	}
582
	return array($xmax,$ymax);
583
    }
584
 
585
    function GetCSIMareas() {
586
	$n = count($this->plots);
587
	$csimareas='';
588
	for($i=0; $i < $n; ++$i) {
589
	    $csimareas .= $this->plots[$i]->csimareas;
590
	}
591
	return $csimareas;
592
    }
593
 
594
    // Stroke all the bars next to each other
595
    function Stroke($img,$xscale,$yscale) {
596
	$tmp=$xscale->off;
597
	$n = count($this->plots);
598
	$subwidth = $this->width/$this->nbrplots ;
599
 
600
	for( $i=0; $i < $n; ++$i ) {
601
	    $this->plots[$i]->ymin=$this->ybase;
602
	    $this->plots[$i]->SetWidth($subwidth);
603
 
604
	    // If the client have used SetTextTickInterval() then
605
	    // major_step will be > 1 and the positioning will fail.
606
	    // If we assume it is always one the positioning will work
607
	    // fine with a text scale but this will not work with
608
	    // arbitrary linear scale
609
	    $xscale->off = $tmp+$i*round($xscale->scale_factor* $subwidth);
610
	    $this->plots[$i]->Stroke($img,$xscale,$yscale);
611
	}
612
	$xscale->off=$tmp;
613
    }
614
} // Class
615
 
616
//===================================================
617
// CLASS AccBarPlot
618
// Description: Produce accumulated bar plots
619
//===================================================
620
class AccBarPlot extends BarPlot {
621
    private $plots=null,$nbrplots=0;
622
//---------------
623
// CONSTRUCTOR
624
    function AccBarPlot($plots) {
625
	$this->plots = $plots;
626
	$this->nbrplots = count($plots);
627
	if( $this->nbrplots < 1 ) {
628
	    JpGraphError::Raise('Cannot create AccBarPlot from empty plot array.');
629
	}
630
	for($i=0; $i < $this->nbrplots; ++$i ) {
631
	    if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
632
		JpGraphError::Raise("Acc bar plot element nbr $i is undefined or empty.");
633
	    }
634
	}
635
	$this->numpoints = $plots[0]->numpoints;
636
	$this->value = new DisplayValue();
637
    }
638
 
639
//---------------
640
// PUBLIC METHODS
641
    function Legend($graph) {
642
	$n = count($this->plots);
643
	for( $i=$n-1; $i >= 0; --$i ) {
644
	    $c = get_class($this->plots[$i]);
645
	    if( !($this->plots[$i] instanceof BarPlot) ) {
646
		JpGraphError::Raise('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.')');
647
	    }
648
	    $this->plots[$i]->DoLegend($graph);
649
	}
650
    }
651
 
652
    function Max() {
653
	list($xmax) = $this->plots[0]->Max();
654
	$nmax=0;
655
	for($i=0; $i < count($this->plots); ++$i) {
656
	    $n = count($this->plots[$i]->coords[0]);
657
	    $nmax = max($nmax,$n);
658
	    list($x) = $this->plots[$i]->Max();
659
	    $xmax = max($xmax,$x);
660
	}
661
	for( $i = 0; $i < $nmax; $i++ ) {
662
	    // Get y-value for bar $i by adding the
663
	    // individual bars from all the plots added.
664
	    // It would be wrong to just add the
665
	    // individual plots max y-value since that
666
	    // would in most cases give to large y-value.
667
	    $y=0;
668
	    if( !isset($this->plots[0]->coords[0][$i]) ) {
669
		JpGraphError::RaiseL(2014);
670
	    }
671
	    if( $this->plots[0]->coords[0][$i] > 0 )
672
		$y=$this->plots[0]->coords[0][$i];
673
	    for( $j = 1; $j < $this->nbrplots; $j++ ) {
674
		if( !isset($this->plots[$j]->coords[0][$i]) ) {
675
		    JpGraphError::RaiseL(2014);
676
		}
677
		if( $this->plots[$j]->coords[0][$i] > 0 )
678
		    $y += $this->plots[$j]->coords[0][$i];
679
	    }
680
	    $ymax[$i] = $y;
681
	}
682
	$ymax = max($ymax);
683
 
684
	// Bar always start at baseline
685
	if( $ymax <= $this->ybase )
686
	    $ymax = $this->ybase;
687
	return array($xmax,$ymax);
688
    }
689
 
690
    function Min() {
691
	$nmax=0;
692
	list($xmin,$ysetmin) = $this->plots[0]->Min();
693
	for($i=0; $i < count($this->plots); ++$i) {
694
	    $n = count($this->plots[$i]->coords[0]);
695
	    $nmax = max($nmax,$n);
696
	    list($x,$y) = $this->plots[$i]->Min();
697
	    $xmin = Min($xmin,$x);
698
	    $ysetmin = Min($y,$ysetmin);
699
	}
700
	for( $i = 0; $i < $nmax; $i++ ) {
701
	    // Get y-value for bar $i by adding the
702
	    // individual bars from all the plots added.
703
	    // It would be wrong to just add the
704
	    // individual plots max y-value since that
705
	    // would in most cases give to large y-value.
706
	    $y=0;
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( $this->plots[$j]->coords[0][$i] < 0 )
711
		    $y += $this->plots[ $j ]->coords[0][$i];
712
	    }
713
	    $ymin[$i] = $y;
714
	}
715
	$ymin = Min($ysetmin,Min($ymin));
716
	// Bar always start at baseline
717
	if( $ymin >= $this->ybase )
718
	    $ymin = $this->ybase;
719
	return array($xmin,$ymin);
720
    }
721
 
722
    // Stroke acc bar plot
723
    function Stroke($img,$xscale,$yscale) {
724
	$pattern=NULL;
725
	$img->SetLineWeight($this->weight);
726
	for($i=0; $i < $this->numpoints-1; $i++) {
727
	    $accy = 0;
728
	    $accy_neg = 0;
729
	    for($j=0; $j < $this->nbrplots; ++$j ) {
730
		$img->SetColor($this->plots[$j]->color);
731
 
732
		if ( $this->plots[$j]->coords[0][$i] >= 0) {
733
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
734
		    $accyt=$yscale->Translate($accy);
735
		    $accy+=$this->plots[$j]->coords[0][$i];
736
		}
737
		else {
738
		    //if ( $this->plots[$j]->coords[0][$i] < 0 || $accy_neg < 0 ) {
739
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
740
		    $accyt=$yscale->Translate($accy_neg);
741
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
742
		}
743
 
744
		$xt=$xscale->Translate($i);
745
 
746
		if( $this->abswidth > -1 )
747
		    $abswidth=$this->abswidth;
748
		else
749
		    $abswidth=round($this->width*$xscale->scale_factor,0);
750
 
751
		$pts=array($xt,$accyt,$xt,$yt,$xt+$abswidth,$yt,$xt+$abswidth,$accyt);
752
 
753
		if( $this->bar_shadow ) {
754
		    $ssh = $this->bar_shadow_hsize;
755
		    $ssv = $this->bar_shadow_vsize;
756
 
757
		    // We must also differ if we are a positive or negative bar.
758
		    if( $j === 0 ) {
759
			// This gets extra complicated since we have to
760
			// see all plots to see if we are negative. It could
761
			// for example be that all plots are 0 until the very
762
			// last one. We therefore need to save the initial setup
763
			// for both the negative and positive case
764
 
765
			// In case the final bar is positive
766
			$sp[0]=$pts[6]+1; $sp[1]=$pts[7];
767
			$sp[2]=$pts[6]+$ssh; $sp[3]=$pts[7]-$ssv;
768
 
769
			// In case the final bar is negative
770
			$nsp[0]=$pts[0]; $nsp[1]=$pts[1];
771
			$nsp[2]=$pts[0]+$ssh; $nsp[3]=$pts[1]-$ssv;
772
			$nsp[4]=$pts[6]+$ssh; $nsp[5]=$pts[7]-$ssv;
773
			$nsp[10]=$pts[6]+1; $nsp[11]=$pts[7];
774
		    }
775
 
776
		    if( $j === $this->nbrplots-1 ) {
777
			// If this is the last plot of the bar and
778
			// the total value is larger than 0 then we
779
			// add the shadow.
780
			if( is_array($this->bar_shadow_color) ) {
781
			    $numcolors = count($this->bar_shadow_color);
782
			    if( $numcolors == 0 ) {
783
				JpGraphError::Raise('You have specified an empty array for shadow colors in the bar plot.');
784
			    }
785
			    $img->PushColor($this->bar_shadow_color[$i % $numcolors]);
786
			}
787
			else {
788
			    $img->PushColor($this->bar_shadow_color);
789
			}
790
 
791
			if( $accy > 0 ) {
792
			    $sp[4]=$pts[4]+$ssh; $sp[5]=$pts[5]-$ssv;
793
			    $sp[6]=$pts[2]+$ssh; $sp[7]=$pts[3]-$ssv;
794
			    $sp[8]=$pts[2]; $sp[9]=$pts[3]-1;
795
			    $sp[10]=$pts[4]+1; $sp[11]=$pts[5];
796
			    $img->FilledPolygon($sp,4);
797
			}
798
			elseif( $accy_neg < 0 ) {
799
			    $nsp[6]=$pts[4]+$ssh; $nsp[7]=$pts[5]-$ssv;
800
			    $nsp[8]=$pts[4]+1; $nsp[9]=$pts[5];
801
			    $img->FilledPolygon($nsp,4);
802
			}
803
			$img->PopColor();
804
		    }
805
		}
806
 
807
 
808
		// If value is NULL or 0, then don't draw a bar at all
809
		if ($this->plots[$j]->coords[0][$i] == 0 ) continue;
810
 
811
		if( $this->plots[$j]->grad ) {
812
		    $grad = new Gradient($img);
813
		    $grad->FilledRectangle(
814
			$pts[2],$pts[3],
815
			$pts[6],$pts[7],
816
			$this->plots[$j]->grad_fromcolor,
817
			$this->plots[$j]->grad_tocolor,
818
			$this->plots[$j]->grad_style);
819
		} else {
820
		    if (is_array($this->plots[$j]->fill_color) ) {
821
			$numcolors = count($this->plots[$j]->fill_color);
822
			$img->SetColor($this->plots[$j]->fill_color[$i % $numcolors]);
823
		    }
824
		    else {
825
			$img->SetColor($this->plots[$j]->fill_color);
826
		    }
827
		    $img->FilledPolygon($pts);
828
		    $img->SetColor($this->plots[$j]->color);
829
		}
830
 
831
		// Stroke the pattern
832
		if( $this->plots[$j]->iPattern > -1 ) {
833
		    if( $pattern===NULL )
834
			$pattern = new RectPatternFactory();
835
 
836
		    $prect = $pattern->Create($this->plots[$j]->iPattern,$this->plots[$j]->iPatternColor,1);
837
		    $prect->SetDensity($this->plots[$j]->iPatternDensity);
838
		    if( $this->plots[$j]->coords[0][$i] < 0 ) {
839
			$rx = $pts[0];
840
			$ry = $pts[1];
841
		    }
842
		    else {
843
			$rx = $pts[2];
844
			$ry = $pts[3];
845
		    }
846
		    $width = abs($pts[4]-$pts[0])+1;
847
		    $height = abs($pts[1]-$pts[3])+1;
848
		    $prect->SetPos(new Rectangle($rx,$ry,$width,$height));
849
		    $prect->Stroke($img);
850
		}
851
 
852
 
853
		// CSIM array
854
 
855
		if( $i < count($this->plots[$j]->csimtargets) ) {
856
		    // Create the client side image map
857
		    $rpts = $img->ArrRotate($pts);
858
		    $csimcoord=round($rpts[0]).", ".round($rpts[1]);
859
		    for( $k=1; $k < 4; ++$k){
860
			$csimcoord .= ", ".round($rpts[2*$k]).", ".round($rpts[2*$k+1]);
861
		    }
862
		    if( ! empty($this->plots[$j]->csimtargets[$i]) ) {
863
			$this->csimareas.= '<area shape="poly" coords="'.$csimcoord.'" ';
864
			$this->csimareas.= " href=\"".$this->plots[$j]->csimtargets[$i]."\"";
865
			$sval='';
866
			if( !empty($this->plots[$j]->csimalts[$i]) ) {
867
			    $sval=sprintf($this->plots[$j]->csimalts[$i],$this->plots[$j]->coords[0][$i]);
868
			    $this->csimareas .= " title=\"$sval\" ";
869
			}
870
			$this->csimareas .= " alt=\"$sval\" />\n";
871
		    }
872
		}
873
 
874
		$pts[] = $pts[0];
875
		$pts[] = $pts[1];
876
		$img->Polygon($pts);
877
	    }
878
 
879
	    // Draw labels for each acc.bar
880
 
881
	    $x=$pts[2]+($pts[4]-$pts[2])/2;
882
	    if($this->bar_shadow) $x += $ssh;
883
 
884
	    // First stroke the accumulated value for the entire bar
885
	    // This value is always placed at the top/bottom of the bars
886
	    if( $accy_neg < 0 ) {
887
		$y=$yscale->Translate($accy_neg);
888
		$this->value->Stroke($img,$accy_neg,$x,$y);
889
	    }
890
	    else {
891
		$y=$yscale->Translate($accy);
892
		$this->value->Stroke($img,$accy,$x,$y);
893
	    }
894
 
895
	    $accy = 0;
896
	    $accy_neg = 0;
897
	    for($j=0; $j < $this->nbrplots; ++$j ) {
898
 
899
		// We don't print 0 values in an accumulated bar plot
900
		if( $this->plots[$j]->coords[0][$i] == 0 ) continue;
901
 
902
		if ($this->plots[$j]->coords[0][$i] > 0) {
903
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy);
904
		    $accyt=$yscale->Translate($accy);
905
		    if(  $this->plots[$j]->valuepos=='center' ) {
906
			$y = $accyt-($accyt-$yt)/2;
907
		    }
908
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
909
			$y = $accyt;
910
		    }
911
		    else { // top or max
912
			$y = $accyt-($accyt-$yt);
913
		    }
914
		    $accy+=$this->plots[$j]->coords[0][$i];
915
		    if(  $this->plots[$j]->valuepos=='center' ) {
916
			$this->plots[$j]->value->SetAlign("center","center");
917
			$this->plots[$j]->value->SetMargin(0);
918
		    }
919
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
920
			$this->plots[$j]->value->SetAlign('center','bottom');
921
			$this->plots[$j]->value->SetMargin(2);
922
		    }
923
		    else {
924
			$this->plots[$j]->value->SetAlign('center','top');
925
			$this->plots[$j]->value->SetMargin(1);
926
		    }
927
		} else {
928
		    $yt=$yscale->Translate($this->plots[$j]->coords[0][$i]+$accy_neg);
929
		    $accyt=$yscale->Translate($accy_neg);
930
		    $accy_neg+=$this->plots[$j]->coords[0][$i];
931
		    if(  $this->plots[$j]->valuepos=='center' ) {
932
			$y = $accyt-($accyt-$yt)/2;
933
		    }
934
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
935
			$y = $accyt;
936
		    }
937
		    else {
938
			$y = $accyt-($accyt-$yt);
939
		    }
940
		    if(  $this->plots[$j]->valuepos=='center' ) {
941
			$this->plots[$j]->value->SetAlign("center","center");
942
			$this->plots[$j]->value->SetMargin(0);
943
		    }
944
		    elseif( $this->plots[$j]->valuepos=='bottom' ) {
945
			$this->plots[$j]->value->SetAlign('center',$j==0 ? 'bottom':'top');
946
			$this->plots[$j]->value->SetMargin(-2);
947
		    }
948
		    else {
949
			$this->plots[$j]->value->SetAlign('center','bottom');
950
			$this->plots[$j]->value->SetMargin(-1);
951
		    }
952
		}
953
		$this->plots[$j]->value->Stroke($img,$this->plots[$j]->coords[0][$i],$x,$y);
954
	    }
955
 
956
	}
957
	return true;
958
    }
959
} // Class
960
 
961
/* EOF */
962
?>