项目场景:
因为想做一个大五人格心理分析的项目,因此结合了ECharts+springboot做了一个后台管理系统。
问题描述:
前端ECharts部分不显示后端传送的数据,并且不提示任何错误信息。后端要进行交互的数据因为没有数据所以缩成了一个点。
问题排查:
1、后端排查
后端可以正常发送数据,并且数据正确。
2、前端排查
(1)相关依赖
没问题
<script src="../js/vue.js"></script>
<script src="../js/axios.js"></script>
<script src="/js/element-ui/lib/index.js"></script>
<script src="../js/echarts.js"></script>
(2)图表作用域
没问题
<div id="app" style="width: 600px;height:400px;">
(3)Vue中的数据
没问题
option: {
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {
left: 'center',
data: [
'personality standard',
'personality interviewee',
'animal interviewee'
]
},
radar: [
{
indicator: [
{ text: 'tiger', max: 30 },
{ text: 'peacock', max: 30 },
{ text: 'koala', max: 30 },
{ text: 'owl', max: 30 },
{ text: 'chameleon', max: 30 }
],
center: ['25%', '40%'],
radius: 80
},
{
indicator: [
{ text: 'extraversion', max: 20 },
{ text: 'agreeableness', max: 20 },
{ text: 'seriousness', max: 20 },
{ text: 'neuroticism', max: 20 },
{ text: 'experienceopenness', max: 20 }
],
center: ['75%', '40%'],
radius: 80
},
],
series: [
{
type: 'radar',
tooltip: {
trigger: 'item'
},
data: [
{
value: [0, 0, 0, 0,0],
name: 'animal interviewee'
}
]
},
{
type: 'radar',
radarIndex: 1,
tooltip: {
trigger: 'item'
},
data: [
{
value: [13, 16, 13.5, 10,15],
name: 'personality standard'
},
{
value: [0, 0, 0, 0,0],
name: 'personality interviewee'
}
]
}
]
},
animal: {
tiger: 0,
peacock: 0,
koala: 0,
owl: 0,
chameleon: 0,
personalityHandled: {
extraversion: 0,
agreeableness: 0,
seriousness: 0,
neuroticism: 0,
experienceopenness: 0,
}
},
(3)数据转化
既然前端可以拿到数据,那么一定是数据转化出现了问题。经过排查,果不其然是数据转化的问题。我之前写的代码结构如下。因为Vue和下面的myChart 是在script块是平行的,因此两者是异步进行处理的,所以就发生了,Vue还没有处理好数据,但是myChart已经完成了初始化的这个情况,因为myChart已经完成了初始化,所以Vue的数据就无法注入了。因此只要把myChart初始化的操作放入Vue中就可以了。
let app=new Vue({
data:{
option:{
}
}
//数据接收并转化的代码
})
var myChart = echarts.init(document.getElementById('app'));
myChart.setOption(app.option);
(4)全部错误代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../js/vue.js"></script>
<script src="../js/axios.js"></script>
<script src="/js/element-ui/lib/index.js"></script>
<script src="../js/echarts.js"></script>
</head>
<body>
<div id="app" style="width: 600px;height:400px;">
</div>
<script type="text/javascript">
let app=new Vue({
el:"#app",
data: {
option: {
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {
left: 'center',
data: [
'personality standard',
'personality interviewee',
'animal interviewee'
]
},
radar: [
{
indicator: [
{ text: 'tiger', max: 30 },
{ text: 'peacock', max: 30 },
{ text: 'koala', max: 30 },
{ text: 'owl', max: 30 },
{ text: 'chameleon', max: 30 }
],
center: ['25%', '40%'],
radius: 80
},
{
indicator: [
{ text: 'extraversion', max: 20 },
{ text: 'agreeableness', max: 20 },
{ text: 'seriousness', max: 20 },
{ text: 'neuroticism', max: 20 },
{ text: 'experienceopenness', max: 20 }
],
center: ['75%', '40%'],
radius: 80
},
],
series: [
{
type: 'radar',
tooltip: {
trigger: 'item'
},
data: [
{
value: [0, 0, 0, 0,0],
name: 'animal interviewee'
}
]
},
{
type: 'radar',
radarIndex: 1,
tooltip: {
trigger: 'item'
},
data: [
{
value: [13, 16, 13.5, 10,15],
name: 'personality standard'
},
{
value: [0, 0, 0, 0,0],
name: 'personality interviewee'
}
]
}
]
},
animal: {
tiger: 0,
peacock: 0,
koala: 0,
owl: 0,
chameleon: 0,
personalityHandled: {
extraversion: 0,
agreeableness: 0,
seriousness: 0,
neuroticism: 0,
experienceopenness: 0,
}
},
},
methods:{
findAnimalById(id){
axios.get("http://localhost:8080/container/id/"+id)
.then(resp =>{
if(resp.data.code===20041){
this.animal= resp.data.data;
this.$message({
showClose: true,
message: '查询成功',
type: 'success'
});
// 设置数据
this.option.series[0].data[0].value[0]=this.$data.animal.tiger;
this.option.series[0].data[0].value[1]=this.$data.animal.peacock;
this.option.series[0].data[0].value[2]=this.$data.animal.koala;
this.option.series[0].data[0].value[3]=this.$data.animal.owl;
this.option.series[0].data[0].value[4]=this.$data.animal.chameleon;
this.option.series[1].data[1].value[0]=this.$data.animal.personalityHandled.extraversion;
this.option.series[1].data[1].value[1]=this.$data.animal.personalityHandled.agreeableness;
this.option.series[1].data[1].value[2]=this.$data.animal.personalityHandled.seriousness;
this.option.series[1].data[1].value[3]=this.$data.animal.personalityHandled.neuroticism;
this.option.series[1].data[1].value[4]=this.$data.animal.personalityHandled.experienceopenness;
}else {
console.log(resp.data);
this.$message({
showClose: true,
message: '查询失败',
type: 'error'
});
}
})
},
},
created(){
// 拿到id
let id = localStorage.getItem("id");
// 查询
this.findAnimalById(id);
}
});
var myChart = echarts.init(document.getElementById('app'));
myChart.setOption(app.option);
</script>
</body>
</html>
解决方案:
1、伪代码
let app=new Vue({
data:{
option:{
}
}
var myChart = echarts.init(document.getElementById('app'));
myChart.setOption(app.option);
//数据接收并转化的代码
})
2、全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../js/vue.js"></script>
<script src="../js/axios.js"></script>
<script src="/js/element-ui/lib/index.js"></script>
<script src="../js/echarts.js"></script>
</head>
<body>
<div id="app" style="width: 600px;height:400px;">
</div>
<script type="text/javascript">
let app=new Vue({
el:"#app",
data: {
option: {
title: {
text: ''
},
tooltip: {
trigger: 'axis'
},
legend: {
left: 'center',
data: [
'personality standard',
'personality interviewee',
'animal interviewee'
]
},
radar: [
{
indicator: [
{ text: 'tiger', max: 30 },
{ text: 'peacock', max: 30 },
{ text: 'koala', max: 30 },
{ text: 'owl', max: 30 },
{ text: 'chameleon', max: 30 }
],
center: ['25%', '40%'],
radius: 80
},
{
indicator: [
{ text: 'extraversion', max: 20 },
{ text: 'agreeableness', max: 20 },
{ text: 'seriousness', max: 20 },
{ text: 'neuroticism', max: 20 },
{ text: 'experienceopenness', max: 20 }
],
center: ['75%', '40%'],
radius: 80
},
],
series: [
{
type: 'radar',
tooltip: {
trigger: 'item'
},
data: [
{
value: [0, 0, 0, 0,0],
name: 'animal interviewee'
}
]
},
{
type: 'radar',
radarIndex: 1,
tooltip: {
trigger: 'item'
},
data: [
{
value: [13, 16, 13.5, 10,15],
name: 'personality standard'
},
{
value: [0, 0, 0, 0,0],
name: 'personality interviewee'
}
]
}
]
},
animal: {
tiger: 0,
peacock: 0,
koala: 0,
owl: 0,
chameleon: 0,
personalityHandled: {
extraversion: 0,
agreeableness: 0,
seriousness: 0,
neuroticism: 0,
experienceopenness: 0,
}
},
},
methods:{
findAnimalById(id){
axios.get("http://localhost:8080/container/id/"+id)
.then(resp =>{
if(resp.data.code===20041){
this.animal= resp.data.data;
this.$message({
showClose: true,
message: '查询成功',
type: 'success'
});
// 设置数据
this.option.series[0].data[0].value[0]=this.$data.animal.tiger;
this.option.series[0].data[0].value[1]=this.$data.animal.peacock;
this.option.series[0].data[0].value[2]=this.$data.animal.koala;
this.option.series[0].data[0].value[3]=this.$data.animal.owl;
this.option.series[0].data[0].value[4]=this.$data.animal.chameleon;
this.option.series[1].data[1].value[0]=this.$data.animal.personalityHandled.extraversion;
this.option.series[1].data[1].value[1]=this.$data.animal.personalityHandled.agreeableness;
this.option.series[1].data[1].value[2]=this.$data.animal.personalityHandled.seriousness;
this.option.series[1].data[1].value[3]=this.$data.animal.personalityHandled.neuroticism;
this.option.series[1].data[1].value[4]=this.$data.animal.personalityHandled.experienceopenness;
var myChart = echarts.init(document.getElementById('app'));
myChart.setOption(app.option);
}else {
console.log(resp.data);
this.$message({
showClose: true,
message: '查询失败',
type: 'error'
});
}
})
},
},
created(){
// 拿到id
let id = localStorage.getItem("id");
// 查询
this.findAnimalById(id);
}
});
</script>
</body>
</html>