- 概述
JQuery.Gantt是一個開源的基於JQuery庫的用於實現甘特圖效果的可擴展功能的JS組件庫。

- 源碼下載
- 前端頁面
-
- 資源引用
-
-
- CSS樣式文件
-
<link rel="stylesheet" href="css/style.css" />
|
-
-
- JS腳本文件
-
<script src="js/jquery-1.7.min.js"></script>
<script src="js/jquery.fn.gantt.js" charset ="GB2312"></script>
<script src="js/jquery.cookie.js"></script>
|
注:如果需要甘特圖中顯示中文,則需要在js文件引用中加上charset特性並設置為GB2312,否則中文內容將顯示為亂碼。
-
- 頁面布局
在需要顯示甘特圖的地方加入以下這個div。
<div class="gantt"></div>
|
- 組件配置
-
- Gantt 配置
$(".selector").gantt({ source:"ajax/data.json", scale:"weeks", minScale:"weeks", maxScale:"months", onItemClick:function(data){ alert("Item clicked - show some details");}, onAddClick:function(dt, rowId){ alert("Empty space clicked - add an item!");}, onRender:function(){ console.log("chart rendered");}});
|
-
- Source 配置
source:[{ name:"Example", desc:"Lorem ipsum dolor sit amet.", values:[...]}]
|
-
- Value 配置
values:[{ to:"/Date(1328832000000)/",from:"/Date(1333411200000)/", desc:"Something", label:"Example Value", customClass:"ganttRed", dataObj: foo.bar[i]}]
注:其中from和to的時間為毫秒數,例如:
/Date(1320192000000)/,計算方式為時間變量減去時間初始值(1970-1-1)的差值換算為毫秒
|
- .NET平台實現時間轉換
public class GanttManager
{
public static readonly DateTime StartTime = TimeZone .CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)).Date;
public static List< GanttItem> DataToGanttList()
{
List<GanttItem > ls = new List<GanttItem >();
GanttItem item = new GanttItem();
item.name = "a";
item.desc = "b";
item.values.id = "1";
item.values.label = "c";
item.values.from = ToMillisecondDate( new DateTime (2011, 11, 2));
item.values.to = ToMillisecondDate( new DateTime (2011, 11, 3));
ls.Add(item);
ls.Add(item);
return ls;
}
public static string ToMillisecondDate( DateTime dt)
{
return "/Date(" + ((dt.Date - StartTime.Date).TotalSeconds * 1000).ToString() + ")/";
}
}
|
- 代碼說明
-
- jquery.cookie.js
-
- jquery.fn.gantt.js
JQuery.Gantt組件的核心腳本文件,所有的甘特圖功能代碼都在這個文件中。
代碼結構解析:
|