Make Pie Chart using PHP script

Programming for Search Engines 101. An area for avid PHP and .NET developers to chat about Programming techniques and how to make better use of search engines.

Moderator: Moderators

Make Pie Chart using PHP script

Postby royvipin » Fri Sep 30, 2011 1:15 am

We can create Pie Chart using the following PHP code:
<?php
$n1 = 10; //Set your values here as either static or dynamic
$n2 = 5;
$n3 = 10;
$n4 = 5;
$n5 = 10;

$image = imagecreate(500, 600); // Create a new 500 x 600 image

// SET UP SOME COLOURS
$yellow = imagecolorallocate($image, 255, 255, 0); // create color R=255, G=255, B=0
$cyan = imagecolorallocate($image, 0, 255, 255); // create color R=0, G=255, B=255
$red = imagecolorallocate($image, 255, 0, 0); // create color red
$blue = imagecolorallocate($image, 0, 0, 255); // create color blue
$lime = imagecolorallocate($image, 0, 255, 0); // create color lime
$magenta = imagecolorallocate($image, 255, 0, 255); // create color magenta

// Add up input data to find total amount.
$total = $n1 + $n2 + $n3 + $n4 + $n5;

$startAngle = 0;
$endAngle = $StartAngle + 360 * $n1 / $total;
ImageFilledArc($image, 250, 350, 200, 200, $startAngle, $endAngle, $red, IMG_ARC_PIE);

$startAngle = $endAngle;
$endAngle = $endAngle + 360 * $n2 / $total;
ImageFilledArc($image, 250, 350, 200, 200, $startAngle, $endAngle, $blue, IMG_ARC_PIE);

$startAngle = $endAngle;
$endAngle = $endAngle + 360 * $n3 / $total;
ImageFilledArc($image, 250, 350, 200, 200, $startAngle, $endAngle, $lime, IMG_ARC_PIE);

$startAngle = $endAngle;
$endAngle = $endAngle + 360 * $n4 / $total;
ImageFilledArc($image, 250, 350, 200, 200, $startAngle, $endAngle, $cyan, IMG_ARC_PIE);

$startAngle = $endAngle;
$endAngle = $endAngle + 360 * $n5 / $total;
ImageFilledArc($image, 250, 350, 200, 200, $startAngle, $endAngle, $magenta, IMG_ARC_PIE);

header ("Content-type: image/png");
ImagePNG($image); // Save image to file.
ImageDestroy($image); // Cleanup memory
?>
royvipin
 

Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests