views:

88

answers:

1

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 pass it to the barchart. Please help. I am stuck with this.

thanks in advance.

+1  A: 

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");
Tatu Ulmanen
Exactly. myData is supposed to be an array of arrays, not an array of objects.
timdev
Thanks a lot.I am getting usernames and ssn in myData. but, alert("array to barchart "+myData);var myChart = new JSChart('graph', 'bar');myChart.setDataArray(myData);myChart.draw();But while running, I am getting an alert message saying:JScharts: Input data in wrong fromat for selected chart type. No data loaded.