当前位置:首页 > VUE

vue实现甘特图

2026-02-10 12:44:57VUE

使用 vue-gantt-elastic 实现甘特图

vue-gantt-elastic 是一个基于 Vue.js 的甘特图组件,支持拖拽、缩放、自定义样式等功能。

安装依赖:

npm install vue-gantt-elastic

基础实现代码:

vue实现甘特图

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

<script>
import GanttElastic from 'vue-gantt-elastic'

export default {
  components: {
    GanttElastic
  },
  data() {
    return {
      options: {
        title: {
          label: '项目计划',
          html: false
        },
        row: {
          height: 24
        }
      },
      tasks: [
        {
          id: 1,
          label: '任务1',
          user: '张三',
          start: '2023-01-01',
          end: '2023-01-05'
        },
        {
          id: 2,
          label: '任务2',
          user: '李四',
          start: '2023-01-03',
          end: '2023-01-08'
        }
      ]
    }
  },
  methods: {
    tasksChanged(tasks) {
      this.tasks = tasks
    }
  }
}
</script>

使用 vue-draggable-gantt 实现可拖拽甘特图

vue-draggable-gantt 提供拖拽功能,适合需要交互的场景。

安装依赖:

vue实现甘特图

npm install vue-draggable-gantt

基础实现代码:

<template>
  <div>
    <gantt-chart
      :tasks="tasks"
      @task-updated="handleTaskUpdated"
    />
  </div>
</template>

<script>
import { GanttChart } from 'vue-draggable-gantt'

export default {
  components: {
    GanttChart
  },
  data() {
    return {
      tasks: [
        {
          id: 1,
          name: '开发',
          start: '2023-01-01',
          end: '2023-01-10',
          progress: 50
        },
        {
          id: 2,
          name: '测试',
          start: '2023-01-08',
          end: '2023-01-15',
          progress: 20
        }
      ]
    }
  },
  methods: {
    handleTaskUpdated(task) {
      console.log('任务更新:', task)
    }
  }
}
</script>

使用原生实现简单甘特图

对于简单需求,可以使用原生HTML+CSS实现。

基础实现代码:

<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: 5 },
        { id: 2, name: '任务B', start: 3, duration: 7 }
      ],
      dayWidth: 30
    }
  },
  methods: {
    taskStyle(task) {
      return {
        left: `${task.start * this.dayWidth}px`,
        width: `${task.duration * this.dayWidth}px`
      }
    }
  }
}
</script>

<style>
.gantt-container {
  position: relative;
  height: 300px;
  border: 1px solid #ccc;
}

.gantt-task {
  position: absolute;
  height: 30px;
  background-color: #4CAF50;
  color: white;
  padding: 5px;
  box-sizing: border-box;
  border-radius: 3px;
}
</style>

甘特图功能扩展建议

  1. 时间轴缩放:添加时间轴缩放控件,支持按天/周/月查看
  2. 任务依赖:使用箭头连接相关任务,显示依赖关系
  3. 进度显示:在任务条上叠加进度条,显示完成百分比
  4. 资源分配:为任务添加人员分配信息,支持多资源分配
  5. 关键路径:高亮显示项目关键路径任务
  6. 导出功能:支持导出为图片或PDF格式

性能优化建议

  1. 虚拟滚动:对于大量任务,实现虚拟滚动提高性能
  2. 数据分页:超大数据量时采用分页加载
  3. 防抖处理:对频繁操作如拖拽进行防抖处理
  4. Web Worker:复杂计算放入Web Worker执行
  5. 按需渲染:只渲染可见区域的任务

以上方法可根据项目需求选择合适方案,复杂场景推荐使用现成库,简单需求可考虑原生实现。

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

相关文章

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现pc

vue实现pc

Vue 实现 PC 端应用开发 Vue.js 是一个流行的前端框架,适用于构建 PC 端 Web 应用。以下是关键步骤和最佳实践: 项目初始化 使用 Vue CLI 或 Vite 创建项目:…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…