-
下载
使用前,先下载普通js版插件或使用npm进行安装。
-
引用MZGantt插件
在html页面头部中引入如下js包:
<!-- 引入jquery库 -->
<script language="javascript" src="jquery.min.js"></script>
<!-- 引入甘特图插件 -->
<link rel="stylesheet" type="text/css" href="./gantt/mzgantt.css" />
<script language="javascript" src="./gantt/mzgantt.js"></script>
<script language="javascript" src="./gantt/edit.js"></script>
import {MZGantt, MZGanttEditor} from './mzgantt';
MZGantt.LicenseKey = "您的授权key";
-
定义甘特图容器(div)
在html页面内定义一个div,作为展示甘特图的容器:
<div id="GanttChartDIV" style="width:100%;"></div>
-
定义甘特图参数(可定义空对象{},插件将使用使用默认值)
此步骤可选。在需要个性化显示甘特图时,可以通过设置甘特图参数来实现。部分可设置参数如下:
var ganttConfig = {
useFor: "Task",
ganttStatus: "e",
columnDefs: [
{name: 'seq', field: "seq", type:"level"},
{name: 'checkbox', field: "checkbox"},
{name: 'iconColsVal', field: "iconColsVal", width: 30},
{name: 'name', field: "name", title: "任务名称", width: 150},
{name: 'resId', field: "resId", title: "责任人", width: 50, options: resourceList},
{name: 'dur', field: "dur", title: "工期", width: 60},
{name: 'planStart', field: "planStart", title: "计划开始", width: 120, editable: false},
{name: 'planEnd', field: "planEnd", title: "计划完成", width: 120, colCallBack:custCallbackFunc1},
{name: 'planDur', field: "planDur", title: "计划人天", width: 60},
{name: 'rsltStart', field: "rsltStart", title: "实际开始", width: 120},
{name: 'rsltEnd', field: "rsltEnd", title: "实际完成", width: 120},
{name: 'rsltDur', field: "rsltDur", title: "实际人天", width: 60},
{name: 'pctComp', field: "pctComp", title: "完成度", width: 60},
{name:"testCol1", field: "custColsVal", title:"列名1", type: "TextBox", default: 10, width:50,align:"left"},
{name:"testCol2", field: "custColsVal", title:"列名2", type: "DropDownList", options:dropDownListData, width:100, align:"left"},
{name:"testCol3", field: "custColsVal", title:"列名3","width":150,"align":"left", colCallBack:custCallbackFunc2, altText: "查看图文"}
],
showTrack: 1,
showDuplicate: 0,
showMileStone: 1,
showDependencies: 1,
showDayDate: "date",
showWeekIndex: "",
weekWorkSetting:
{D1:1, D2:1, D3:1, D4:1, D5:1, D6:0, D0:0},
holidayList: [
'2023-02-09',
'2023-02-21',
'2023-03-20'],
workdayList: ['2023-03-18'],
mileStoneLines: [
{date:'2023-11-05',name:'startMS', title:'项目设计讨论', color:'#FF00BF', thickness: "2px"},
{date:'2023-11-25',name:'pjMeeting', title:'代码review', color:'#088A29', thickness: "2px"},
{date:'today',name:'today', title:'今日', color:'#FF0000', thickness: "2px"}
],
captionType: 'Caption',
captionPosition: 'Right',
dateFormat: 'yyyy-mm-dd',
weekStartDay: 0,
workingHourRange: ['1','23'],
hourFormat: 0,
dragChgDur: "1",
autoCalDur: 1,
includeHoliday: "N",
includeToTime: "Y",
durPrecision: 2,
fixColsCnt: 3,
fixParent: 1,
idType: "Snow",
compBackground: '#0000FF',
holidayBGColor: '#E6E6E6',
currentTimeBackColor: ['h','#FF9797'],
intervalColor: ['#FFFFFF','#F4F4F4'],
selectedRowColor: '#CAE1FF',
selectedCellColor: '#2E9AFE',
borderColor: '#9E9E9E',
barBorderRadius: 6,
barBorderWeight: 1,
barBorderColor: '#9E0000',
barOpacity: 1,
barGridBorderWeight: 1,
resultBarStyle: 'mark',
markResultBarColor:
{beforePlanBGColor: '#00FF00',
exceedPlanDBColor: '#cc0000'},
barHeightAdj: 0,
barDistanceAdj: 0,
dragHandlerBackColor: '#D8D8D8',
dependLineColor: '#228B22',
dependLineMouseOverColor: '#FE9A2E',
criticalPathBGColor: '#FE9A2E',
leftSideWidth: 300,
contentHeight: 360,
rowHeight: 35,
iconColWidth: 30,
nameColWidth: 150,
dateColWidth: 120,
hourColWidth: 20,
dayColWidth: 20,
monthColWidth: 70,
quarterColWidth: 100
};
myGantt.config(ganttConfig);
-
后台获取数据,或定义数据
let ganttData= [
{
"id": 1,
"seq": 1000,
"iconColsVal": [],
"name": "单位A施工期间",
"plan": [{}],
"rsltStart": "",
"rsltEnd": "",
"rsltDur": 1,
"planBarColor": "",
"isMS": 0,
"resId": "1",
"pctComp": 0,
"isGroup": 1,
"parentId": "",
"isExpand": 1,
"preNodes": "",
"caption": "重点关注",
"testCol1": "10",
"testCol2": "02",
"testCol3": "<img src='./images/stop.png'>",
}, {
"id": 11,
"seq": 2000,
"iconColsVal": [{
"src": "./images/stop.png",
"title": "停止"
}
],
"name": "任务名称1",
"plan": [{
"start": "",
"end": "",
"dur": "2"
}
],
"rsltStart": "2023-07-02",
"rsltEnd": "2023-07-06",
"rsltDur": 1,
"planBarColor": "#2EFE9A",
"isMS": 0,
"resId": "2",
"pctComp": 50,
"isGroup": 0,
"parentId": 1,
"isExpand": 1,
"preNodes": "",
"caption": "",
"testCol1": "1",
"testCol2": "02",
"testCol3": "",
"custRsltBars": [
{
name: "p1",
title: "分段1",
start: "2023-07-02",
end: "2023-07-04",
style: "background:#2E9AFE;color:#FFFFFF;text-align:left;"
}, {
name: "p2",
title: "分段2",
start: "2023-07-06",
end: "2023-07-17",
html: "<span style='height:7px;border-radius:5px 5px;position:absolute;left:5px;top:-1px;background:red;font-size:9px;color:#FFFFFF'>美丽</span>",
style: "background:#FF0000;color:#FFFFFF;text-align:left;"
},
],
}, {
"id": 12,
"seq": 3000,
"iconColsVal": [],
"name": "任务名称2",
"plan": [{
"start": "2023-07-06",
"end": "2023-07-10"
}
],
"rsltStart": "2023-07-07",
"rsltEnd": "2023-07-10",
"rsltDur": 1,
"planBarColor": "#2EFE9A",
"isMS": 0,
"resId": "2",
"pctComp": 10,
"isGroup": 0,
"parentId": 1,
"isExpand": 1,
"preNodes": "",
"caption": "备注1",
"testCol1": "1",
"testCol2": "03",
"testCol3": "",
}, {
"id": 13,
"seq": 4000,
"iconColsVal": [{
"src": "./images/star.png",
"title": "星级"
}
],
"name": "任务名称3",
"plan": [{
"start": "2023-07-11",
"end": "2023-07-15"
}
],
"rsltStart": "2023-07-11",
"rsltEnd": "2023-07-14",
"rsltDur": 1,
"planBarColor": "#2EFE9A",
"isMS": 0,
"resId": "2",
"pctComp": 20,
"isGroup": 0,
"parentId": 1,
"isExpand": 1,
"preNodes": 12,
"caption": "",
"testCol1": "50",
"testCol2": "01",
"testCol3": "",
}, {
"id": 14,
"seq": 5000,
"iconColsVal": [],
"name": "任务名称4",
"plan": [{
"start": "",
"end": "2023-07-17"
}
],
"rsltStart": "2023-07-16",
"rsltEnd": "2023-07-17",
"rsltDur": 1,
"planBarColor": vPlanBackground,
"isMS": 0,
"resId": "2",
"pctComp": 0,
"isGroup": 0,
"parentId": 1,
"isExpand": 1,
"preNodes": "",
"caption": "",
"testCol1": "20",
"testCol2": "01",
"testCol3": "",
},
{
"id": 2,
"seq": 6000,
"iconColsVal": [],
"name": "单位B施工期间",
"plan": [{}
],
"rsltStart": "",
"rsltEnd": "",
"rsltDur": 1,
"planBarColor": "",
"isMS": 0,
"resId": "1",
"pctComp": 0,
"isGroup": 1,
"parentId": "",
"isExpand": 1,
"preNodes": "",
"caption": "",
"testCol1": "4",
"testCol2": "01",
"testCol3": "<img src='./images/stop.png'>",
}, {
"id": 21,
"seq": 7000,
"iconColsVal": [{
"src": "./images/stop.png",
"title": "停止"
}
],
"name": "任务名称1",
"plan": [{
"start": "2023-07-19",
"end": "2023-07-29"
}
],
"rsltStart": "2023-07-26",
"rsltEnd": "2023-07-29",
"rsltDur": 1,
"planBarColor": vPlanBackground,
"isMS": 0,
"resId": "2",
"pctComp": 40,
"isGroup": 0,
"parentId": 2,
"isExpand": 1,
"preNodes": "",
"caption": "",
"testCol1": "4",
"testCol2": "02",
"testCol3": "",
},
......
];
-
创建甘特图对象
const myGantt = MZGantt.init();
MZGanttEditor.start(myGantt);
MZGanttExport.start(myGantt);
myGantt.createGantt("GanttChartDIV", "day", ganttConfig, data);
myGantt.config(ganttConfig);
myGantt.bindGanttData(data);
myGantt.drawGantt()
-
监听事件
MZGantt支持监听渲染或者点击等事件,可以使用这些事件实现一些特殊的控制效果。
myGantt.on("cellRender", function (row, cellObj) {
var field = cellObj.field;
var cellStyle = {};
if (field == "name") {
if (row.plan[0].dur > 5) {
cellStyle["cellHTML"] = '<span style="color:red;" >' + row.name + '</span>';
}
if (row.plan[0].dur >= 2 && row.plan[0].dur <= 5) {
cellStyle["cellHTML"] = '<span style="color:green;" >' + row.name + '</span>';
}
if (row.plan[0].dur < 2) {
cellStyle["cellHTML"] = '<span style="color:black;" >' + row.name + '</span>';
}
}
if (field == "resId") {
if (row.name == "任务名称1") {
cellStyle["ce11Style"] = 'color:green;font-weight:bold';
}
}
return cellStyle;
});
myGantt.on("barRender", function (row) {
var barStyle = {};
if (row.name === "任务名称2") {
barStyle["planBarStyle"] = 'background:red;';
barStyle["planBarRightHtml"] = '<div style="display:block;">努力最酷啊</div>';
}
return barStyle;
});
myGantt.on("clickBar", function (row, eventXY, dom) {
console.log("click: 任务:" + row.name + ", 点击日期:" + eventXY.date);
});
myGantt.on("rightClick", function (row, eventXY, dom) {
console.log("right click: 任务:" + row.name + ", 点击对象id:" + dom.id);
console.log("点击日期:" + eventXY.date);
});
-
多种加载方式
为了提升加载效率,插件支持多种加载方式。
方式1:普通加载(一次性加载并渲染所有任务)
myGantt.createGantt("GanttChartDIV", "day");
myGantt.config(ganttConfig);
myGantt.bindGanttData(data);
myGantt.createGantt("GanttChartDIV", "day", ganttConfig, data);
方式2:数据监听渲染加载
var ganttConfig = {
loadType: "listenLoad"
};
myGantt.createGantt("GanttChartDIV", "day");
myGantt.config(ganttConfig);
var dataListener = myGantt.listener;
dataListener.rawGanttData = [
{
"id": 1,
"seq": 1000,
"iconColsVal": [],
"name": "单位A施工期间",
"rsltStart": "",
"rsltEnd": "",
"rsltDur": 1,
"planBarColor": "#C0EBE8",
"linkTo": "http://",
"isMS": 0,
"resId": "1",
"pctComp": 0,
"isGroup": 1,
"parentId": "",
"isExpand": 1,
"preNodes": "",
"caption": "重点关注",
"plan": [{ "start": "2023-07-05", "end": "2023-07-06", "dur": 10 }]
}
];
方式3:分页渲染(一次性加载所有数据,分页渲染)
var ganttConfig = {
loadType: "loadAllToPage",
pageSize: 20
};
myGantt.createGantt("GanttChartDIV", "day");
myGantt.config(ganttConfig);
myGantt.bindGanttData(data);
方式4:分页加载渲染(每次加载并渲染一定行数,逐页加载并渲染)
function loadPageData(pageNode) {
...
}
var ganttConfig = {
loadType: "loadByPage",
pageSize: 20,
loadDataFunc: loadPageData
};
myGantt.createGantt("GanttChartDIV", "day");
myGantt.config(ganttConfig);
myGantt.drawGantt();
下表格式为MZGantt甘特图数据行数据模型,在绑定数据到甘特图前,需要按照此格式作成数据。可以批量绑定,参考bindGanttDate方式;也可单行绑定,多用于与外部弹框结合。