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_PIE.PHP
4
// Description:	Pie plot extension for JpGraph
5
// Created: 	2001-02-14
6
// Ver:		$Id: jpgraph_pie.php 846 2007-03-10 10:36:47Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
 
12
 
13
// Defines for PiePlot::SetLabelType()
14
DEFINE("PIE_VALUE_ABS",1);
15
DEFINE("PIE_VALUE_PER",0);
16
DEFINE("PIE_VALUE_PERCENTAGE",0);
17
DEFINE("PIE_VALUE_ADJPERCENTAGE",2);
18
DEFINE("PIE_VALUE_ADJPER",2);
19
 
20
//===================================================
21
// CLASS PiePlot
22
// Description: Draws a pie plot
23
//===================================================
24
class PiePlot {
25
    var $posx=0.5,$posy=0.5;
26
    var $radius=0.3;
27
    var $explode_radius=array(),$explode_all=false,$explode_r=20;
28
    var $labels=null, $legends=null;
29
    var $csimtargets=null;  // Array of targets for CSIM
30
    var $csimareas='';		// Generated CSIM text
31
    var $csimalts=null;		// ALT tags for corresponding target
32
    var $data=null;
33
    var $title;
34
    var $startangle=0;
35
    var $weight=1, $color="black";
36
    var $legend_margin=6,$show_labels=true;
37
    var $themearr = array(
38
	"earth" 	=> array(136,34,40,45,46,62,63,134,74,10,120,136,141,168,180,77,209,218,346,395,89,430),
39
	"pastel" => array(27,415,128,59,66,79,105,110,42,147,152,230,236,240,331,337,405,38),
40
	"water"  => array(8,370,24,40,335,56,213,237,268,14,326,387,10,388),
41
	"sand"   => array(27,168,34,170,19,50,65,72,131,209,46,393));
42
    var $theme="earth";
43
    var $setslicecolors=array();
44
    var $labeltype=0; // Default to percentage
45
    var $pie_border=true,$pie_interior_border=true;
46
    var $value;
47
    var $ishadowcolor='',$ishadowdrop=4;
48
    var $ilabelposadj=1;
49
    var $legendcsimtargets = array();
50
    var $legendcsimalts = array();
51
    var $adjusted_data = array();
52
    var $guideline = null,$guidelinemargin=10;
53
    var $iShowGuideLineForSingle = false;
54
    var $iGuideLineCurve = false,$iGuideVFactor=1.4,$iGuideLineRFactor=0.8;
55
//---------------
56
// CONSTRUCTOR
57
    function PiePlot($data) {
58
	$this->data = array_reverse($data);
59
	$this->title = new Text("");
60
	$this->title->SetFont(FF_FONT1,FS_BOLD);
61
	$this->value = new DisplayValue();
62
	$this->value->Show();
63
	$this->value->SetFormat('%.1f%%');
64
	$this->guideline = new LineProperty();
65
    }
66
 
67
//---------------
68
// PUBLIC METHODS
69
    function SetCenter($x,$y=0.5) {
70
	$this->posx = $x;
71
	$this->posy = $y;
72
    }
73
 
74
    // Enable guideline and set drwaing policy
75
    function SetGuideLines($aFlg=true,$aCurved=true,$aAlways=false) {
76
	$this->guideline->Show($aFlg);
77
	$this->iShowGuideLineForSingle = $aAlways;
78
	$this->iGuideLineCurve = $aCurved;
79
    }
80
 
81
    // Adjuste the distance between labels and labels and pie
82
    function SetGuideLinesAdjust($aVFactor,$aRFactor=0.8) {
83
	$this->iGuideVFactor=$aVFactor;
84
	$this->iGuideLineRFactor=$aRFactor;
85
    }
86
 
87
    function SetColor($aColor) {
88
	$this->color = $aColor;
89
    }
90
 
91
    function SetSliceColors($aColors) {
92
	$this->setslicecolors = $aColors;
93
    }
94
 
95
    function SetShadow($aColor='darkgray',$aDropWidth=4) {
96
	$this->ishadowcolor = $aColor;
97
	$this->ishadowdrop = $aDropWidth;
98
    }
99
 
100
    function SetCSIMTargets($targets,$alts=null) {
101
	$this->csimtargets=array_reverse($targets);
102
	if( is_array($alts) )
103
	    $this->csimalts=array_reverse($alts);
104
    }
105
 
106
    function GetCSIMareas() {
107
	return $this->csimareas;
108
    }
109
 
110
    function AddSliceToCSIM($i,$xc,$yc,$radius,$sa,$ea) {
111
 
112
        //Slice number, ellipse centre (x,y), height, width, start angle, end angle
113
	while( $sa > 2*M_PI ) $sa = $sa - 2*M_PI;
114
	while( $ea > 2*M_PI ) $ea = $ea - 2*M_PI;
115
 
116
	$sa = 2*M_PI - $sa;
117
	$ea = 2*M_PI - $ea;
118
 
119
	// Special case when we have only one slice since then both start and end
120
	// angle will be == 0
121
	if( abs($sa - $ea) < 0.0001 ) {
122
	    $sa=2*M_PI; $ea=0;
123
	}
124
 
125
	//add coordinates of the centre to the map
126
	$xc = floor($xc);$yc=floor($yc);
127
	$coords = "$xc, $yc";
128
 
129
	//add coordinates of the first point on the arc to the map
130
	$xp = floor(($radius*cos($ea))+$xc);
131
	$yp = floor($yc-$radius*sin($ea));
132
	$coords.= ", $xp, $yp";
133
	//add coordinates every 0.2 radians
134
	$a=$ea+0.2;
135
 
136
	// If we cross the 260-limit with a slice we need to handle
137
	// the fact that end angle is smaller than start
138
	if( $sa < $ea ) {
139
	    while ($a <= 2*M_PI) {
140
		$xp = floor($radius*cos($a)+$xc);
141
		$yp = floor($yc-$radius*sin($a));
142
		$coords.= ", $xp, $yp";
143
		$a += 0.2;
144
	    }
145
	    $a -= 2*M_PI;
146
	}
147
 
148
	while ($a < $sa) {
149
	    $xp = floor($radius*cos($a)+$xc);
150
	    $yp = floor($yc-$radius*sin($a));
151
	    $coords.= ", $xp, $yp";
152
	    $a += 0.2;
153
	}
154
 
155
	//Add the last point on the arc
156
	$xp = floor($radius*cos($sa)+$xc);
157
	$yp = floor($yc-$radius*sin($sa));
158
	$coords.= ", $xp, $yp";
159
	if( !empty($this->csimtargets[$i]) ) {
160
	    $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".$this->csimtargets[$i]."\"";
161
	    $tmp="";
162
	    if( !empty($this->csimalts[$i]) ) {
163
		$tmp=sprintf($this->csimalts[$i],$this->data[$i]);
164
		$this->csimareas .= " title=\"$tmp\"";
165
	    }
166
	    $this->csimareas .= " alt=\"$tmp\" />\n";
167
	}
168
    }
169
 
170
 
171
    function SetTheme($aTheme) {
172
	if( in_array($aTheme,array_keys($this->themearr)) )
173
	    $this->theme = $aTheme;
174
	else
175
	    JpGraphError::RaiseL(15001,$aTheme);//("PiePLot::SetTheme() Unknown theme: $aTheme");
176
    }
177
 
178
    function ExplodeSlice($e,$radius=20) {
179
	if( ! is_integer($e) )
180
	    JpGraphError::RaiseL(15002);//('Argument to PiePlot::ExplodeSlice() must be an integer');
181
	$this->explode_radius[$e]=$radius;
182
    }
183
 
184
    function ExplodeAll($radius=20) {
185
	$this->explode_all=true;
186
	$this->explode_r = $radius;
187
    }
188
 
189
    function Explode($aExplodeArr) {
190
	if( !is_array($aExplodeArr) ) {
191
	    JpGraphError::RaiseL(15003);
192
//("Argument to PiePlot::Explode() must be an array with integer distances.");
193
	}
194
	$this->explode_radius = $aExplodeArr;
195
    }
196
 
197
    function SetStartAngle($aStart) {
198
	if( $aStart < 0 || $aStart > 360 ) {
199
	    JpGraphError::RaiseL(15004);//('Slice start angle must be between 0 and 360 degrees.');
200
	}
201
	$this->startangle = 360-$aStart;
202
	$this->startangle *= M_PI/180;
203
    }
204
 
205
    function SetFont($family,$style=FS_NORMAL,$size=10) {
206
	JpGraphError::RaiseL(15005);//('PiePlot::SetFont() is deprecated. Use PiePlot->value->SetFont() instead.');
207
    }
208
 
209
    // Size in percentage
210
    function SetSize($aSize) {
211
	if( ($aSize>0 && $aSize<=0.5) || ($aSize>10 && $aSize<1000) )
212
	    $this->radius = $aSize;
213
	else
214
	    JpGraphError::RaiseL(15006);
215
//("PiePlot::SetSize() Radius for pie must either be specified as a fraction [0, 0.5] of the size of the image or as an absolute size in pixels  in the range [10, 1000]");
216
    }
217
 
218
    function SetFontColor($aColor) {
219
	JpGraphError::RaiseL(15007);
220
//('PiePlot::SetFontColor() is deprecated. Use PiePlot->value->SetColor() instead.');
221
    }
222
 
223
    // Set label arrays
224
    function SetLegends($aLegend) {
225
	$this->legends = $aLegend;
226
    }
227
 
228
    // Set text labels for slices
229
    function SetLabels($aLabels,$aLblPosAdj="auto") {
230
	$this->labels = array_reverse($aLabels);
231
	$this->ilabelposadj=$aLblPosAdj;
232
    }
233
 
234
    function SetLabelPos($aLblPosAdj) {
235
	$this->ilabelposadj=$aLblPosAdj;
236
    }
237
 
238
    // Should we display actual value or percentage?
239
    function SetLabelType($t) {
240
	if( $t < 0 || $t > 2 )
241
	    JpGraphError::RaiseL(15008,$t);
242
//("PiePlot::SetLabelType() Type for pie plots must be 0 or 1 (not $t).");
243
	$this->labeltype=$t;
244
    }
245
 
246
    // Deprecated.
247
    function SetValueType($aType) {
248
	$this->SetLabelType($aType);
249
    }
250
 
251
    // Should the circle around a pie plot be displayed
252
    function ShowBorder($exterior=true,$interior=true) {
253
	$this->pie_border = $exterior;
254
	$this->pie_interior_border = $interior;
255
    }
256
 
257
    // Setup the legends (Framework method)
258
    function Legend(&$graph) {
259
	$colors = array_keys($graph->img->rgb->rgb_table);
260
   	sort($colors);
261
   	$ta=$this->themearr[$this->theme];
262
   	$n = count($this->data);
263
 
264
   	if( $this->setslicecolors==null ) {
265
	    $numcolors=count($ta);
266
	    if( is_a($this,'PiePlot3D') ) {
267
		$ta = array_reverse(array_slice($ta,0,$n));
268
	    }
269
	}
270
   	else {
271
	    $this->setslicecolors = array_slice($this->setslicecolors,0,$n);
272
	    $numcolors=count($this->setslicecolors);
273
	    if( $graph->pieaa && !is_a($this,'PiePlot3D') ) {
274
		$this->setslicecolors = array_reverse($this->setslicecolors);
275
	    }
276
	}
277
 
278
	$sum=0;
279
	for($i=0; $i < $n; ++$i)
280
	    $sum += $this->data[$i];
281
 
282
	// Bail out with error if the sum is 0
283
	if( $sum==0 )
284
	    JpGraphError::RaiseL(15009);//("Illegal pie plot. Sum of all data is zero for Pie!");
285
 
286
	// Make sure we don't plot more values than data points
287
	// (in case the user added more legends than data points)
288
	$n = min(count($this->legends),count($this->data));
289
	if( $this->legends != "" ) {
290
	    $this->legends = array_reverse(array_slice($this->legends,0,$n));
291
	}
292
	for( $i=$n-1; $i >= 0; --$i ) {
293
	    $l = $this->legends[$i];
294
	    // Replace possible format with actual values
295
	    if( count($this->csimalts) > $i ) {
296
		$fmt = $this->csimalts[$i];
297
	    }
298
	    else {
299
		$fmt = "%d"; // Deafult Alt if no other has been specified
300
	    }
301
	    if( $this->labeltype==0 ) {
302
		$l = sprintf($l,100*$this->data[$i]/$sum);
303
		$alt = sprintf($fmt,$this->data[$i]);
304
 
305
	    }
306
	    elseif( $this->labeltype == 1)  {
307
		$l = sprintf($l,$this->data[$i]);
308
		$alt = sprintf($fmt,$this->data[$i]);
309
 
310
	    }
311
	    else {
312
		$l = sprintf($l,$this->adjusted_data[$i]);
313
		$alt = sprintf($fmt,$this->adjusted_data[$i]);
314
	    }
315
 
316
	    if( $this->setslicecolors==null ) {
317
		$graph->legend->Add($l,$colors[$ta[$i%$numcolors]],"",0,$this->csimtargets[$i],$alt);
318
	    }
319
	    else {
320
		$graph->legend->Add($l,$this->setslicecolors[$i%$numcolors],"",0,$this->csimtargets[$i],$alt);
321
	    }
322
	}
323
    }
324
 
325
    // Adjust the rounded percetage value so that the sum of
326
    // of the pie slices are always 100%
327
    // Using the Hare/Niemeyer method
328
    function AdjPercentage($aData,$aPrec=0) {
329
	$mul=100;
330
	if( $aPrec > 0 && $aPrec < 3 ) {
331
	    if( $aPrec == 1 )
332
		$mul=1000;
333
		else
334
		    $mul=10000;
335
	}
336
 
337
	$tmp = array();
338
	$result = array();
339
	$quote_sum=0;
340
	$n = count($aData) ;
341
	for( $i=0, $sum=0; $i < $n; ++$i )
342
	    $sum+=$aData[$i];
343
	foreach($aData as $index => $value) {
344
	    $tmp_percentage=$value/$sum*$mul;
345
	    $result[$index]=floor($tmp_percentage);
346
	    $tmp[$index]=$tmp_percentage-$result[$index];
347
	    $quote_sum+=$result[$index];
348
	}
349
	if( $quote_sum == $mul) {
350
	    if( $mul > 100 ) {
351
		$tmp = $mul / 100;
352
		for( $i=0; $i < $n; ++$i ) {
353
		    $result[$i] /= $tmp ;
354
		}
355
	    }
356
	    return $result;
357
	}
358
	arsort($tmp,SORT_NUMERIC);
359
	reset($tmp);
360
	for($i=0; $i < $mul-$quote_sum; $i++)
361
	{
362
	    $result[key($tmp)]++;
363
	    next($tmp);
364
	}
365
	if( $mul > 100 ) {
366
	    $tmp = $mul / 100;
367
	    for( $i=0; $i < $n; ++$i ) {
368
		$result[$i] /= $tmp ;
369
	    }
370
	}
371
	return $result;
372
    }
373
 
374
 
375
    function Stroke(&$img,$aaoption=0) {
376
	// aaoption is used to handle antialias
377
	// aaoption == 0 a normal pie
378
	// aaoption == 1 just the body
379
	// aaoption == 2 just the values
380
 
381
	// Explode scaling. If anti anti alias we scale the image
382
	// twice and we also need to scale the exploding distance
383
	$expscale = $aaoption === 1 ? 2 : 1;
384
 
385
	if( $this->labeltype == 2 ) {
386
	    // Adjust the data so that it will add up to 100%
387
	    $this->adjusted_data = $this->AdjPercentage($this->data);
388
	}
389
 
390
	$colors = array_keys($img->rgb->rgb_table);
391
   	sort($colors);
392
   	$ta=$this->themearr[$this->theme];
393
	$n = count($this->data);
394
 
395
   	if( $this->setslicecolors==null ) {
396
	    $numcolors=count($ta);
397
	}
398
   	else {
399
	    $this->setslicecolors = array_reverse(array_slice($this->setslicecolors,0,$n));
400
	    $numcolors=count($this->setslicecolors);
401
	    $tt = array_slice($this->setslicecolors,$n % $numcolors);
402
	    $tt2 = array_slice($this->setslicecolors,0,$n % $numcolors);
403
	    $tt2 = array_merge($tt, $tt2);
404
	    $this->setslicecolors = $tt + $tt2;
405
	}
406
 
407
	// Draw the slices
408
	$sum=0;
409
	for($i=0; $i < $n; ++$i)
410
	    $sum += $this->data[$i];
411
 
412
	// Bail out with error if the sum is 0
413
	if( $sum==0 )
414
	    JpGraphError::RaiseL(15009);//("Sum of all data is 0 for Pie.");
415
 
416
	// Set up the pie-circle
417
	if( $this->radius <= 1 )
418
	    $radius = floor($this->radius*min($img->width,$img->height));
419
	else {
420
	    $radius = $aaoption === 1 ? $this->radius*2 : $this->radius;
421
	}
422
 
423
	if( $this->posx <= 1 && $this->posx > 0 )
424
	    $xc = round($this->posx*$img->width);
425
	else
426
	    $xc = $this->posx ;
427
 
428
	if( $this->posy <= 1 && $this->posy > 0 )
429
	    $yc = round($this->posy*$img->height);
430
	else
431
	    $yc = $this->posy ;
432
 
433
	$n = count($this->data);
434
 
435
	if( $this->explode_all )
436
	    for($i=0; $i < $n; ++$i)
437
		$this->explode_radius[$i]=$this->explode_r;
438
 
439
	if( $this->ishadowcolor != "" && $aaoption !== 2) {
440
	    $accsum=0;
441
	    $angle2 = $this->startangle;
442
	    $img->SetColor($this->ishadowcolor);
443
	    for($i=0; $sum > 0 && $i < $n; ++$i) {
444
		$j = $n-$i-1;
445
		$d = $this->data[$i];
446
		$angle1 = $angle2;
447
		$accsum += $d;
448
		$angle2 = $this->startangle+2*M_PI*$accsum/$sum;
449
		if( empty($this->explode_radius[$j]) )
450
		    $this->explode_radius[$j]=0;
451
 
452
		if( $d < 0.00001 ) continue;
453
 
454
		$la = 2*M_PI - (abs($angle2-$angle1)/2.0+$angle1);
455
 
456
		$xcm = $xc + $this->explode_radius[$j]*cos($la)*$expscale;
457
		$ycm = $yc - $this->explode_radius[$j]*sin($la)*$expscale;
458
 
459
		$xcm += $this->ishadowdrop*$expscale;
460
		$ycm += $this->ishadowdrop*$expscale;
461
 
462
		$img->CakeSlice($xcm,$ycm,$radius,$radius,$angle1*180/M_PI,$angle2*180/M_PI,$this->ishadowcolor);
463
 
464
	    }
465
	}
466
 
467
	$accsum=0;
468
	$angle2 = $this->startangle;
469
	$img->SetColor($this->color);
470
	for($i=0; $sum>0 && $i < $n; ++$i) {
471
	    $j = $n-$i-1;
472
	    if( empty($this->explode_radius[$j]) )
473
		$this->explode_radius[$j]=0;
474
	    $d = $this->data[$i];
475
	    $angle1 = $angle2;
476
	    $accsum += $d;
477
	    $angle2 = $this->startangle+2*M_PI*$accsum/$sum;
478
	    $this->la[$i] = 2*M_PI - (abs($angle2-$angle1)/2.0+$angle1);
479
 
480
	    if( $d < 0.00001 ) continue;
481
 
482
	    if( $this->setslicecolors==null )
483
		$slicecolor=$colors[$ta[$i%$numcolors]];
484
	    else
485
		$slicecolor=$this->setslicecolors[$i%$numcolors];
486
 
487
	    if( $this->pie_interior_border && $aaoption===0 )
488
		$img->SetColor($this->color);
489
	    else
490
		$img->SetColor($slicecolor);
491
 
492
	    $arccolor = $this->pie_border && $aaoption===0 ? $this->color : "";
493
 
494
	    $xcm = $xc + $this->explode_radius[$j]*cos($this->la[$i])*$expscale;
495
	    $ycm = $yc - $this->explode_radius[$j]*sin($this->la[$i])*$expscale;
496
 
497
	    if( $aaoption !== 2 ) {
498
		$img->CakeSlice($xcm,$ycm,$radius-1,$radius-1,
499
				$angle1*180/M_PI,$angle2*180/M_PI,$slicecolor,$arccolor);
500
	    }
501
 
502
	    if( $this->csimtargets && $aaoption !== 1 ) {
503
		$this->AddSliceToCSIM($i,$xcm,$ycm,$radius,$angle1,$angle2);
504
	    }
505
	}
506
 
507
	// Format the titles for each slice
508
	if( $aaoption!==2) {
509
	    for( $i=0; $i < $n; ++$i) {
510
		if( $this->labeltype==0 ) {
511
		    if( $sum != 0 )
512
			$l = 100.0*$this->data[$i]/$sum;
513
		    else
514
			$l = 0.0;
515
		}
516
		elseif( $this->labeltype==1 ) {
517
		    $l = $this->data[$i]*1.0;
518
		}
519
		else {
520
		    $l = $this->adjusted_data[$i];
521
		}
522
		if( isset($this->labels[$i]) && is_string($this->labels[$i]) )
523
		    $this->labels[$i]=sprintf($this->labels[$i],$l);
524
		else
525
		    $this->labels[$i]=$l;
526
	    }
527
	}
528
 
529
	If( $this->value->show && $aaoption !== 1 ) {
530
	    $this->StrokeAllLabels($img,$xc,$yc,$radius);
531
	}
532
 
533
	// Adjust title position
534
	if( $aaoption !== 1 ) {
535
	    $this->title->Pos($xc,
536
			  $yc-$this->title->GetFontHeight($img)-$radius-$this->title->margin,
537
			  "center","bottom");
538
	    $this->title->Stroke($img);
539
	}
540
 
541
    }
542
 
543
//---------------
544
// PRIVATE METHODS
545
 
546
    function NormAngle($a) {
547
	while( $a < 0 ) $a += 2*M_PI;
548
	while( $a > 2*M_PI ) $a -= 2*M_PI;
549
	return $a;
550
    }
551
 
552
    function Quadrant($a) {
553
	$a=$this->NormAngle($a);
554
	if( $a > 0 && $a <= M_PI/2 )
555
	    return 0;
556
	if( $a > M_PI/2 && $a <= M_PI )
557
	    return 1;
558
	if( $a > M_PI && $a <= 1.5*M_PI )
559
	    return 2;
560
	if( $a > 1.5*M_PI )
561
	    return 3;
562
    }
563
 
564
    function StrokeGuideLabels(&$img,$xc,$yc,$radius) {
565
	$n = count($this->labels);
566
 
567
	//-----------------------------------------------------------------------
568
	// Step 1 of the algorithm is to construct a number of clusters
569
	// a cluster is defined as all slices within the same quadrant (almost)
570
	// that has an angualr distance less than the treshold
571
	//-----------------------------------------------------------------------
572
	$tresh_hold=25 * M_PI/180; // 25 degrees difference to be in a cluster
573
	$incluster=false;	// flag if we are currently in a cluster or not
574
	$clusters = array();	// array of clusters
575
	$cidx=-1;		// running cluster index
576
 
577
	// Go through all the labels and construct a number of clusters
578
	for($i=0; $i < $n-1; ++$i) {
579
	    // Calc the angle distance between two consecutive slices
580
	    $a1=$this->la[$i];
581
	    $a2=$this->la[$i+1];
582
	    $q1 = $this->Quadrant($a1);
583
	    $q2 = $this->Quadrant($a2);
584
	    $diff = abs($a1-$a2);
585
	    if( $diff < $tresh_hold ) {
586
		if( $incluster ) {
587
		    $clusters[$cidx][1]++;
588
		    // Each cluster can only cover one quadrant
589
		    // Do we cross a quadrant ( and must break the cluster)
590
		    if( $q1 !=  $q2 ) {
591
			// If we cross a quadrant boundary we normally start a
592
			// new cluster. However we need to take the 12'a clock
593
			// and 6'a clock positions into a special consideration.
594
			// Case 1: WE go from q=1 to q=2 if the last slice on
595
			// the cluster for q=1 is close to 12'a clock and the
596
			// first slice in q=0 is small we extend the previous
597
			// cluster
598
			if( $q1 == 1 && $q2 == 0 && $a2 > (90-15)*M_PI/180 ) {
599
			    if( $i < $n-2 ) {
600
				$a3 = $this->la[$i+2];
601
				// If there isn't a cluster coming up with the next-next slice
602
				// we extend the previous cluster to cover this slice as well
603
				if( abs($a3-$a2) >= $tresh_hold ) {
604
				    $clusters[$cidx][1]++;
605
				    $i++;
606
				}
607
			    }
608
			}
609
			elseif( $q1 == 3 && $q2 == 2 && $a2 > (270-15)*M_PI/180 ) {
610
			    if( $i < $n-2 ) {
611
				$a3 = $this->la[$i+2];
612
				// If there isn't a cluster coming up with the next-next slice
613
				// we extend the previous cluster to cover this slice as well
614
				if( abs($a3-$a2) >= $tresh_hold ) {
615
				    $clusters[$cidx][1]++;
616
				    $i++;
617
				}
618
			    }
619
			}
620
 
621
			if( $q1==2 && $q2==1 && $a2 > (180-15)*M_PI/180 ) {
622
			    $clusters[$cidx][1]++;
623
			    $i++;
624
			}
625
 
626
			$incluster = false;
627
		    }
628
		}
629
		elseif( $q1 == $q2)  {
630
		    $incluster = true;
631
		    // Now we have a special case for quadrant 0. If we previously
632
		    // have a cluster of one in quadrant 0 we just extend that
633
		    // cluster. If we don't do this then we risk that the label
634
		    // for the cluster of one will cross the guide-line
635
		    if( $q1 == 0 && $cidx > -1 &&
636
			$clusters[$cidx][1] == 1 &&
637
			$this->Quadrant($this->la[$clusters[$cidx][0]]) == 0 ) {
638
			$clusters[$cidx][1]++;
639
		    }
640
		    else {
641
			$cidx++;
642
			$clusters[$cidx][0] = $i;
643
			$clusters[$cidx][1] = 1;
644
		    }
645
		}
646
		else {
647
		    // Create a "cluster" of one since we are just crossing
648
		    // a quadrant
649
		    $cidx++;
650
		    $clusters[$cidx][0] = $i;
651
		    $clusters[$cidx][1] = 1;
652
		}
653
	    }
654
	    else {
655
		if( $incluster ) {
656
		    // Add the last slice
657
		    $clusters[$cidx][1]++;
658
		    $incluster = false;
659
		}
660
		else { // Create a "cluster" of one
661
		    $cidx++;
662
		    $clusters[$cidx][0] = $i;
663
		    $clusters[$cidx][1] = 1;
664
		}
665
	    }
666
	}
667
	// Handle the very last slice
668
	if( $incluster ) {
669
	    $clusters[$cidx][1]++;
670
	}
671
	else { // Create a "cluster" of one
672
	    $cidx++;
673
	    $clusters[$cidx][0] = $i;
674
	    $clusters[$cidx][1] = 1;
675
	}
676
 
677
	/*
678
	if( true ) {
679
	    // Debug printout in labels
680
	    for( $i=0; $i <= $cidx; ++$i ) {
681
		for( $j=0; $j < $clusters[$i][1]; ++$j ) {
682
		    $a = $this->la[$clusters[$i][0]+$j];
683
		    $aa = round($a*180/M_PI);
684
		    $q = $this->Quadrant($a);
685
		    $this->labels[$clusters[$i][0]+$j]="[$q:$aa] $i:$j";
686
		}
687
	    }
688
	}
689
	*/
690
 
691
	//-----------------------------------------------------------------------
692
	// Step 2 of the algorithm is use the clusters and draw the labels
693
	// and guidelines
694
	//-----------------------------------------------------------------------
695
 
696
	// We use the font height as the base factor for how far we need to
697
	// spread the labels in the Y-direction.
698
	$img->SetFont($this->value->ff,$this->value->fs,$this->value->fsize);
699
	$fh = $img->GetFontHeight();
700
	$origvstep=$fh*$this->iGuideVFactor;
701
	$this->value->SetMargin(0);
702
 
703
	// Number of clusters found
704
	$nc = count($clusters);
705
 
706
	// Walk through all the clusters
707
	for($i=0; $i < $nc; ++$i) {
708
 
709
	    // Start angle and number of slices in this cluster
710
	    $csize = $clusters[$i][1];
711
	    $a = $this->la[$clusters[$i][0]];
712
	    $q = $this->Quadrant($a);
713
 
714
	    // Now set up the start and end conditions to make sure that
715
	    // in each cluster we walk through the all the slices starting with the slice
716
	    // closest to the equator. Since all slices are numbered clockwise from "3'a clock"
717
	    // we have different conditions depending on in which quadrant the slice lies within.
718
	    if( $q == 0 ) {
719
		$start = $csize-1; $idx = $start; $step = -1; $vstep = -$origvstep;
720
	    }
721
	    elseif( $q == 1 ) {
722
		$start = 0; $idx = $start; $step = 1; $vstep = -$origvstep;
723
	    }
724
	    elseif( $q == 2 ) {
725
		$start = $csize-1; $idx = $start; $step = -1; $vstep = $origvstep;
726
	    }
727
	    elseif( $q == 3 ) {
728
		$start = 0; $idx = $start; $step = 1; $vstep = $origvstep;
729
	    }
730
 
731
	    // Walk through all slices within this cluster
732
	    for($j=0; $j < $csize; ++$j) {
733
		// Now adjust the position of the labels in each cluster starting
734
		// with the slice that is closest to the equator of the pie
735
		$a = $this->la[$clusters[$i][0]+$idx];
736
 
737
		// Guide line start in the center of the arc of the slice
738
		$r = $radius+$this->explode_radius[$n-1-($clusters[$i][0]+$idx)];
739
		$x = round($r*cos($a)+$xc);
740
		$y = round($yc-$r*sin($a));
741
 
742
		// The distance from the arc depends on chosen font and the "R-Factor"
743
		$r += $fh*$this->iGuideLineRFactor;
744
 
745
		// Should the labels be placed curved along the pie or in straight columns
746
		// outside the pie?
747
		if( $this->iGuideLineCurve )
748
		    $xt=round($r*cos($a)+$xc);
749
 
750
		// If this is the first slice in the cluster we need some first time
751
		// proessing
752
		if( $idx == $start ) {
753
		    if( ! $this->iGuideLineCurve )
754
			$xt=round($r*cos($a)+$xc);
755
		    $yt=round($yc-$r*sin($a));
756
 
757
		    // Some special consideration in case this cluster starts
758
		    // in quadrant 1 or 3 very close to the "equator" (< 20 degrees)
759
		    // and the previous clusters last slice is within the tolerance.
760
		    // In that case we add a font height to this labels Y-position
761
		    // so it doesn't collide with
762
		    // the slice in the previous cluster
763
		    $prevcluster = ($i + ($nc-1) ) % $nc;
764
		    $previdx=$clusters[$prevcluster][0]+$clusters[$prevcluster][1]-1;
765
		    if( $q == 1 && $a > 160*M_PI/180 ) {
766
			// Get the angle for the previous clusters last slice
767
			$diff = abs($a-$this->la[$previdx]);
768
			 if( $diff < $tresh_hold ) {
769
			     $yt -= $fh;
770
			 }
771
		    }
772
		    elseif( $q == 3 && $a > 340*M_PI/180 ) {
773
			// We need to subtract 360 to compare angle distance between
774
			// q=0 and q=3
775
			$diff = abs($a-$this->la[$previdx]-360*M_PI/180);
776
			if( $diff < $tresh_hold ) {
777
			     $yt += $fh;
778
			}
779
		    }
780
 
781
		}
782
		else {
783
		    // The step is at minimum $vstep but if the slices are relatively large
784
		    // we make sure that we add at least a step that corresponds to the vertical
785
		    // distance between the centers at the arc on the slice
786
		    $prev_a = $this->la[$clusters[$i][0]+($idx-$step)];
787
		    $dy = abs($radius*(sin($a)-sin($prev_a))*1.2);
788
		    if( $vstep > 0 )
789
			$yt += max($vstep,$dy);
790
		    else
791
			$yt += min($vstep,-$dy);
792
		}
793
 
794
		$label = $this->labels[$clusters[$i][0]+$idx];
795
 
796
		if( $csize == 1 ) {
797
		    // A "meta" cluster with only one slice
798
		    $r = $radius+$this->explode_radius[$n-1-($clusters[$i][0]+$idx)];
799
		    $rr = $r+$img->GetFontHeight()/2;
800
		    $xt=round($rr*cos($a)+$xc);
801
		    $yt=round($yc-$rr*sin($a));
802
		    $this->StrokeLabel($label,$img,$xc,$yc,$a,$r);
803
		    if( $this->iShowGuideLineForSingle )
804
			$this->guideline->Stroke($img,$x,$y,$xt,$yt);
805
		}
806
		else {
807
		    $this->guideline->Stroke($img,$x,$y,$xt,$yt);
808
		    if( $q==1 || $q==2 ) {
809
			// Left side of Pie
810
			$this->guideline->Stroke($img,$xt,$yt,$xt-$this->guidelinemargin,$yt);
811
			$lbladj = -$this->guidelinemargin-5;
812
			$this->value->halign = "right";
813
			$this->value->valign = "center";
814
		    }
815
		    else {
816
			// Right side of pie
817
			$this->guideline->Stroke($img,$xt,$yt,$xt+$this->guidelinemargin,$yt);
818
			$lbladj = $this->guidelinemargin+5;
819
			$this->value->halign = "left";
820
			$this->value->valign = "center";
821
		    }
822
		    $this->value->Stroke($img,$label,$xt+$lbladj,$yt);
823
		}
824
 
825
		// Udate idx to point to next slice in the cluster to process
826
		$idx += $step;
827
	    }
828
	}
829
    }
830
 
831
    function StrokeAllLabels(&$img,$xc,$yc,$radius) {
832
	// First normalize all angles for labels
833
	$n = count($this->la);
834
	for($i=0; $i < $n; ++$i) {
835
	    $this->la[$i] = $this->NormAngle($this->la[$i]);
836
	}
837
	if( $this->guideline->iShow ) {
838
	    $this->StrokeGuideLabels($img,$xc,$yc,$radius);
839
	}
840
	else {
841
	    $n = count($this->labels);
842
	    for($i=0; $i < $n; ++$i) {
843
		$this->StrokeLabel($this->labels[$i],$img,$xc,$yc,
844
				   $this->la[$i],
845
				   $radius + $this->explode_radius[$n-1-$i]);
846
	    }
847
	}
848
    }
849
 
850
    // Position the labels of each slice
851
    function StrokeLabel($label,&$img,$xc,$yc,$a,$radius) {
852
 
853
	// Default value
854
	if( $this->ilabelposadj === 'auto' )
855
	    $this->ilabelposadj = 0.65;
856
	$r = $radius;
857
 
858
	// We position the values diferently depending on if they are inside
859
	// or outside the pie
860
	if( $this->ilabelposadj < 1.0 ) {
861
 
862
	    $this->value->SetAlign('center','center');
863
	    $this->value->margin = 0;
864
 
865
	    $xt=round($this->ilabelposadj*$r*cos($a)+$xc);
866
	    $yt=round($yc-$this->ilabelposadj*$r*sin($a));
867
 
868
	    $this->value->Stroke($img,$label,$xt,$yt);
869
	}
870
	else {
871
 
872
	    $this->value->halign = "left";
873
	    $this->value->valign = "top";
874
	    $this->value->margin = 0;
875
 
876
	    // Position the axis title.
877
	    // dx, dy is the offset from the top left corner of the bounding box that sorrounds the text
878
	    // that intersects with the extension of the corresponding axis. The code looks a little
879
	    // bit messy but this is really the only way of having a reasonable position of the
880
	    // axis titles.
881
	    $img->SetFont($this->value->ff,$this->value->fs,$this->value->fsize);
882
	    $h=$img->GetTextHeight($label);
883
	    // For numeric values the format of the display value
884
	    // must be taken into account
885
	    if( is_numeric($label) ) {
886
		if( $label > 0 )
887
		    $w=$img->GetTextWidth(sprintf($this->value->format,$label));
888
		else
889
		    $w=$img->GetTextWidth(sprintf($this->value->negformat,$label));
890
	    }
891
	    else
892
		$w=$img->GetTextWidth($label);
893
	    if( $this->ilabelposadj > 1.0 && $this->ilabelposadj < 5.0) {
894
		$r *= $this->ilabelposadj;
895
	    }
896
 
897
	    $r += $img->GetFontHeight()/1.5;
898
 
899
	    $xt=round($r*cos($a)+$xc);
900
	    $yt=round($yc-$r*sin($a));
901
 
902
	    // Normalize angle
903
	    while( $a < 0 ) $a += 2*M_PI;
904
	    while( $a > 2*M_PI ) $a -= 2*M_PI;
905
 
906
	    if( $a>=7*M_PI/4 || $a <= M_PI/4 ) $dx=0;
907
	    if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dx=($a-M_PI/4)*2/M_PI;
908
	    if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dx=1;
909
	    if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dx=(1-($a-M_PI*5/4)*2/M_PI);
910
 
911
	    if( $a>=7*M_PI/4 ) $dy=(($a-M_PI)-3*M_PI/4)*2/M_PI;
912
	    if( $a<=M_PI/4 ) $dy=(1-$a*2/M_PI);
913
	    if( $a>=M_PI/4 && $a <= 3*M_PI/4 ) $dy=1;
914
	    if( $a>=3*M_PI/4 && $a <= 5*M_PI/4 ) $dy=(1-($a-3*M_PI/4)*2/M_PI);
915
	    if( $a>=5*M_PI/4 && $a <= 7*M_PI/4 ) $dy=0;
916
 
917
	    $this->value->Stroke($img,$label,$xt-$dx*$w,$yt-$dy*$h);
918
	}
919
    }
920
} // Class
921
 
922
 
923
//===================================================
924
// CLASS PiePlotC
925
// Description: Same as a normal pie plot but with a
926
// filled circle in the center
927
//===================================================
928
class PiePlotC extends PiePlot {
929
    var $imidsize=0.5;		// Fraction of total width
930
    var $imidcolor='white';
931
    var $midtitle='';
932
    var $middlecsimtarget="",$middlecsimalt="";
933
 
934
    function PiePlotC($data,$aCenterTitle='') {
935
	parent::PiePlot($data);
936
	$this->midtitle = new Text();
937
	$this->midtitle->ParagraphAlign('center');
938
    }
939
 
940
    function SetMid($aTitle,$aColor='white',$aSize=0.5) {
941
	$this->midtitle->Set($aTitle);
942
 
943
	$this->imidsize = $aSize ;
944
	$this->imidcolor = $aColor ;
945
    }
946
 
947
    function SetMidTitle($aTitle) {
948
	$this->midtitle->Set($aTitle);
949
    }
950
 
951
    function SetMidSize($aSize) {
952
	$this->imidsize = $aSize ;
953
    }
954
 
955
    function SetMidColor($aColor) {
956
	$this->imidcolor = $aColor ;
957
    }
958
 
959
    function SetMidCSIM($aTarget,$aAlt) {
960
	$this->middlecsimtarget = $aTarget;
961
	$this->middlecsimalt = $aAlt;
962
    }
963
 
964
    function AddSliceToCSIM($i,$xc,$yc,$radius,$sa,$ea) {
965
 
966
        //Slice number, ellipse centre (x,y), radius, start angle, end angle
967
	while( $sa > 2*M_PI ) $sa = $sa - 2*M_PI;
968
	while( $ea > 2*M_PI ) $ea = $ea - 2*M_PI;
969
 
970
	$sa = 2*M_PI - $sa;
971
	$ea = 2*M_PI - $ea;
972
 
973
	// Special case when we have only one slice since then both start and end
974
	// angle will be == 0
975
	if( abs($sa - $ea) < 0.0001 ) {
976
	    $sa=2*M_PI; $ea=0;
977
	}
978
 
979
	// Add inner circle first point
980
	$xp = floor(($this->imidsize*$radius*cos($ea))+$xc);
981
	$yp = floor($yc-($this->imidsize*$radius*sin($ea)));
982
	$coords = "$xp, $yp";
983
 
984
	//add coordinates every 0.25 radians
985
	$a=$ea+0.25;
986
 
987
	// If we cross the 260-limit with a slice we need to handle
988
	// the fact that end angle is smaller than start
989
	if( $sa < $ea ) {
990
	    while ($a <= 2*M_PI) {
991
		$xp = floor($radius*cos($a)+$xc);
992
		$yp = floor($yc-$radius*sin($a));
993
		$coords.= ", $xp, $yp";
994
		$a += 0.25;
995
	    }
996
	    $a -= 2*M_PI;
997
	}
998
 
999
	while ($a < $sa) {
1000
	    $xp = floor(($this->imidsize*$radius*cos($a)+$xc));
1001
	    $yp = floor($yc-($this->imidsize*$radius*sin($a)));
1002
	    $coords.= ", $xp, $yp";
1003
	    $a += 0.25;
1004
	}
1005
 
1006
	// Make sure we end at the last point
1007
	$xp = floor(($this->imidsize*$radius*cos($sa)+$xc));
1008
	$yp = floor($yc-($this->imidsize*$radius*sin($sa)));
1009
	$coords.= ", $xp, $yp";
1010
 
1011
	// Straight line to outer circle
1012
	$xp = floor($radius*cos($sa)+$xc);
1013
	$yp = floor($yc-$radius*sin($sa));
1014
	$coords.= ", $xp, $yp";
1015
 
1016
	//add coordinates every 0.25 radians
1017
	$a=$sa - 0.25;
1018
	while ($a > $ea) {
1019
	    $xp = floor($radius*cos($a)+$xc);
1020
	    $yp = floor($yc-$radius*sin($a));
1021
	    $coords.= ", $xp, $yp";
1022
	    $a -= 0.25;
1023
	}
1024
 
1025
	//Add the last point on the arc
1026
	$xp = floor($radius*cos($ea)+$xc);
1027
	$yp = floor($yc-$radius*sin($ea));
1028
	$coords.= ", $xp, $yp";
1029
 
1030
	// Close the arc
1031
	$xp = floor(($this->imidsize*$radius*cos($ea))+$xc);
1032
	$yp = floor($yc-($this->imidsize*$radius*sin($ea)));
1033
	$coords .= ", $xp, $yp";
1034
 
1035
	if( !empty($this->csimtargets[$i]) ) {
1036
	    $this->csimareas .= "<area shape=\"poly\" coords=\"$coords\" href=\"".
1037
		$this->csimtargets[$i]."\"";
1038
	    if( !empty($this->csimalts[$i]) ) {
1039
		$tmp=sprintf($this->csimalts[$i],$this->data[$i]);
1040
		$this->csimareas .= " title=\"$tmp\"";
1041
	    }
1042
	    $this->csimareas .= " alt=\"$tmp\" />\n";
1043
	}
1044
    }
1045
 
1046
 
1047
    function Stroke(&$img,$aaoption=0) {
1048
 
1049
	// Stroke the pie but don't stroke values
1050
	$tmp =  $this->value->show;
1051
	$this->value->show = false;
1052
	parent::Stroke($img,$aaoption);
1053
	$this->value->show = $tmp;
1054
 
1055
 	$xc = round($this->posx*$img->width);
1056
	$yc = round($this->posy*$img->height);
1057
 
1058
	$radius = floor($this->radius * min($img->width,$img->height)) ;
1059
 
1060
 
1061
	if( $this->imidsize > 0 && $aaoption !== 2 ) {
1062
 
1063
	    if( $this->ishadowcolor != "" ) {
1064
		$img->SetColor($this->ishadowcolor);
1065
		$img->FilledCircle($xc+$this->ishadowdrop,$yc+$this->ishadowdrop,
1066
				   round($radius*$this->imidsize));
1067
	    }
1068
 
1069
	    $img->SetColor($this->imidcolor);
1070
	    $img->FilledCircle($xc,$yc,round($radius*$this->imidsize));
1071
 
1072
	    if(  $this->pie_border && $aaoption === 0 ) {
1073
		$img->SetColor($this->color);
1074
		$img->Circle($xc,$yc,round($radius*$this->imidsize));
1075
	    }
1076
 
1077
	    if( !empty($this->middlecsimtarget) )
1078
		$this->AddMiddleCSIM($xc,$yc,round($radius*$this->imidsize));
1079
 
1080
	}
1081
 
1082
	if( $this->value->show && $aaoption !== 1) {
1083
	    $this->StrokeAllLabels($img,$xc,$yc,$radius);
1084
	    $this->midtitle->Pos($xc,$yc,'center','center');
1085
	    $this->midtitle->Stroke($img);
1086
	}
1087
 
1088
    }
1089
 
1090
    function AddMiddleCSIM($xc,$yc,$r) {
1091
	$xc=round($xc);$yc=round($yc);$r=round($r);
1092
	$this->csimareas .= "<area shape=\"circle\" coords=\"$xc,$yc,$r\" href=\"".
1093
	    $this->middlecsimtarget."\"";
1094
	if( !empty($this->middlecsimalt) ) {
1095
	    $tmp = $this->middlecsimalt;
1096
	    $this->csimareas .= " title=\"$tmp\"";
1097
	}
1098
	$this->csimareas .= " alt=\"$tmp\" />\n";
1099
    }
1100
 
1101
    function StrokeLabel($label,$img,$xc,$yc,$a,$r) {
1102
 
1103
	if( $this->ilabelposadj === 'auto' )
1104
	    $this->ilabelposadj = (1-$this->imidsize)/2+$this->imidsize;
1105
 
1106
	parent::StrokeLabel($label,$img,$xc,$yc,$a,$r);
1107
 
1108
    }
1109
 
1110
}
1111
 
1112
 
1113
//===================================================
1114
// CLASS PieGraph
1115
// Description:
1116
//===================================================
1117
class PieGraph extends Graph {
1118
    var $posx, $posy, $radius;
1119
    var $legends=array();
1120
    var $plots=array();
1121
    var $pieaa = false ;
1122
//---------------
1123
// CONSTRUCTOR
1124
    function PieGraph($width=300,$height=200,$cachedName="",$timeout=0,$inline=1) {
1125
	$this->Graph($width,$height,$cachedName,$timeout,$inline);
1126
	$this->posx=$width/2;
1127
	$this->posy=$height/2;
1128
	$this->SetColor(array(255,255,255));
1129
    }
1130
 
1131
//---------------
1132
// PUBLIC METHODS
1133
    function Add($aObj) {
1134
 
1135
	if( is_array($aObj) && count($aObj) > 0 )
1136
	    $cl = $aObj[0];
1137
	else
1138
	    $cl = $aObj;
1139
 
1140
	if( is_a($cl,'Text') )
1141
	    $this->AddText($aObj);
1142
	elseif( is_a($cl,'IconPlot') )
1143
	    $this->AddIcon($aObj);
1144
	else {
1145
	    if( is_array($aObj) ) {
1146
		$n = count($aObj);
1147
		for($i=0; $i < $n; ++$i ) {
1148
		    $this->plots[] = $aObj[$i];
1149
		}
1150
	    }
1151
	    else {
1152
		$this->plots[] = $aObj;
1153
	    }
1154
	}
1155
    }
1156
 
1157
    function SetAntiAliasing($aFlg=true) {
1158
	$this->pieaa = $aFlg;
1159
    }
1160
 
1161
    function SetColor($c) {
1162
	$this->SetMarginColor($c);
1163
    }
1164
 
1165
    function DisplayCSIMAreas() {
1166
	    $csim="";
1167
	    foreach($this->plots as $p ) {
1168
		$csim .= $p->GetCSIMareas();
1169
	    }
1170
	    //$csim.= $this->legend->GetCSIMareas();
1171
	    if (preg_match_all("/area shape=\"(\w+)\" coords=\"([0-9\, ]+)\"/", $csim, $coords)) {
1172
		$this->img->SetColor($this->csimcolor);
1173
		$n = count($coords[0]);
1174
		for ($i=0; $i < $n; $i++) {
1175
		    if ($coords[1][$i]=="poly") {
1176
			preg_match_all('/\s*([0-9]+)\s*,\s*([0-9]+)\s*,*/',$coords[2][$i],$pts);
1177
			$this->img->SetStartPoint($pts[1][count($pts[0])-1],$pts[2][count($pts[0])-1]);
1178
			$m = count($pts[0]);
1179
			for ($j=0; $j < $m; $j++) {
1180
			    $this->img->LineTo($pts[1][$j],$pts[2][$j]);
1181
			}
1182
		    } else if ($coords[1][$i]=="rect") {
1183
			$pts = preg_split('/,/', $coords[2][$i]);
1184
			$this->img->SetStartPoint($pts[0],$pts[1]);
1185
			$this->img->LineTo($pts[2],$pts[1]);
1186
			$this->img->LineTo($pts[2],$pts[3]);
1187
			$this->img->LineTo($pts[0],$pts[3]);
1188
			$this->img->LineTo($pts[0],$pts[1]);
1189
 
1190
		    }
1191
		}
1192
	    }
1193
    }
1194
 
1195
    // Method description
1196
    function Stroke($aStrokeFileName="") {
1197
	// If the filename is the predefined value = '_csim_special_'
1198
	// we assume that the call to stroke only needs to do enough
1199
	// to correctly generate the CSIM maps.
1200
	// We use this variable to skip things we don't strictly need
1201
	// to do to generate the image map to improve performance
1202
	// a best we can. Therefor you will see a lot of tests !$_csim in the
1203
	// code below.
1204
	$_csim = ($aStrokeFileName===_CSIM_SPECIALFILE);
1205
 
1206
	// We need to know if we have stroked the plot in the
1207
	// GetCSIMareas. Otherwise the CSIM hasn't been generated
1208
	// and in the case of GetCSIM called before stroke to generate
1209
	// CSIM without storing an image to disk GetCSIM must call Stroke.
1210
	$this->iHasStroked = true;
1211
 
1212
	$n = count($this->plots);
1213
 
1214
	if( $this->pieaa ) {
1215
 
1216
	    if( !$_csim ) {
1217
		if( $this->background_image != "" ) {
1218
		    $this->StrokeFrameBackground();
1219
		}
1220
		else {
1221
		    $this->StrokeFrame();
1222
		}
1223
	    }
1224
 
1225
 
1226
	    $w = $this->img->width;
1227
	    $h = $this->img->height;
1228
	    $oldimg = $this->img->img;
1229
 
1230
	    $this->img->CreateImgCanvas(2*$w,2*$h);
1231
 
1232
	    $this->img->SetColor( $this->margin_color );
1233
	    $this->img->FilledRectangle(0,0,2*$w-1,2*$h-1);
1234
 
1235
	    // Make all icons *2 i size since we will be scaling down the
1236
	    // image to do the anti aliasing
1237
	    $ni = count($this->iIcons);
1238
	    for($i=0; $i < $ni; ++$i) {
1239
		$this->iIcons[$i]->iScale *= 2 ;
1240
		if( $this->iIcons[$i]->iX > 1 )
1241
		    $this->iIcons[$i]->iX *= 2 ;
1242
		if( $this->iIcons[$i]->iY > 1 )
1243
		    $this->iIcons[$i]->iY *= 2 ;
1244
	    }
1245
 
1246
	    $this->StrokeIcons();
1247
 
1248
	    for($i=0; $i < $n; ++$i) {
1249
		if( $this->plots[$i]->posx > 1 )
1250
		    $this->plots[$i]->posx *= 2 ;
1251
		if( $this->plots[$i]->posy > 1 )
1252
		    $this->plots[$i]->posy *= 2 ;
1253
 
1254
		$this->plots[$i]->Stroke($this->img,1);
1255
 
1256
		if( $this->plots[$i]->posx > 1 )
1257
		    $this->plots[$i]->posx /= 2 ;
1258
		if( $this->plots[$i]->posy > 1 )
1259
		    $this->plots[$i]->posy /= 2 ;
1260
	    }
1261
	    $indent = $this->doframe ? ($this->frame_weight + ($this->doshadow ? $this->shadow_width : 0 )) : 0 ;
1262
	    $indent += $this->framebevel ? $this->framebeveldepth + 1 : 0 ;
1263
	    $this->img->CopyCanvasH($oldimg,$this->img->img,$indent,$indent,$indent,$indent,
1264
				    $w-2*$indent,$h-2*$indent,2*($w-$indent),2*($h-$indent));
1265
 
1266
	    $this->img->img = $oldimg ;
1267
	    $this->img->width = $w ;
1268
	    $this->img->height = $h ;
1269
 
1270
	    for($i=0; $i < $n; ++$i) {
1271
		$this->plots[$i]->Stroke($this->img,2); // Stroke labels
1272
		$this->plots[$i]->Legend($this);
1273
	    }
1274
 
1275
	}
1276
	else {
1277
	    // No antialias
1278
	    if( !$_csim ) {
1279
		if( $this->background_image != "" ) {
1280
		    $this->StrokeFrameBackground();
1281
		}
1282
		else {
1283
		    $this->StrokeFrame();
1284
		    $this->StrokeBackgroundGrad();
1285
		}
1286
	    }
1287
 
1288
	    $this->StrokeIcons();
1289
	    for($i=0; $i < $n; ++$i) {
1290
		$this->plots[$i]->Stroke($this->img);
1291
		$this->plots[$i]->Legend($this);
1292
	    }
1293
	}
1294
 
1295
	$this->legend->Stroke($this->img);
1296
	$this->footer->Stroke($this->img);
1297
	$this->StrokeTitles();
1298
 
1299
	if( !$_csim ) {
1300
 
1301
	    // Stroke texts
1302
	    if( $this->texts != null ) {
1303
		$n = count($this->texts);
1304
		for($i=0; $i < $n; ++$i ) {
1305
		    $this->texts[$i]->Stroke($this->img);
1306
		}
1307
	    }
1308
 
1309
	    if( _JPG_DEBUG ) {
1310
		$this->DisplayCSIMAreas();
1311
	    }
1312
 
1313
	    // Should we do any final image transformation
1314
	    if( $this->iImgTrans ) {
1315
		if( !class_exists('ImgTrans') ) {
1316
		    require_once('jpgraph_imgtrans.php');
1317
		    //JpGraphError::Raise('In order to use image transformation you must include the file jpgraph_imgtrans.php in your script.');
1318
		}
1319
 
1320
		$tform = new ImgTrans($this->img->img);
1321
		$this->img->img = $tform->Skew3D($this->iImgTransHorizon,$this->iImgTransSkewDist,
1322
						 $this->iImgTransDirection,$this->iImgTransHighQ,
1323
						 $this->iImgTransMinSize,$this->iImgTransFillColor,
1324
						 $this->iImgTransBorder);
1325
	    }
1326
 
1327
 
1328
	    // If the filename is given as the special "__handle"
1329
	    // then the image handler is returned and the image is NOT
1330
	    // streamed back
1331
	    if( $aStrokeFileName == _IMG_HANDLER ) {
1332
		return $this->img->img;
1333
	    }
1334
	    else {
1335
		// Finally stream the generated picture
1336
		$this->cache->PutAndStream($this->img,$this->cache_name,$this->inline,
1337
					   $aStrokeFileName);
1338
	    }
1339
	}
1340
    }
1341
} // Class
1342
 
1343
/* EOF */
1344
?>