启动项目时,echarts渲染失败,在created,mounted,updated。
项目场景:
用echarts做报表的时候,只能通过按钮的形式启动报表。无论把echarts.init放在created,mounted还是updated中都无法完成自启动。
问题描述
echart报错:TypeError: Cannot read properties of null (reading ‘getAttribute‘)“
数据undefined等等报错。
原因分析:
第一:created,mounted渲染时,data没有数据,init失败。
第二:updated时,组件先被构建,此时myChart.setOption的数据还没有从后台取出,init只有空框架。
解决方案:
把 echarts.init() 和 myChart.setOption() 放在vue的 updataed()生命周期 中,并且在组件中加入 v-if="data.length>0" 放在组件中。
附上完整代码:
-
<template>
-
<div>
-
<div>
-
<div align="center" class="div-button">
-
<a-button-group>
-
<a-button type="primary" icon="el-icon-s-data" @click="histogram">柱形分析图</a-button>
-
<a-button type="success" @click="piechart">饼状分析图<i class="el-icon-pie-chart"></i></a-button>
-
<a-button type="primary" icon="el-icon-s-data" @click="brokenline">折线分析图</a-button>
-
</a-button-group>
-
</div>
-
</div>
-
<div v-if="keyData.length>0" ref="chart" :style="{width: '500px', height: '500px'}"></div>
-
</div>
-
-
</template>
-
-
<script>
-
-
export default {
-
name: 'echarts3',
-
data: function () {
-
return {
-
// tableData: [],
-
keyData: [],
-
valueData: [],
-
timeData: [],//折线图数据
-
pieData: [],//饼图数据
-
}
-
},
-
methods: {
-
queryList: function () {
-
let url = "/qygl/echarts/list";
-
this.axios.get(url, '').then((resp) => {
-
if (resp.data) {
-
this.keyData = [];
-
this.valueData = [];
-
this.timeData = [];
-
this.pieData = [];
-
resp.data.forEach((item) => {
-
this.keyData.push(item.tname);
-
this.valueData.push(item.sumprice);
-
this.pieData.push({
-
name: item.tname,
-
value: item.sumprice
-
});
-
});
-
console.log(this.valueData);
-
} else {
-
this.$message.warning(resp.message);
-
}
-
})
-
},
-
histogram: function () {
-
this.$echarts.dispose(this.$refs.chart);//销毁echarts实例
-
var myChart = this.$echarts.init(this.$refs.chart);//创建echarts实例
-
myChart.setOption({
-
title: {
-
text: '柱状分析图'
-
},
-
tooltip: {
-
trigger: 'axis',
-
formatter: '{a} <br/>{b} : {c} '
-
},
-
xAxis: {
-
type: 'category',
-
data: this.keyData,
-
},
-
yAxis: {
-
type: 'value',
-
},
-
series: [{
-
name: '[tname,sumprice]',
-
data: this.valueData,
-
type: 'bar'
-
}]
-
})
-
},
-
piechart: function () {
-
this.$echarts.dispose(this.$refs.chart);
-
var myChart = this.$echarts.init(this.$refs.chart);
-
myChart.setOption({
-
title: {
-
text: '饼状分析图',
-
subtext: '',
-
left: 'center'
-
},
-
tooltip: {
-
trigger: 'item',
-
formatter: '{a} <br/>{b} : {c} ({d}%)'
-
},
-
legend: {
-
orient: 'vertical',
-
left: 'left',
-
},
-
series: [{
-
name: '[tname,sumprice]',
-
type: 'pie',
-
radius: '50%',
-
data: this.pieData,
-
emphasis: {
-
itemStyle: {
-
shadowBlur: 10,
-
shadowOffsetX: 0,
-
shadowColor: 'rgba(0,0,0,0.5)'
-
}
-
}
-
}]
-
})
-
},
-
brokenline: function () {
-
this.$echarts.dispose(this.$refs.chart);
-
var myChart = this.$echarts.init(this.$refs.chart);
-
myChart.setOption({
-
title: {
-
text: '折线分析图',
-
subtext: '',
-
},
-
tooltip: {
-
trigger: 'axis',
-
formatter: '{a} <br/> {c}'
-
},
-
xAxis: {
-
type: 'time',
-
data: []
-
},
-
yAxis: {
-
type: 'value',
-
},
-
series: [{
-
name: '[录入时间,总价格]',
-
data: this.timeData,
-
// data: [[1533164400000,150],[1533168000000,150],[1533189600000,200],[1533204000000,350],[1533211200000,200]],
-
type: 'line'
-
}]
-
})
-
},
-
},
-
updated() {
-
this.histogram();
-
},
-
created() {
-
{
-
this.queryList();
-
// this.histogram();//error
-
}
-
},
-
mounted: function () {
-
},
-
}
-
</script>
-
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhibaikh
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22