当前位置:首页 > 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集成示例:

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 实现滑动效果 使用 CSS 过渡和动画 通过 Vue 的 transition 组件结合 CSS 过渡或动画实现滑动效果。适用于简单的元素入场/离场滑动。 <template>…

vue实现自动翻译

vue实现自动翻译

Vue 实现自动翻译的方法 在 Vue 项目中实现自动翻译功能可以通过多种方式完成,以下是几种常见的方法: 使用第三方翻译 API 注册并获取翻译 API 的密钥(如 Google Transla…

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现多人视频

vue实现多人视频

实现多人视频通话的Vue方案 使用WebRTC技术结合Vue框架可以构建多人视频通话应用。以下是核心实现方法: 技术栈选择 Vue 2/3作为前端框架 WebRTC用于实时通信 Socket.…

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <t…