显示饼图正常,只是将vote2和vote3不能分开,两种颜色混淆了,只显示vote2的颜色,vote3黄色显示为vote2的橙色,大家帮忙看看
查看饼图URL:
http://localhost/poll/voteimg.php?vote1=1&vote2=1&vote3=1&vote4=1&vote5=1
通过GET方法传递值
代码:
1
2$vote1 = $_GET['vote1'];
3$vote2 = $_GET['vote2'];
4$vote3 = $_GET['vote3'];
5$vote4 = $_GET['vote4'];
6$vote5 = $_GET['vote5'];
7$votez = $vote1+$vote2+$vote3+$vote4+$vote5;
8if($vote1>0){
9$vote1a = $vote1/$votez*360;
10}
11else{
12$vote1a = 0;
13}
14if($vote2>0){
15$vote2a = $vote2/$votez*360;
16}
17else{
18$vote2a = 0;
19}
20if($vote3>0){
21$vote3a = $vote3/$votez*360;
22}
23else{
24$vote3a = 0;
25}
26if($vote4>0){
27$vote4a = $vote4/$votez*360;
28}
29else{
30$vote4a = 0;
31}
32if($vote5>0){
33$vote5a = $vote5/$votez*360;
34}
35else{
36$vote5a = 0;
37}
38$img_output=ImageCreate(188,170);
39$color0a = imagecolorallocate($img_output,255,255,255); //白色
40$color0b = imagecolorallocate($img_output,0,0,0); //黑色
41$color1a = imagecolorallocate($img_output,255,0,0); //红色a
42$color1b = imagecolorallocate($img_output,200,0,0); //红色b
43$color2a = imagecolorallocate($img_output,255,128,64); //橙色a
44$color2b = imagecolorallocate($img_output,198,64,0); //橙色b
45$color3a = imagecolorallocate($img_output,255,255,0); //黄色a
46$color3b = imagecolorallocate($img_output,200,200,0); //黄色b
47$color4a = imagecolorallocate($img_output,133,248,18); //绿色a
48$color4b = imagecolorallocate($img_output,0,200,0); //绿色b
49$color5a = imagecolorallocate($img_output,0,0,255); //蓝色a
50$color5b = imagecolorallocate($img_output,0,0,200); //蓝色b
51imagecolortransparent($img_output,$color0a);
52$picture_x = 93;
53$picture_y = 80;
54$picture_w = 180;
55$picture_h = 140;
56for($temp_a=$picture_y+11;$temp_a>$picture_y;$temp_a--){
57if($vote5>0){
58for($temp_b=$picture_w;$temp_b>0;$temp_b--){
59imagearc($img_output,$picture_x,$temp_a,$temp_b,$temp_b*5/8,0,$vote5a,$color5b);
60}
61}
62if($vote4>0){
63for($temp_b=$picture_w;$temp_b>0;$temp_b--){
64imagearc($img_output,$picture_x,$temp_a,$temp_b,$temp_b*5/8,$vote5a,$vote5a+$vote4a,$color4b);
65}
66}
67if($vote3>0){
68for($temp_b=$picture_w;$temp_b>0;$temp_b--){
69imagearc($img_output,$picture_x,$temp_a,$temp_b,$temp_b*5/8,$vote5a+$vote4a,$vote5a+$vote4a+$vote3a,$color3b);
70}
71}
72if($vote2>0){
73for($temp_b=$picture_w;$temp_b>0;$temp_b--){
74imagearc($img_output,$picture_x,$temp_a,$temp_b,$temp_b*5/8,$vote5a+$vote4a+vote3a,$vote5a+$vote4a+$vote3a+$vote2a,$color2b);
75}
76}
77if($vote1>0){
78for($temp_b=$picture_w;$temp_b>0;$temp_b--){
79imagearc($img_output,$picture_x,$temp_a,$temp_b,$temp_b*5/8,$vote5a+$vote4a+$vote3a+$vote2a,360,$color1b);
80}
81}
82}
83if($vote5>0){
84for($temp_b=$picture_w;$temp_b>0;$temp_b--){
85imagearc($img_output,$picture_x,$picture_y,$temp_b,$temp_b*5/8,0,$vote5a,$color5a);
86}
87}
88if($vote4>0){
89for($temp_b=$picture_w;$temp_b>0;$temp_b--){
90imagearc($img_output,$picture_x,$picture_y,$temp_b,$temp_b*5/8,$vote5a,$vote5a+$vote4a,$color4a);
91}
92}
93if($vote3>0){
94for($temp_b=$picture_w;$temp_b>0;$temp_b--){
95imagearc($img_output,$picture_x,$picture_y,$temp_b,$temp_b*5/8,$vote5a+$vote4a,$vote5a+$vote4a+$vote3a,$color3a);
96}
97}
98if($vote2>0){
99for($temp_b=$picture_w;$temp_b>0;$temp_b--){
100imagearc($img_output,$picture_x,$picture_y,$temp_b,$temp_b*5/8,$vote5a+$vote4a+vote3a,$vote5a+$vote4a+$vote3a+$vote2a,$color2a);
101}
102}
103if($vote1>0){
104for($temp_b=$picture_w;$temp_b>0;$temp_b--){
105imagearc($img_output,$picture_x,$picture_y,$temp_b,$temp_b*5/8,$vote5a+$vote4a+$vote3a+$vote2a,360,$color1a);
106}
107}
108Header("Content-type: image/PNG");
109ImagePNG($img_output);
110ImageDestroy($img_output);
---------------------------------------------------------------
你的算法太烦琐了
饼图是由多个扇形组成的。
画扇形是这样的:
1、画一弧
2、求出该弧在圆周上的两个端点的坐标
3、分别从圆心到弧的端点画线
4、视需要充填该区域
---------------------------------------------------------------
可以参考这个:http://unix.1816.net/bbs/read.php?fid=1&tid=12&fpage=2
1
2class BiscuitPicture3D//类名称
3{
4var $image;
5var $width;
6var $height;
7var $x;
8var $y;
9var $z;
10var $font;
11var $percents;
12var $darkColors;
13var $lightColors;
14function BiscuitPicture3D( $width, $height, $width3D, $height3D, $startX, $startY, $startZ ){
15$this->image = imagecreate( $width, $height );
16$white = imagecolorallocate( $this->image, 0xff, 0xff, 0xff );
17$this->width = $width3D;
18$this->height = $height3D;
19$this->x = $startX;
20$this->y = $startY;
21$this->z = $startZ;
22$this->font = '/var/www/simsun.ttc'
23}
24function drawPic(){
25$i = 0;
26while( isset($this->percents[$i]) ){
27$this->percents[$i] = 360 * $this->percents[$i] / 100;
28$i++;
29}
30for ( $i = $this->y; $i > $this->y - $this->z; $i-- ){
31$j = 0;
32while( isset($this->percents[$j+1]) ){
33if ( $j == 0 ){
34imagefilledarc( $this->image, $this->x, $i, $this->width, $this->height, 0, $this->percents[$j], $this->darkColors[$j], IMG_ARC_PIE );
35}
36else{
37$startPos = 0;
38for( $temp = 0; $temp < $j; $temp++ ){
39$startPos = $startPos + $this->percents[$temp];
40}
41imagefilledarc( $this->image, $this->x, $i, $this->width, $this->height, $startPos, $this->percents[$j]+$startPos, $this->darkColors[$j], IMG_ARC_PIE );
42}
43$j++;
44}
45imagefilledarc( $this->image, $this->x, $i, $this->width, $this->height, 360-$this->percents[$j], 360, $this->darkColors[$j], IMG_ARC_PIE );
46}
47$j = 0;
48while( isset($this->percents[$j+1]) ){
49if ( $j == 0 ){
50imagefilledarc( $this->image, $this->x, $this->y-$this->z, $this->width, $this->height, 0, $this->percents[$j], $this->lightColors[$j], IMG_ARC_PIE );
51}
52else{
53$startPos = 0;
54for( $temp = 0; $temp < $j; $temp++ ){
55$startPos = $startPos + $this->percents[$temp];
56}
57imagefilledarc( $this->image, $this->x, $this->y-$this->z, $this->width, $this->height, $startPos, $this->percents[$j]+$startPos, $this->lightColors[$j], IMG_ARC_PIE );
58}
59$j++;
60}
61imagefilledarc( $this->image, $this->x, $this->y-$this->z, $this->width, $this->height, 360-$this->percents[$j], 360, $this->lightColors[$j], IMG_ARC_PIE );
62}
63function showPic(){
64header('Content-type: image/png');
65imagepng( $this->image );
66imagedestroy( $this->image );
67}
68function addItem( $percent, $R1, $G1, $B1, $R2, $G2, $B2 ){
69$this->percents[] = $percent;
70$this->darkColors[] = imagecolorallocate( $this->image, $R1, $G1, $B1 );
71$this->lightColors[] = imagecolorallocate( $this->image, $R2, $G2, $B2 );
72}
73function writeChinese( $str, $fontSize, $startX, $startY, $R, $G, $B ){
74$str = iconv( "gb2312", "utf-8", $str );
75$color = imagecolorallocate( $this->image, $R, $G, $B );
76imagettftext( $this->image, $fontSize, 0, $startX, $startY, $color, $this->font, $str );}
77}
78$picWidth = 400;
79$picHeight = 400;
80$width3D = 300;
81$height3D = 100;
82$x = $picWidth / 2;
83$y = $picHeight / 2;
84$z = 40;
85$b = new BiscuitPicture3D( $picWidth, $picHeight, $width3D, $height3D, $x, $y, $z );
86$b->addItem( 10, 0x90, 0x00, 0x00, 0xff, 0x00, 0x00 );
87$b->addItem( 15, 0xe1, 0xe1, 0x00, 0xff, 0xff, 0x00 );
88$b->addItem( 20, 0x00, 0x00, 0x50, 0x00, 0x00, 0x80 );
89$b->addItem( 25, 0x90, 0x90, 0x90, 0xC0, 0xC0, 0xC0 );
90$b->addItem( 30, 16, 185, 35, 16, 208, 35 );
91$b->drawPic();
92$b->writeChinese( '3D饼状图', 22, 150, 350, 0, 0, 0 );
93$b->showPic();