js 实现甘特图
网上找了半天web上的甘特图,都没有找到,于是自己动手写了一个,为了易于观看,把数据写在了2个数组中,需要的朋友可以适当的修改一下从XML中读取数据,
实现效果如下:
好了废话不多说代码如下:查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>test</title>
<style type="text/css">
.tableMain{ border-left:solid 1px #cdcdcd;border-top:solid 1px #cdcdcd; font-size:10.5pt;}
.tableMain td{ padding:1px 5px; border-bottom:solid 1px #cdcdcd;border-right:solid 1px #cdcdcd}
.td1{ background:url(/2/1/images/temp/Column_bg.jpg) #00ff00; border:0 !important;}
</style>
<script type="text/javascript">
function Drawing() {
var arrRow=new Array("调研","数据库","底层","代码编写","阶段测试","交付测试","问题反馈","修改","测试","上线");
var arrColumn=new Array(5,8,2,4,2,3,3,2,2,1);
var ColumnSum=ArraySum(arrColumn);
var str = "<table class=\"tableMain\" cellpadding=\"0\" cellspacing=\"0\">";
var startNumber = 0;
str += "<tr>";
for (k = 0; k < ColumnSum; k++) {
str += "<td>" + (k == 0 ? "项目\\时间" : k) + "</td>";
}
str += "</tr>";
for (i = 0; i < arrRow.length; i++) {
str += "<tr>";
str += "<td>"+arrRow[i]+"</td>";
for (k = 1; k < ColumnSum; k++) {
if (k > startNumber && k <= arrColumn[i]+startNumber)
str += "<td class=\"td1\">" + (k == startNumber+1 ? arrColumn[i] : " ") + "</td>";
else
str += "<td> </td>";
}
str += "</tr>";
startNumber += arrColumn[i];
}
str += "</table>";
return str;
}
function ArraySum(arr) {
var sum = 0;
for (i = 0; i < arr.length; i++) {
sum+=arr[i];
}
return sum;
}
</script>
</head>
<body><script type="text/javascript">
document.write(Drawing());
</script>
</body>
</html>
注意:代码是给需要的人看的,本人水平有限也不是作家请不要妄加评论。