Commit 7435d8124b1730994225450d87acc84c8eb0beb2

Authored by fengwotao
1 parent 7f47e522

pref:优化首页右边设备状态统计圆环图渲染方式

1 1 <template>
2   - <a-row :gutter="16" align="middle">
3   - <a-col v-for="(item, index) in seriesStatusData" :key="index" :span="8">
4   - <div :id="`chartPie${item.key}`" style="width: 150px; height: 200px"></div>
  2 + <a-row type="flex" justify="space-between" align="middle">
  3 + <a-col
  4 + class="flex items-center"
  5 + v-for="(item, index) in seriesStatusData"
  6 + :key="index"
  7 + :span="8"
  8 + >
  9 + <div
  10 + :id="`chartPie${item.key}`"
  11 + style="width: 150px; height: 200px; position: relative; top: -0.7rem"
  12 + ></div>
5 13 </a-col>
6 14 </a-row>
7 15 </template>
8 16 <script lang="ts">
9   - import { defineComponent, PropType, onMounted, toRefs } from 'vue';
  17 + import {
  18 + defineComponent,
  19 + PropType,
  20 + onMounted,
  21 + toRefs,
  22 + shallowReactive,
  23 + onUnmounted,
  24 + nextTick,
  25 + } from 'vue';
10 26 import * as echarts from 'echarts';
11 27 import { seriesDataT } from './props';
12 28
... ... @@ -19,74 +35,88 @@
19 35 },
20 36
21 37 setup(props) {
  38 + const chartsInstance = shallowReactive<{ [key: string]: echarts.ECharts }>({});
  39 +
22 40 const { seriesStatusData } = toRefs(props);
  41 +
23 42 const total = seriesStatusData.value
24 43 .map((m) => m.value)
25 44 .reduce((prev, cur) => prev! + cur!, 0);
26 45
27   - onMounted(() => {
28   - seriesStatusData.value.forEach((item) => {
29   - initEchart(item.key, item, item.value);
30   - });
31   - });
32   -
33   - const initEchart = (id, item, value) => {
34   - //牵引线条颜色
35   - const labelLine = {
36   - normal: {
37   - show: true,
38   - length: 20,
39   - length2: 20,
40   - lineStyle: {
41   - color: '#808080',
42   - },
43   - },
44   - } as any;
45   - const legendKey = item.key == 'onLine' ? '在线' : item.key == 'offLine' ? '离线' : '待激活';
46   - const echartInstance = echarts.init(
47   - document.getElementById(`chartPie${id}`) as HTMLElement
48   - );
49   - echartInstance.setOption({
50   - backgroundColor: '#ffffff',
51   - tooltip: {
52   - trigger: 'item',
53   - formatter: `${legendKey}设备${((value / total!) * 100).toFixed(2)}%`,
54   - },
55   - graphic: [
56   - {
57   - type: 'text',
58   - left: '42%',
59   - top: '48%',
60   - z: 10,
61   - style: {
62   - fill: '#1a1a1a',
63   - text: value + '个',
64   - font: '14px Microsoft YaHei',
  46 + nextTick(() => {
  47 + for (const item of seriesStatusData.value) {
  48 + const { key, value } = item;
  49 + //牵引线条颜色
  50 + const labelLine = {
  51 + normal: {
  52 + show: true,
  53 + length: 20,
  54 + length2: 20,
  55 + lineStyle: {
  56 + color: '#808080',
65 57 },
66 58 },
67   - ],
68   - series: [
69   - {
70   - data: [item],
71   - avoidLabelOverlap: false,
72   - type: 'pie',
73   - radius: ['40%', '60%'],
74   - label: {
75   - normal: {
76   - show: true,
77   - position: 'outer',
78   - formatter: `${legendKey}${((value / total!) * 100).toFixed(2)}%`,
79   - borderWidth: 5,
80   - borderRadius: 0,
81   - padding: [90, -60],
  59 + } as any;
  60 + const legendKey = key == 'onLine' ? '在线' : key == 'offLine' ? '离线' : '待激活';
  61 + chartsInstance[key!] = echarts.init(
  62 + document.getElementById(`chartPie${key}`) as HTMLElement
  63 + );
  64 + const option = {
  65 + backgroundColor: '#ffffff',
  66 + tooltip: {
  67 + trigger: 'item',
  68 + formatter: `${legendKey}设备${((value! / total!) * 100).toFixed(2)}%`,
  69 + },
  70 + graphic: [
  71 + {
  72 + type: 'text',
  73 + left: '42%',
  74 + top: '48%',
  75 + z: 10,
  76 + style: {
  77 + fill: '#1a1a1a',
  78 + text: value + '个',
  79 + font: '14px Microsoft YaHei',
82 80 },
83 81 },
84   - labelLine,
85   - },
86   - ],
  82 + ],
  83 + series: [
  84 + {
  85 + data: [item],
  86 + avoidLabelOverlap: false,
  87 + type: 'pie',
  88 + radius: ['40%', '60%'],
  89 + label: {
  90 + normal: {
  91 + show: true,
  92 + position: 'outer',
  93 + formatter: `${legendKey}${((value! / total!) * 100).toFixed(2)}%`,
  94 + borderWidth: 5,
  95 + borderRadius: 0,
  96 + padding: [90, -60],
  97 + },
  98 + },
  99 + labelLine,
  100 + },
  101 + ],
  102 + };
  103 + chartsInstance[key!].setOption(option);
  104 + }
  105 + });
  106 +
  107 + const resize = () => {
  108 + Object.keys(chartsInstance).forEach((key) => {
  109 + chartsInstance[key].resize();
87 110 });
88   - window.addEventListener('resize', () => echartInstance.resize());
89 111 };
  112 +
  113 + onMounted(() => {
  114 + window.addEventListener('resize', resize);
  115 + });
  116 +
  117 + onUnmounted(() => {
  118 + window.removeEventListener('resize', resize);
  119 + });
90 120 },
91 121 });
92 122 </script>
... ...