Need some help if there is a problem with the code or the data itself. Although I suspect its with the code because I tried to validate the JSON data I received it is properly formatted...
Here is my JSON data
{"view":"cities","model":{"top10Cities":[{"id":1024,"name":"Mumbai (Bombay)","countrycode":"IND","district":"Maharashtra","population":"10500000"},{"id":2331,"name":"Seoul","countrycode":"KOR","district":"Seoul","population":"9981619"},{"id":206,"name":"São Paulo","countrycode":"BRA","district":"São Paulo","population":"9968485"},{"id":1890,"name":"Shanghai","countrycode":"CHN","district":"Shanghai","population":"9696300"},{"id":939,"name":"Jakarta","countrycode":"IDN","district":"Jakarta Raya","population":"9604900"},{"id":2822,"name":"Karachi","countrycode":"PAK","district":"Sindh","population":"9269265"},{"id":3357,"name":"Istanbul","countrycode":"TUR","district":"Istanbul","population":"8787958"},{"id":2515,"name":"Ciudad de México","countrycode":"MEX","district":"Distrito Federal","population":"8591309"},{"id":3580,"name":"Moscow","countrycode":"RUS","district":"Moscow (City)","population":"8389200"},{"id":3793,"name":"New York","countrycode":"USA","district":"New York","population":"8008278"}],"fusionCharts":{"chart":"{\"showValues\":\"0\",\"caption\":\"Cities by Country Code\",\"theme\":\"zune\"}","data":"[{\"label\":\"Mumbai (Bombay)\",\"value\":\"10500000\"},{\"label\":\"Seoul\",\"value\":\"9981619\"},{\"label\":\"São Paulo\",\"value\":\"9968485\"},{\"label\":\"Shanghai\",\"value\":\"9696300\"},{\"label\":\"Jakarta\",\"value\":\"9604900\"},{\"label\":\"Karachi\",\"value\":\"9269265\"},{\"label\":\"Istanbul\",\"value\":\"8787958\"},{\"label\":\"Ciudad de México\",\"value\":\"8591309\"},{\"label\":\"Moscow\",\"value\":\"8389200\"},{\"label\":\"New York\",\"value\":\"8008278\"}]"}},"cleared":false}
Here is the code I used to receive the JSON data
$(document).ready( function () {
var jsonD = $.getJSON("http://localhost:8080/v1/cityData", function ( data ) {
console.log("Success "+ data.toString());
alert(data);
$("#fChart").insertFusionCharts({
type: "column2d",
width: "450",
height: "250",
dateFormat: "JSONURL",
dataSource: data
});
})
});
I tried the tips stated on this article but its still not working. Any advice on how to fix this? Thanks
Update 2
modified jquery code as suggested by Nisanth but the chart library is still not working. It says invalid data, maybe this is a problem with fusion charts library itself?
$(document).ready( function () {
var jsonD = {};
$.getJSON("http://localhost:8080/v1/cityData", function ( data ) {
console.log("Success ");
alert(data);
jsonD = data;
}).done( function () {
var fcData = JSON.stringify(jsonD);
console.log("fcData: "+fcData);
$("#fChart").insertFusionCharts({
type: "column2d",
width: "450",
height: "250",
dateFormat: "JSON",
dataSource: fcData
});
});
});
The code is working fine i've added a log inside .done function and it showed the value of fcData see below.
Changed fcData = jsonD.model.fusionCharts and received the error below.
Uncaught TypeError: b.match is not a function
at Fb (fusioncharts.js:214)
at constructor._drawCategory (fusioncharts.js:1263)
at constructor._drawComponents (fusioncharts.js:1223)
at constructor.draw (fusioncharts.js:1220)
at k._drawAxis (fusioncharts.js:981)
at k._updateVisuals (fusioncharts.js:976)
at k.draw (fusioncharts.js:979)
at k.init (fusioncharts.js:946)
at Object.m.createChart (fusioncharts.js:875)
at n.core.render (fusioncharts.js:1801)
Update 3
Code update
}).done( function () {
var cfChart= jsonD.model.fusionCharts.chart;
var cfData= jsonD.model.fusionCharts.chart;
$("#fChart").insertFusionCharts({
type: "column2d",
width: "450",
height: "250",
dateFormat: "JSON",
dataSource: {chart:cfChart,data:cfData}
});
});
});
Error Log
Uncaught TypeError: b.match is not a function
at Fb (fusioncharts.js:214)
at constructor._drawCategory (fusioncharts.js:1263)
at constructor._drawComponents (fusioncharts.js:1223)
at constructor.draw (fusioncharts.js:1220)
at k._drawAxis (fusioncharts.js:981)
at k._updateVisuals (fusioncharts.js:976)
at k.draw (fusioncharts.js:979)
at k.init (fusioncharts.js:946)
at Object.m.createChart (fusioncharts.js:875)
at n.core.render (fusioncharts.js:1801)
Working code
$(document).ready( function () {
var jsonD = {};
$.getJSON("http://localhost:8080/v1/cityData", function ( data ) {
console.log("Success ");
//alert(data);
jsonD = data;
}).done( function () {
var cfChart= JSON.parse(jsonD.model.fusionCharts.chart);
var cfData= JSON.parse(jsonD.model.fusionCharts.data);
$("#fChart").insertFusionCharts({
type: "column2d",
width: "450",
height: "250",
dateFormat: "JSON",
dataSource: {chart:cfChart,data:cfData}
});
});
});