当前位置:首页 > 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 中实现搜索功能通常涉及数据绑定、事件监听和过滤逻辑。以下是几种常见的实现方式: 使用计算属性过滤列表 <template> <div>…

vue实现购物按钮

vue实现购物按钮

Vue 购物按钮实现方法 基础按钮实现 使用 Vue 的模板语法创建基础按钮组件,绑定点击事件处理购物逻辑: <template> <button @click="addTo…

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法…

vue 实现在线预览

vue 实现在线预览

Vue 实现在线预览的常见方法 使用 iframe 嵌入 通过 iframe 标签可以嵌入多种类型的文件,如 PDF、图片、网页等。需要确保文件地址可访问。 <template>…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…