Jump to content

Looking for a direction – Pie Graph,PHP,MySQL


lilman

Recommended Posts

The angle for each slice is given by
[pre]
  value
-----------  *  360
total value
[/pre]

Then you need to increment the start and end angles of each slice to create the pie

[code]
<?php
$values = array(20, 30, 40);    // values from your db

$im = imagecreate(200,200);
$bg = imagecolorallocate($im, 0, 0, 0);
$colors = array(
    imagecolorallocate($im, 0xFF, 0, 0),    //red
    imagecolorallocate($im, 0, 0xFF, 0),    //green
    imagecolorallocate($im, 0, 0, 0xFF)      //blue
);

$s_angle = 0;                          // pie slice start angle
$e_angle = 0;                          // pie slice end angle
$total = array_sum($values);
$k = count($values);
$cx = $cy = 100;                      // centre of the pie chart
for ($i=0; $i<$k; $i++)  {
    $a = $values[$i] / $total * 360;  // angle for this slice
    $e_angle += $a;
    imagefilledarc($im, $cx, $cy, 180, 180, $s_angle, $e_angle, $colors[$i], IMG_ARC_PIE);
    $s_angle += $a;                    // start angle for next slice
}

header ('content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.