pengwei
2025-03-07 7cc3ed0f7aca03e39fa65d617f2a51ff531a12f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<template>
  <el-card style="border-radius: 0" :body-style="{ padding: '1rem 2rem' }" v-loading="loading">
    <template #header>
      <div style="font-weight: bold; font-size: 1.2rem">物料类型统计</div>
    </template>
    <div ref="goodsTypeChartDom" style="height: 25rem; width: 100%"></div>
  </el-card>
</template>
 
<script setup>
import { ref, onMounted, onUnmounted, nextTick } from "vue";
// 引入echarts
import * as echarts from "echarts";
import { GetGoodsTypeDistApi } from '@/api/dashboard';
import { ElLoading } from 'element-plus'
const loading=ref(true)
 
const xData = ref([]);
const yData = ref([]);
 
 
// 创建一个响应式引用来保存DOM元素
const goodsTypeChartDom = ref(null);
let chartInstance = null;
 
// 初始化ECharts实例并设置配置项(这里以折线图为例,但可灵活替换)
onMounted(async () => {
  await GetGoodsTypeDistApi().then(res => {
    xData.value = res.data.map(i => i.name)
    yData.value = res.data
  })
 
 
  await nextTick(); // 确保DOM已经渲染完成
  chartInstance = echarts.init(goodsTypeChartDom.value);
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    color: ["#EAEA26", "#906BF9", "#FE5656", "#01E17E"],
    series: [
      {
        name: '物料占比',
        type: 'pie',
        radius: '50%',
        center: ['50%', '50%'],
        data: yData.value,
        itemStyle: {
          emphasis: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        },
        itemStyle: {
          normal: {
            label: {
              show: true,
              //                                position:'inside',
              formatter: '{b} : {c} ({d}%)'
            }
          },
          labelLine: { show: true }
        }
      }
    ]
  };
  chartInstance.setOption(option);
  nextTick(() => {
    loading.value = false
  });
});
 
// 销毁ECharts实例
onUnmounted(() => {
  if (chartInstance != null && chartInstance.dispose) {
    chartInstance.dispose();
  }
});
 
//窗口大小变化重绘echart
window.onresize = function () {
  if (chartInstance != null && chartInstance.resize) {
    chartInstance.resize();
  }
};
</script>
 
<style></style>