Tim158
01-06-2004, 07:57 AM
I was configuring this pie chart script (see below) to create some statistics when something mysterious happened - the red slice turned blue.
What's caused this and how can I fix permanently? help!
//Connect to database & retrieve data
$total = ($win + $mac + $linux + $unix + $craw);
$other = ($overall - $total);
// initialize some variables
$sum = 0;
$degrees = Array();
$percentage = Array();
$diameter = 200;
$radius = $diameter/2;
$slice[1] = $win;
$slice[2] = $mac;
$slice[3] = $linux;
$slice[4] = $unix;
$slice[5] = $craw;
$slice[6] = $other;
// calculate sum of slices
for ($x=1; $x<=6; $x++)
{
$sum += $slice[$x];
}
// convert each slice into corresponding degrees of 360-degree circle
for ($y=1; $y<=6; $y++)
{
$degrees[$y] = ($slice[$y]/$sum) * 360;
}
//Get percentage of each slice
for ($p=1; $p<=6; $p++)
{
$percentage[$p] = ($slice[$p]/$sum) * 100;
$percentage[$p] = round ($percentage[$p], 1);
}
// set up image and colours
Header("Content-Type: image/png");
$im = ImageCreate(530, 300);
$red = ImageColorAllocate($im, 255, 0, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
$green = ImageColorAllocate($im, 0, 255, 0);
$yellow = ImageColorAllocate($im, 255, 255, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);
$grey = ImageColorAllocate($im, 204, 204, 204);
// fill image with white
ImageFill($im, 0, 0, $white);
//Pie Chart Key
ImageFilledRectangle ($im, 276, 36, 296, 56, $grey);
ImageFilledRectangle ($im, 275, 35, 295, 55, $black);
ImageString($im, 2, 300, 40, "Search Engine Spiders ($percentage[5]% - $slice[5])", $black);
ImageFilledRectangle ($im, 276, 66, 296, 86, $grey);
ImageRectangle ($im, 275, 65, 295, 85, $black);
ImageString($im, 2, 300, 70, "Unknown ($percentage[6]% - $slice[6])", $black);
ImageFilledRectangle ($im, 276, 96, 296, 116, $grey);
ImageFilledRectangle ($im, 276, 96, 294, 114, $red);
ImageRectangle ($im, 275, 95, 295, 115, $black);
ImageString($im, 2, 300, 100, "Microsoft Windows ($percentage[1]% - $slice[1])", $black);
ImageFilledRectangle ($im, 276, 126, 296, 146, $grey);
ImageFilledRectangle ($im, 276, 126, 294, 144, $blue);
ImageRectangle ($im, 275, 125, 295, 145, $black);
ImageString($im, 2, 300, 130, "Macintosh ($percentage[2]% - $slice[2])", $black);
ImageFilledRectangle ($im, 276, 156, 296, 176, $grey);
ImageFilledRectangle ($im, 276, 156, 294, 174, $green);
ImageRectangle ($im, 275, 155, 295, 175, $black);
ImageString($im, 2, 300, 160, "Linux ($percentage[3]% - $slice[3])", $black);
ImageFilledRectangle ($im, 276, 186, 296, 206, $grey);
ImageFilledRectangle ($im, 276, 186, 294, 204, $yellow);
ImageRectangle ($im, 275, 185, 295, 205, $black);
ImageString($im, 2, 300, 190, "UNIX ($percentage[4]% - $slice[4])", $black);
// draw baseline
ImageLine($im, 150,150, 225, 150, $black);
for ($z=1; $z<=6; $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=1; $z<=6; $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
if ($z == 1)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $red);
}
else if ($z == 2)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $blue);
}
else if ($z == 3)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $green);
}
else if ($z == 4)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $yellow);
}
else if ($z == 5)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $black);
}
else if ($z == 6)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $grey);
}
}
// write string
ImageString($im, 30, 70, 10, "Operating Systems", $black);
// output to browser
ImagePNG($im);
?>
What's caused this and how can I fix permanently? help!
//Connect to database & retrieve data
$total = ($win + $mac + $linux + $unix + $craw);
$other = ($overall - $total);
// initialize some variables
$sum = 0;
$degrees = Array();
$percentage = Array();
$diameter = 200;
$radius = $diameter/2;
$slice[1] = $win;
$slice[2] = $mac;
$slice[3] = $linux;
$slice[4] = $unix;
$slice[5] = $craw;
$slice[6] = $other;
// calculate sum of slices
for ($x=1; $x<=6; $x++)
{
$sum += $slice[$x];
}
// convert each slice into corresponding degrees of 360-degree circle
for ($y=1; $y<=6; $y++)
{
$degrees[$y] = ($slice[$y]/$sum) * 360;
}
//Get percentage of each slice
for ($p=1; $p<=6; $p++)
{
$percentage[$p] = ($slice[$p]/$sum) * 100;
$percentage[$p] = round ($percentage[$p], 1);
}
// set up image and colours
Header("Content-Type: image/png");
$im = ImageCreate(530, 300);
$red = ImageColorAllocate($im, 255, 0, 0);
$blue = ImageColorAllocate($im, 0, 0, 255);
$green = ImageColorAllocate($im, 0, 255, 0);
$yellow = ImageColorAllocate($im, 255, 255, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);
$grey = ImageColorAllocate($im, 204, 204, 204);
// fill image with white
ImageFill($im, 0, 0, $white);
//Pie Chart Key
ImageFilledRectangle ($im, 276, 36, 296, 56, $grey);
ImageFilledRectangle ($im, 275, 35, 295, 55, $black);
ImageString($im, 2, 300, 40, "Search Engine Spiders ($percentage[5]% - $slice[5])", $black);
ImageFilledRectangle ($im, 276, 66, 296, 86, $grey);
ImageRectangle ($im, 275, 65, 295, 85, $black);
ImageString($im, 2, 300, 70, "Unknown ($percentage[6]% - $slice[6])", $black);
ImageFilledRectangle ($im, 276, 96, 296, 116, $grey);
ImageFilledRectangle ($im, 276, 96, 294, 114, $red);
ImageRectangle ($im, 275, 95, 295, 115, $black);
ImageString($im, 2, 300, 100, "Microsoft Windows ($percentage[1]% - $slice[1])", $black);
ImageFilledRectangle ($im, 276, 126, 296, 146, $grey);
ImageFilledRectangle ($im, 276, 126, 294, 144, $blue);
ImageRectangle ($im, 275, 125, 295, 145, $black);
ImageString($im, 2, 300, 130, "Macintosh ($percentage[2]% - $slice[2])", $black);
ImageFilledRectangle ($im, 276, 156, 296, 176, $grey);
ImageFilledRectangle ($im, 276, 156, 294, 174, $green);
ImageRectangle ($im, 275, 155, 295, 175, $black);
ImageString($im, 2, 300, 160, "Linux ($percentage[3]% - $slice[3])", $black);
ImageFilledRectangle ($im, 276, 186, 296, 206, $grey);
ImageFilledRectangle ($im, 276, 186, 294, 204, $yellow);
ImageRectangle ($im, 275, 185, 295, 205, $black);
ImageString($im, 2, 300, 190, "UNIX ($percentage[4]% - $slice[4])", $black);
// draw baseline
ImageLine($im, 150,150, 225, 150, $black);
for ($z=1; $z<=6; $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=1; $z<=6; $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
if ($z == 1)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $red);
}
else if ($z == 2)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $blue);
}
else if ($z == 3)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $green);
}
else if ($z == 4)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $yellow);
}
else if ($z == 5)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $black);
}
else if ($z == 6)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $grey);
}
}
// write string
ImageString($im, 30, 70, 10, "Operating Systems", $black);
// output to browser
ImagePNG($im);
?>