labels are vital to all pies!
Making pie chart
brandtj wrote:I would think that labeling them would take an extreme amount of math... because you would have to calculate the bbox size, shrink it accordingly, place them in opportune places, and generally a ton more stuff.
I'm bored, I'll play around with it... but is it fairly important that they are labeled?
Cool thanks so much
Sure it's not a problem... This is something very quick that I whipped up:
<?php
$radius = 100;
$p_green = 30;
$image = @imagecreate (2 * $radius + 1, 2 * $radius + 1);
$white = @imagecolorallocate ($image, 255, 255, 255);
$black = @imagecolorallocate ($image, 0, 0, 0);
$green = @imagecolorallocate ($image, 0, 255, 0);
$red = @imagecolorallocate ($image, 255, 0, 0);
@imagefilledrectangle ($image, 0, 0, 2 * $radius + 1, 2 * $radius + 1, $white);
@imagefilledarc ($image, $radius, $radius, 2 * $radius, 2 * $radius, 0, 3.6 * $p_green, $green, IMG_ARC_PIE);
@imagefilledarc ($image, $radius, $radius, 2 * $radius, 2 * $radius, 3.6 * $p_green, 360, $red, IMG_ARC_PIE);
$text_g = @imagettfbbox (10, 0, './arial.ttf', $p_green . '%');
@imagettftext ($image, 10, 0, 170, 180 - $text_g[7], $black, './arial.ttf', $p_green . '%');
$text_r = @imagettfbbox (10, 0, './arial.ttf', (100 - $p_green) . '%');
@imagettftext ($image, 10, 0, 30 - $text_r[2], 20, $black, './arial.ttf', (100 - $p_green) . '%');
@imageellipse ($image, $radius, $radius, 2 * $radius, 2 * $radius, $black);
header ('Content-type: image/png');
@imagepng ($image);
@imagedestroy ($image);
?>
Now the downside to this code is that it always puts the numbers in the same place (and of course, you could put a name in that place rather than the percentage)... so if the green portion is only 5%, both labels will "look like" they're labeling the red section.
i dont know how to label them - but would an easier solution not be... make the image a bit wider and do like a legend at the side.
rather than trying to get the label on the pie.
Might look better aswell tbh.
Just a thought.
You might want to have a look at the baaChart link in my sig.
Pie charts with labels and/or legends
I have this working great (thanks a lot for the postings) but if I want to send variables from the 1st page it doesnt work:
This works;
echo '<IMG src="mypie.php?a=10&b=10&c=20">';
This doesnt;
$a=10;
$b=20;
$c= 25;
$otros=100-($a+$b+$c);
echo '<IMG src="mypie.php?a=$a&b=$b&c=$c">';
Can anyone tell me why not and give me a clue of how tto get around it?
Many thanks
try
$a = $_GET['a];
etc
Many thanks for your help. I have already tried that .. and tried it again at your suggestion but it doesn't work.
As you only pass a, b, and c, you need to calculate
$otros=100-($a+$b+$c);
inside mypie.php
echo '<IMG src="mypie.php?a='.$a.'&b='.$b.'&c='.$c.'">';
try that, remember you cant use variable names directly between single quotes.
THAT'S IT!! FANTASTIC. Solved all the problems.
Many many thanks!