vue3使用Echarts图表生成项目进度甘特图

先看效果

代码展示

<template>
  <h1>项目进度甘特图</h1>
  <div id="app">
    <!-- Echarts 图表 -->
    <div ref="progressChart" class="progressChart"></div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';
import axios from '@/utils/axios-config';
const progressChart = ref(null); // Ref for Echarts initialization


onMounted(async () => {
  
  const projectId = localStorage.getItem('projectId');
  
  try {
    
    const response = await axios.get(`/project-node/get/${projectId}`);
    const projectData = response.data;

    // Initialize Echarts chart after fetching data
    const chart = echarts.init(progressChart.value);

    const option = {
      tooltip: {
        trigger: 'axis',
        formatter(params) {
          if (params[1].data && params[0].data) {
            return `<div>开始时间:${params[1].data}</div>` + `<div>结束时间:${params[0].data}</div>`;
          } else {
            return '';
          }
        },
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        containLabel: true,
        show: false,
        right: 80,
        left: 40,
        bottom: 40,
        top: 20,
        backgroundColor: '#fff'
      },
      legend: {
        data: ['持续时间'],
        align: 'auto',
        top: 'bottom'
      },
      xAxis: {
        type: 'time',
        position: 'top',
        axisTick: {
          show: false
        },
        axisLine: {
          show: false
        },
        splitLine: {
          show: true
        }
      },
      yAxis: {
        inverse: true,
        axisTick: {
          show: false
        },
        axisLine: {
          show: false
        },
        data: ['策划评审', '立项评审', '详细方案评审', '测试验证', '样机评审', '样机定版', '小批量评审', '批量评审', '结项 ']
      },
      //color: ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple','#007acc','green'], // 自定义调色盘的颜色
      series: [
        {
          name: '持续时间',
          type: 'bar',
          stack: 'duration',
          itemStyle: {
            color: '#007acc',
            borderColor: '#fff',
            borderWidth: 1
          },
          zlevel: -1,
          data: [
            projectData.mastermindFinish,
            projectData.projectFinish,
            projectData.schemeFinish,
            projectData.testFinish,
            projectData.prototypeFinish,
            projectData.orderingFinish,
            projectData.smallbatchFinish,
            projectData.batchFinish,
            projectData.junctionFinish
          ]
        },
        {
          name: '持续时间',
          type: 'bar',
          stack: 'duration',
          itemStyle: {
            color: '#fff'
          },
          zlevel: -1,
          z: 9,
          data: [
            projectData.mastermindPlan,
            projectData.projectPlan,
            projectData.schemePlan,
            projectData.testPlan,
            projectData.prototypePlan,
            projectData.orderingPlan,
            projectData.smallbatchPlan,
            projectData.batchPlan,
            projectData.junctionPlan
          ]
        }
        
      ]
    };

    chart.setOption(option);

    // Resize chart on window resize
    window.addEventListener('resize', () => {
      chart.resize();
    });

  } catch (error) {
    console.error('Error fetching project data:', error);
  }
});
</script>

<style scoped>
#app {
  display: flex;
  justify-content: center;
  align-items: center;
}
.progressChart {
  width: 80%;
  height: 500px;
  border: 1px solid #aaa;
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值