打印

[php] 奉献一个PHP版的动态三维饼状图

本主题由 SinNeR 于 2008-5-19 23:15 设置高亮
上次看到金翅擘海给的静态三维饼状图很是心动;终于变成PHP版了;需要的同志拿去用吧

加上了注释;大多数人应该可以看明白了
复制内容到剪贴板
代码:
<?php
//参数含义(数组,横坐标,纵坐标,图表的宽度,图表的高度,图表标题,单位)
function table2($stat_array,$table_left,$table_top,$all_width,$all_height,$table_title,$unit){
       $bg_color[1]="#ff1919";//定义各个色块的颜色;如果你觉得10个还不够;请继续定义
       $bg_color[2]="#ffff19";
       $bg_color[3]="#1919ff";
       $bg_color[4]="#19ff19";
       $bg_color[5]="#fc0";
       $bg_color[6]="#3cc";
       $bg_color[7]="#ff19ff";
       $bg_color[8]="#993300";
       $bg_color[9]="#f60";
       $bg_color[10]="#ff8c19";
       $T_N = count($stat_array)+1;//计算传入的数组item数目
       $num =max($T_N,1);//取数组值和1中的最大值;直接写成$num =count($stat_array)+1;也行;当时没考虑仔细:D
       $allvalues=0;
       for($i=1;$i<$num;$i++) $allvalues = $allvalues+$stat_array[$i][1];//计算总值
       $k=0;
       for($i=1;$i<$num-1;$i++){
              $pie[$i]=sprintf ("%01.4f", $stat_array[$i][1]/$allvalues);//计算每块所站的比例;取小数点后4位
              $k=$k+$pie[$i];}
       $pie[$num]=sprintf ("%01.4f", (1-$k));
       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>";//以下开始输出VML;可以不改
       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>";
       echo " <td align='center' valign='middle'><div style='font-size:15pt; font-family:黑体;'><B>".$table_title."</B></div></td>";
       echo " </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=1;$i<$num;$i++){
              echo " <v:Rect id='rec".$i."' style='position:relative;left:15400;top:".($i*9000/11+1450).";width:4300;height:800;display:none' fillcolor='#efefef' strokecolor='".$bg_color[$i]."'>";
              echo " <v:fill opacity='.6' color2='fill darken(118)' o:opacity2='.6' rotate='t' method='linear sigma' focus='100%' type='gradient'/>";
             // 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:Rect>";
              echo " <v:Rect style='position:relative;left:15500;top:".($i*9000/11+1500).";width:600;height:700' fillcolor='".$bg_color[$i]."' stroked='f'/>";
              echo " <v:Rect style='position:relative;left:16300;top:".($i*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][2].":".$stat_array[$i][1].$unit."</div></v:TextBox>";
              echo " </v:Rect> " ;}
       echo "</v:group>";
       $k1=180;
       $k4=10;
       for($i=1;$i<$num;$i++){
echo "<a href='james.php'>";
             $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][2]." 数值:".$stat_array[$i][1].$unit." 所占比例:".($pie[$i]*100)."%";
              echo " <div style='cursor:hand;'>";
              echo " <v:shape id='cake".$i."' type='#Cake_3D' title='".$titlestr."'";
              echo " 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."'";
              echo " adj='".$kkk.",0' fillcolor='".$bg_color[$i]."' 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'";
              echo " 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;
echo "</a>";
      }
}
?>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<STYLE>
v\:* { Behavior: url(#default#VML) }
o\:* { behavior: url(#default#VML) }
</STYLE>
</head>
<body>
<SCRIPT type="text/javascript">//定义操作饼状图的JS函数
<!--
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>
</body>
</html>
<?php//传入数组;这里就是用的人要改的地方了;数组从数据库或其他地方得到:D
$total[1][1]=2000;
$total[2][1]=1800;
$total[3][1]=1700;
$total[4][1]=1500;
$total[5][1]=1222;
$total[6][1]=1100;
$total[7][1]=1000;
$total[8][1]=800;
$total[9][1]=700;
$total[10][1]=600;
$total[1][2]="项目1";
$total[2][2]="项目2";
$total[3][2]="项目3";
$total[4][2]="项目4";
$total[5][2]="项目5";
$total[6][2]="项目6";
$total[7][2]="项目7";
$total[8][2]="项目8";
$total[9][2]="项目9";
$total[10][2]="项目10";
table2($total,120,20,900,500,"三维饼状图","元");//调用函数
//参数含义(数组,横坐标,纵坐标,图表的宽度,图表的高度,图表标题,单位)
?>
[ 本帖最后由 yxljames 于 2008-5-20 19:36 编辑 ]
附件: 您所在的用户组无法下载或查看附件,您需要注册/登录后才能查看!

TOP

不错
用的关键是vml嘛
冬天来临,春天就不会远了。

TOP

兼容性如何?
怎么不用GD

TOP

呵呵;总算有人欣赏了

顺便说一句这已经是我目前的最好水平;想要更好的效果只有两条路;等我更好或来个高手改改

还是来个高手改改吧;这样快点;大家共同进步

TOP

有一个重叠啦
有些缺点,
一没有写成函数或类,这样散装品大家不是很好用,
二不能根据外部传入的数组决定他块数,不过这个更改起来比较容易,希望楼主能够封装下,
三用GD库可能会减少代码量当然效果可能没这么好。



[ 本帖最后由 jd808 于 2008-5-20 15:18 编辑 ]
www.gyqpw.com  广源汽车配件报价网

TOP

已经是函数了啊
//参数含义(数组,横坐标,纵坐标,图表的宽度,图表的高度,图表标题,单位)
function table2($stat_array,$table_left,$table_top,$all_width,$all_height,$table_title,$unit)
块数也是通过外部的数组决定的
$T_N = count($stat_array)+1;
$num =max($T_N,1);//决定应该生成的块数

也不能怪你看得不仔细;主要是我注释没写;不好意思;我补上吧

TOP

确实有重叠哦,看看是怎么回事哦
封装一下比较好。比较同意楼上的楼上的看法。

TOP

检查了一遍;没发现问题所在;深空帮我看看吧

TOP