Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pie Chart plotting ? 1

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
0
0
GB
I'm working with html/php/mySql and am developing a site with a degree of user interaction.
I'm using the data collected from the users to generate bar charts on pages to show results of polls.
How can I acheive this in the form of a pie chart ?
Can anyone provide me with an example ?
(Just to be awkward, ideally each pie piece would need to be coloured differently).
Thanks in advance
Steve
 
These should get you started:

Creating Dynamic Images (tutorial):

Image functions (reference):

These functions might be important (look up in reference):
imagearc -- Draw a partial ellipse
imageellipse -- Draw an ellipse
imagefilledarc -- Draw a partial ellipse and fill it
imagefilledellipse -- Draw a filled ellipse

imagettftext -- Write text to the image using TrueType fonts
imagefttext -- Write text to the image using fonts using FreeType 2

--------------
NOTE: Not all of those may be important/used.

Good luck. Hope this gets you started.
 
Heres a page, may not work as intended with PHP 4.2.*,
call it like <img src=&quot;pie.php?slice[1]=2&slice[2]=4&slice[3]=9&slice[4]=5&slice[5]=11&quot;>

Hope this helps.

//Karv

----------pie.php-----------------
<?

// initialize some variables
$sum = 0;
$degrees = Array();
$diameter = 200;
$radius = $diameter/2;

// calculate sum of slices
for ($x=1; $x<=5; $x++)
{
$sum += $slice[$x];
}

// convert each slice into corresponding percentage of 360-degree circle
for ($y=1; $y<=5; $y++)
{
$degrees[$y] = ($slice[$y]/$sum) * 360;
}

// set up image and colours
Header(&quot;Content-Type: image/png&quot;);
$im = ImageCreate(300, 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);

// fill image with white
ImageFill($im, 0, 0, $white);

// draw baseline
ImageLine($im, 150,150, 225, 150, $black);

for ($z=1; $z<=5; $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<=5; $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)
{
// Fill the new slice with colour ($im, pos x, pos y, bordercolour, fillcolour)
ImageFillToBorder($im, $mid_x, $mid_y, $black, $red);
// this line adds the label, ($im, FONTSIZE, position, x position y, TEXT, COLOUR)
ImageString($im, 3, $mid_x, $mid_y, a, $black);
}
else if ($z == 2)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $blue);
ImageString($im, 3, $mid_x, $mid_y, test, $black);
}
else if ($z == 3)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $green);
ImageString($im, 3, $mid_x, $mid_y, piccy, $black);
}
else if ($z == 4)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $yellow);
ImageString($im, 3, $mid_x, $mid_y, should, $black);
}
else if ($z == 5)
{
ImageFillToBorder($im, $mid_x, $mid_y, $black, $white);
ImageString($im, 3, $mid_x, $mid_y, show, $black);
}
}

// Image title
ImageString($im, 5, 100, 10, &quot;You like Pie&quot;, $black);

// output to browser
ImagePNG($im);

?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for the samples and links.
I'm sure this will get me on my way with my ideas.
Steve
 
Check out jpgraph! :-D -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top