Ajax access to data into the echarts cause analysis and solutions _ajax related

Source: Internet
Author: User

In doing a project that needs to be used on a echarts map, it was successful to get the data in the background with Ajax and generate the desired JSON string. But, put in Echarts option.series[0].data, get no data. You can't see the values you get from the background on the generated map. Baidu and Bing, the answer to the wide variety of answers, still unresolved problems, and finally a colleague Daniel to solve, here to share with you. Hope to be helpful to you,,,,

Nonsense not much to say, directly on the code:

$ (function () {var data = []; function setOption (data) {var MyChart = Echarts.init (document.getElementById (' main '));
Window.onresize = mychart.resize; var option = {title: {text: ' National ... Distribution map ',//subtext: ' Purely fictitious ', X: ' Left '}, tooltip: {trigger: ' item '},//legend: {//Orient: ' Vertical ',//x: ' Left ',//D ata:[' Iphone3 ', ' iphone4 ', ' iphone5 '//}, DataRange: {min:0, max:10, x: ' Left ', y: ' Bottom ', text:[' high ', ' low '],//text, default number Value text color:[' #ff5e5e ', ' #ffa25e ', ' #ffd05e ', ' #fce6b2 ', ' #e1dbcd '], calculable:true},//Toolbox: {//Show:true,//Orie NT: ' vertical ',//x: ' Right ',//y: ' center ',//feature: {//Mark: {show:true},//DataView: {show:true, readOnly : false},//restore: {show:true},//Saveasimage: {show:true}//}//},//Roamcontroller: {//Show:true,//x: ' R Ight ',//Maptypecontrol: {//' country ': TRUE//}//}, Series: [{name: ' ... ', type: ' Map ', Maptype: ' Country ', Roam:fal SE, itemstyle:{normal:{label:{show:true}}, Emphasis:{label:{show:true}},
Data:data}]};
mychart.setoption (option); $.getjson (' Hotspotservlet ', function (data) {//option.series[0].data=data.result;//Load data for Echarts object//
mychart.setoption (option);
//}); }//Get ...
The ranking data 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 + +) {VA
R dId = parseint (objdata[i].id);
var dname= "Tianjin";
if (did==1) {//Dname= "Beijing";//} 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);//execute setOption function.
Argument}});
} getmapdata (2); });

The most important thing is that the data obtained from the backstage is put into the echarts through the form of reference.

At that time, when I did not pass the form of the argument, the data obtained in the echarts is always empty.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.