Newer
Older

Thomas Lake
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?
function piechart($title, $slice, $itemName) {
function matchset($xx)
{
$arrx = array_values($xx);
$i = 0;
while (list ($key, $val) = each ($arrx))
{
$xy[$i] = $val;
$i++;
}
$cnt = $i;
return $xy;
}
$sliced = matchset($slice);
$countqw = count($sliced);
$ItemNames = matchset($itemName);
// initialize some variables
$sum = 0;
$degrees = Array();
$diameter = 250;
$radius = $diameter/2;
// calculate sum of slices
for ($x=0; $x<$countqw ; $x++)
{
$sum += $sliced[$x];
}
// convert each slice into corresponding percentage of 360-degree circle
$degCount = 0;
for ($y=0; $y<$countqw; $y++)
{
if((($sliced[$y]/$sum) * 360) > '0')
{
$degrees[$degCount] = ($sliced[$y]/$sum) * 360;
$degCount++;
}
}
// set up image and colours
Header("Content-Type: image/png");
$im = ImageCreate(550, 300);
$black = ImageColorAllocateAlpha($im, 0, 0, 0, 0);
$white = ImageColorAllocateAlpha($im, 255, 255, 255, 127);
$hexCode = array("255,153,0","0,204,153","204,255,102","255,102,102","102,204,255","204,153,255","255,0,0","51,0,255","255,51,153","204,0,255","255,255,51","51,255,51","255,102,0");
// fill image with white
ImageFill($im, 0, 0, $white);
// draw baseline
ImageLine($im, 150,150, 225, 150, $black);
for ($z=0; $z<$countqw; $z++)
{
// calculate and draw arc corresponding to each slice
ImageArc($im, 150, 150, $diameter, $diameter, $last_angle,
($last_angle+$degrees[$z]), $black);
$last_angle = $last_angle+$degrees[$z];
// calculate coordinate of end-point of each arc by obtaining
// length of segment and adding radius
// remember that cos() and sin() return value in radians
// and have to be converted back to degrees!
$end_x = round(150 + ($radius * cos($last_angle*pi()/180)));
$end_y = round(150 + ($radius * sin($last_angle*pi()/180)));
// demarcate slice with another line
ImageLine($im, 150, 150, $end_x, $end_y, $black);
}
// this section is meant to calculate the mid-point of each slice
// so that it can be filled with colour
// initialize some variables
$prev_angle = 0;
$pointer = 0;
for ($z=0; $z<$countqw; $z++)
{
// to calculate mid-point of a slice, the procedure is to use an angle
//bisector
// and then obtain the mid-point of that bisector
$pointer = $prev_angle + $degrees[$z];
$this_angle = ($prev_angle + $pointer) / 2;
$prev_angle = $pointer;
// get end-point of angle bisector
$end_x = round(150 + ($radius * cos($this_angle*pi()/180)));
$end_y = round(150 + ($radius * sin($this_angle*pi()/180)));
// given start point (150,150) and end-point above, mid-point can be
// calculated with standard mid-point formula
$mid_x = round((150+($end_x))/2);
$mid_y = round((150+($end_y))/2);
// depending on which slice, fill with appropriate colour
$hexCodeSplit = explode(',',$hexCode[$z]);
$WedgeColor = ImageColorAllocate($im, $hexCodeSplit[0],$hexCodeSplit[1],$hexCodeSplit[2]);
ImageFillToBorder($im, $mid_x, $mid_y, $black, $WedgeColor);
}
// write string
ImageString($im,5, 250, 10, "$title", $black);
$red = ImageColorAllocate($im, 255, 153, 153);
$blue = ImageColorAllocate($im, 0, 0, 255);
// Create Color key and slice description
$adjPosition = 40;
for ($z=0; $z<$degCount; $z++)
{
$percent = ($degrees[$z]/360)*100;
$percent = round($percent,2);
$adjPosition = $adjPosition + 15;
$hexCodeSplit = explode(',',$hexCode[$z]);
$percentLen = strlen($percent);
if($percentLen == '4'){$percent = " "."$percent";}
if($percentLen == '3'){$percent = " "."$percent";}
if($percentLen == '2'){$percent = " "."$percent";}
if($percentLen == '1'){$percent = " "."$percent";}
ImageString($im,2, 300, ($adjPosition+1), "$percent%", $black);
$WedgeColor = ImageColorAllocate($im, $hexCodeSplit[0],$hexCodeSplit[1],$hexCodeSplit[2]);
ImageFilledRectangle($im, 340, $adjPosition, 350, ($adjPosition+10), $black);
ImageFilledRectangle($im, 341, ($adjPosition+1), 349, ($adjPosition+9), $WedgeColor);
if($sliced[$z] >= "1000" && $sliced[$z] < "1000000")
{
$sliced[$z] = $sliced[$z]/1000;
$sliced[$z] = sprintf("%01.2f", "$sliced[$z]")."G";
}
else
$sliced[$z] = "$sliced[$z]"."M";
$sliceLen = strlen($sliced[$z]);
if($sliceLen == '5'){$sliced[$z] = " "."$sliced[$z]";}
if($sliceLen == '4'){$sliced[$z] = " "."$sliced[$z]";}
if($sliceLen == '3'){$sliced[$z] = " "."$sliced[$z]";}
if($sliceLen == '2'){$sliced[$z] = " "."$sliced[$z]";}
if($sliceLen == '1'){$sliced[$z] = " "."$sliced[$z]";}
ImageString($im,2, 360, ($adjPosition+1), "$sliced[$z]", $black);
ImageString($im,2, 410, ($adjPosition+1), "$ItemNames[$z]", $black);
}
// output to browser
ImagePNG($im);
}
?>