当前位置:首页 > VUE

纯vue项目实现甘特图

2026-01-21 07:01:26VUE

使用第三方库(如vue-gantt-elastic)

安装依赖库vue-gantt-elastic,该库专为Vue设计,支持拖拽、缩放、自定义样式等功能。

npm install vue-gantt-elastic

在组件中引入并配置数据:

<template>
  <gantt-elastic
    :tasks="tasks"
    :options="options"
    @tasks-changed="updateTasks"
  ></gantt-elastic>
</template>

<script>
import GanttElastic from "vue-gantt-elastic";
export default {
  components: { GanttElastic },
  data() {
    return {
      tasks: [
        { id: 1, text: "Task 1", start: "2023-01-10", duration: 5 },
        { id: 2, text: "Task 2", start: "2023-01-15", duration: 3 }
      ],
      options: {
        title: { label: "Project Timeline" }
      }
    };
  },
  methods: {
    updateTasks(updatedTasks) {
      this.tasks = updatedTasks;
    }
  }
};
</script>

基于原生SVG手动实现

通过SVG绘制甘特图,结合Vue的数据绑定实现动态更新。
定义任务数据结构和计算坐标的逻辑:

data() {
  return {
    tasks: [
      { id: 1, name: "Design", start: new Date(2023, 0, 1), end: new Date(2023, 0, 10) },
      { id: 2, name: "Development", start: new Date(2023, 0, 5), end: new Date(2023, 0, 20) }
    ],
    scale: 20 // 像素/天
  };
},
computed: {
  svgPaths() {
    return this.tasks.map(task => {
      const x1 = (task.start - new Date(2023, 0, 1)) / (1000 * 60 * 60 * 24) * this.scale;
      const width = (task.end - task.start) / (1000 * 60 * 60 * 24) * this.scale;
      return { x1, width, task };
    });
  }
}

模板中使用v-for渲染SVG元素:

<svg width="800" height="400">
  <g v-for="(path, index) in svgPaths" :key="index">
    <rect 
      :x="path.x1" 
      y="20" 
      :width="path.width" 
      height="30" 
      fill="#4CAF50"
      @click="selectTask(path.task)"
    />
    <text :x="path.x1" y="50">{{ path.task.name }}</text>
  </g>
</svg>

集成FullCalendar与Gantt插件

FullCalendar的resource-timeline视图可模拟甘特图功能。
安装依赖并配置时间轴视图:

npm install @fullcalendar/vue @fullcalendar/resource-timeline

组件实现示例:

<template>
  <FullCalendar :options="calendarOptions" />
</template>

<script>
import FullCalendar from "@fullcalendar/vue";
import resourceTimelinePlugin from "@fullcalendar/resource-timeline";
export default {
  components: { FullCalendar },
  data() {
    return {
      calendarOptions: {
        plugins: [resourceTimelinePlugin],
        initialView: "resourceTimelineWeek",
        resources: [
          { id: "1", title: "Task A" },
          { id: "2", title: "Task B" }
        ],
        events: [
          { resourceId: "1", start: "2023-01-01", end: "2023-01-10", title: "Design" },
          { resourceId: "2", start: "2023-01-05", end: "2023-01-15", title: "Develop" }
        ]
      }
    };
  }
};
</script>

注意事项

  • 时间格式处理:确保日期数据使用ISO格式或JavaScript的Date对象,避免时区问题。
  • 响应式设计:通过监听窗口大小变化动态调整SVG或Canvas的尺寸。
  • 性能优化:大数据量时考虑虚拟滚动(如使用vue-virtual-scroller)。

以上方法可根据项目复杂度选择,第三方库适合快速实现,原生方案适合高度定制需求。

纯vue项目实现甘特图

标签: 项目甘特图
分享给朋友:

相关文章

react项目如何

react项目如何

创建React项目 使用create-react-app工具快速初始化项目。确保已安装Node.js(建议版本≥12.0.0),运行以下命令生成新项目: npx create-react-app…

uniapp项目案例

uniapp项目案例

uniapp项目案例概述 uniapp作为跨平台开发框架,广泛应用于多端应用开发(H5、小程序、App等)。以下是一些典型项目案例及实现方法,涵盖电商、社交、工具等场景。 电商类案例 案例:…

uniapp项目教程

uniapp项目教程

uniapp项目教程 uniapp 是一个基于 Vue.js 的跨平台开发框架,支持一次开发,多端发布(包括 iOS、Android、H5、小程序等)。以下是详细的教程指南,帮助快速上手 uniapp…

vue ui实现创建vue项目

vue ui实现创建vue项目

使用 Vue UI 创建 Vue 项目的步骤 Vue CLI 提供了一个图形化界面(Vue UI)来创建和管理 Vue 项目。以下是详细的操作步骤: 安装 Vue CLI 确保已安装 Node.j…

vue项目如何实现

vue项目如何实现

安装Vue.js 通过npm或yarn安装Vue.js。确保Node.js环境已配置完成。 npm install vue # 或 yarn add vue 创建Vue项目 使用Vue CLI工具快…

react如何创建项目

react如何创建项目

使用 Create React App 创建项目 Create React App (CRA) 是官方推荐的 React 项目脚手架工具,适合快速初始化一个现代化的 React 项目。 安装 C…