当前位置:首页 > 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项目实现甘特图

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

相关文章

vue 实现的项目

vue 实现的项目

Vue 实现的项目类型 Vue.js 是一个渐进式 JavaScript 框架,适用于构建各种规模的项目。以下是一些常见的 Vue 实现项目类型: 单页应用 (SPA) Vue 非常适合构建单页应…

甘特图vue实现

甘特图vue实现

甘特图在 Vue 中的实现方法 使用现成库(推荐) 推荐使用 vue-ganttastic 或 dhtmlx-gantt 等现成库快速实现甘特图功能。 安装 vue-ganttastic: npm…

vue实现甘特图

vue实现甘特图

Vue 实现甘特图的方法 使用开源库(推荐) Vue-Ganttastic 是一个专为 Vue 设计的轻量级甘特图库,支持拖拽、缩放和自定义样式。安装后可直接嵌入组件: npm install v…

uniapp项目教程

uniapp项目教程

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

uniapp项目源码

uniapp项目源码

Uniapp 项目源码获取方式 官方示例与模板 Uniapp 官方提供了多个开源项目模板和示例,涵盖基础框架、UI组件库等。可通过以下途径获取: 在 DCloud 插件市场 搜索“uniapp…

uniapp前端项目

uniapp前端项目

Uniapp 前端项目开发指南 Uniapp 是一个基于 Vue.js 的跨平台开发框架,支持一次开发,多端发布(如微信小程序、H5、App 等)。以下是关于 Uniapp 前端项目的关键信息:…