|
 |
imagefilledarc (PHP 4 >= 4.0.6, PHP 5) imagefilledarc -- Draw a partial ellipse and fill it Descriptionbool imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
imagefilledarc() draws a partial ellipse centered at
cx, cy (top left is
0, 0) in the image represented by image.
Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.
W
and h specifies the ellipse's width and
height respectively while the start and end points are specified
in degrees indicated by the s and
e arguments.
style is a bitwise OR of the following possibilities:
IMG_ARC_PIE IMG_ARC_CHORD IMG_ARC_NOFILL IMG_ARC_EDGED
IMG_ARC_PIE and IMG_ARC_CHORD are
mutually exclusive; IMG_ARC_CHORD just
connects the starting and ending angles with a straight line, while
IMG_ARC_PIE produces a rounded edge.
IMG_ARC_NOFILL indicates that the arc
or chord should be outlined, not filled. IMG_ARC_EDGED,
used together with IMG_ARC_NOFILL, indicates that the
beginning and ending angles should be connected to the center - this is a
good way to outline (rather than fill) a 'pie slice'.
Пример 1. Creating a 3D looking pie
<?php
$image = imagecreatetruecolor(100, 100);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
for ($i = 60; $i > 50; $i--) {
imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
|
|
Замечание: Эта функция нуждается в GD версии 2.0.1 или выше.
imagefilledarc
imazir at gmail dot com
31-Jan-2006 02:53
The previous example does not work. Try those modifications and you will have the expected results :
<?
$Randomized = rand(1,20);
for($i=0;$i<=$Randomized;$i++){$data[$i]=rand(2,20);};$imgx='200';$imgy='200';$cx = '100';$cy ='50'; $sx = '200';$sy='100';$sz ='20';$data_sum = array_sum($data);
for($i=0;$i<=$Randomized;$i++){
$angle[$i] = (($data[$i] / $data_sum) * 360);
$angle_sum[$i] = array_sum($angle);
};
$im = imagecreate ($imgx,$imgy);
$background = imagecolorallocate($im, 255, 255, 255);
for($i=0;$i<=$Randomized;$i++){
$r=rand(100,255);$g=rand(100,255);$b=rand(100,255);
$colors[$i] = imagecolorallocate($im,$r,$g,$b);
$colord[$i] = imagecolorallocate($im,($r/2),($g/2),($b/2));
}
for($z=1;$z<=$sz;$z++){
for($i=1;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,($cy+$sz)-$z,$sx,$sy,$angle_sum[$i-1]
,$angle_sum[$i],$colord[$i],IMG_ARC_PIE);
};
};
for($i=1;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,$cy,$sx,$sy,$angle_sum[$i-1] ,$angle_sum[$i], $colors[$i], IMG_ARC_PIE);
};
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Byron at theworks dot net dot nz
10-Oct-2005 11:52
try this,
<?
$Randomized = rand(1,20);
for($i=0;$i<=$Randomized;$i++){$data[$i]=rand(2,20);};$imgx='200';$imgy='200';$cx = '100';$cy ='50'; $sx = '200';$sy='100';$sz ='20';$data_sum = array_sum($data);
for($i=0;$i<=$Randomized;$i++){
$angle[$i] = (($data[$i] / $data_sum) * 360);
$angle_sum[$i] = array_sum($angle);
};
$im = imagecreate ($imgx,$imgy);
$background = imagecolorallocate($im, 255, 255, 255);
for($i=0;$i<=$Randomized;$i++){
$r=rand(100,255);$g=rand(100,255);$b=rand(100,255);
$colors[$i] = imagecolorallocate($im,$r,$g,$b);
$colord[$i] = imagecolorallocate($im,($r/2),($g/2),($b/2));
}
for($i=0;$z<=$sz;$z++){
for($i=0;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,($cy+$sz)-$z,$sx,$sy,$angle_sum[$i-1]
,$angle_sum[$i],$colord[$i],IMG_ARC_PIE);
};
};
for($i=0;$i<=$Randomized;$i++){
imagefilledarc($im,$cx,$cy,$sx,$sy,$angle_sum[$i-1] ,$angle_sum[$i], $colors[$i], IMG_ARC_PIE);
};
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
paulcharltonthomson at hotmail dot com
01-Jul-2005 11:00
Here's a slightly better way to get the colours for the pie chart shaded wall as posted by double-zonk at wp dot pl
<?php
$rgb0 = array (255, 153, 204);
$rgb1 = array (255, 153, 0);
$rgb2 = array (153, 204, 0);
$rgb3 = array (51, 153, 102);
$rgb4 = array (51, 204, 204);
$rgb5 = array (51, 102, 255);
$rgb6 = array (128, 0, 128);
$rgb7 = array (150, 150, 150);
for ($r = 0; $r < 8; ++$r)
{
if(${"rgb" . $r}[0] < 50) $shadowr = 0; else $shadowr = ${"rgb" . $r}[0] - 50;
if(${"rgb" . $r}[1] < 50) $shadowg = 0; else $shadowg = ${"rgb" . $r}[1] - 50;
if(${"rgb" . $r}[2] < 50) $shadowb = 0; else $shadowb = ${"rgb" . $r}[2] - 50;
${"wall" . $r} = array ($shadowr, $shadowg, $shadowb);
}
for ($s = 0; $s < 8; ++$s)
{
$kolor[$s] = imagecolorallocate($image, ${"rgb" . $s}[0], ${"rgb" . $s}[1], ${"rgb" . $s}[2]);
$cien[$s] = imagecolorallocate($image, ${"wall" . $s}[0], ${"wall" . $s}[1], ${"wall" . $s}[2]);
}
?>
04-Jun-2005 12:25
For 3D-graphs you will want to have a nice shadow. Here is an easy wat to calculate a good matching shadow to a color.
$color is the color for the pie-piece above, $shadow_color is the shadow under it.
<?php
$mask = 0; $percent = 0.70; $R = rand(0, 124); $G = rand(0, 124); $B = rand(0, 124); $RS = round($R * $percent) + round($mask * (1-$percent));
$GS = round($G * $percent) + round($mask * (1-$percent));
$BS = round($B * $percent) + round($mask * (1-$percent));
$color = imagecolorallocate( $im, $R, $G, $B);
$shadow_color = imagecolorallocate( $im, $RS, $GS, $BS);
?>
double-zonk at wp dot pl
08-Apr-2005 12:25
<?
$width = 300;
$height = 200;
$dane[] = 1;
$dane[] = 2;
$dane[] = 3;
$dane[] = 4;
$dane[] = 5;
$dane[] = 6;
$dane[] = 7;
$dane[] = 8;
$dane[] = 9;
$dane[] = 10;
$dane[] = 11;
$image = imageCreate($width, $height);
$background = imageColorAllocate($image, 205, 205, 205);
$kolor[0] = imageColorAllocate($image, 255, 203, 3);
$kolor[1] = imageColorAllocate($image, 220, 101, 29);
$kolor[2] = imageColorAllocate($image, 189, 24, 51);
$kolor[3] = imageColorAllocate($image, 214, 0, 127);
$kolor[4] = imageColorAllocate($image, 98, 1, 96);
$kolor[5] = imageColorAllocate($image, 0, 62, 136);
$kolor[6] = imageColorAllocate($image, 0, 102, 179);
$kolor[7] = imageColorAllocate($image, 0, 145, 195);
$kolor[8] = imageColorAllocate($image, 0, 115, 106);
$kolor[9] = imageColorAllocate($image, 178, 210, 52);
$kolor[10] = imageColorAllocate($image, 137, 91, 74);
$kolor[11] = imageColorAllocate($image, 82, 56, 47);
$cien[0] = imagecolorallocate($image, 205, 153, 0);
$cien[1] = imagecolorallocate($image, 170, 51, 0);
$cien[2] = imagecolorallocate($image, 139, 0, 1);
$cien[3] = imagecolorallocate($image, 164, 0, 77);
$cien[4] = imagecolorallocate($image, 48, 0, 46);
$cien[5] = imagecolorallocate($image, 0, 12, 86);
$cien[6] = imagecolorallocate($image, 0, 52, 129);
$cien[7] = imagecolorallocate($image, 0, 95, 145);
$cien[8] = imagecolorallocate($image, 0, 65, 56);
$cien[9] = imagecolorallocate($image, 128, 160, 2);
$cien[10] = imagecolorallocate($image, 87, 41, 24);
$cien[11] = imagecolorallocate($image, 32, 6, 0);
$suma = array_sum($dane);
$stopnie = 0;
for($i = 0; $i < count($dane); $i++){
$start[$i]= $stopnie;
$stop[$i] = $stopnie + round(($dane[$i] / $suma) * 360, 0);
$stopnie = $stop[$i];
}
$x = $width / 2;
$size = $width / 10;
$y = $height / 2 - $size / 2 - 1;
for($g = $size; $g > 0; $g--){
for($n = 0; $n < count($dane); $n++){
imagefilledarc($image, $x, $y+$g, $width, $height - $size, $start[$n], $stop[$n], $cien[$n], IMG_ARC_PIE);
}
}
for($i = 0; $i < count($dane); $i++){
imagefilledarc($image, $x, $y, $width, $height - $size, $start[$i], $stop[$i], $kolor[$i], IMG_ARC_PIE);
}
header("Content-type: image/png");
imagePNG($image);
imageDestroy($image);
?>
t_therkelsen at hotmail dot com
08-Mar-2005 01:21
Note that imageFilledArc() and imageArc() both take ints as degree measurements. This is no problem if you're *only* using imageArc() and/or imageFilledArc(). However, if you're using calculated degrees and plan to superimpose other drawing elements (eg., you want to make vertical lines between the shadow 3D effect) you need to floor() your degrees before converting them to radians, otherwise you'll get precision errors.
A small example illustrating the 'feature'...
<?php
$img = imageCreate(400, 400);
$back = imageColorAllocate($img, 0, 0, 0);
$front = imageColorAllocate($img, 255, 255, 255);
$sd = 45.5;
$ed = 130.5;
imageFilledArc($img, 200, 200, 300, 300, $sd, $ed,
$front, IMG_ARC_PIE|IMG_ARC_NOFILL|IMG_ARC_EDGED);
imageArc($img, 200, 230, 300, 300, $sd, $ed, $front);
imageLine($img,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+200,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+230,
$front);
imageLine($img,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+200,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+230,
$front);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
And this is how it should be...
<?php
$img = imageCreate(400, 400);
$back = imageColorAllocate($img, 0, 0, 0);
$front = imageColorAllocate($img, 255, 255, 255);
$sd = floor(45.5);
$ed = floor(130.5);
imageFilledArc($img, 200, 200, 300, 300, $sd, $ed,
$front, IMG_ARC_PIE|IMG_ARC_NOFILL|IMG_ARC_EDGED);
imageArc($img, 200, 230, 300, 300, $sd, $ed, $front);
imageLine($img,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+200,
cos(deg2rad($sd))*150+200, sin(deg2rad($sd))*150+230,
$front);
imageLine($img,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+200,
cos(deg2rad($ed))*150+200, sin(deg2rad($ed))*150+230,
$front);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
braulio at example dot com
03-Mar-2005 08:54
He aadido un par de cosillas al ejemplo de "hans at lintoo dot dk", sobre todo el grosor (thickness) del efecto 3D...
<?php
$im = imagecreate (200, 200);
$background = imagecolorallocate($im, 226, 226, 226);
$randcolor[0] = imagecolorallocate($im, 255, 203, 3);
$randcolor[1] = imagecolorallocate($im, 220, 101, 29);
$randcolor[2] = imagecolorallocate($im, 189, 24, 51);
$randcolor[3] = imagecolorallocate($im, 214, 0, 127);
$randcolor[4] = imagecolorallocate($im, 98, 1, 96);
$randcolor[5] = imagecolorallocate($im, 0, 62, 136);
$randcolor[6] = imagecolorallocate($im, 0, 102, 179);
$randcolor[7] = imagecolorallocate($im, 0, 145, 195);
$randcolor[8] = imagecolorallocate($im, 0, 115, 106);
$randcolor[9] = imagecolorallocate($im, 178, 210, 52);
$randcolor[10] = imagecolorallocate($im, 137, 91, 74);
$randcolor[11] = imagecolorallocate($im, 82, 56, 47);
$darkcolor[0] = imagecolorallocate($im, 205, 153, 0);
$darkcolor[1] = imagecolorallocate($im, 170, 51, 0);
$drakcolor[2] = imagecolorallocate($im, 139, 0, 1);
$darkcolor[3] = imagecolorallocate($im, 164, 0, 77);
$darkcolor[4] = imagecolorallocate($im, 48, 0, 46);
$darkcolor[5] = imagecolorallocate($im, 0, 12, 86);
$darkcolor[6] = imagecolorallocate($im, 0, 52, 129);
$darkcolor[7] = imagecolorallocate($im, 0, 95, 145);
$darkcolor[8] = imagecolorallocate($im, 0, 65, 56);
$darkcolor[9] = imagecolorallocate($im, 128, 160, 2);
$darkcolor[10] = imagecolorallocate($im, 87, 41, 24);
$darkcolor[11] = imagecolorallocate($im, 32, 6, 0);
$data = array( 100, 200, 50, 40); $num_data = count ($data);
$datasum = array_sum($data);
$anglesum[0] = 0;
$angle[0] = 0;
$i = 0;
while ($i < $num_data) {
++$i;
$n = $i - 1;
$part[$i] = $data[$n] / $datasum;
$angle[$i] = floor($part[$i] * 360);
$anglesum[$i] = array_sum($angle);
}
$cx = 120;
$cy = 100;
$ancho = 100;
$largo = 2 * $ancho; $grosor = 25; for ($g = $grosor; $g > 0; $g--) {
$n = 0;
while ($n < $num_data) {
++$n;
$f = $n - 1;
if ($angle[$n] != 0)
imagefilledarc($im, $cx, $cy + $g, $largo, $ancho, $anglesum[$f], $anglesum[$n], $darkcolor[$f], IMG_ARC_PIE);
}
}
$i = 0;
while ($i < $num_data) {
++$i;
$n = $i - 1;
if ($angle[$i] != 0) {
imagefilledarc($im, $cx, $cy, $largo, $ancho, $anglesum[$n], $anglesum[$i], $randcolor[$n], IMG_ARC_PIE);
}
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
rich at dicksonlife dot com
24-Sep-2004 07:22
Even more efficiency:
The original code snippet and the following suggestions are inefficient in that they rely on the overlying php to fill vertically using loops rather than taking advantage of the underlying drawing routines. Also, this is done by repeatedly drawing filled partial elipses and circular calculations are typically expensive (PHP may use tables, I'm not sure) The original code could be rewritten as
<?php
imagefilledarc($image, 50, 60, 100, 50, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 50, 60, 100, 50, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 50, 60, 100, 50, 75, 360 , $darkred, IMG_ARC_PIE);
$c1=50*cos(45/180*M_PI);
$s1=25*sin(45/180*M_PI);
$c2=50*cos(75/180*M_PI);
$s2=25*sin(75/180*M_PI);
$area1=array(100,60,100,50,50+$c1,50+$s1,50+$c1,60+$s1);
$area2=array(50+$c1,50+$s1,50+$c1,60+$s1,50+$c2,60+$s2,50+$c2,50+$s2);
$area3=array(50+$c2,50+$s2,50+$c2,60+$s2,0,60,0,50);
imagefilledpolygon($image, $area1 , 4 , $darknavy);
imagefilledpolygon($image, $area2 , 4 , $darkgray);
imagefilledpolygon($image, $area3 , 4 , $darkred);
imagefilledarc($image, 50, 50, 100, 50, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 50, 50, 100, 50, 75, 360 , $red, IMG_ARC_PIE);
?>
Note that the polygons are perhaps slightly inefficient. If there was an imagefilledtriangle, this code would be simpler. Given how fundamental triangles are, perhaps for a future version?
Rich
micha _a.t_ psytrance _d.o.t_ info
08-Sep-2004 07:09
for nice colors and adapted shadows from amount of values i try:
<?php
function _errechne_gradzahlen( $werte ) { foreach( $werte as $wert ) { $sum += $wert; }
foreach( $werte as $wert ) { $gradzahlen[] = 360 * ( $wert / $sum ); }
return $gradzahlen;
}
function _randomcol ( $im ) {
return imagecolorallocate( $im, rand(100, 224), rand(100, 224), rand(128, 224) );
}
$values = array( 100, 200, 50, 100, 43, 32 ); $werte = _errechne_gradzahlen( $values ); $width = 200;
$height = 200;
$half_width = floor( $width / 2 );
$half_height = floor($height / 2);
$im = ImageCreateTrueColor( $width, $height );
foreach( $werte as $key => $wert ) {
$color = _randomcol( $im );
$shadow = $color - 20000; $colors[] = $color;
$shadows[] = $shadow;
for ($i = ($half_height + 10); $i > $half_height; $i--) {
imagefilledarc(
$im,
$half_width, $i,
$width, $half_height,
$offset, ($offset + $wert), $shadows[$key], IMG_ARC_NOFILL);
}
$offset = $offset + $wert;
}
$offset = 0;
foreach( $werte as $key => $wert ) { imagefilledarc(
$im,
$half_width, $half_width,
$width, $half_height, $offset, ($offset + $wert),
$colors[$key], IMG_ARC_PIE);
$offset = $offset + $wert;
}
header( "Content-type: image/png" );
imagepng ( $im );
imagedestroy( $im );
?>
sry for my crab english and the disordered code, i cut and 'translate' it from a class i wrote before.
poopie
05-Aug-2004 09:08
The examples given so far for drawing a 3D looking pie chart are extremely inefficient and can give a huge performance hit to scripts that draw a lot of pies and in particular those that do offline processing on disk, rather than send a single pie chart to the browser (either way this modification saves you a lot of CPU cycles).
Modify the portion of the code that creates the 3D effect to only draw the outline (with IMG_ARC_NOFILL) of the pie layers below the top filled pie:
// make the 3D effect (modified for the original example)
for ($i = 60; $i >= 50; $i--) {
imagefilledarc($image, 50, $i, 100, 50, 0, 45, $darknavy, IMG_ARC_NOFILL);
imagefilledarc($image, 50, $i, 100, 50, 45, 75 , $darkgray, IMG_ARC_NOFILL);
imagefilledarc($image, 50, $i, 100, 50, 75, 360 , $darkred, IMG_ARC_NOFILL);
}
Note the >= on the for loop, which fills in a gap that is created without the =
hans at lintoo dot dk
17-Jul-2004 12:14
I found some bugs in my script.. and therefore I am posting the fixes:
Bugs:
<?php
$drakcolor[2] = imagecolorallocate($im, 139, 0, 1);
$darkcolor[2] = imagecolorallocate($im, 139, 0, 1);
?>
And then if you modify the code a little and, then make a image 200x125 then you won't be wasting space.
Changes:
<?php
$im = imagecreate (200, 125);
for ($i = 60; $i > 50; $i--) {
imagefilledarc($im, 100, $i, 200, 100, $anglesum[$f], $anglesum[$n], $darkcolor[$f], IMG_ARC_PIE);
}
imagefilledarc($im, 100, 50, 200, 100, $anglesum[$n], $anglesum[$i], $randcolor[$n], IMG_ARC_PIE);
?>
you can see a online demo at: http://webstatistik.lintoo.dk/
hans at lintoo dot dk
16-Jul-2004 03:27
I modificed the code to make a 3d pie from data collected elsewhere.... in this case it was for a statistics page...
when testing you can use:
enjoy
<?php
$im = imagecreate (200, 200);
$background = imagecolorallocate($im, 226, 226, 226);
$randcolor[0] = imagecolorallocate($im, 255, 203, 3);
$randcolor[1] = imagecolorallocate($im, 220, 101, 29);
$randcolor[2] = imagecolorallocate($im, 189, 24, 51);
$randcolor[3] = imagecolorallocate($im, 214, 0, 127);
$randcolor[4] = imagecolorallocate($im, 98, 1, 96);
$randcolor[5] = imagecolorallocate($im, 0, 62, 136);
$randcolor[6] = imagecolorallocate($im, 0, 102, 179);
$randcolor[7] = imagecolorallocate($im, 0, 145, 195);
$randcolor[8] = imagecolorallocate($im, 0, 115, 106);
$randcolor[9] = imagecolorallocate($im, 178, 210, 52);
$randcolor[10] = imagecolorallocate($im, 137, 91, 74);
$randcolor[11] = imagecolorallocate($im, 82, 56, 47);
$darkcolor[0] = imagecolorallocate($im, 205, 153, 0);
$darkcolor[1] = imagecolorallocate($im, 170, 51, 0);
$drakcolor[2] = imagecolorallocate($im, 139, 0, 1);
$darkcolor[3] = imagecolorallocate($im, 164, 0, 77);
$darkcolor[4] = imagecolorallocate($im, 48, 0, 46);
$darkcolor[5] = imagecolorallocate($im, 0, 12, 86);
$darkcolor[6] = imagecolorallocate($im, 0, 52, 129);
$darkcolor[7] = imagecolorallocate($im, 0, 95, 145);
$darkcolor[8] = imagecolorallocate($im, 0, 65, 56);
$darkcolor[9] = imagecolorallocate($im, 128, 160, 2);
$darkcolor[10] = imagecolorallocate($im, 87, 41, 24);
$darkcolor[11] = imagecolorallocate($im, 32, 6, 0);
$i = 0;
while ($i <= 11) {
$data[$i] = $_GET[++$i];
}
$datasum = array_sum($data);
$anglesum[0] = 0;
$angle[0] = 0;
$i = 0;
while ($i <= 11) {
++$i;
$n = $i - 1;
$part[$i] = $data[$n] / $datasum;
$angle[$i] = floor($part[$i] * 360);
$anglesum[$i] = array_sum($angle);
}
$n = 0;$i=0;
while ($n <= 11) {
++$n;
$f = $n - 1;
if ($angle[$n] != 0) {
for ($i = 110; $i > 100; $i--) {
imagefilledarc($im, 100, $i, 200, 100, $anglesum[$f], $anglesum[$n], $darkcolor[$f], IMG_ARC_PIE);
}
}
}
$i = 0;
while ($i <= 11) {
++$i;
$n = $i - 1;
if ($angle[$i] != 0) {
imagefilledarc($im, 100, 100, 200, 100, $anglesum[$n], $anglesum[$i], $randcolor[$n], IMG_ARC_PIE);
}
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
caist - www.caist.com
07-Jan-2004 08:11
if you want to place a text onto the edge of a circle you need to
get a point on the circle:
$pos_x=$radius*sin(deg2rad($angle));
$pos_y=sqrt($radius*$radius-$pos_x*$pos_x);
if you want the point to be in the middle of a pie piece
you need a starting and ending angle
$pos_x=$radius*sin(deg2rad($angle_end-($angle_start)/2));
$pos_y=sqrt($radius*$radius-$pos_x*$pos_x);
hope it helps
jphb at scit dot wlv dot ac dot uk
09-Jul-2003 10:19
Note that whereas imagefilledarc() measures angles from
3 o'clock clockwise, imagettftext() measures angles from
3 o'clock anticlockwise so if you're, for example, trying to
annotate a pie chart it can get a bit confusing.
jcoder at insightbb dot com
04-Oct-2002 07:29
/*
From a set of numbers from a query return and an
array of colors this will generate a pie chart
*/
$a=array();
while($d=mysql_fetch_array($start_angle_query))
{
$a[]=$d[0];
}
$b=array();
$b =$a;
/* make two arrays one for start of angle 2nd
for end of angle
*/
$sum=array_sum($a);
$degree_adjuster=360/$sum;
//constant for circle
array_unshift($a,0 );
// add 0 for start angle
$start_angle=array();
// new array to hold values for start vector
$end_angle=array();
//new array to hold values for end vector
array_pop($a);
//trim one array for start so numbers don't end on 360
while($a) //iterate till array is empty
{
array_push($start_angle,(end($start_angle)+ $a[0]));
/*enter numbers in array $start_angle adding the last number in
array and adding the first number from modified original array*/
array_shift($a);//delete first value of array
}
while($b)
{
array_push($end_angle,(end($end_angle)+$b[0]));
array_shift($b);
}
for($i=0;$i<$angle_num_rows;$i++)
{
imagefilledarc($pie,130,149,260,260,
($start_angle[$i]*$degree_adjuster),$end_angle[$i]*$degree_adjuster,
$colors[$i],IMG_ARC_FILL);
}
| |