当前位置:首页 > VUE

甘特图vue实现

2026-01-07 21:36:39VUE

甘特图 Vue 实现方法

使用开源库 vue-ganttastic

安装依赖:

npm install vue-ganttastic

基础实现代码:

<template>
  <GanttChart
    :tasks="tasks"
    :options="options"
  />
</template>

<script>
import { GanttChart } from 'vue-ganttastic'

export default {
  components: {
    GanttChart
  },
  data() {
    return {
      tasks: [
        {
          id: 1,
          name: '任务1',
          start: '2023-01-01',
          end: '2023-01-05'
        },
        {
          id: 2,
          name: '任务2',
          start: '2023-01-06',
          end: '2023-01-10'
        }
      ],
      options: {
        viewMode: 'day',
        style: {
          'bar-color': '#4682B4',
          'bar-radius': '4px'
        }
      }
    }
  }
}
</script>

自定义实现方案

基础HTML结构:

<template>
  <div class="gantt-container">
    <div class="gantt-header">
      <div v-for="day in days" :key="day" class="gantt-header-day">
        {{ day }}
      </div>
    </div>
    <div class="gantt-body">
      <div v-for="task in tasks" :key="task.id" class="gantt-task">
        <div class="task-name">{{ task.name }}</div>
        <div class="task-bar-container">
          <div 
            class="task-bar"
            :style="{
              width: calculateWidth(task),
              marginLeft: calculateMargin(task)
            }"
          ></div>
        </div>
      </div>
    </div>
  </div>
</template>

计算逻辑:

methods: {
  calculateWidth(task) {
    const start = new Date(task.start)
    const end = new Date(task.end)
    const diff = (end - start) / (1000 * 60 * 60 * 24)
    return `${diff * 30}px`
  },
  calculateMargin(task) {
    const start = new Date(task.start)
    const projectStart = new Date(this.projectStart)
    const diff = (start - projectStart) / (1000 * 60 * 60 * 24)
    return `${diff * 30}px`
  }
}

进阶功能实现

时间轴缩放控制:

data() {
  return {
    zoomLevel: 1,
    zoomOptions: [0.5, 1, 2, 3]
  }
},
methods: {
  changeZoom(level) {
    this.zoomLevel = level
  }
}

任务拖拽功能:

mounted() {
  interact('.task-bar')
    .draggable({
      onmove: this.handleDrag
    })
},
methods: {
  handleDrag(event) {
    const target = event.target
    const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx
    target.style.left = `${x}px`
    target.setAttribute('data-x', x)
  }
}

样式优化建议

CSS样式示例:

.gantt-container {
  width: 100%;
  overflow-x: auto;
}

.gantt-header {
  display: flex;
  border-bottom: 1px solid #ddd;
}

.gantt-header-day {
  min-width: 30px;
  text-align: center;
  padding: 5px;
}

.task-bar {
  height: 20px;
  background-color: #4CAF50;
  position: relative;
  border-radius: 3px;
}

数据动态加载

API集成示例:

甘特图vue实现

async created() {
  try {
    const response = await axios.get('/api/tasks')
    this.tasks = response.data
  } catch (error) {
    console.error('Error fetching tasks:', error)
  }
}

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

相关文章

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现分站

vue实现分站

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

vue实现修改

vue实现修改

Vue 实现修改功能的方法 在 Vue 中实现修改功能通常涉及表单绑定、状态管理和 API 调用。以下是几种常见实现方式: 双向数据绑定实现表单修改 使用 v-model 指令绑定表单元素与组件数据…

vue实现销售程序

vue实现销售程序

Vue实现销售程序的基本步骤 搭建Vue项目基础结构 使用Vue CLI或Vite初始化项目,安装必要依赖(如Vue Router、Vuex/Pinia、Element UI/Ant Design V…

vue实现版本树

vue实现版本树

Vue 实现版本树 版本树通常用于展示项目或文档的版本历史,类似于 Git 的提交历史。以下是基于 Vue 的实现方法: 数据结构设计 版本树的数据通常是一个树形结构,每个节点包含版本信息(如版本号…