当前位置:首页 > 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 实现自定义甘特图

安装依赖

甘特图vue实现

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甘特图组件

甘特图vue实现

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

添加任务依赖关系

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

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

相关文章

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <templ…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue实现积分

vue实现积分

Vue 实现积分功能 在 Vue 中实现积分功能通常涉及前端展示、交互逻辑以及与后端 API 的通信。以下是常见的实现方法和步骤: 积分展示组件 创建一个积分展示组件,用于显示当前用户的积分。可以使…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue实现逻辑

vue实现逻辑

Vue 实现逻辑的核心概念 Vue.js 是一个渐进式 JavaScript 框架,其核心逻辑围绕数据驱动和组件化开发。通过响应式系统、虚拟 DOM 和组件生命周期等机制,Vue 实现了高效的前端开发…