免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1436 | 回复: 0
打印 上一主题 下一主题

利用PHP的画图函数画百分比饼图的例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-21 18:37 |只看该作者 |倒序浏览

利用PHP的画图函数画百分比饼图的例子
注:一、要使用PHP的画图函数,必须激活gd2的dll扩展,在windows下,只要去掉php.ini中的
      extension=php_gd2.dll前面的分号就可以。
      二、本例中的drawcake,drawcakef1两个函数就是主要的画饼图的函数,调整椭圆的长焦、短焦,和圆柱的高度可以调整出你想要的外观,drawcakef2函数构造了一个百分比数组,用来演示。
      三、eclipse_point函数用于从椭圆曲线上取得点,createColorTable函数用于生成一个色彩表
      四、Header("Content-type: image/gif");用于告诉浏览器这个页面的类型为git图片,这一点非常重要,否则你显示出来的可能是一堆乱码。一个需要注意的问题是header消息需要在最开始的时候发送,所以如果你在其他页面include了下面的代码,请确保cake.php的文件头、尾,即在之外,没有任何的空行或者空格,因为这些空行或者空格都可能被当作html代码而在include的时候先于header消息执行了。这时你很可能会看到一个这样的
Warning: Cannot modify header information - headers already sent by (output started at D:\html\cake.php:384) in D:\html\demo.php on line 27
当你在做第六步的时候很可能会出现这个错误。

五、下面是示例代码,请将其保存在一个文件,如cake.php中。访问这个页面,将看到图片。
六、新建一个文件如demo.php, 然后在将
Header("Content-type: image/gif");
drawcakef2(280, 160,100);
这两行代码移到这个文件中,即可以在其他文档中调用画图函数。
也可以新建一个html文件,其内容类似,这同样可以画出图片。


=0&&$degree270&&$degree0 && $degree=0&&$degree270&&$degree0 && $degree>16) & 255;
$g = ($value>>8) & 255;
$b = $value & 255;

// return (array("red"=>$r, "green"=>$g, "blue"=>$b));
return (array($r,$g,$b));
}
function ImageColorAllocateEx($im,$color_value) {
list($r, $g, $b) = color_format_convert($color_value);
return imagecolorallocate($im, $r, $g, $b);
}
function createColorTable($im) {
$val_tab = array(
// "AliceBlue" =>0xF0F8FF,
// "AntiqueWhite" =>0xFAEBD7,
// "Aqua" =>0x00FFFF,
// "Aquamarine" =>0x7FFFD4,
// "Azure" =>0xF0FFFF,
// "Beige" =>0xF5F5DC,
// "Bisque" =>0xFFE4C4,
// "Black" =>0x000000,
// "BlanchedAlmond" =>0xFFEBCD,
// "Blue" =>0x0000FF,
// "BlueViolet" =>0x8A2BE2,
// "Brown" =>0xA52A2A,
// "BurlyWood" =>0xDEB887,
// "CadetBlue" =>0x5F9EA0,
// "Chartreuse" =>0x7FFF00,
// "Chocolate" =>0xD2691E,
// "Coral" =>0xFF7F50,
// "CornflowerBlue" =>0x6495ED,
// "Cornsilk" =>0xFFF8DC,
// "Crimson" =>0xDC143C,
// "Cyan" =>0x00FFFF,
// "DarkBlue" =>0x00008B,
// "DarkCyan" =>0x008B8B,
// "DarkGoldenRod" =>0xB8860B,
// "DarkGray" =>0xA9A9A9,
// "DarkGreen" =>0x006400,
// "DarkKhaki" =>0xBDB76B,
// "DarkMagenta" =>0x8B008B,
// "DarkOliveGreen" =>0x556B2F,
// "Darkorange" =>0xFF8C00,
// "DarkOrchid" =>0x9932CC,
// "DarkRed" =>0x8B0000,
// "DarkSalmon" =>0xE9967A,
// "DarkSeaGreen" =>0x8FBC8F,
// "DarkSlateBlue" =>0x483D8B,
// "DarkSlateGray" =>0x2F4F4F,
// "DarkTurquoise" =>0x00CED1,
// "DarkViolet" =>0x9400D3,
// "DeepPink" =>0xFF1493,
// "DeepSkyBlue" =>0x00BFFF,
// "DimGray" =>0x696969,
// "DodgerBlue" =>0x1E90FF,
// "Feldspar" =>0xD19275,
// "FireBrick" =>0xB22222,
// "FloralWhite" =>0xFFFAF0,
// "ForestGreen" =>0x228B22,
// "Fuchsia" =>0xFF00FF,
// "Gainsboro" =>0xDCDCDC,
// "GhostWhite" =>0xF8F8FF,
// "Gold" =>0xFFD700,
// "GoldenRod" =>0xDAA520,
// "Gray" =>0x808080,
// "Green" =>0x008000,
// "GreenYellow" =>0xADFF2F,
// "HoneyDew" =>0xF0FFF0,
// "HotPink" =>0xFF69B4,
// "IndianRed " =>0xCD5C5C,
// "Indigo " =>0x4B0082,
// "Ivory" =>0xFFFFF0,
// "Khaki" =>0xF0E68C,
// "Lavender" =>0xE6E6FA,
// "LavenderBlush" =>0xFFF0F5,
// "LawnGreen" =>0x7CFC00,
// "LemonChiffon" =>0xFFFACD,
// "LightBlue" =>0xADD8E6,
// "LightCoral" =>0xF08080,
// "LightCyan" =>0xE0FFFF,
// "LightGoldenRodYellow" =>0xFAFAD2,
// "LightGrey" =>0xD3D3D3,  
"LightGreen" =>0x90EE90,
"LightPink" =>0xFFB6C1,
"LightSalmon" =>0xFFA07A,
"LightSeaGreen" =>0x20B2AA,
"LightSkyBlue" =>0x87CEFA,
"LightSlateBlue" =>0x8470FF,
"LightSlateGray" =>0x778899,
"LightSteelBlue" =>0xB0C4DE,
"LightYellow" =>0xFFFFE0,
"Lime" =>0x00FF00,
"LimeGreen" =>0x32CD32,
"Linen" =>0xFAF0E6,
"Magenta" =>0xFF00FF,
"Maroon" =>0x800000,
"MediumAquaMarine" =>0x66CDAA,
"MediumBlue" =>0x0000CD,
"MediumOrchid" =>0xBA55D3,
"MediumPurple" =>0x9370D8,
"MediumSeaGreen" =>0x3CB371,
"MediumSlateBlue" =>0x7B68EE,
"MediumSpringGreen" =>0x00FA9A,
"MediumTurquoise" =>0x48D1CC,
"MediumVioletRed" =>0xC71585,
"MidnightBlue" =>0x191970,
"MintCream" =>0xF5FFFA,
"MistyRose" =>0xFFE4E1,
"Moccasin" =>0xFFE4B5,
"NavajoWhite" =>0xFFDEAD,
"Navy" =>0x000080,
"OldLace" =>0xFDF5E6,
"Olive" =>0x808000,
"OliveDrab" =>0x6B8E23,
"Orange" =>0xFFA500,
"OrangeRed" =>0xFF4500,
"Orchid" =>0xDA70D6,
"PaleGoldenRod" =>0xEEE8AA,
"PaleGreen" =>0x98FB98,
"PaleTurquoise" =>0xAFEEEE,
"PaleVioletRed" =>0xD87093,
"PapayaWhip" =>0xFFEFD5,
"PeachPuff" =>0xFFDAB9,
"Peru" =>0xCD853F,
"Pink" =>0xFFC0CB,
"Plum" =>0xDDA0DD,
"PowderBlue" =>0xB0E0E6,
"Purple" =>0x800080,
"Red" =>0xFF0000,
"RosyBrown" =>0xBC8F8F,
"RoyalBlue" =>0x4169E1,
"SaddleBrown" =>0x8B4513,
"Salmon" =>0xFA8072,
"SandyBrown" =>0xF4A460,
"SeaGreen" =>0x2E8B57,
"SeaShell" =>0xFFF5EE,
"Sienna" =>0xA0522D,
"Silver" =>0xC0C0C0,
"SkyBlue" =>0x87CEEB,
"SlateBlue" =>0x6A5ACD,
"SlateGray" =>0x708090,
"Snow" =>0xFFFAFA,
"SpringGreen" =>0x00FF7F,
"SteelBlue" =>0x4682B4,
"Tan" =>0xD2B48C,
"Teal" =>0x008080,
"Thistle" =>0xD8BFD8,
"Tomato" =>0xFF6347,
"Turquoise" =>0x40E0D0,
"Violet" =>0xEE82EE,
"VioletRed" =>0xD02090,
"Wheat" =>0xF5DEB3,
"White" =>0xFFFFFF,
"WhiteSmoke" =>0xF5F5F5,
"Yellow" =>0xFFFF00,
"YellowGreen" =>0x9ACD32
);
$color_talbe = array();
foreach($val_tab as $name => $val) {
$name = ImageColorAllocateEx($im, $val);
array_push($color_talbe, $name);
}
return $color_talbe;
}
function drawcake(
   $imx, $imy,
   $centerx, $centery,
   $diaw, $diah,
   $height,
   $pers=array(),
   $title="Graph Test") {
//Header("Content-type: image/gif");
$im = imagecreate($imx,$imy);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$blue = ImageColorAllocate($im, 0,0,255);
$blue_light = ImageColorAllocate($im, 100,100,255);
$color = createColorTable($im);
//imageline($im, 1, 1, 350, 25, $black);
//draw cake
imagearc($im, $centerx, $centery, $diaw, $diah, 0, 360, $white);
imageline($im, $centerx-$diaw/2, $centery, $centerx-$diaw/2, $centery+$height, $white);
imageline($im, $centerx+$diaw/2, $centery, $centerx+$diaw/2, $centery+$height, $white);
imagearc($im, $centerx, $centery+$height, $diaw, $diah, 0, 180, $white);
imagefill($im, $centerx, $centery, $blue);
imagefill($im, $centerx, $centery+$diah/2+$height-1, $blue_light);
$startDegree = 0;
$endDegree = 0;
$pos = 0;
$left_floor = 50;
$right_floor = 50;
//draw pieces
foreach($pers as $per) {
$startDegree = $endDegree;
// $per = new Percentage();
$endDegree = $per->percent*360.0/100.0 + $startDegree;
list($sx, $sy) = eclipse_point($startDegree, $diaw, $diah, $centerx, $centery);
imageline($im, $centerx, $centery, $sx, $sy, $white);
list($ex, $ey) = eclipse_point($endDegree, $diaw, $diah, $centerx, $centery);
imageline($im, $centerx, $centery, $ex, $ey, $white);
list($innerx, $innery) = get_inner_point($startDegree, $endDegree, $diaw, $diah, $centerx, $centery);
imagefill($im, $innerx, $innery, $color[$pos]);
//draw demos
list($midx,$midy) = eclipse_point(($endDegree+$startDegree)/2, $diaw, $diah, $centerx, $centery);
if($midx > $centerx) {
  //draw color box
  ImageFilledRectangle($im, $imx-160,$right_floor,$imx-150,$right_floor+10, $color[$pos]);
  //draw name and percentage
  imagestring($im, 3, $imx-140, $right_floor, $per->name, $color[$pos]);
  imagestring($im, 3, $imx-120, $right_floor+10, "$per->percent%", $color[$pos]);
  //draw link
  imageline($im, $midx, $midy, $centerx+$diaw/2+20,$right_floor+5, $color[$pos]);
  imageline($im, $imx-160,$right_floor+5,$centerx+$diaw/2+20,$right_floor+5, $color[$pos]);
  $right_floor += 110;
}
else {
  //draw color box
  ImageFilledRectangle($im, 150,$left_floor,160,$left_floor+10, $color[$pos]);
  //draw name and percentage
  imagestring($im, 3, 10, $left_floor, $per->name, $color[$pos]);
  imagestring($im, 3, 30, $left_floor+10, "$per->percent%", $color[$pos]);
  //draw link
  imageline($im, $midx, $midy, $centerx-$diaw/2-20,$left_floor+5, $color[$pos]);
  imageline($im, 150,$left_floor+5,$centerx-$diaw/2-20,$left_floor+5, $color[$pos]);
  $left_floor += 50;
}

$pos++;
//print debug info
//$pos--;
//imagestring($im, 3, 4, 30+$pos*120, $midx, $red);
//imagestring($im, 3, 4, 50+$pos*120, $midy, $red);
//imagestring($im, 3, 4, 70+$pos*120, "next", $red);
//imagestring($im, 3, 4, 90+$pos*120, $ex, $red);
//imagestring($im, 3, 4, 110+$pos*120, $ey, $red);
//imagestring($im, 3, 4, 120+$pos*120, "next 1", $red);
//$pos++;
imagestring($im, 3, 4, 300+ $pos*15, "$endDegree", $white);
}
imagestring($im, 5, 4, 10, $title, $white);
ImageGif($im);
ImageDestroy($im);
}
function drawcakef1(
   $diaw, $diah,
   $height,
   $pers=array(),
   $title="Graph Test") {
$centerx = $diaw/2 + 150+20+20+10;
$centery = $diah/2 + 50;
$imx = $centerx+$diaw/2 + 150+20+20+10;
$imy = $centery+$diah/2 + $height + 100;
drawcake($imx,$imy, $centerx, $centery, $diaw, $diah, $height, $pers, $title);
}
function drawcakef2(
   $diaw, $diah,
   $height,
   $pers=array(),
   $title="Graph Test") {
$per1 = new Percentage();
$per1->name = "Sport Channel";
$per1->percent = 12;
$per2 = new Percentage();
$per2->name = "Game Channel";
$per2->percent = 28;
$per3 = new Percentage();
$per3->name = "Movie & TV";
$per3->percent = 20;
$per4 = new Percentage();
$per4->name = "NEWS Channel";
$per4->percent = 5;
$per5 = new Percentage();
$per5->name = "GZTV teaching";
$per5->percent = 35;
array_push($pers,$per1,$per2,$per3,$per4,$per5);
drawcakef1($diaw,$diah,$height,$pers,$title);
}
class Percentage {
public $name;
public $percent;
public $quntity;

public function Percentage() {
}
}
Header("Content-type: image/gif");
drawcakef2(280, 160,100);
//eclipse_point(330,280,160,180,110);
//list($re,$ge,$be) = color_format_convert(0xaba4b9);
//printf("red is %x \n", $re);
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25317/showart_188526.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP