当前位置:首页 > VUE

vue实现甘特图

2026-01-08 01:02:11VUE

使用 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: '任务A', start: '2023-01-01', end: '2023-01-05' },
        { id: 2, name: '任务B', start: '2023-01-03', end: '2023-01-08' }
      ],
      options: {
        viewMode: 'day',
        style: {
          'bar-color': '#4CAF50',
          'bar-radius': '4px'
        }
      }
    };
  }
};
</script>

自定义实现(SVG + 计算逻辑)

核心逻辑结构:

<template>
  <div class="gantt-container">
    <svg :width="width" :height="height">
      <!-- 时间轴 -->
      <g v-for="(day, i) in days" :key="i">
        <text :x="i * dayWidth + 10" y="20">{{ day }}</text>
      </g>

      <!-- 任务条 -->
      <rect
        v-for="task in tasks"
        :key="task.id"
        :x="getTaskX(task.start)"
        :y="40 + (task.id * 30)"
        :width="getTaskWidth(task.start, task.end)"
        height="20"
        fill="#4CAF50"
      />
    </svg>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tasks: [
        { id: 1, name: '任务A', start: '2023-01-01', end: '2023-01-05' },
        { id: 2, name: '任务B', start: '2023-01-03', end: '2023-01-08' }
      ],
      days: ['1月1日', '1月2日', '1月3日', '1月4日', '1月5日'],
      dayWidth: 60,
      width: 800,
      height: 400
    };
  },
  methods: {
    getTaskX(startDate) {
      const startIndex = this.days.findIndex(day => day.includes(startDate.split('-')[2]));
      return startIndex * this.dayWidth;
    },
    getTaskWidth(startDate, endDate) {
      const diffDays = (new Date(endDate) - new Date(startDate)) / (1000 * 60 * 60 * 24);
      return diffDays * this.dayWidth;
    }
  }
};
</script>

集成第三方库(如 Chart.js)

安装 Chart.js 和 vue-chartjs:

npm install chart.js vue-chartjs

甘特图配置示例:

import { HorizontalBar } from 'vue-chartjs';

export default {
  extends: HorizontalBar,
  data() {
    return {
      chartData: {
        labels: ['任务A', '任务B'],
        datasets: [{
          data: [
            { x: new Date('2023-01-01'), y: new Date('2023-01-05') },
            { x: new Date('2023-01-03'), y: new Date('2023-01-08') }
          ],
          backgroundColor: '#4CAF50'
        }]
      },
      options: {
        scales: {
          xAxes: [{
            type: 'time',
            position: 'top'
          }],
          yAxes: [{
            ticks: { display: false }
          }]
        }
      }
    };
  },
  mounted() {
    this.renderChart(this.chartData, this.options);
  }
};

关键实现要点

日期计算处理

const dateDiff = (date1, date2) => {
  const diffTime = Math.abs(new Date(date2) - new Date(date1));
  return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
};

响应式宽度调整

window.addEventListener('resize', () => {
  this.width = window.innerWidth * 0.8;
});

任务拖拽交互

handleDragStart(e, task) {
  e.dataTransfer.setData('taskId', task.id);
},
handleDrop(e, newDate) {
  const taskId = e.dataTransfer.getData('taskId');
  this.updateTaskDate(taskId, newDate);
}

样式优化建议

CSS 样式示例:

.gantt-container {
  overflow-x: auto;
  border: 1px solid #eee;
}

.task-bar {
  cursor: move;
  transition: all 0.2s;
}

.task-bar:hover {
  opacity: 0.8;
}

vue实现甘特图

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

相关文章

vue实现anchor

vue实现anchor

Vue 实现 Anchor(锚点)功能 在 Vue 中实现锚点功能可以通过多种方式完成,以下是几种常见的方法: 使用 HTML 原生锚点 HTML 原生锚点是最简单的方式,通过 <a>…

vue实现评论编辑

vue实现评论编辑

Vue 实现评论编辑功能 数据绑定与表单结构 使用 v-model 双向绑定评论内容到表单输入框,确保编辑时能显示原内容并同步修改。示例模板结构: <template> <di…

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…