当前位置:首页 > 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 配置自定义样式:

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 实现拖拽调整

安装依赖:

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
    }
  }
}

响应式布局处理

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

vue实现甘特图

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

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

相关文章

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue 实现vshow

vue 实现vshow

Vue 实现 v-show 在 Vue 中,v-show 是一个常用的指令,用于根据条件控制元素的显示或隐藏。与 v-if 不同,v-show 不会移除 DOM 元素,而是通过 CSS 的 displ…

vue 实现templateurl

vue 实现templateurl

在 Vue 中,templateUrl 是 Angular 中的一个概念,用于指定外部 HTML 文件作为模板。Vue 本身不直接支持 templateUrl,但可以通过以下方法实现类似功能: 使用…

vue实现座位

vue实现座位

Vue实现座位布局 使用Vue实现座位布局可以通过组件化和响应式数据管理来简化开发流程。以下是一个完整的实现方案: 数据准备 定义座位数据结构,通常使用二维数组表示行列关系: data() {…

vue实现layout

vue实现layout

Vue 实现 Layout 布局的方法 在 Vue 中实现 Layout 布局通常涉及路由嵌套、组件化设计和动态渲染。以下是几种常见的实现方式: 使用嵌套路由 通过 Vue Router 的嵌套路由…