Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to to draw a chart from the following json string:

{
"d":{
  "results":[
 {
   "_metadata":{
      "uri": "http://www.something.com/hi",
      "type" : "something.preview.hi"
      }, "Period", "1988", "Age": 20, "Size" : 80
 },
   "_metadata":{
       "uri": "http://www.something.com/hi",
       "type" : "something.preview.hi"
      }, "Period", "1989", "Age": 25, "Size" : 90
 } 
   ]  }}

I use jquery.flot.js library to draw the chart. Here is the example to draw the chart and it works fine:

var d1 = [];
for (var i = 0; i < 14; i += 0.5)
    d1.push([i, Math.sin(i)]);
var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

myChart.setDataArray(d1);
myChart.setDataArray(d2);  

According to example I have converted the json to an array of object:

 var results = $.parseJSON(responseText)["d"]["results"];            
        var sampleData = [];
        for (var i = 0, len = results.length; i < len; i++) {
            var result = results[i];

            sampleData.push({ Perioden: result.Perioden,
                Size: result.Size,
                Age: result.Age}); 
}

 var myData = [];
        for (var i in sampleData) {
            myData.push([sampleData[i].Period, sampleData[i].Age]);
        }

        var myData1 = [];
        for (var i in sampleData) {
            myData1.push([sampleData[i].Period, sampleData[i].Size]);
        }

myChart.setDataArray(myData);
myChart.setDataArray(myData1);

but I get the error that the data format is wrong for the data chart. Can anyody see what is the different between my code and the example?

share|improve this question

2 Answers

up vote 0 down vote accepted

I guess that your json is well formated. but the exemple you are providing is not (trailing "i" after second "uri2 line and "," instead of ":" after "Period"

second thing, you have Period in your json and Perioden is your js code. check that you are using the same name everywhere

then you can try to console.log(myData, myData1) to see if you have the expected results

tell us if it's enough or if you have another problem

share|improve this answer
The issues that you named were type faults. I edited my question. My problem is still remained. – wingman55 May 10 '12 at 9:15
you still have "Period", "1988" instead of "Period": "1988". can you show us the output of console.log(myData, myData1) ? – Mathieu May 10 '12 at 10:51
I found out what was the problem: Here is the solution: sampleData.push({ Perioden: result.Perioden, Size: parseInt(result.Size), Age: parseInt(result.Age)}); } – wingman55 May 10 '12 at 11:06

Try this:

var myData = [];
sampleData.forEach(function(item){
        myData.push([item.Perioden, item.Age]);
});
share|improve this answer
I tried it, but no difference :( – wingman55 May 10 '12 at 9:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.