当前位置:首页 > React

react甘特图实现

2026-01-26 15:07:07React

使用 react-gantt 库实现

安装 react-gantt 库:

npm install react-gantt

引入库并创建基本甘特图:

import { Gantt } from 'react-gantt';
import 'react-gantt/dist/index.css';

const tasks = [
  {
    id: '1',
    name: '任务1',
    start: new Date(2023, 0, 1),
    end: new Date(2023, 0, 5)
  },
  {
    id: '2',
    name: '任务2',
    start: new Date(2023, 0, 3),
    end: new Date(2023, 0, 8)
  }
];

function App() {
  return <Gantt tasks={tasks} />;
}

使用 react-gantt-timeline 实现

安装依赖:

npm install react-gantt-timeline

基本实现代码:

import { GanttRow, Gantt } from 'react-gantt-timeline';

const data = [
  {
    id: 1,
    title: '任务A',
    start: new Date(2023, 0, 1),
    end: new Date(2023, 0, 5)
  }
];

function App() {
  return (
    <Gantt>
      {data.map(item => (
        <GanttRow
          key={item.id}
          title={item.title}
          start={item.start}
          end={item.end}
        />
      ))}
    </Gantt>
  );
}

使用 dhtmlx-gantt 实现

安装 dhtmlx-gantt:

npm install dhtmlx-gantt

基本配置:

import { useEffect } from 'react';
import { gantt } from 'dhtmlx-gantt';
import 'dhtmlx-gantt/codebase/dhtmlxgantt.css';

function GanttChart() {
  useEffect(() => {
    gantt.init('gantt_container');
    gantt.parse({
      data: [
        { id: 1, text: '任务1', start_date: '2023-01-01', duration: 5 }
      ]
    });
  }, []);

  return <div id="gantt_container" style={{width: '100%', height: '500px'}}></div>;
}

自定义实现方案

使用 SVG 和 React 状态管理创建基础甘特图:

import { useState } from 'react';

const GanttChart = () => {
  const [tasks] = useState([
    { id: 1, name: '开发', start: 0, end: 5 },
    { id: 2, name: '测试', start: 3, end: 8 }
  ]);

  return (
    <div style={{ width: '100%', overflowX: 'auto' }}>
      <svg width="800" height="200">
        {tasks.map(task => (
          <g key={task.id}>
            <rect
              x={task.start * 30}
              y={task.id * 40}
              width={(task.end - task.start) * 30}
              height="30"
              fill="#4CAF50"
            />
            <text x={task.start * 30 + 5} y={task.id * 40 + 20} fill="white">
              {task.name}
            </text>
          </g>
        ))}
      </svg>
    </div>
  );
};

甘特图功能扩展

添加依赖关系连线(使用 react-gantt-timeline):

<Gantt>
  <GanttRow
    id="1"
    title="任务1"
    start={new Date(2023, 0, 1)}
    end={new Date(2023, 0, 5)}
  />
  <GanttRow
    id="2"
    title="任务2"
    start={new Date(2023, 0, 6)}
    end={new Date(2023, 0, 10)}
    dependencies={['1']}
  />
</Gantt>

添加进度条显示(dhtmlx-gantt):

react甘特图实现

gantt.parse({
  data: [
    {
      id: 1,
      text: '任务',
      start_date: '2023-01-01',
      duration: 10,
      progress: 0.5
    }
  ]
});

标签: 甘特图react
分享给朋友:

相关文章

如何用react

如何用react

使用React的基本步骤 安装Node.js和npm 确保系统已安装Node.js和npm。Node.js自带npm,可从官网下载安装包。安装完成后,通过命令行验证版本: node -v npm -…

react如何下载

react如何下载

安装 React 的步骤 通过 npm 或 yarn 安装 React,确保已安装 Node.js(版本建议 ≥ 14.0.0)。打开终端或命令行工具,运行以下命令: npx create-reac…

如何清洁react

如何清洁react

清洁 React 项目的方法 删除未使用的依赖项 运行 npm ls 或 yarn list 检查已安装的依赖项,使用 npm uninstall <package> 或 yarn rem…

react 如何获取dom

react 如何获取dom

获取 DOM 的方法 在 React 中,可以通过 useRef 或 createRef 来获取 DOM 节点。useRef 是函数组件中常用的方法,而 createRef 通常用于类组件。 使用…

react如何修改值

react如何修改值

修改 React 组件的值 在 React 中修改值通常涉及状态管理。根据组件的类型(类组件或函数组件)和状态管理方式(本地状态或全局状态),方法有所不同。 使用 useState(函数组件) 在函…

如何封装表单react

如何封装表单react

封装 React 表单的方法 封装 React 表单可以通过创建可复用的表单组件或自定义 Hook 来实现。以下是几种常见的方法: 使用受控组件封装表单 通过状态管理表单数据,将表单字段的值与 Re…