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
| <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 props = defineProps({
| unit: {
| type: String,
| },
| rotate: {
| type: Number,
| default: 0,
| }
| });
|
| const chartContainer = ref(null);
| let myChart = null;
| let color = ["#0E7CE2", "#E271DE", "#00FFFF", "#F8456B", "#4AEAB0", "#FF8352"];
|
| 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% 处的颜色
| },
| ],
| };
| }
|
| function getOptions(xLabels = [], data = []) {
| let barColor = getLinearColor("#7bda59", "#059789");
| let maxColor = getLinearColor("#e3352b", "#f09f2e");
| let minColor = getLinearColor("#f09f2e", "#d3c209");
| let legends = [];
| return {
| grid: {
| left: 60,
| right: "1%",
| },
| // legend: {
| // x: "22%",
| // top: "8%",
| // textStyle: {
| // color: "#ffffff",
| // },
| // data: xLabels,
| // },
| tooltip: {
| trigger: "axis",
| axisPointer: {
| type: "shadow",
| },
| },
| xAxis: {
| type: "category",
| data: xLabels,
| axisLabel: {
| // 旋转标签的角度
| rotate: props.rotate,
| interval: props.rotate ? 0 : undefined,
| }
| },
| yAxis: {
| type: "value",
| name: props.unit ? `单位: ${props.unit}` : "",
| min: function (data) {
| let min = data.min;
| if (min == Infinity) {
| return 0;
| }
| return Number((min - min * 0.2).toFixed(2));
| },
| max: function (data) {
| let max = data.max;
| if (max == -Infinity) {
| max = 1;
| }
| return Number((max + max * 0.2).toFixed(2));
| },
| },
| series: [
| {
| color,
| data,
| type: "bar",
| // barWidth: 20,
| itemStyle: {
| color: function (params) {
| return color[params.dataIndex % color.length];
| },
| },
| label: {
| show: true,
| position: "top",
| color: "#fff",
| fontSize: 20,
| },
| showBackground: true,
| backgroundStyle: {
| color: "rgba(0, 0, 0, 0.1)",
| },
| },
| ],
| };
| }
|
| 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>
|
|