winglis4
asked on
imagefilledarc - Ugly code to create a pie chart
This is really ugly, but it seems to work. Now how to improve it???
I pull an array from my db called $graphValues, and pull a sum to get a $total - the $total will represent 360 degrees of the pie chart:
// Figure out start and end points for each graph segment
$degperhit = 360 / $total; // 360 / Total # of hits = # of degrees per unit
Then I turn $graphValues into a temp array so I can screw around with it, ie. Figure out start and end values for imagefilledarc by turning the sum of the actual values into how many degrees of the circle it will be, rounded down with 'floor', and then drop a value and do it again.
$tempValArray = $graphValues; // temp array to work with for segment start and end values
for($i = count($tempValArray); $i > 0; $i--){
$almostStart[] = floor(array_sum($tempValAr ray)*$degp erhit);
array_pop($tempValArray);
}
$almostStart = array_reverse($almostStart );
This will give me end values as is.
$endArray = $almostStart;
Start values set by adding a 0 to the front of the array, then dropping the last value, which will always be 360
array_unshift($almostStart , "0");
array_pop($almostStart);
$startArray = $almostStart;
-------------------------- ---------- ---------- --------
This is pretty darn ugly right? Any thoughts on a 'cleaner' or more efficient way of doing it???
I pull an array from my db called $graphValues, and pull a sum to get a $total - the $total will represent 360 degrees of the pie chart:
// Figure out start and end points for each graph segment
$degperhit = 360 / $total; // 360 / Total # of hits = # of degrees per unit
Then I turn $graphValues into a temp array so I can screw around with it, ie. Figure out start and end values for imagefilledarc by turning the sum of the actual values into how many degrees of the circle it will be, rounded down with 'floor', and then drop a value and do it again.
$tempValArray = $graphValues; // temp array to work with for segment start and end values
for($i = count($tempValArray); $i > 0; $i--){
$almostStart[] = floor(array_sum($tempValAr
array_pop($tempValArray);
}
$almostStart = array_reverse($almostStart
This will give me end values as is.
$endArray = $almostStart;
Start values set by adding a 0 to the front of the array, then dropping the last value, which will always be 360
array_unshift($almostStart
array_pop($almostStart);
$startArray = $almostStart;
--------------------------
This is pretty darn ugly right? Any thoughts on a 'cleaner' or more efficient way of doing it???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, I really want to try it on my own (well, almost on my own, hehe) as a learning experience. I've done some bar graphs, but wanted to stretch my brain and see if I could get the same data into a pie chart. I may get JPGraph for the 'real' production of my graphs, though.
I really was psyched that I figured out how to get the db values into degrees, and how to get it all into a format that imagefililedarc could use. It ain't pretty but it works!!! :-)
I really was psyched that I figured out how to get the db values into degrees, and how to get it all into a format that imagefililedarc could use. It ain't pretty but it works!!! :-)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I just played around with JPGraph a bit - not bad!!! :-)
I gotta give props to Squinky, though. Those Flash graphs look too good!!!!
Thanks fellas.
winglis4
I gotta give props to Squinky, though. Those Flash graphs look too good!!!!
Thanks fellas.
winglis4
ASKER
then I draw the chart with this:
(image is 400x400)
for ($i=0; $i < count($endArray); $i++){
imagefilledarc($image, 200, 200, 380, 240, $startArray[$i], $endArray[$i], $colorArray[$i], IMG_ARC_PIE);
}