目的:可以重复利用组件
封装组件
<template>
<div :id="_id" class="echargraph"></div>
</template>
import Vue from "vue";
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
const Echarts = require("echarts");
name: "chart",
props: {
_id: String,
_titleText: String,
_colorArr: Array,
xAxisData: Array,
yAxisMax: Number,
yAxisInterval: Number,
seriesData: Array,
_yText: String,
pieRadius: [String, Array, Number]
},
watch: {
seriesData(val) {
switch (this._yText) {
case "radar":
this.radar(this._id, this._titleText, this.seriesData);
break;
}
}
},
mounted() {
switch (this._yText) {
case "radar":
this.radar(this._id, this._titleText, this.seriesData);
break;
}
},
radar(_id, _titleText, seriesData) {
let myChart = Echarts.init(document.getElementById(_id));
let legendData = [];
seriesData.map(item => {
legendData.push(item.name);
});
let option = {
title: {
text: _titleText
},
color: "rgba(183,225,232,0.8)",
tooltip: {
backgroundColor: "rgba(255,255,255,0.5)",
textStyle: {
color: "black"
}
},
radar: {
name: {
textStyle: {
color: "#333"
}
},
indicator: [
{ name: "知识度", max: 6500 },
{ name: "任务力", max: 16000 },
{ name: "学习力", max: 30000 },
{ name: "服务力", max: 38000 },
{ name: "情感力", max: 52000 },
{ name: "销售力", max: 25000 }
]
},
series: [
{
type: "radar",
areaStyle: {
color: "rgba(183,225,232,0.8)"
},
data: seriesData
}
]
};
myChart.setOption(option);
},
调用组件
components: {
chart
},
<chart
:_id="'newAndOld'"
:_titleText="'分布比例图'"
:_colorArr="visitorColor"
:pieRadius="visitorRadius"
:seriesData="visitorData"
:_yText="'Pie'"
class="shadows echargraph"
></chart>