",
}, {
"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: "美丽",
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": "
",
}, {
"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": "",
},
......
];
```
6. 创建甘特图对象
```javascript
// 实例化甘特图对象(在vue中需要执行此语句进行甘特图对象实例化)
const myGantt = MZGantt.init();
// 启动功能模块
// (1)使用插件npm安装包时,如果需要编辑、导出和移动端支持功能,引用相应功能包后,需要在此启动
// (2)使用插件普通js包时,以下启动(start)命令可以略去。
//启动甘特图编辑
MZGanttEditor.start(myGantt);
// 启动导出功能
MZGanttExport.start(myGantt);
/*
创建并展示甘特图
参数1:(必须)容器div对象id
参数2:(可选)时间刻度类型
参数3:(可选)配置选项
参数4:(可选)数据
*/
myGantt.createGantt("GanttChartDIV", "day", ganttConfig, data);
// 注意:此处如果为设置ganttConfig和data,可以独立使用配置方法和绑定数据等方法
// 甘特图参数配置
myGantt.config(ganttConfig);
// 加载数据
myGantt.bindGanttData(data);
// 甘特图展示
myGantt.drawGantt()
```
7. 监听事件
MZGantt支持监听渲染或者点击等事件,可以使用这些事件实现一些特殊的控制效果。
```javascript
// 单元格渲染
myGantt.on("cellRender", function (row, cellObj) {
// *******************************************************
// 可对以下类型单元格(field)进行渲染控制:
// 一. 内置类型列:
// name 名称
// iconColsVal 图标列
// resId 资源/负责人
// planStart 计划开始
// planEnd 计划完成
// planDur 计划人天
// rsltStart 实际开始
// rsltEnd 实际完成
// rsltDur 实际工日
// pctComp 完成度
// 二.自定义列
// testCol1 自定义列名(name)
// *******************************************************
// 获取单元格名称
var field = cellObj.field;
// 定义渲染样式对象
var cellStyle = {};
// 样式设置:任务名称
if (field == "name") {
if (row.plan[0].dur > 5) {
cellStyle["cellHTML"] = '' + row.name + '';
// cell.style.color = "red";
}
if (row.plan[0].dur >= 2 && row.plan[0].dur <= 5) {
cellStyle["cellHTML"] = '' + row.name + '';
}
if (row.plan[0].dur < 2) {
cellStyle["cellHTML"] = '' + row.name + '';
}
}
// 样式设置:负责人
if (field == "resId") {
if (row.name == "任务名称1") {
cellStyle["ce11Style"] = 'color:green;font-weight:bold';
}
}
// // 样式设置:自定义列(使用自定义列名称)
// if (field == "testCol2") {
// cellStyle["cellHTML"] = '';
// }
return cellStyle;
});
// 进度条渲染
myGantt.on("barRender", function (row) {
// 定义渲染样式对象
var barStyle = {};
if (row.name === "任务名称2") {
barStyle["planBarStyle"] = 'background:red;';
// barStyle["planBarLeftHtml"] = '