当前位置:首页 > 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):

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

react甘特图实现

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

相关文章

电脑如何安装react

电脑如何安装react

安装 Node.js 和 npm React 依赖于 Node.js 和 npm(Node Package Manager)。从 Node.js 官网下载并安装最新稳定版本,安装完成后会自动包含 np…

react 如何引入jquery

react 如何引入jquery

引入 jQuery 到 React 项目 在 React 项目中引入 jQuery 可以通过多种方式实现,但需要注意 React 和 jQuery 操作 DOM 的方式可能冲突,因此建议仅在必要时使用…

react 如何分页

react 如何分页

分页实现方法 在React中实现分页功能可以通过多种方式完成,具体取决于数据来源(如API或本地数据)和UI库的选择。以下是常见的实现方法: 使用本地数据分页 对于存储在组件状态或Contex…

react如何浮动

react如何浮动

使用 CSS 实现浮动 在 React 中实现浮动效果可以通过 CSS 的 float 属性完成。在组件的样式文件或内联样式中直接设置 float: left 或 float: right。…

如何开发react

如何开发react

开发React应用的基本步骤 安装Node.js和npm 确保系统中已安装Node.js(包含npm)。可通过官网下载安装包,安装后验证版本: node -v npm -v 创建React项目…

react如何检测

react如何检测

检测 React 应用的方法 使用 PropTypes 进行类型检查 PropTypes 是 React 内置的类型检查工具,用于验证组件的 props 是否符合预期。在开发环境中,PropTypes…