Vue -- 数据更新echarts表格不更新问题
项目使用的是 vue-element-admin
<template>
<div>
<div ref="line" :class="className" :style="{height:height,width:width}" />
</div>
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../mixins/resize'
export default {
mixins: [resize],
props: {
className: {
type: String,
default: "chart"
},
width: {
type: String,
default: "100%"
},
height: {
type: String,
default: "350px"
},
autoResize: {
type: Boolean,
default: true
},
lineChartData: {
type: Object,
required: true
}
},
data() {
return {
chart: null
};
},
mounted() {
this.$nextTick(() => {
this.initChart()
})
},
watch: {
lineChartData: {
deep: true,
handler(val) {
this.setOptions(val);
}
}
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
},
methods: {
initChart() {
this.chart = echarts.init(this.$refs.line, 'macarons')
// this.chart = echarts.init(this.$el, "macarons");
this.setOptions(this.lineChartData)
},
setOptions({ data, value, name } = {}) {
var max = value && value.length ? null : 100;
this.chart.setOption({
xAxis: {
data: data
// boundaryGap: false,
// axisTick: {
// show: false
// }
},
grid: {
left: 10,
right: '3%',
bottom: 20,
top: 30,
containLabel: true
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
},
padding: [5, 10]
},
yAxis: {
max: max,
minInterval: 1,
axisTick: {
show: false
}
},
legend: {
data: [name]
},
series: [{
name: name,
itemStyle: {
normal: {
color: '#FF005A',
lineStyle: {
color: '#FF005A',
width: 2
}
}
},
smooth: true,
type: 'line',
data: value,
animationDuration: 2800,
animationEasing: 'cubicInOut'
}]
})
}
}
}
</script>
有些数据我的项目是一起给的,切换自己做的处理,如果不能直接赋值就使用this.$set设置data值
formatTab() {
// this.lineChartData.name = "收藏数";
this.$set(this.lineChartData, 'name', "收藏数" )
var name = "totalCollectNum";
switch (this.form.radio2) {
case "1":
// this.lineChartData.name = "收藏数";
this.$set(this.lineChartData, 'name', "收藏数" )
name = "totalCollectNum";
break;
case "2":
// this.lineChartData.name = "点赞数";
this.$set(this.lineChartData, 'name', "点赞数" )
name = "totalLikeNum";
break;
case "3":
// this.lineChartData.name = "阅读数";
this.$set(this.lineChartData, 'name', "阅读数" )
name = "totalReadNum";
break;
default:
// this.lineChartData.name = "收藏数";
this.$set(this.lineChartData, 'name', "收藏数" )
name = "totalCollectNum";
}
var value = [];
var data = [];
var tableData = this.list;
for (var i = 0; i < tableData.length; i++) {
// data.push( this.parseTime(new Date(tableData[i].recordDate), "{y}-{m}-{d}")) // 横坐标
data.push(tableData[i].recordDate); // 横坐标
value.push(tableData[i][name]);
}
// this.lineChartData.value = value;
// this.lineChartData.data = data;
this.$set(this.lineChartData, 'data', data )
this.$set(this.lineChartData, 'value', value )
},