问题描述:
做Echarts可视化图表,前后端分离项目,前端异步获取后端接口数据时 echarts图表不显示问题
left_center_char() {
var datesli = [];
var confirmedsli = [];
var confirmedli = [];
this.$axios.get("/hive2/getPre").then((res) => {
var recoveredli=[]
var recoveredsli=[]
var getPreli=[datesli,confirmedli,recoveredli,confirmedsli,recoveredsli]
var getpre=res.data.data.pre
for(let i=0;i<getpre.length;i++){
datesli.push(getpre[i]['dates'])
confirmedli.push(getpre[i]['confirmed'])
recoveredli.push(getpre[i]['recovered'])
confirmedsli.push(getpre[i]['confirmeds'])
recoveredsli.push(getpre[i]['recovereds'])
}
}).catch((error) => {
console.warn(error)
})
console.log("----------------------------------")
console.log(datesli)
var echarts = require('echarts');
var myChart = echarts.init(document.getElementById('left_center_in'));
var fontColor = '#30eee9';
var option = {
grid: {
left: '2%',
top: '4%',
bottom: '3%',
containLabel: true
},
tooltip: {
show: true,
trigger: 'item'
},
legend: {
show: true,
x: 'center',
y: '10',
icon: 'stack',
itemWidth: 8,
itemHeight: 8,
textStyle: {
color: '#1bb4f6'
},
data: ['确诊数', '预测值']
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisLabel: {
color: fontColor
},
axisLine: {
show: true,
lineStyle: {
color: '#397cbc'
}
},
axisTick: {
show: false,
},
splitLine: {
show: false,
lineStyle: {
color: '#195384'
}
},
data: datesli
}],
yAxis: [{
type: 'value',
name: '',
min: 84000,
max: 89000,
axisLabel: {
formatter: '{value}',
textStyle: {
color: '#2ad1d2'
}
},
axisLine: {
lineStyle: {
color: '#27b4c2'
}
},
axisTick: {
show: false,
},
splitLine: {
show: true,
lineStyle: {
color: '#11366e'
}
}
},
],
series: [{
name: '确诊数',
type: 'line',
showAllSymbol: true,
smooth: true,
stack: '总量',
symbol: 'circle',
symbolSize: 2,
itemStyle: {
normal: {
color: '#0092f6',
lineStyle: {
color: "#0092f6",
width: 1
},
}
},
markPoint: {
itemStyle: {
normal: {
color: 'red'
}
}
},
data: confirmedsli
},
{
name: '预测值',
type: 'line',
stack: '总量1',
symbol: 'circle',
symbolSize: 2,
showAllSymbol: true,
smooth: true,
itemStyle: {
normal: {
color: '#00d4c7',
lineStyle: {
color: "#00d4c7",
width: 1
},
}
},
data: confirmedli
},
]
};
myChart.setOption(option);
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
console.log(option.xAxis);
},
调试代码的时候数据已经请求成功,而且打印option对象里面的属性,奇葩的是数据也有的
原因分析:
想要解决这个问题就要彻底了解什么是异步请求和什么是同步请求
同步请求,客户端向服务端发送请求,服务端响应以后客户端才渲染页面 异步请求,客户端向服务端发送请求,客户端不等服务端响应就行行页面渲染,一般做页面的局部刷新。
那么造成上面原因就是我的页面向服务端发送请求,页面不等服务端响应就已经渲染图表,尽管后面数据请求成功但此时页面已经渲染成功,页面此时只知道他所渲染图表时数据为空,所以不会显示数据
解决方案:
解决方法有两种:一,将渲染图表的代码移交到请求的代码块中 如下
left_center_char() {
var datesli = [];
var confirmedsli = [];
var confirmedli = [];
this.$axios.get("/hive2/getPre").then((res) => {
var recoveredli=[]
var recoveredsli=[]
var getPreli=[datesli,confirmedli,recoveredli,confirmedsli,recoveredsli]
var getpre=res.data.data.pre
for(let i=0;i<getpre.length;i++){
datesli.push(getpre[i]['dates'])
confirmedli.push(getpre[i]['confirmed'])
recoveredli.push(getpre[i]['recovered'])
confirmedsli.push(getpre[i]['confirmeds'])
recoveredsli.push(getpre[i]['recovereds'])
}
var echarts = require('echarts');
var myChart = echarts.init(document.getElementById('left_center_in'));
var fontColor = '#30eee9';
var option = {
grid: {
left: '2%',
top: '4%',
bottom: '3%',
containLabel: true
},
tooltip: {
show: true,
trigger: 'item'
},
legend: {
show: true,
x: 'center',
y: '10',
icon: 'stack',
itemWidth: 8,
itemHeight: 8,
textStyle: {
color: '#1bb4f6'
},
data: ['确诊数', '预测值']
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisLabel: {
color: fontColor
},
axisLine: {
show: true,
lineStyle: {
color: '#397cbc'
}
},
axisTick: {
show: false,
},
splitLine: {
show: false,
lineStyle: {
color: '#195384'
}
},
data: datesli
}],
yAxis: [{
type: 'value',
name: '',
min: 84000,
max: 89000,
axisLabel: {
formatter: '{value}',
textStyle: {
color: '#2ad1d2'
}
},
axisLine: {
lineStyle: {
color: '#27b4c2'
}
},
axisTick: {
show: false,
},
splitLine: {
show: true,
lineStyle: {
color: '#11366e'
}
}
},
],
series: [{
name: '确诊数',
type: 'line',
showAllSymbol: true,
smooth: true,
stack: '总量',
symbol: 'circle',
symbolSize: 2,
itemStyle: {
normal: {
color: '#0092f6',
lineStyle: {
color: "#0092f6",
width: 1
},
}
},
markPoint: {
itemStyle: {
normal: {
color: 'red'
}
}
},
data: confirmedsli
},
{
name: '预测值',
type: 'line',
stack: '总量1',
symbol: 'circle',
symbolSize: 2,
showAllSymbol: true,
smooth: true,
itemStyle: {
normal: {
color: '#00d4c7',
lineStyle: {
color: "#00d4c7",
width: 1
},
}
},
data: confirmedli
},
]
};
myChart.setOption(option);
}).catch((error) => {
console.warn(error)
})
},
二,在渲染图标时重新加载数据
left_center_char() {
var echarts = require('echarts');
var myChart = echarts.init(document.getElementById('left_center_in'));
var fontColor = '#30eee9';
var option = {
grid: {
left: '2%',
top: '4%',
bottom: '3%',
containLabel: true
},
tooltip: {
show: true,
trigger: 'item'
},
legend: {
show: true,
x: 'center',
y: '10',
icon: 'stack',
itemWidth: 8,
itemHeight: 8,
textStyle: {
color: '#1bb4f6'
},
data: ['确诊数', '预测值']
},
xAxis: [{
type: 'category',
boundaryGap: false,
axisLabel: {
color: fontColor
},
axisLine: {
show: true,
lineStyle: {
color: '#397cbc'
}
},
axisTick: {
show: false,
},
splitLine: {
show: false,
lineStyle: {
color: '#195384'
}
},
data: []
}],
yAxis: [{
type: 'value',
name: '',
min: 84000,
max: 89000,
axisLabel: {
formatter: '{value}',
textStyle: {
color: '#2ad1d2'
}
},
axisLine: {
lineStyle: {
color: '#27b4c2'
}
},
axisTick: {
show: false,
},
splitLine: {
show: true,
lineStyle: {
color: '#11366e'
}
}
},
],
series: [{
name: '确诊数',
type: 'line',
showAllSymbol: true,
smooth: true,
stack: '总量',
symbol: 'circle',
symbolSize: 2,
itemStyle: {
normal: {
color: '#0092f6',
lineStyle: {
color: "#0092f6",
width: 1
},
}
},
markPoint: {
itemStyle: {
normal: {
color: 'red'
}
}
},
data: []
},
{
name: '预测值',
type: 'line',
stack: '总量1',
symbol: 'circle',
symbolSize: 2,
showAllSymbol: true,
smooth: true,
itemStyle: {
normal: {
color: '#00d4c7',
lineStyle: {
color: "#00d4c7",
width: 1
},
}
},
data: []
},
]
};
myChart.setOption(option);
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
console.log(option.xAxis);
this.$axios.get("/hive2/getPre").then((res) => {
console.log(res)
var datesli=[]
var confirmedli=[]
var recoveredli=[]
var confirmedsli=[]
var recoveredsli=[]
var getPreli=[datesli,confirmedli,recoveredli,confirmedsli,recoveredsli]
var getpre=res.data.data.pre
for(let i=0;i<getpre.length;i++){
datesli.push(getpre[i]['dates'])
confirmedli.push(getpre[i]['confirmed'])
recoveredli.push(getpre[i]['recovered'])
confirmedsli.push(getpre[i]['confirmeds'])
recoveredsli.push(getpre[i]['recovereds'])
}
myChart.setOption({
xAxis: {
data: datesli
},
series: [{
data:confirmedsli
},{
data:confirmedli
}]
})
}).catch((error) => {
console.warn(error)
})
},
成功
如果有不对的地方希望大家给出宝贵建议
|