IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> Echarts异步获取数据不显示问题 -> 正文阅读

[JavaScript知识库]Echarts异步获取数据不显示问题

问题描述:

做Echarts可视化图表,前后端分离项目,前端异步获取后端接口数据时 echarts图表不显示问题
在这里插入图片描述

left_center_char() {
				
				var datesli = [];
				var confirmedsli = [];
				var confirmedli = [];

				this.$axios.get("/hive2/getPre").then((res) => {
					
					// var datesli=[]
					// var confirmedli=[]  //预测值
					var recoveredli=[]
					// var confirmedsli=[]  //真实值
					var recoveredsli=[]
					var getPreli=[datesli,confirmedli,recoveredli,confirmedsli,recoveredsli]
					
					var getpre=res.data.data.pre
//					console.log(getpre)
					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');
				// 基于准备好的dom,获取main节点init初始化echarts实例
				var myChart = echarts.init(document.getElementById('left_center_in'));

				var fontColor = '#30eee9';

				// 指定图表的配置项和数据
				var option = {

					//只需要在此加入所需内容
					grid: {
						left: '2%',
						//						right: '1%',
						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
  //data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
					}],
					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
							          //  data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330]
						},
						{
							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 datesli=[]
					// var confirmedli=[]  //预测值
					var recoveredli=[]
					// var confirmedsli=[]  //真实值
					var recoveredsli=[]
					var getPreli=[datesli,confirmedli,recoveredli,confirmedsli,recoveredsli]
					
					var getpre=res.data.data.pre
//					console.log(getpre)
					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');
				// 基于准备好的dom,获取main节点init初始化echarts实例
				var myChart = echarts.init(document.getElementById('left_center_in'));

				var fontColor = '#30eee9';

				// 指定图表的配置项和数据
				var option = {

					//只需要在此加入所需内容
					grid: {
						left: '2%',
						//						right: '1%',
						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
  //data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
					}],
					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
							          //  data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330]
						},
						{
							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');
				// 基于准备好的dom,获取main节点init初始化echarts实例
				var myChart = echarts.init(document.getElementById('left_center_in'));

				var fontColor = '#30eee9';

				// 指定图表的配置项和数据
				var option = {

					//只需要在此加入所需内容
					grid: {
						left: '2%',
						//						right: '1%',
						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: []
  //data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
					}],
					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: []
							          //  data: [120, 132, 101, 134, 90, 230, 210, 182, 191, 234, 290, 330]
						},
						{
							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);

				//获取预测数据,目标格式:[0, 50, 100, 150, 200, 250, 300, 350]
				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
//					console.log(getpre)
					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'])
					}
					// this.dates=datesli
					// this.confirmed=confirmedli
					// this.recovered=recoveredli
					// this.confirmeds=confirmedsli
					// this.recovereds=recoveredsli
//					console.log(this.recovered)
					myChart.setOption({
						xAxis: {
                            data: datesli
                        },
                        series: [{
                            // 根据名字对应到相应的系列
//                          name: '销量',
                            data:confirmedsli
                        },{
                        	data:confirmedli
                        }]
					})
//					that.right_center_char(this.dates, this.recovereds,this.recovered)
				}).catch((error) => {
					console.warn(error)
				})
			},

在这里插入图片描述
成功

如果有不对的地方希望大家给出宝贵建议

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-12-01 17:37:04  更:2021-12-01 17:37:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 23:37:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码