鸿蒙智能电子锁前端项目
he wei
2025-03-15 f8fd43f1307b0ae3a55f5a5a1390fd13b506fa88
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<template>
  <div class="chart-wraper">
    <div class="chart" ref="chartContainer"></div>
  </div>
</template>
 
<script setup>
import * as echarts from "echarts";
import "./theme/custom";
 
import { onMounted, ref, watchEffect, nextTick, onBeforeUnmount } from "vue";
 
const chartContainer = ref(null);
let myChart = null;
let textColor = "#fff";
let color = ["#0E7CE2", "#FF8352", "#E271DE", "#F8456B", "#00FFFF", "#4AEAB0"];
function getLinearColor(color0, color1) {
  return {
    type: "linear",
    x: 0,
    y: 0,
    x2: 0,
    y2: 1,
    colorStops: [
      {
        offset: 0,
        color: color0, // 0% 处的颜色
      },
      {
        offset: 1,
        color: color1, // 100% 处的颜色
      },
    ],
  };
}
let formatNumber = function (num) {
  let reg = /(?=(\B)(\d{3})+$)/g;
  return num.toString().replace(reg, ",");
};
 
let getPercent = function (value, total) {
  if (total == 0) {
    return "0%";
  }
  return Math.round((value / total) * 1000) / 10 + "%";
};
function getOptions(echartData = []) {
  let title = "总量";
 
  // let echartData = [
  //   {
  //     name: "A类",
  //     value: "3720",
  //   },
  //   {
  //     name: "B类",
  //     value: "2920",
  //   },
  // ];
 
  let total = echartData.reduce((a, b) => {
    return a + b.value * 1;
  }, 0);
 
  return {
    color: color,
    title: [
      {
        text: "{name|" + title + "}\n{val|" + formatNumber(total) + "}",
        top: "center",
        left: "center",
        textStyle: {
          rich: {
            name: {
              fontSize: 14,
              fontWeight: "normal",
              color: textColor,
              padding: [10, 0],
            },
            val: {
              fontSize: 32,
              fontWeight: "bold",
              color: textColor,
            },
          },
        },
      },
    ],
    series: [
      {
        type: "pie",
        radius: ["45%", "60%"],
        center: ["50%", "50%"],
        data: echartData,
        emphasis: {
          scale: false,
        },
        itemStyle: {
          borderColor: textColor,
          borderWidth: 2,
        },
        labelLine: {
          length: 20,
          length2: 20,
          lineStyle: {
            color: textColor,
          },
        },
        label: {
          formatter: (params) => {
            return (
              "{name|" +
              params.name +
              "}\n{value|" +
              formatNumber(params.value) +
              "}\n{value|" +
              getPercent(params.value, total) +
              "}"
            );
          },
          rich: {
            name: {
              fontSize: 14,
              padding: [0, 10, 0, 4],
              color: textColor,
            },
            value: {
              fontSize: 18,
              fontWeight: "bold",
              color: textColor,
            },
          },
        },
      },
    ],
  };
}
 
function initChart() {
  if (chartContainer.value) {
    myChart = echarts.init(chartContainer.value, "custom");
 
    let option = getOptions();
    myChart.setOption(option);
 
    // 监听窗口变化重新渲染图表
    window.addEventListener("resize", () => {
      myChart.resize();
    });
  }
}
 
function updateChart(xLabels, datas) {
  let option = getOptions(xLabels, datas);
  myChart.setOption(option);
}
 
onMounted(() => {
  initChart();
});
 
watchEffect(() => {
  // 如果需要基于某些响应式数据更新图表,可以在这里处理
});
 
onBeforeUnmount(() => {
  if (myChart) {
    myChart.dispose();
  }
});
 
defineExpose({
  updateChart,
});
</script>
 
<style scoped lang="scss">
.chart-wraper {
  height: 100%;
  position: relative;
  .chart {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }
}
</style>