<template>
<div ref="boxEcharts" class="echarts"></div>
</template>
<script>
import request from "@/utils/request";
export default {
data() {
return {
leftEch: "",
list: [],
query: {
currentPage: 1,
pageSize: 20,
},
};
},
created() {
this.initData(); // echarts需要的数据
},
mounted() {
this.initEcharts();
this.screenAdapter();
window.addEventListener("resize", this.screenAdapter);
},
// 销毁定时器
destroyed() {
window.removeEventListener("resize", this.screenAdapter);
},
methods: {
// echarts需要的数据
initData() {
request({
url: `/api/SubDev/YkzptEqaccount`,
method: "GET",
data: this.query,
}).then((res) => {
this.list = res.data;
this.updateChart(); // 更新echarts数据
});
},
// 设备使用状态(初始化图表【饼图事列】)
initEcharts() {
this.leftEch = echarts.init(this.$refs.boxEcharts);
let initoption = {
title: {
text: "设备使用状态统计",
left: "center",
top: 10,
},
tooltip: {
trigger: "item",
},
grid: {
left: "5%",
right: "5%",
bottom: "5%",
top: "15%",
containLabel: true,
},
legend: {
orient: "vertical",
left: "left",
left: 20,
top: 10,
},
series: [
{
type: "pie",
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: "rgba(0, 0, 0, 0.5)",
},
},
},
],
};
this.leftEch.setOption(initoption);
},
// 设备使用状态(数据更新图表)
updateChart() {
// 当拿到数据后,准备数据的配置项
const dataOption = {
// 如:
series: [
{
type: "pie",
data: this.list,
},
],
};
this.leftEch.setOption(dataOption);
},
// 设备使用状态(当浏览器窗口大小发生变化,完成屏幕适配)
screenAdapter() {
let titleFontSize = (this.$refs.boxEcharts.offsetWidth / 100) * 3.6;
// 浏览器分辨率大小相关的配置项
const adapterOption = {
// 如:
title: {
textStyle: {
fontSize: titleFontSize / 2,
},
},
legend: {
textStyle: {
fontSize: titleFontSize / 3,
},
itemWidth: titleFontSize / 2,
itemHeight: titleFontSize / 3,
},
series: [
{
radius: titleFontSize * 3,
},
],
};
this.leftEch.setOption(adapterOption);
// 手动调用图表的 resize 才能产生效果
this.leftEch.resize();
},
},
};
</script>