页面上的多个甘特图
信息
此功能包含在 Gantt PRO 版本中,可通过 Commercial(自 2021 年 10 月 6 日起)、Enterprise 和 Ultimate 许可证获得。
本质上,dhtmlxGantt 作为一个静态对象存在,其默认实例始终存在于页面上。你可以随时通过全局 gantt 对象访问它。不过,如果有需要,你也可以创建一个新的 gantt 实例。
甘特实例配置
要创建一个新的 dhtmlxGantt 实例,请使用 Gantt.getGanttInstance() 方法:
// 注意 "Gantt" 首字母为大写
const ganttChart = Gantt.getGanttInstance();
该方法可以接收一个配置对象作为参数:
const gantt = Gantt.getGanttInstance({
plugins:{
auto_scheduling: true,
},
container: "gantt_here",
config: {
work_time: true,
duration_unit: "minute",
auto_scheduling_compatibility: true,
auto_scheduling: true,
auto_scheduling_strict: true,
auto_scheduling_initial: true,
start_date: new Date(2020, 0, 1),
end_date: new Date(2021, 0, 1),
},
calendars: [
{
id:"global",
worktime: {
hours: ["8:00-17:00"],
days: [ 0, 1, 1, 1, 1, 0 ,0],
customWeeks: {
lastMonthOfYear: {
from: new Date(2020, 11, 1),// 2020年12月1日
to: new Date(2021, 0, 1),// 2021年1月1日 00:00,
hours: ["9:00-13:00"],
days: [ 0, 1, 1, 1, 1, 1, 0]
},
firstMonthOfNextYear:{
from: new Date(2021, 0, 1),// 2021年1月1日
to: new Date(2021, 1, 1),// 2021年2月1日 00:00,
hours: ["14:00-16:00"],
days: [ 1, 1, 1, 1, 1, 0, 1]
}
}
}
}
],
data: {
tasks: [
{ id: 11, text: "Project #1", type: "project", "open": true, "parent": 0 },
{ id: 1, start_date: "05-04-2020", text: "1", duration: 1, parent: "11",
type: "task" },
{ id: 2, start_date: "05-04-2020", text: "2", duration: 3, parent: "11",
type: "task" },
{ id: 3, start_date: "05-04-2020", text: "3", duration: 3, parent: "11",
type: "task" },
{ id: 4, start_date: "05-04-2020", text: "4", duration: 3, parent: "11",
type: "task" },
{ id: 5, start_date: "05-04-2020", text: "5", duration: 1, parent: "11",
type: "task" }
],
links: [
{ source: "1", target: "2", type: "0", id: 1 },
{ source: "1", target: "3", type: "0", id: 2 },
{ source: "1", target: "4", type: "0", id: 3 },
{ source: "2", target: "4", type: "0", id: 4 },
{ source: "3", target: "4", type: "0", id: 5 },
{ source: "4", target: "5", type: "0", id: 6 }
]
}
});
这将创建一个使用指定选项初始化的甘特图。
config 对象支持以下属性:
- container - (string|HTMLElement) 甘特图渲染的 HTML 容器或其 id。如果省略,甘特图将在没有容器的情况下初始化。
- config - (object) 甘特图的配置设置
- calendars - (array) 要加载到 gantt 的工作日历数组。日历格式应符合 gantt.addCalendar 方法的要求。
- templates - (object) 包含模 板的对象
- events - (object) 包含事件处理程序的对象。
为新 Gantt 实例指定事件处理程序时,使用如下格式:
const gantt = Gantt.getGanttInstance({
events: {
onTaskCreated: function(task){
task.owner = null;
return true;
},
onTaskClick: function(id){
alert(gantt.getTask(id).text);
return true;
}
}
})
- data - (object|string) 要加载的数据或用于获取数据的 URL
- plugins - (object) 需要激活的插件
- locale - (string|object) 两位语言代码或要激活的本地化对象
注意,如果不传递参数调用 Gantt.getGanttInstance()