当前位置:首页 > VUE

vue实现甘特图

2026-03-07 12:35:03VUE

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

安装依赖包:

npm install vue-gantt-elastic

基础实现代码:

<template>
  <div>
    <gantt-elastic
      :options="options"
      :tasks="tasks"
    ></gantt-elastic>
  </div>
</template>

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

export default {
  components: {
    GanttElastic
  },
  data() {
    return {
      options: {
        title: {
          label: '项目计划',
          html: false
        }
      },
      tasks: [
        {
          id: 'task1',
          label: '需求分析',
          user: '张三',
          start: '2023-01-01',
          end: '2023-01-07',
          percent: 100
        },
        {
          id: 'task2',
          label: 'UI设计',
          user: '李四',
          start: '2023-01-08',
          end: '2023-01-15',
          percent: 80
        }
      ]
    }
  }
}
</script>

自定义甘特图样式

通过修改 options 配置自定义样式:

vue实现甘特图

options: {
  row: {
    height: 40
  },
  calendar: {
    hour: {
      display: false
    }
  },
  chart: {
    progress: {
      bar: {
        width: 20
      }
    }
  }
}

实现交互功能

添加任务点击事件:

<gantt-elastic
  :options="options"
  :tasks="tasks"
  @task-click="handleTaskClick"
></gantt-elastic>

methods: {
  handleTaskClick(task) {
    console.log('点击任务:', task)
  }
}

使用 vue-draggable-resizable 实现拖拽调整

安装依赖:

vue实现甘特图

npm install vue-draggable-resizable

实现任务拖拽:

<template>
  <div>
    <gantt-elastic
      ref="gantt"
      :options="options"
      :tasks="tasks"
      @task-drag-end="handleDragEnd"
    ></gantt-elastic>
  </div>
</template>

methods: {
  handleDragEnd({ task, changes }) {
    Object.assign(task, changes)
  }
}

时间轴自定义配置

设置不同时间粒度:

options: {
  calendar: {
    month: {
      display: true
    },
    week: {
      display: true
    },
    day: {
      display: true
    }
  }
}

响应式布局处理

监听窗口变化调整甘特图:

mounted() {
  window.addEventListener('resize', this.handleResize)
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize)
},
methods: {
  handleResize() {
    this.$refs.gantt.resize()
  }
}

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

相关文章

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…

vue框架实现

vue框架实现

Vue 框架实现方法 Vue 是一个渐进式 JavaScript 框架,适用于构建用户界面。以下是一些核心实现方法,涵盖基本使用、组件化、状态管理和优化技巧。 基本项目搭建 使用 Vue CLI…

vue实现分站

vue实现分站

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