views:

777

answers:

2

I'm trying to take an existing JSON object and create smaller JSON chunks out of it specific for creating separate graphs. Here is my code which uses jQuery:

function updateStationGraphs (bsid) {
    $.getJSON("includes/system/ajaxDataInterface.php", {format:'flot',target:bsid}, function(data) {
     var graphOptions = {
      series: {
        lines: { show: true },
        points: { show: true }
      }
       };
     var handsetData = new Array(data[bsid].maxHandsets,data[bsid].avgHandsets);
     $.plot($('.graphHandsets'),handsetData,graphOptions);
    });

}

Clarification: handsetData is expecting something of the format:

  [ { label: "Foo", data: [ [10, 1], [17, -14], [30, 5] ] },
    { label: "Bar", data: [ [11, 13], [19, 11], [30, -7] ] } ]

My problem is when calling updateStationGraphs('A5A50000') for instance, it reports data[bsid] is undefined. I had a similar problem previously but my problem turned out to be that I was redeclaring my 'data' object again and was playing with the wrong object. This doesn't seem to be the case here and I can't figure out why it won't let me access data[bsid].

Here is what data is suppose to look like:

var data = {"A5A50000":{"time":{"label":"time","data":[[1244045863,"2009-06-03 16:17:43"],[1244045803,"2009-06-03 16:16:43"],[1244045743,"2009-06-03 16:15:43"],[1244045683,"2009-06-03 16:14:43"],[1244045623,"2009-06-03 16:13:43"],[1244045563,"2009-06-03 16:12:43"],[1244045503,"2009-06-03 16:11:43"],[1244045443,"2009-06-03 16:10:43"],[1244045383,"2009-06-03 16:09:43"],[1244045323,"2009-06-03 16:08:43"]]},"avgHandsets":{"label":"avgHandsets","data":[[1244045863,204.7143],[1244045803,205.9444],[1244045743,205.3333],[1244045683,205.3889],[1244045623,204.5882],[1244045563,204.8235],[1244045503,205],[1244045443,205.9412],[1244045383,205.6667],[1244045323,204.1176]]},"maxHandsets":{"label":"maxHandsets","data":[[1244045863,314],[1244045803,314],[1244045743,315],[1244045683,315],[1244045623,315],[1244045563,314],[1244045503,314],[1244045443,316],[1244045383,316],[1244045323,312]]}}};

Suggestions are greatly appreciated. Thank in advance.

+2  A: 

I tested your code and every thing is fine. I get the flot graph generated correctly after the $.getJSON() call.

You need to make sure that the JSON data returned does contain the variable declaration and semicolon at the end as in your example.

In other words, it should be the JSON only.

{"A5A50000":{"time":{"label":"time","data":[[1244045863.........]}}}

Instead of:

var data = {"A5A50000":{"time":{"label":"time","data":[[1244045863,....

This may be the problem.

Jose Basilio
I really wish it were that simple. I added the var declaration to help other test the code themselves, and I check the response and am getting the proper object in the right format. I'm just stumped. Maybe I'm doing something silly and couldn't see it because it was Friday at the end of the day.
Michael
Well. Using your example and returning that json data from another page, I get the flot graph just fine. Is there anything missing from your example?
Jose Basilio
A: 

In hindsight, after rest and a fresh start, I was able to realize I was accessing data[bsid] when bsid = 'a5a50000' instead of bsid = 'A5A50000'. ALWAYS CHECK YOUR CASE! sigh

Michael