我是靠谱客的博主 爱笑楼房,这篇文章主要介绍关于vue 结合原生js 解决echarts resize问题,现在分享给大家,希望可以做个参考。

解决页面echarts 图表 resize问题

左侧的菜单栏模块是可以收缩的,点击左上角的 icon 可以收缩,但是点击左上角的icon后,右侧的echarts图并未进行对应的收缩,使用 element-resize-detector 插件能够解决问题(如何解决Vue项目中使用echarts,宽度变化导致图不能resize问题)但是会有点卡顿,所以采用原生的方法来处理,再结合windows 自带的原生 resize 事件处理 浏览器宽度高度变化,可以不使用插件来处理问题了。

复制代码
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<template> <div id="leftEchartPie" ref="leftEchartPie" ></div> </template> <script> export default { props: { leftPieData: { type: Object, default: {} } }, data() { return { myChart: null, option: null, observe: null, optionData: [] }; }, watch: { leftPieData: { deep: true, handler(leftPieData) { if (leftPieData) { this.initChart(leftPieData); } } } }, mounted() { const option = { title: { zlevel: 0, text: null, rich: { value: { color: "#3F495A", fontSize: 14, lineHeight: 16 }, name: { color: "#3F495A", fontSize: 14, lineHeight: 16 } }, top: "center", left: "19.3%", textAlign: "center", textStyle: { rich: { value: { color: "#3F495A", fontSize: 14, lineHeight: 16 }, name: { fontSize: 16, color: "#3F495A", lineHeight: 30 } } } }, tooltip: { show: true, trigger: "item", formatter: "{a} <br/>{b}: {c} ({d}%)" }, legend: {}, series: [ { name: "Security Status", type: "pie", center: ["20%", "50%"], radius: ["80%", "45%"], itemStyle: { borderWidth: 6, borderColor: "#fff" }, hoverAnimation: false, avoidLabelOverlap: false, label: { show: false, position: "center" }, emphasis: { label: { show: false, fontSize: "30", fontWeight: "bold" } }, labelLine: { show: false }, data: [] } ] }; this.option = option; var dom = document.getElementById("leftEchartPie"); var myChart = this.echarts.init(dom); this.myChart = myChart; // 使用浏览器原生的MutationObserver来处理 const observer = new MutationObserver(() => { setTimeout(this.myChart.resize, 201); }); const config = { attributes: true, childList: true, subtree: false }; this.observe = observer; observer.observe(document.getElementsByClassName("spoc-menu")[0], config); // 处理浏览器窗口大小变化触发resize window.addEventListener("resize", this.resizeEchart, true); }, beforeDestroy() { this.observe.dis; window.removeEventListener("resize", this.resizeEchart, true); }, methods: { resizeEchart() { this.myChart.resize(); }, initChart(leftPieData) { this.option.text = [ "{name|Total}", "{value|" + leftPieData.totalNum + "}" ].join("n"); this.option.legend.data = [ `Very High Risk`, `High Risk`, `Medium Risk`, `Low Risk`, `Very Low Risk` ]; this.option.series[0].data = [ { value: this.leftPieData.veryHighRiskNum, name: `Very High Risk`, itemStyle: { color: "#FF4D4F" } }, { value: this.leftPieData.highRiskNum, name: `High Risk`, itemStyle: { color: "#FA9314" } }, { value: this.leftPieData.mediumRiskNum, name: `Medium Risk`, itemStyle: { color: "#FACB14" } }, { value: this.leftPieData.lowRiskNum, name: `Low Risk`, itemStyle: { color: "#4ED6CC" } }, { value: this.leftPieData.veryLowNum, name: `Very Low Risk`, itemStyle: { color: "#2BD374" } } ]; this.optionData = this.option.series[0].data; const optionData = this.optionData; this.option.legend = { textStyle: { fontSize: 14, color: "#3F495A" }, orient: "vertical", left: "60%", itemGap: 30, // 字高 top: "middle", icon: "circle", itemHeight: 10, //改变圆圈大小 // data: [], formatter(params) { if (optionData.length) { switch (params) { case "Very High Risk": return params + " " + optionData[0].value; break; case "High Risk": return params + " " + optionData[1].value; break; case "Medium Risk": return params + " " + optionData[2].value; break; case "Low Risk": return params + " " + optionData[3].value; break; case "Very Low Risk": return params + " " + optionData[4].value; break; default: return ""; } } } }; // 绘制图表 this.myChart.setOption(this.option); } } }; </script> <style> #leftEchartPie { width: 100%; height: calc(100% - 60px); } </style>

核心代码

复制代码
1
2
3
4
5
6
7
8
9
// 使用浏览器原生的MutationObserver来处理 const observer = new MutationObserver(() => { setTimeout(this.myChart.resize, 201); }); const config = { attributes: true, childList: true, subtree: false }; this.observe = observer; observer.observe(document.getElementsByClassName("spoc-menu")[0], config); // 处理浏览器窗口大小变化触发resize window.addEventListener("resize", this.resizeEchart, true);

关于MutationObserver的使用 参考

最后注意需要beforeDestroy 里面销毁掉 监听的时间和 MutationObserver 监听的事件,避免事件一直在页面里面,消耗内存。

最后开发完后,对比了下和之前使用的插件,原生js处理的体验比插件反应快,插件给人的感觉盾一点

ok 这就是关于 vue 结合原生js 解决echarts resize问题 的开发心得啦 ~希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是爱笑楼房最近收集整理的关于关于vue 结合原生js 解决echarts resize问题的全部内容,更多相关关于vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(186)

评论列表共有 0 条评论

立即
投稿
返回
顶部