甘特图组件gantt-chart-component
一个功能强大、高性能的甘特图组件,支持任务管理、依赖关系、多种视图模式和丰富的交互功能。
特性
- 🚀 高性能渲染:使用虚拟滚动和缓存优化,轻松处理数千条任务数据
- 📊 多视图模式:支持日、周、月、季度和年视图
- 🔄 拖拽与调整:拖动任务或调整任务时长
- 🔗 依赖关系:支持任务间的多种依赖关系
- 🔙 撤销/重做:完整的操作历史记录
- 💾 导出功能:支持导出为PNG、PDF或Excel
- 🎨 主题定制:可完全自定义样式和主题
- 📱 响应式设计:适配各种屏幕尺寸
- 🌍 国际化支持:支持多语言和本地化
- 🧩 框架无关:可用于React、Vue等主流框架
快速开始
安装
npm install gantt-chart-component
yarn add gantt-chart-component
基础使用
import React, { useRef } from 'react';
import { EnhancedGanttChart } from gantt-chart-component';
function App() {
const ganttRef = useRef(null);
// 示例任务数据
const tasks = [
{
id: '1',
name: '需求分析',
start: '2023-03-01',
end: '2023-03-05',
progress: 100,
type: 'task'
},
{
id: '2',
name: '设计阶段',
start: '2023-03-06',
end: '2023-03-10',
progress: 80,
type: 'task'
}
];
// 示例依赖关系
const dependencies = [
{
fromId: '1',
toId: '2',
type: 'finish_to_start'
}
];
return (
<div style={{ height: '500px' }}>
<EnhancedGanttChart
ref={ganttRef}
tasks={tasks}
dependencies={dependencies}
viewMode="week"
onTaskClick={(task) => console.log('任务点击:', task)}
/>
{/* 示例工具栏 */}
<div>
<button onClick={() => ganttRef.current.undo()}>撤销</button>
<button onClick={() => ganttRef.current.redo()}>重做</button>
<button onClick={() => ganttRef.current.exportAsPNG()}>导出PNG</button>
</div>
</div>
);
}
核心API
<EnhancedGanttChart> 属性
tasks | Task[] | [] | 任务数据列表 |
dependencies | Dependency[] | [] | 依赖关系列表 |
resources | Resource[] | [] | 资源列表 |
viewMode | 'day'|'week'|'month' | 'day' | 视图模式 |
sampleCount | number | 10 | 如果不提供tasks,则生成的示例任务数量 |
options | GanttOptions | {} | 详细配置选项 |
onTasksChange | (tasks: Task[]) => void | - | 任务变更回调 |
onDependenciesChange | (deps: Dependency[]) => void | - | 依赖变更回调 |
onTaskClick | (task: Task) => void | - | 任务点击回调 |
onTaskDoubleClick | (task: Task) => void | - | 任务双击回调 |
onDateRangeChange | (range: DateRange) => void | - | 日期范围变更回调 |
GanttOptions 配置项
{
theme: {
primary: '#1890ff',
secondary: '#13c2c2',
success: '#52c41a',
warning: '#faad14',
error: '#f5222d',
taskBackground: '#e6f7ff',
taskBorder: '#91d5ff',
milestoneColor: '#722ed1',
gridLine: '#f0f0f0',
fontFamily: 'sans-serif',
fontSize: 12
},
allowTaskDrag: true,
allowTaskResize: true,
readOnly: false,
enableDependencies: true,
showProgress: true,
showWeekends: true,
showToday: true,
dateFormat: 'YYYY-MM-DD',
columnWidth: 40,
rowHeight: 40,
workingDays: [1,2,3,4,5],
}
方法
通过 ref 访问组件实例可以使用以下方法:
addTask(task: Partial<Task>): 添加新任务
updateTask(task: Task): 更新任务
removeTask(taskId: string): 删除任务
setViewMode(mode: ViewMode): 设置视图模式
scrollToTask(taskId: string): 滚动到指定任务
exportAsPNG(options?: ExportOptions): 导出为PNG
exportAsPDF(options?: ExportOptions): 导出为PDF
undo(): 撤销操作
redo(): 重做操作
数据结构
Task (任务)
interface Task {
id: string;
name: string;
start: string;
end: string;
progress: number;
type: TaskType;
parentId?: string;
color?: string;
collapsed?: boolean;
metadata?: any;
}
Dependency (依赖关系)
interface Dependency {
fromId: string;
toId: string;
type: DependencyType;
metadata?: any;
}
Resource (资源)
interface Resource {
id: string;
name: string;
color?: string;
capacity?: number;
metadata?: any;
}
示例
详细示例请查看 src/components/gantt-chart/examples/ 目录下的示例文件。
浏览器兼容性
- Chrome (最新版)
- Firefox (最新版)
- Safari (最新版)
- Edge (最新版)
- IE11 (需要polyfills)
许可证
MIT
GitHub Pages 部署
此项目已配置为自动部署到GitHub Pages。每当推送到main分支时,GitHub Actions将自动构建并部署到GitHub Pages。
如何配置
手动部署
你也可以手动部署到GitHub Pages:
npm run deploy
这将构建项目并推送到gh-pages分支。