javascript - Looping through the JSON returned for creating my chart -
i trying create info series needed canvasjs chart dynamically , far had no joy. somehow code not working. getting next error:
syntaxerror: missing : after property id for(var i=0; i<datapoints.length; i++){
this json data
[ { "t": "t3", "y": 6.8, "x": "2004-07-05" }, { "t": "t4", "y": 29, "x": "2004-07-05" }, { "t": "tsh", "y": 0.01, "x": "2004-07-05" }, { "t": "thyroglobulin level", "y": 0.5, "x": "2004-07-05" }, { "t": "t3", "y": 5.2, "x": "2005-06-15" }, { "t": "t4", "y": 30, "x": "2005-06-15" }, { "t": "tsh", "y": 0.02, "x": "2005-06-15" }, { "t": "thyroglobulin level", "y": 0.5, "x": "2005-06-15" } ]
here code retrieving json , creating info series
$(document).ready(function(){ $("#find").click(function(e){ e.preventdefault(); $.ajax({ // url request url: "bloodtest.php", // info send (will converted query string) data: {pnhsno: "1001001002"}, // whether post or request type: "get", // type of info expect datatype : "json", // code run if request succeeds; // response passed function success: function(json){ if(json.length !=0){ var datapoints = json.map(function (p) { p.x = new date(p.x); homecoming p; }); $("#chart").canvasjschart({ //pass chart options title:{text:"blood test results"}, axisx:{valueformatstring:"dd-mm-yyyy",labelangle:-45}, data: [{ type: "line", //change column, spline, line, pie, etc for(var i=0; i<datapoints.length; i++){ if(datapoints[i].t =="t3"){ datapoints:[ {x:datapoints.x, y:datapoints.y}] } } ] }); } } }); }); });
at point in code syntax error occurs, you're attempting create object literal for
loop within of it. compiler crapping out because can't interpret program. it's expecting object property identifier, not for
loop.
specifically, illegal part:
data: [{ type: "line", for(...) <----- javascript doesn't allow 'for' loop here...
javascript jquery json iteration
No comments:
Post a Comment