当前位置:首页 > VUE

甘特图vue实现

2026-03-07 07:08:44VUE

使用 vue-ganttastic 库实现甘特图

安装 vue-ganttastic 库

npm install vue-ganttastic

基础用法示例

<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'
        },
        {
          id: 'task2',
          name: '任务二',
          start: '2023-01-06',
          end: '2023-01-10'
        }
      ]
    }
  }
}
</script>

使用 vue-echarts 实现自定义甘特图

安装依赖

npm install echarts vue-echarts

甘特图配置示例

<template>
  <v-chart :option="option" style="height:400px"/>
</template>

<script>
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { BarChart } from 'echarts/charts'
import {
  GridComponent,
  TooltipComponent,
  LegendComponent
} from 'echarts/components'
import VChart from 'vue-echarts'

use([
  CanvasRenderer,
  BarChart,
  GridComponent,
  TooltipComponent,
  LegendComponent
])

export default {
  components: { VChart },
  data() {
    return {
      option: {
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' }
        },
        xAxis: {
          type: 'value',
          min: 0,
          max: 10
        },
        yAxis: {
          type: 'category',
          data: ['任务A', '任务B']
        },
        series: [
          {
            type: 'bar',
            data: [
              [3, 0, 5],
              [1, 1, 4]
            ],
            barGap: '0%'
          }
        ]
      }
    }
  }
}
</script>

使用原生SVG实现简单甘特图

基础SVG甘特图组件

<template>
  <div class="gantt-container">
    <svg width="100%" height="300">
      <g v-for="(task, index) in tasks" :key="task.id">
        <rect
          :x="calcX(task.start)"
          y="20"
          :width="calcWidth(task.start, task.end)"
          height="30"
          fill="#4CAF50"
        />
        <text
          :x="calcX(task.start) + 5"
          y="40"
          fill="white"
        >{{ task.name }}</text>
      </g>
    </svg>
  </div>
</template>

<script>
export default {
  props: {
    tasks: Array
  },
  methods: {
    calcX(date) {
      return new Date(date).getDate() * 30
    },
    calcWidth(start, end) {
      const days = (new Date(end) - new Date(start)) / (1000 * 60 * 60 * 24)
      return days * 30
    }
  }
}
</script>

甘特图功能扩展建议

添加拖拽功能

// 使用vue-draggable库实现任务条拖拽
import draggable from 'vuedraggable'

// 在组件中添加拖拽事件处理
onDragEnd(event) {
  // 更新任务位置数据
}

添加时间缩放控件

// 在图表配置中添加缩放滑块
option.dataZoom = [
  {
    type: 'slider',
    xAxisIndex: 0,
    start: 0,
    end: 100
  }
]

添加任务依赖关系

甘特图vue实现

// 在任务数据中添加依赖项
tasks: [
  {
    id: 'task1',
    dependencies: ['task2']
  }
]

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

相关文章

vue伪函数实现

vue伪函数实现

Vue 伪函数实现方法 伪函数(Mock Function)在 Vue 中常用于测试或模拟某些逻辑,以下是几种常见的实现方式: 使用 Jest 的 jest.fn() 在单元测试中,可以通过 Jes…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue实现展开

vue实现展开

展开功能的实现方法 在Vue中实现展开功能可以通过多种方式完成,常见的有使用v-show、v-if指令,结合CSS过渡动画或第三方组件库。 使用v-show控制显示隐藏 通过v-show指令可以简单…

vue实现左右菜单联动实现

vue实现左右菜单联动实现

Vue 实现左右菜单联动 使用事件总线通信 在 Vue 中可以通过事件总线实现左右菜单的通信。创建一个中央事件总线实例,让左右菜单通过事件监听和触发实现联动。 // eventBus.js impo…

vue cli实现原理

vue cli实现原理

Vue CLI 实现原理 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,其核心实现原理可以分为以下几个部分: 核心架构 Vue CLI 采用插件化架构,通过 @vue/cli 提…