Home >Web Front-end >JS Tutorial >Analysis and solutions to the reasons why the data obtained by Ajax is not displayed in echarts
When working on a project that required the use of echarts maps, I successfully obtained the data provided by the background through ajax and generated the desired JSON string. However, when placed in echarts option.series[0].data, no data can be obtained. You cannot see the value you get from the background on the generated map. I will share my solution with you below. Friends who need it can refer to it
When doing a project that requires the use of echarts map, The data provided by the background was successfully obtained through ajax and the desired JSON string was generated. However, when placed in echarts option.series[0].data, no data can be obtained. The values you get from the background cannot be seen on the generated map. After searching through Baidu and Bing, the answers given were various, but the problem was still not solved. In the end, a colleague, an expert, solved it, and I would like to share it with everyone. I hope it will be helpful to everyone,,,,
Without further ado, let’s go directly to the code:
$(function () { var data = []; function setOption(data){ var myChart = echarts.init(document.getElementById('main')); //window.onresize = myChart.resize; var option = { title : { text: '全国...分布图', // subtext: '纯属虚构', x:'left' }, tooltip : { trigger: 'item' }, // legend: { // orient: 'vertical', // x:'left', // data:['iphone3','iphone4','iphone5'] // }, dataRange: { min: 0, max: 10, x: 'left', y: 'bottom', text:['高','低'], // 文本,默认为数值文本 color:['#ff5e5e', '#ffa25e', '#ffd05e','#fce6b2','#e1dbcd'], calculable : true }, // toolbox: { // show: true, // orient : 'vertical', // x: 'right', // y: 'center', // feature : { // mark : {show: true}, // dataView : {show: true, readOnly: false}, // restore : {show: true}, // saveAsImage : {show: true} // } // }, // roamController: { // show: true, // x: 'right', // mapTypeControl: { // 'china': true // } // }, series : [ { name: '...', type: 'map', mapType: 'china', roam: false, itemStyle:{ normal:{label:{show:true}}, emphasis:{label:{show:true}} }, data:data } ] }; myChart.setOption(option); //$.getJSON('HotspotServlet',function(data){ //option.series[0].data=data.result; // 为echarts对象加载数据 //myChart.setOption(option); //}); } //获取...排行数据 function getMapData(limit){ $.ajax({ url:'http://127.0.0.1/api/adminunit/score/top/'+limit, type:'post', dataType:'JSON', success:function(objdata){ //var str = JSON.parse(objdata); for(var i = 0;i < objdata.length;i ++){ var dId = parseInt(objdata[i].id); //var dName="天津市"; //if(dId==1){ // dName="北京市"; //} var dName = objdata[i].name; var dScore = parseInt(objdata[i].score); var oneData = {}; var oneData = {id:dId,name:dName,value:dScore}; data.push(oneData); } console.log(data); //option.series[0].data=data; setOption(data);//执行setOption函数。传参 } }); } getMapData(2); });
The most important thing is that the data obtained from the background is placed in the form of parameters. In echarts.
When I did it, I didn’t pass parameters in the form, and the data obtained in echarts was always empty.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Ajax submission form page refreshes quickly solution
Two solutions for Ajax opening a new window and being intercepted by the browser
The above is the detailed content of Analysis and solutions to the reasons why the data obtained by Ajax is not displayed in echarts. For more information, please follow other related articles on the PHP Chinese website!