PHP立体3D效果饼状统计图的生成代码

发布时间:2020-12-25编辑:脚本学堂
分享一例php代码,生成立体的带3D效果的饼状统计图,有需要的朋友参考学习下。

本节内容:
PHP生成立体3D效果的饼状图,php饼状图代码示例。

例子:
 

复制代码 代码示例:

<?php
/**
* php生成有立体3D效果的饼状图
* edit:www.jbxue.c
*/

//转载请标注:制作人Seven(QQ9256114)
//参数含义(数组,横坐标,纵坐标,图表的宽度,图表的高度,图表标题,单位)
Function table2($stat_array,$table_left,$table_top,$all_width,$all_height,$table_title,$unit){
   $num =Count($stat_array);

   For($i=0;$i<$num;$i++){
      Static $allvalues=0;
      $allvalues = $allvalues+$stat_array[$i][0];
   }

$pie=Array();
For($i=0;$i<$num;$i++){
Static $k=0;
$pie[$i]=Round($stat_array[$i][0]/$allvalues,2);
$k=$k+$pie[$i];
}

$pie[$num]=Round((1-$k),2);

Echo "<v:shapetype id='Cake_3D' coordsize='21600,21600' o:spt='95' adj='11796480,5400' path='al10800,10800@0@0@2@14,10800,10800,10800,10800@3@15xe'></v:shapetype>";

Echo "<v:shapetype id='3dtxt' coordsize='21600,21600' o:spt='136' adj='10800' path='m@7,l@8,m@5,21600l@6,21600e'> ";
Echo "<v:path textpathok='t' o:connecttype='custom' o:connectlocs='@9,0;@10,10800;@11,21600;@12,10800' o:connectangles='270,180,90,0'/>";
Echo "<v:textpath on='t' fitshape='t'/>";
Echo "<o:lock v:ext='edit' text='t' shapetype='t'/>";
Echo "</v:shapetype>";

Echo "<v:rect id='background' style='position:absolute;left:".$table_left."px;top:".$table_top."px;WIDTH:".$all_width."px;HEIGHT:".$all_height."px;' fillcolor='#EFEFEF' strokecolor='gray'>";
Echo "<v:shadow on='t' type='single' color='silver' offset='4pt,4pt'/>";
Echo "</v:rect>";

Echo "<v:group ID='table' style='position:absolute;left:".$table_left."px;top:".$table_top."px;WIDTH:".$all_width."px;HEIGHT:".$all_height."px;' coordsize = '21000,11500'>";
Echo "<v:Rect style='position:relative;left:500;top:200;width:20000;height:800'filled='false' stroked='false'>";
Echo "<v:TextBox inset='0pt,0pt,0pt,0pt'>";
Echo "<table width='100%' border='0' align='center' cellspacing='0'>";
Echo "<tr><td align='center' valign='middle'><div style='font-size:15pt; font-family:黑体;'><B>".$table_title."</B></div></td></tr>";
Echo "</table>";
Echo "</v:TextBox>";
Echo "</v:Rect> ";

Echo "<v:rect id='back' style='position:relative;left:500;top:1000;width:20000; height:10000;' onmouseover='movereset(1)' onmouseout='movereset(0)' fillcolor='#9cf' strokecolor='#888888'>";
Echo "<v:fill rotate='t' angle='-45' focus='100%' type='gradient'/>";
Echo "</v:rect>";

Echo "<v:rect id='back' style='position:relative;left:15000;top:1400;width:5000; height:".(($num+1)*9000/11+200).";' fillcolor='#9cf' stroked='t' strokecolor='#0099ff'>";
Echo "<v:fill rotate='t' angle='-175' focus='100%' type='gradient'/>";
Echo "<v:shadow on='t' type='single' color='silver' offset='3pt,3pt'/>";
Echo "</v:rect>";

Echo "<v:Rect style='position:relative;left:15500;top:1500;width:4000;height:700' fillcolor='#000000' stroked='f' strokecolor='#000000'>";
Echo "<v:TextBox inset='8pt,4pt,3pt,3pt' style='font-size:11pt;'><div align='left'><font color='#ffffff'><B>总数:".$allvalues.$unit."</B></font></div></v:TextBox>";
Echo "</v:Rect> ";

For($i=0;$i<$num;$i++){
Echo "<v:Rect id='rec".$i."' style='position:relative;left:15400;top:".(($i+1)*9000/11+1450).";width:4300;height:800;display:none' fillcolor='#efefef' strokecolor='".$stat_array[$i][2]."'>";
Echo "<v:fill opacity='.6' color2='fill darken(118)' o:opacity2='.6' rotate='t' method='linear sigma' focus='100%' type='gradient'/>";
Echo "</v:Rect>";
Echo "<v:Rect style='position:relative;left:15500;top:".(($i+1)*9000/11+1500).";width:600;height:700' fillcolor='".$stat_array[$i][2]."' stroked='f'/>";
Echo "<v:Rect style='position:relative;left:16300;top:".(($i+1)*9000/11+1500).";width:3400;height:700' filled='f' stroked='f'>";
Echo "<v:TextBox inset='0pt,5pt,0pt,0pt' style='font-size:9pt;'><div align='left'>".$stat_array[$i][1].":".$stat_array[$i][0].$unit."</div></v:TextBox>";
Echo "</v:Rect> ";
}

Echo "</v:group>";

$k1=180;
$k4=10;
For($i=0;$i<$num;$i++){
$k2=360*$pie[$i]/2;
$k3=$k1+$k2;
IF($k3>=360){
   $k3=$k3-360;
}
$kkk=(-11796480*$pie[$i]+5898240);

$k5=3.1414926*2*(180-($k3-180))/360;
$R=$all_height/2;
$txt_x = $table_left+$all_height/8-30+$R+$R*sin($k5)*0.7;
$txt_y = $table_top+$all_height/14-39+$R+$R*cos($k5)*0.7*0.5;

$titlestr = "名 称:".$stat_array[$i][1]."&#13;&#10;数 值:".$stat_array[$i][0].$unit."&#13;&#10;所占比例:".($pie[$i]*100)."% ";
Echo "<div style='cursor:hand;'>";
Echo "<v:shape id='cake".$i."' type='#Cake_3D' title='".$titlestr."' style='position:absolute;left:".($table_left+$all_height/8)."px;top:".($table_top+$all_height/14)."px;WIDTH:".$all_height."px;HEIGHT:".$all_height."px;rotation:".$k3.";z-index:".$k4."' adj='".$kkk.",0' fillcolor='".$stat_array[$i][2]."' onmouseover='moveup(cake".$i.",".($table_top+$all_height/14).",txt".$i.",rec".$i.")'; onmouseout='movedown(cake".$i.",".($table_top+$all_height/14).",txt".$i.",rec".$i.");'>";
Echo "<v:fill opacity='60293f' color2='fill lighten(120)' o:opacity2='60293f' rotate='t' angle='-135' method='linear sigma' focus='100%' type='gradient'/>";
Echo "<o:extrusion v:ext='view' on='t'backdepth='25' rotationangle='60' viewpoint='0,0'viewpointorigin='0,0' skewamt='0' lightposition='-50000,-50000' lightposition2='50000'/>";
Echo "</v:shape>";
Echo "<v:shape id='txt".$i."' type='#3dtxt' style='position:absolute;left:".$txt_x."px;top:".$txt_y."px;z-index:20;display:none;width:50; height:18;' fillcolor='#ffffff' onmouseover='ontxt(cake".$i.",".($table_top+$all_height/14).",txt".$i.",rec".$i.")'>";
Echo "<v:fill opacity='60293f' color2='fill lighten(120)' o:opacity2='60293f' rotate='t' angle='-135' method='linear sigma' focus='100%' type='gradient'/>";
Echo "<v:textpath style='font-family:'宋体';v-text-kern:t' trim='t' fitpath='t' string='".($pie[$i]*100)."%'/>";
Echo "<o:extrusion v:ext='view' backdepth='8pt' on='t' lightposition='0,0' lightposition2='0,0'/>";
Echo "</v:shape>";
Echo "</div>";
$k1=$k1+$k2*2;
IF($k1>=360){
   $k1=$k1-360;
}
IF($k1>180){
   $k4=$k4+1;
}else{
   $k4=$k4-1;
}
}
}
?>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<STYLE>v:* { Behavior: url(#default#VML)}o:* { behavior: url(#default#VML)}</STYLE>
</head>
<body>
<SCRIPT LANGUAGE="javascript">
<!--
onit=true
num=0
function moveup(iteam,top,txt,rec){
temp=eval(iteam)
tempat=eval(top)
temptxt=eval(txt)
temprec=eval(rec)
at=parseInt(temp.style.top)
temprec.style.display = "";
if (num>27){
   temptxt.style.display = "";
}
if(at>(tempat-28)&&onit){
   num++
   temp.style.top=at-1
   Stop=setTimeout("moveup(temp,tempat,temptxt,temprec)",10)
}else{
   return
}
}
function movedown(iteam,top,txt,rec){
temp=eval(iteam)
temptxt=eval(txt)
temprec=eval(rec)
clearTimeout(Stop)
temp.style.top=top
num=0
temptxt.style.display = "none";
temprec.style.display = "none";
}
function ontxt(iteam,top,txt,rec){
temp = eval(iteam);
temptxt = eval(txt);
temprec = eval(rec)
if (onit){
   temp.style.top = top-28;
   temptxt.style.display = "";
   temprec.style.display = "";
}
}
function movereset(over){
if (over==1){
   onit=false
}else{
   onit=true
}
}
-->
</script>
<?php
$total=array(
    0=>array(0=>2000,1=>"项目1",2=>"#ff1919"),
    1=>array(0=>1800,1=>"项目2",2=>"#ffff19"),
    2=>array(0=>1700,1=>"项目3",2=>"#1919ff"),
    3=>array(0=>1500,1=>"项目4",2=>"#19ff19"),
    4=>array(0=>1222,1=>"项目5",2=>"#fc0"),
    5=>array(0=>1100,1=>"项目6",2=>"#3cc"),
    6=>array(0=>1000,1=>"项目7",2=>"#ff19ff"),
    7=>array(0=>800,1=>"项目8",2=>"#993300"),
    8=>array(0=>700,1=>"项目9",2=>"#f60"),
    9=>array(0=>600,1=>"项目10",2=>"#a18c19")
);
table2($total,140,100,700,400,"三维饼状图","元");
?>
</body>
</html>