- Charts组件封装
<template>
<div :id="echartsId" :style="{ width: width, height: height }"></div>
</template>
<script>
import echarts from 'echarts';
require('echarts/theme/macarons');
export default {
props: {
echartsData: {
type: Object,
default: {},
},
echartsId: {
type: String,
default: '',
},
height: {
type: String,
default: '360px',
},
width: {
type: String,
default: '50%',
},
title: {
type: String,
default: '',
},
chartsType: {
chartsType: String,
default: 'line',
}
},
watch: {
echartsData: {
handler(newValue, oldValue) {
this.drawLine({
xAxisData: this.echartsData.xAxisData,
seriesData: this.echartsData.seriesData,
areaFlag: this.echartsData.areaFlag,
gridData: this.echartsData.gridData,
});
},
},
},
data() {
return {
insuranceChart: null,
insuranceOption: {},
};
},
mounted() {
this.insuranceChart = echarts.init(document.getElementById(this.echartsId));
window.addEventListener('resize', () => {
this.insuranceChart.resize();
});
this.drawLine({
xAxisData: this.echartsData.xAxisData,
seriesData: this.echartsData.seriesData,
areaFlag: this.echartsData.areaFlag,
gridData: this.echartsData.gridData,
});
},
methods: {
drawLine(option) {
this.$nextTick(function () {
let that = this;
this.insuranceChart = echarts.init(
document.getElementById(this.echartsId)
);
this.insuranceOption = {
title: {
text: this.title,
top: '0',
left: 'center',
textStyle: {
fontsize: '12px'
}
},
tooltip: {
trigger: 'axis',
},
backgroundColor: '#fff',
legend: {
bottom: 20,
itemWidth: 12,
itemHeight: 4,
icon: 'roundRect',
width: '100%',
},
grid: {
left: '7%',
right: '3%',
bottom: '25%',
top: '18%',
containLabel: false,
},
xAxis: {
type: 'category',
boundaryGap: true,
axisLine: {
show: false,
},
axisLabel: {
interval: option.xAxisData.length > 30 ? 5 : 0,
},
axisTick: {
lineStyle: {
color: 'rgba(224, 230, 241, 1)',
},
},
data: (function () {
return option.xAxisData.map(function (item, index) {
return {
value: item,
textStyle: {
fontSize: (17 * that.width) / 720,
},
};
});
})(),
},
yAxis: {
type: 'value',
axisLine: {
show: false,
},
axisTick: {
show: false,
},
splitLine: {
lineStyle: {
type: 'dotted',
color: 'rgba(224, 230, 241, 1)',
},
},
},
color: [
'#FFFF00',
'#FFCCFF',
'#FF6666',
'#66FF66',
'#3399FF',
'#330099',
'#990066',
'#FF6600',
'#990000',
'#CCFF99',
'#663366',
'#CC9900',
],
series: (function () {
let series = [];
option.seriesData.map((item, index) => {
let data = {
name: item.name,
type: that.chartsType,
data: item.data,
symbolSize: 6,
symbol: 'circle',
smooth: false,
};
series.push(data);
});
return series;
})(),
};
if (option.xAxisData.length > 10) {
this.insuranceOption.dataZoom = [
{
type: 'slider',
show: false,
xAxisIndex: [0],
start: 0,
end: 50,
zoomLock: true,
},
{
type: 'inside',
xAxisIndex: [0],
start: 0,
end: 50,
zoomLock: true,
},
{
showDetail: false,
left: '0',
right: '0',
bottom: '0',
height: 10,
fillerColor: '#ccc',
backgroundColor: '#fff',
dataBackground: {
lineStyle: {
opacity: 0,
},
areaStyle: {
opacity: 0,
},
},
},
];
} else {
this.insuranceOption.dataZoom = [];
}
this.insuranceChart.setOption(this.insuranceOption);
});
},
},
};
</script>
- 使用
<template>
<Charts :echartsData="echartsData" :echartsId="'测试图表'" :title="title" chartsType="line"> </Charts>
</template>
<script>
import Charts from "@/components/Charts";
export default {
components: {
Charts,
},
data() {
return {
echartsData: {},
title: '测试图表名称'
};
},
created() {
this.getLineChart();
},
methods: {
getLineChart() {
this.echartsData = {
xAxisData: ['1月', '2月', '3月', '4月', '5月', '6月', '7月'],
seriesData: [
{
name: '测试1',
data: [220, 192, 563, 234, 360, 730, 610],
},
{
name: '测试2',
data: [220, 182, 191, 234, 290, 330, 310],
},
{
name: '测试3',
data: [220, 232, 201, 154, 190, 330, 410],
},
{
name: '测试4',
data: [211, 432, 121, 231, 190, 330, 410],
},
{
name: '测试5',
data: [121, 90, 451, 211, 121, 221, 101],
},
{
name: '测试6',
data: [520, 377, 100, 420, 342, 520, 123],
},
],
};
},
},
};
</script>