I want to input JSONObject from jquery.post() to a a simple JS chart(bar chart)?
HI I am very new to both json and js charts.
In the example of bar chart, they are giving a hard coded array like this,
var myData = new Array(['U.S.A.', 69.5], ['Canada', 2.8], ['Japan & SE.Asia', 5.6] );
var myChart = new JSChart('graph', 'bar');
myChart.setDataArray(myData);
Instead of that I want to use the response of $.post() method which is in json. Here is the piece of code.
var myData=[];
$.post("JSONServlet", function(data) {
$.each(data.Userdetails, function(i, data) {
myData[i] = [];
myData[i]['text'] = data['firstname'];
myData[i]['id'] = data['ssn'];
alert("first name " +myData[i]['text']+ " salary " +myData[i]['id']);
// I am getting correct data here, but how to assign this myData to barchart
});
}, "json");
is this the logic to use or how else can i get username and salary from the response and pas开发者_如何学编程s it to the barchart. Please help. I am stuck with this.
thanks in advance.
If I understood you correctly, you're trying to generate a similar barchart as in your first example using firstname
as the key and ssn
as the value. In that case, you can do something like this:
$.post("JSONServlet", function(data) {
var myData = [];
$.each(data.Userdetails, function(i, data) {
myData[i] = [data.firstname, data.ssn];
});
var myChart = new JSChart('graph', 'bar');
myChart.setDataArray(myData);
}, "json");
精彩评论