通过实际代码示例,展示如何将 Gantt 集成到主流前端框架中,并实现拖拽编辑、依赖关系动态更新、资源负载视图等高级功能。
一、与主流框架集成
1.1 React 集成
jsx
javascript
import { useEffect, useRef } from 'react';
import Highcharts from 'highcharts';
import HighchartsGantt from 'highcharts/highcharts-gantt';
// 初始化模块
HighchartsGantt(Highcharts);
function GanttChart({ data }) {
const containerRef = useRef(null);
const chartRef = useRef(null);
useEffect(() => {
const options = {
title: { text: '项目进度' },
series: [{
name: '任务',
data: data
}]
};
chartRef.current = Highcharts.ganttChart(containerRef.current, options);
return () => chartRef.current?.destroy();
}, [data]);
return <div ref={containerRef} style={{ height: '500px' }} />;
}
1.2 Vue 3 集成
vue
javascript
<template>
<div ref="chartContainer" style="height: 500px;"></div>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount, watch } from 'vue';
import Highcharts from 'highcharts';
import HighchartsGantt from 'highcharts/highcharts-gantt';
HighchartsGantt(Highcharts);
const chartContainer = ref(null);
let chart = null;
const props = defineProps(['tasks']);
onMounted(() => {
chart = Highcharts.ganttChart(chartContainer.value, {
title: { text: '项目计划' },
series: [{ name: '任务', data: props.tasks }]
});
});
onBeforeUnmount(() => {
chart?.destroy();
});
watch(() => props.tasks, (newTasks) => {
chart?.series[0].setData(newTasks);
});
</script>
二、高级功能实战
2.1 启用任务拖拽编辑
允许用户通过拖拽调整任务起止时间,实现"所见即所得"的规划体验。
javascript
javascript
const chart = Highcharts.ganttChart(container, {
// 启用拖拽编辑
plotOptions: {
gantt: {
dragDrop: {
draggableX: true, // 水平拖拽调整时间
draggableY: false, // 垂直拖拽调整层级(可选)
dragPrecision: 'day' // 对齐精度
}
}
},
series: [{
name: '任务',
data: tasks,
// 监听拖拽完成事件
point: {
events: {
drop: function() {
// 调用后端 API 保存新时间
fetch('/api/task/update', {
method: 'POST',
body: JSON.stringify({
id: this.id,
start: this.start,
end: this.end
})
});
}
}
}
}]
});
2.2 动态依赖关系
依赖关系是甘特图的核心------当任务A延期时,依赖它的任务B应自动预警。
javascript
javascript
const tasks = [
{ id: 'task1', name: '需求分析', start: ... , end: ... },
{ id: 'task2', name: '设计', start: ... , end: ... },
{
id: 'task3',
name: '开发',
start: ...,
end: ...,
dependency: {
// 依赖 task1 和 task2 都完成后才能开始
id: 'task3',
to: ['task1', 'task2'],
type: 'finish-start'
}
}
];
// 高亮显示关键路径
chart.addSeries({
name: '关键路径',
type: 'dependency',
data: criticalPathTasks,
color: '#FF0000',
lineWidth: 3
});
2.3 资源负载视图
除了任务时间线,Highcharts Gantt 还可以叠加资源使用情况。
javascript
javascript
// 为任务分配资源
const tasks = [
{ name: '需求分析', start: ..., end: ..., resources: ['张三', '李四'] },
{ name: 'UI设计', start: ..., end: ..., resources: ['王五'] }
];
// 资源视图(需要在系列中配置)
series: [{
type: 'gantt',
name: '任务',
data: tasks,
// 按资源着色
colorByPoint: false,
colorBy: 'resource'
}]
2.4 实时数据同步
结合 WebSocket 实现多用户协同更新:
javascript
javascript
const socket = new WebSocket('wss://your-server/project-updates');
socket.onmessage = (event) => {
const { action, data } = JSON.parse(event.data);
if (action === 'taskUpdate') {
const point = chart.get(data.id);
if (point) {
point.update({ start: data.start, end: data.end });
}
}
};
三、性能优化建议
| 场景 | 优化策略 |
|---|---|
| 任务数量 > 500 | 启用 turboThreshold,禁用非必要的动画 |
| 实时高频更新 | 使用 chart.series[0].setData(data, false) 并手动重绘 |
| 移动端适配 | 设置 chart.scrollablePlotArea,支持手势缩放 |
| 大数据量导出 | 使用服务端导出模块,避免浏览器内存溢出 |
四、从零到一:实战项目结构建议
常见问题与排查
| 问题 | 解决方案 |
|---|---|
| 任务重叠显示 | 检查 start 和 end 的时间戳格式(需为毫秒级) |
| 依赖线不显示 | 确认 dependency 中的 id 与任务 id 严格匹配 |
| 拖拽后数值错乱 | 检查 dragDrop.dragPrecision 设置,避免过度对齐 |
| 导出中文乱码 | 设置 exporting.chartOptions 中的字体编码 |
相关资源
结语
Highcharts Gantt 不仅是图表库,更是构建专业项目管理工具的基础框架。通过本文的实战代码,你应该已经掌握了:
-
与主流前端框架的集成方式
-
拖拽编辑、依赖关系、资源视图等高级功能
-
性能优化和常见问题排查
💡 小贴士:在正式开发前,建议先用官方演示项目快速验证业务场景,确认需求后再投入完整开发。