ECharts+springboot项目,前端不显示数据,找错误,找了一下午

项目场景:

        因为想做一个大五人格心理分析的项目,因此结合了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>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 是一个基于 Spring 框架的开发框架,它可以帮助开发者快速搭建 Web 用程序。结合 ECharts 和 MongoDB,可以实现数据可视化分析的功能。 下面是一个简单的使用 Spring Boot、ECharts 和 MongoDB 进行数据可视化分析的流程: 1. 创建 Spring Boot 项目,配置 MongoDB 数据库连接。 2. 创建 MongoDB 数据库 DAO 层,读取数据。 3. 创建控制器层,将数据转换为 ECharts 所需的格式,并返回给前端页面。 4. 前端页面使用 ECharts 的 API,在页面中绘制各种类型的图表。 5. 根据实际需求,对图表进行样式、交互、动画等方面的调整和优化。 下面是一个简单的示例代码: ```java // 创建 MongoDB 数据库 DAO 层,读取数据 @Repository public class MyCollectionDAO { @Autowired private MongoTemplate mongoTemplate; public List<MyCollection> findAll() { return mongoTemplate.findAll(MyCollection.class); } } // 创建控制器层,将数据转换为 ECharts 所需的格式,并返回给前端页面 @RestController @RequestMapping("/chart") public class ChartController { @Autowired private MyCollectionDAO myCollectionDAO; @GetMapping("/data") public Map<String, Object> getData() { List<MyCollection> data = myCollectionDAO.findAll(); // 将数据转换为 ECharts 所需的格式 List<String> names = data.stream().map(item->item.getName()).collect(Collectors.toList()); List<Integer> values = data.stream().map(item->item.getValue()).collect(Collectors.toList()); Map<String, Object> result = new HashMap<>(); result.put("names", names); result.put("values", values); return result; } } // 前端页面使用 ECharts 的 API,在页面中绘制图表 <script> $.get("/chart/data", function(data) { var chart = echarts.init(document.getElementById('chart')); chart.setOption({ title: { text: '数据可视化分析' }, tooltip: {}, xAxis: { data: data.names }, yAxis: {}, series: [{ name: '数值', type: 'bar', data: data.values }] }); }); </script> ``` 以上是一个简单的使用 Spring Boot、ECharts 和 MongoDB 进行数据可视化分析的流程和示例代码。实际用中,还需要更加细致的处理和优化,例如数据筛选、分组、聚合、排序等,以及图表样式、交互、动画等方面的定制和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小海海不怕困难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值