当前位置:首页 > VUE

vue实现甘特图展示

2026-02-11 09:28:53VUE

使用Vue-Gantt组件库(如vue-gantt-elastic)

安装依赖包:

npm install vue-gantt-elastic

基础实现代码:

<template>
  <gantt-elastic
    :tasks="tasks"
    :options="options"
    @tasks-changed="handleTasksChange"
  ></gantt-elastic>
</template>

<script>
import GanttElastic from 'vue-gantt-elastic';
export default {
  components: { GanttElastic },
  data() {
    return {
      tasks: [
        {
          id: 'task1',
          label: '项目启动',
          start: '2023-01-01',
          end: '2023-01-10',
          progress: 50
        }
      ],
      options: {
        title: {
          label: '项目计划'
        }
      }
    };
  },
  methods: {
    handleTasksChange(updatedTasks) {
      this.tasks = updatedTasks;
    }
  }
};
</script>

基于D3.js自定义实现

通过D3.js的缩放和拖拽功能构建交互式甘特图:

vue实现甘特图展示

<template>
  <div ref="ganttContainer" class="gantt-chart"></div>
</template>

<script>
import * as d3 from 'd3';
export default {
  mounted() {
    this.renderGantt();
  },
  data() {
    return {
      tasks: [
        { id: 1, name: '需求分析', start: new Date(2023, 0, 1), end: new Date(2023, 0, 5) }
      ]
    };
  },
  methods: {
    renderGantt() {
      const svg = d3.select(this.$refs.ganttContainer)
        .append('svg')
        .attr('width', 800)
        .attr('height', 400);

      // 时间比例尺
      const xScale = d3.scaleTime()
        .domain([new Date(2023, 0, 1), new Date(2023, 0, 31)])
        .range([0, 700]);

      // 绘制任务条
      svg.selectAll('.task')
        .data(this.tasks)
        .enter()
        .append('rect')
        .attr('class', 'task-bar')
        .attr('x', d => xScale(d.start))
        .attr('y', (d, i) => i * 30 + 20)
        .attr('width', d => xScale(d.end) - xScale(d.start))
        .attr('height', 20)
        .attr('fill', '#4CAF50');
    }
  }
};
</script>

集成第三方甘特图库(如Frappe Gantt)

安装Frappe Gantt:

npm install frappe-gantt

Vue封装实现:

vue实现甘特图展示

<template>
  <div ref="gantt"></div>
</template>

<script>
import Gantt from 'frappe-gantt';
export default {
  mounted() {
    new Gantt(this.$refs.gantt, [
      {
        id: 't1',
        name: '任务1',
        start: '2023-01-01',
        end: '2023-01-05',
        progress: 30
      }
    ], {
      view_mode: 'Week'
    });
  }
};
</script>

响应式数据处理

动态更新甘特图数据示例:

watch: {
  tasks: {
    handler(newTasks) {
      if (this.gantt) {
        this.gantt.refresh(newTasks);
      }
    },
    deep: true
  }
}

样式优化建议

添加CSS增强可视化效果:

.gantt-chart {
  font-family: Arial;
  overflow-x: auto;
}
.task-bar:hover {
  opacity: 0.8;
  stroke: #333;
  stroke-width: 1px;
}

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

相关文章

vue实现drag

vue实现drag

Vue 实现拖拽功能的方法 在 Vue 中实现拖拽功能可以通过原生 HTML5 的拖拽 API 或第三方库如 vuedraggable 来完成。以下是两种常见方法的实现方式。 使用 HTML5 拖拽…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…

vue实现xterm

vue实现xterm

在 Vue 中集成 Xterm.js Xterm.js 是一个基于 TypeScript 的前端终端组件库,可用于在浏览器中实现终端功能。以下是在 Vue 项目中集成 Xterm.js 的详细步骤。…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <templa…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…