<template> <div class="data_body"> <div style="width: 100%; height: 100%"> <div id="charts_canvas" class="echart" style="width: 100%; height: 100%"></div> </div> </div> </template> <script> export default { props: { loginArray: { type: Array }, }, data() { return { chartObj: null, ooption: { xdata: [], type: [], }, option: { tooltip: { trigger: 'item' }, series: [ { type: 'pie', radius: ['80%', '100%'], avoidLabelOverlap: false, itemStyle: { borderRadius: 10, borderColor: '#fff', borderWidth: 2 }, label: { show: false, position: 'center', formatter: '{b}\n{c}' }, emphasis: { label: { show: true, fontSize: 25, formatter: '{c}' } }, labelLine: { show: false }, hoverOffset: 0, // 设置鼠标悬停时不放大 data: [ { value: 10, name: '进行中', itemStyle: { normal: { color: '#3673e8' } } }, { value: 8, name: '未发布', itemStyle: { normal: { color: '#61b8ff' } } }, { value: 6, name: '已完成', itemStyle: { normal: { color: '#96d8a8' } } }, { value: 7, name: '逾期', itemStyle: { normal: { color: '#f5b763' } } }, ] } ] }, }; }, methods: { setChart(array) { // 雷达图显示的标签 let newPromise = new Promise((resolve) => { resolve(); }); //然后异步执行echarts的初始化函数 newPromise.then(() => { const chartObj = this.$echarts.init( this.$el.querySelector("#charts_canvas") ); // this.option.series[0].data = array; // 初始化雷达图 this.chartObj = chartObj; this.chartObj.setOption(this.option); }); }, setJson(array) { this.ooption = { xdata: [], sdata: [], max: 0 } if (!this.chartObj) { this.setChart(array); } else { this.option.series[0].data = array; this.chartObj.setOption(this.option); } } }, watch: { loginArray: { immediate: true, deep: true, handler(newValue, oldValue) { this.setJson(newValue) this.$forceUpdate(); }, }, }, mounted() { this.setJson(this.loginArray) var _this = this; window.addEventListener("resize", () => { if (_this.chartObj) { _this.chartObj.resize(); } }); }, }; </script> <style scoped> .data_body { height: 100%; /* display: flex; */ position: relative; border-radius: 5px; /* border: 1px solid #eee; */ margin: 0 auto; box-sizing: border-box; padding: 0; width: 100%; /* background: #fff; */ } </style>