views:

191

answers:

2

In the past I have used the following method to parse the json data appropriately so I can populate a select box:

var request = null; 

request = $.ajax({ 
type: "POST", 
url: "url goes here", 
dataType: 'text', 
success: function(returned){ 
    returned = JSON.parse(returned) 

    $.each(returned, function(selectid,item) 
    { 
        var sel = $("#"+selectid).get(0); 
        sel.options.length=0; 

        $.each(item, function(i,subitem){ 
            sel.options[i]=new Option(subitem.text, subitem.value); 
        }); 
    }); 

    request = null; 
    } 
}); 

How do I apply the same technique to the code below, but without a select box, just parse the json into the drawChart function?

$.ajax({ 
url: 'chart_json.aspx', 
type: 'POST', 
dataType: 'text', 
success: function(data) { 
    drawChart(data); 
} 
}); 
+1  A: 
$.ajax({
  url: 'get_json.aspx',
  type: 'POST',
  dataType: 'json', // as noted by Sinan
  success: function(data) {
    drawVisualization(data);
  }
});

function drawVisualization(serverData) {
    var chartData = [];
    for(var i = 0; i < serverData.length; i++) {
      chartData.push([serverData[i][0], serverData[i][1].v]);
    }
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'WIP');
    data.addColumn('number', 'Percentage');
    data.addRows(chartData);
    new google.visualization.PieChart(document.getElementById('visualization')).draw(data, {width: 400, height: 240, is3D:true});     
}

The chartData array needs to have 2 columns (since you call addColumn twice on the google.visualization.DataTable) with a string and a number in each row.

Example here

sje397
Hi, Thanks for the reply. I have updated the question above in response to your answer. Basically I get a blank page with an error and I have corrected the json data issue you pointed out.
oshirowanen
I have moved google.load out of the document.ready section, and I am now getting the following error: Argument given to addRows must be either a number or an array Line:
oshirowanen
The array you pass in to addRows needs to be organised according to how you called 'addColumn' on the dataTable. I've updated my answer.
sje397
oshirowanen
Does the json data need to parsed? I am not using a browser which has a built in json parser.
oshirowanen
As mentioned by Sinan, the dataType should be set to json in the ajax request. I added a link to an example (which uses client-side test data).
sje397
I just just tried the script in your example url which works perfectly. But as soon as I try to ajax it, it just gives me a blank screen. I have updated the original question with the full client side page and the current json data.
oshirowanen
I've updated the original question.
oshirowanen
If you set the dataType in the ajax call to 'text', then you need to parse the returned data. If you set it to 'json', jQuery will evaluate the returned text and will pass the resulting javascript object to your success callback function.
sje397
I did try it with type json, but it does not work, I just get a blank screen. but as soon as I remove the ajax part and hard code the json data into a javascript variable, it works perfectly. So I can only assume there is a parsing issue somewhere.
oshirowanen
Use firebug in firefox or the javascript console in chrome - put a breakpoint at the start of your success function and see what the data is that is coming in.
sje397
I'm also wondering - do you mean to use POST in the ajax request?
sje397
I'm getting:'1.v' is null or not an object
oshirowanen
Figured it out. Had to get rid of the document.ready, and just use the standard google.setonload thing.
oshirowanen
I have a follow-up question here: http://stackoverflow.com/questions/3266505/google-chart-formatting. It is just a slight change of the existing code and hopefully something which can be done quickly
oshirowanen
+1  A: 

I think your problem lies in your ajax response, i'd do the following:

in your response:

{ 
  graphData : [
    ['user1',{v:0.00, f:'0.00'}],
    ['user2',{v:0.00, f:'0.00'}],
    ['user3',{v:0.00, f:'0.00'}],
    ['user4',{v:0.00, f:'0.00'}],
    ['user5',{v:0.00, f:'0.00'}]
  ],
  status : "ok"
}

in your js:

$.ajax({
  url: 'get_json.aspx',
  type: 'POST',
  dataType: 'json',//this is important
  success: function(data) {
    //this is not vital, but it's nice to check the response
    if(typeof data === "object" && data.status === "ok"){
        graphData = data.graphData;
        drawVisualization(graphData);
    }
  }
});

Hope this helps, Sinan.

Sinan Y.
I have just tried this, and I am still getting a blank page. I have updated my question to show the full script and what the json data looks like now.
oshirowanen
Going to lunch. Back in a bit.
oshirowanen
I've updated the original question.
oshirowanen
I have a follow-up question here: http://stackoverflow.com/questions/3266505/google-chart-formatting. It is just a slight change of the existing code and hopefully something which can be done quickly
oshirowanen