一、背景
公司業務需要,管理層做項目管理就會制定項目計划,為了更好的的做好項目計划就需要用到做計划常用的工具甘特圖,而且做好計划管理對項目的風險管控也有很大的好處。
二、甘特圖官網以及文檔地址
破解版:***********
三、甘特圖六要素
-
-
tasks 甘特圖任務相關,包含data(數據)、links(數據之間的關系)、collections等 ——> gantt.parse(tasks) 注:parse前一定要先清一下數據不然來回切換不同的甘特圖會渲染出錯 gantt.clearAll()
-
config 甘特圖配置相關,相當於插件的options,不過有些options依賴於plugin(有plugin配值config才生效)——> gantt.config.xxx = xxx
-
plugins 甘特圖插件相關,有些options依賴於plugin(有plugin配值config才生效)——> gantt.plugins(plugins)
-
templates 甘特圖自定義模板相關,比如自定義tooltip_text、 task_text|、task_class等等——> gantt.templates.xxx = xxx
-
events 甘特圖事件相關——> gantt.attachEvent(name, handle, settins)
-
zoomConfig 甘特圖(日周月年)視圖相關——> gantt.ext.zoom.init(zoomConfig)
-
四、甘特圖使用
1.引入
1.引入js
import {gantt , Gantt} from "yys-pro-gantt";
2.導入css
a、js中導入
import "yys-pro-gantt/codebase/dhtmlxgantt.css";
import "yys-pro-gantt/codebase/skins/dhtmlxgantt_terrace.css";
b、style中導入方式
@import "~yys-pro-gantt/codebase/dhtmlxgantt.css";
/*@import "~yys-pro-gantt/codebase/skins/dhtmlxgantt_terrace.css"; // 皮膚*/
@import "./skins/dhtmlxgantt_yys.css"; // 自定義皮膚
不需要下面可以不引入
// import "dhtmlx-gantt/codebase/locale/locale_cn" ; // 本地化
// import "dhtmlx-gantt/codebase/ext/dhtmlxgantt_tooltip.js"; // 任務條懸浮提示
// this.createScript("http://export.dhtmlx.com/gantt/api.js"); // 使用甘特圖自帶的導出功能必須引入
createScript
createScript(url: string) {
const scriptElement = document.createElement("script");
scriptElement.src = url;
const headerElement = document.querySelector("head") as any;
if (headerElement) {
headerElement.appendChild(scriptElement);
}
}
2、甘特圖初始化
template
<div ref="gantt"
id="yys-gantt">
</div>
vuejs
gantt() { // 獲取到dom元素
return this.$refs.gantt as any;
}
get ganttInstance() { // 獲取到甘特圖實例便能調用甘特圖的所有方法
return gantt || Gantt.getGanttInstance();
}
this.ganttInstance.init(this.gantt());
this.ganttInstance.clearAll();
this.ganttInstance.parse(this.tasks);
3.甘特圖更新
a. tasks變更甘特圖更新
@Watch("config", { immediate: true, deep: true })
updateConfig(nConfig: any, oConfig: any) {
if (JSON.stringify(nConfig) === JSON.stringify(oConfig)) { return; }
this.$nextTick(() => {
if (this.config) {
Object.assign(this.ganttInstance.config, this.defaultConfig, this.config);
}
if (this.gantt()) {
this.ganttInstance.init(this.gantt());
gantt.clearAll();
this.ganttInstance.parse(this.tasks);
}
})
}
b. config變更甘特圖更新
@Watch("config", { deep: true })
updateConfig() {
if (this.config) {
Object.assign(this.ganttInstance.config, this.defaultConfig, this.config);
}
if (this.gantt()) {
this.ganttInstance.init(this.gantt());
gantt.clearAll();
this.ganttInstance.parse(this.tasks);
}
}
4.甘特圖銷毀
gantt.destructor()
五.那些踩過的坑
1、data里面的部分屬性的key是不能更改的,比如id,parent,start_date、end_date、progress、open
links里面的全部屬性的key是不能更改的id source target type
2、data如果type是project,那么這條數據的開始時間會取其里面所有數據的最早開始時間,結束時間會取里面所有數據的最晚開始時間,如果這條數據里面的所有數據都是空數據,那么start_date會甘特圖渲染的最早時間,end_date是start_date的后一天,這樣數據就會不准確?
解決方案: data里加個unscheduled屬性來控制是否渲染這條數據,需要注意的是在所有涉及到日期的地方都要加,如tooltips_text 、columns、 拖拽等等
3、 dhtmlx-gantt都是下划線分割,而且api中都是這樣,但在layout中min_width、max_width不生效,要用minWidth、maxWidth替換才生效。
4、排序后的數據默認從頁面獲取的row元素可能是不准確的,需要從dataStroe中獲取。
5、甘特圖在不占全屏的情況下, order_branch = true,拖拽會有限制?
解決方案:
6、在左側表格和列都能拖拽的情況下,會突然彈回到默認寬度?
解決方案:監控config阻止掉更新;
@Watch("config", { deep: true })
updateConfig(nConfig: any, oConfig: any) {
if (JSON.stringify(nConfig) === JSON.stringify(oConfig)) return;
}
7、默認情況下甘特圖經常出現錯誤提示?
解決方案:將show_errors設為false
8、link添加類型&&計划和里程碑規則
link_class: (link: any) => {
// console.log(link)
const {type} = link;
return `link-type-${type}`;
},
target.forEach((linkId: any) => {
const link = this.gantt.getLink(linkId);
const {
sourceTask,
targetTask,
type,
} = this.getSourceTaskAndTargetTaskByLink(link);
switch (type) {
case LinkType.fs:
if ( +targetTask.start_date < +sourceTask.end_date ) {
fsLimit(task, sourceTask);
}
break;
case LinkType.ss:
if (+targetTask.start_date > +sourceTask.start_date) {
limitLeft(task, targetTask);
}
break;
case LinkType.ff:
if (+targetTask.end_date > +sourceTask.end_date) {
limitRight(task, targetTask);
}
break;
case LinkType.sf:
if (+targetTask.start_date > +sourceTask.end_date) {
limitRight(task, targetTask);
}
break;
default:
break;
}
fsMoveLimit(task: any, limit: any) {
const dur = task.end_date - task.start_date;
if (task.type === GANTT_TYPE.計划 && limit.type === GANTT_TYPE.計划) {
task.start_date = new Date(limit.end_date);
task.end_date = new Date(limit.start_date + dur);
}
if (task.type === GANTT_TYPE.計划 && limit.type === GANTT_TYPE.里程碑) {
task.start_date = new Date(limit.end_date);
task.end_date = new Date(limit.start_date + dur);
}
if (task.type === GANTT_TYPE.里程碑 && limit.type === GANTT_TYPE.里程碑) {
task.start_date = new Date(limit.start_date);
}
if (task.type === GANTT_TYPE.里程碑 && limit.type === GANTT_TYPE.計划) {
task.start_date = new Date(limit.end_date);
}
}
fsResizeLimit(task: any, limit: any) {
const dur = task.end_date - task.start_date;
if (task.type === GANTT_TYPE.計划 && limit.type === GANTT_TYPE.計划) {
task.start_date = new Date(limit.end_date);
}
if (task.type === GANTT_TYPE.計划 && limit.type === GANTT_TYPE.里程碑) {
task.start_date = new Date(limit.end_date);
}
}
.gantt_task_link.link-type-0 .gantt_line_wrapper:nth-of-type(2)::before{
content: "fs";
position: absolute;
top: 10px;
left: 15px;
font-size: 16px;
}
.gantt_task_link.link-type-1 .gantt_line_wrapper:nth-of-type(2)::before{
content: "ss";
position: absolute;
top: 10px;
left: 15px;
font-size: 16px;
}
.gantt_task_link.link-type-2 .gantt_line_wrapper:nth-of-type(2)::before{
content: "ff";
position: absolute;
top: 10px;
left: 15px;
font-size: 16px;
}
.gantt_task_link.link-type-3 .gantt_line_wrapper:nth-of-type(2)::before{
content: "sf";
position: absolute;
top: 10px;
left: 15px;
font-size: 16px;
}
轉載請注明出處:https://www.cnblogs.com/ygunoil
