当前位置:首页 > 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结构:

甘特图vue实现

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

进阶功能实现

时间轴缩放控制:

甘特图vue实现

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集成示例:

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 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…