当前位置:首页 > VUE

vue实现甘特图

2026-03-27 22:31:13VUE

使用 vue-ganttastic 库实现甘特图

vue-ganttastic 是一个轻量级的 Vue 甘特图组件库,支持任务拖拽、缩放和自定义样式。

安装依赖:

npm install vue-ganttastic

基础实现代码:

vue实现甘特图

<template>
  <GanttChart :tasks="tasks" />
</template>

<script>
import { GanttChart } from 'vue-ganttastic'
export default {
  components: { GanttChart },
  data() {
    return {
      tasks: [
        {
          id: 'task1',
          name: '任务一',
          start: '2023-01-01',
          end: '2023-01-05',
          progress: 50
        },
        {
          id: 'task2',
          name: '任务二',
          start: '2023-01-03',
          end: '2023-01-08',
          progress: 20
        }
      ]
    }
  }
}
</script>

使用 dhtmlx-gantt 实现专业级甘特图

dhtmlx-gantt 是功能强大的商业甘特图库,提供 Vue 封装版本。

安装依赖:

vue实现甘特图

npm install dhtmlx-gantt

基础实现代码:

<template>
  <div ref="gantt_container" style="width:100%; height:400px;"></div>
</template>

<script>
import { gantt } from 'dhtmlx-gantt'
import 'dhtmlx-gantt/codebase/dhtmlxgantt.css'

export default {
  mounted() {
    gantt.init(this.$refs.gantt_container)
    gantt.parse({
      data: [
        { id: 1, text: "任务一", start_date: "2023-01-01", duration: 5 },
        { id: 2, text: "任务二", start_date: "2023-01-03", duration: 4 }
      ],
      links: [
        { id: 1, source: 1, target: 2, type: "0" }
      ]
    })
  }
}
</script>

自定义实现简单甘特图

对于简单需求,可以使用纯 Vue 实现基础甘特图功能。

实现代码示例:

<template>
  <div class="gantt-container">
    <div v-for="task in tasks" :key="task.id" class="gantt-task" 
         :style="taskStyle(task)">
      {{ task.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tasks: [
        { id: 1, name: '任务A', start: 0, duration: 3 },
        { id: 2, name: '任务B', start: 2, duration: 4 }
      ],
      dayWidth: 50
    }
  },
  methods: {
    taskStyle(task) {
      return {
        width: `${task.duration * this.dayWidth}px`,
        left: `${task.start * this.dayWidth}px`,
        backgroundColor: `hsl(${task.id * 60}, 70%, 80%)`
      }
    }
  }
}
</script>

<style>
.gantt-container {
  position: relative;
  height: 300px;
  border: 1px solid #ddd;
}
.gantt-task {
  position: absolute;
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  border-radius: 3px;
  margin-top: 5px;
}
</style>

甘特图功能扩展建议

  1. 时间轴控制:添加缩放按钮调整时间粒度(天/周/月)
  2. 任务交互:实现拖拽调整任务时间、右键菜单操作
  3. 进度显示:在任务条上叠加进度指示条
  4. 依赖关系:用箭头线显示任务间的依赖关系
  5. 资源分配:添加人员/资源分配显示功能

性能优化方案

  1. 虚拟滚动:只渲染可视区域内的任务项
  2. 数据分页:对大规模数据采用分页加载
  3. 防抖处理:对频繁操作如拖拽进行防抖优化
  4. Web Worker:将复杂计算移入 Web Worker

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

相关文章

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue实现队列

vue实现队列

Vue 实现队列功能 在 Vue 中实现队列功能可以通过多种方式完成,以下是几种常见的方法: 使用数组模拟队列 队列遵循先进先出(FIFO)原则,可以用数组的 push 和 shift 方法模拟入队…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…

vue 实现 confirm

vue 实现 confirm

实现确认对话框的方法 在Vue中实现确认对话框可以通过多种方式完成,包括使用内置组件、第三方库或自定义组件。 使用浏览器原生confirm 最简单的实现方式是直接调用浏览器原生的confirm方法。…

vue实现分站

vue实现分站

Vue 实现分站的方案 在 Vue 中实现分站功能通常涉及路由配置、环境变量管理和动态加载资源。以下是几种常见的实现方式: 基于路由的分站实现 通过 Vue Router 配置多路由,区分不同站点的…

vue系统实现

vue系统实现

Vue 系统实现的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装核心依赖(如 Vue 3、Vue Router、Pinia)。配置开发环境(如 ESLint、Prett…