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

基本实现代码:

react甘特图实现

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

基本配置:

react甘特图实现

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 中的动画衰减效果实现 在 React 中实现衰减效果(如滚动衰减、拖动释放后的惯性滑动)通常需要结合物理动画原理或第三方动画库。以下是几种常见方法: 使用 CSS 动画和 @keyfra…

react如何调试

react如何调试

调试 React 应用的方法 使用 React Developer Tools React Developer Tools 是 Chrome 和 Firefox 的浏览器扩展,可以检查组件树、状态和…

react如何注销

react如何注销

React 组件的注销方法 在 React 中,组件的注销通常指的是在组件卸载时执行清理操作,例如取消订阅、清除定时器或释放资源。以下是几种常见的方法: 使用 useEffect 钩子的清理函数…

react如何启动

react如何启动

启动 React 项目的步骤 确保已安装 Node.js React 需要 Node.js 环境运行,建议安装最新稳定版(LTS)。可通过以下命令检查是否已安装: node -v npm -v…

vue实现甘特图展示

vue实现甘特图展示

vue实现甘特图展示的方法 使用开源库vue-ganttastic vue-ganttastic是一个专为Vue设计的轻量级甘特图组件,支持任务拖拽、时间轴缩放等功能。安装后可直接在组件中引入: n…

react如何引用jquery

react如何引用jquery

在React项目中引用jQuery可以通过以下方法实现: 安装jQuery依赖 通过npm或yarn安装jQuery包: npm install jquery # 或 yarn add jquer…