javascript - Google Line Chart Adding Array of Objects -
i have array of objects i've created parsing json string:
var measurementdata = @html.raw(jsonconvert.serializeobject(this.model.item1)); var stringifieddata = json.stringify(measurementdata); var parseddata = json.parse(stringifieddata);
this gives me number of objects, depending on model, looking this:
now question is, how add these google line chart without having hardcode datatable?
this i've got now, works somewhat:
var data = new google.visualization.datatable(); data.addcolumn('string', 'timestamp'); data.addcolumn('number', selectedmeasurements); data.addrows([ [parseddata[index[0]].timestamp, parseddata[index[0]][selectedmeasurements]], [parseddata[index[1]].timestamp, parseddata[index[1]][selectedmeasurements]] ]); chart.draw(data, options, { isstacked: true, vaxis: { viewwindowmode: 'explicit', viewwindow: { max: 100, min: 0 } } });
here i'm adding 2 arrays data datatable, since there 2 objects add. if have one? or 10? imagine kind of foreach inside .addrows
i'm not sure how accomplish this.
any appreciated!
you can use "setvalue(rowindex, columnindex, value)" method:
var jsondata = '[{"slideid":"d2011", "instrumentid":"i335", "incmin":"37.13", "incmax": "37.19", "brmin":"31.4"}, {"slideid":"d2014", "instrumentid":"i335", "incmin":"37.13", "incmax": "37.19", "brmin":"31.4"}]'; var parseddata = json.parse(jsondata); var len = parseddata.length; var data = new google.visualization.datatable(); data.addcolumn('string', 'timestamp'); data.addcolumn('number', selectedmeasurements); data.addrows(len); (var n = 0; n < len; n++) { var i=0; (var key in parseddata[n]) { data.setvalue(n, i, parseddata[n][key]); i++; } } chart.draw(data, options, { isstacked: true, vaxis: { viewwindowmode: 'explicit', viewwindow: { max: 100, min: 0 } } });
Comments
Post a Comment