jquery - How to read data in json file in javascript to use it for google charts? -
i have data in path static/data.json.i trying plot chart using google charts.i want take out data in data.json file , use google charts.
data.json given below
data = '[{"k": "ram" , "v":0.515625},{"k": "camera" , "v":0.515625},{"k": "design" , "v":0.05},{"k": "processor" , "v":0},]';
my html code in template/isworked.html.
below code isworked.html
<html> <head> <!--load ajax api--> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="application/json" src="static/d.json"></script> <script type="text/javascript"> // load visualization api , piechart package. google.load('visualization', '1.0', {'packages':['corechart']}); // set callback run when google visualization api loaded. google.setonloadcallback(drawchart); // callback creates , populates data table, // instantiates pie chart, passes in data , // draws it. var mydata = json.parse(data); function drawchart() { // create data table. //loading data json var data = new google.visualization.datatable(); data.addcolumn('string', 'feature'); data.addcolumn('number', 'positive'); var f,n; for(var = 0 ; i<data.length;i++){ f = data[i].k n = data[i].v data.addrows([[f,n]]) } // set chart options var options = {'title':'pie chart', 'width':800, 'height':700}; // instantiate , draw our chart, passing in options. var chart = new google.visualization.piechart(document.getelementbyid('chart_div')); chart.draw(data, options); } </script> </head>
i not able plot graph. data.json generated python script. running everyting using flask framework in ubuntu
i have no experience in topic. can me?
uberkael correct.
update: corrected multiple problems code , data, e.g., trailing comma in data. works ... click "run code snippet" button.
"mydata" problem. attempting use data before json file has loaded. triggers error. , mydata not used anything. need add div hold pie chart. code snippet below works, json data incorrect drawing pie chart. correct data , should work you.
<!doctype html> <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="text/javascript"> var mydata = [{"k": "ram" , "v":0.515625},{"k": "camera" , "v":0.515625},{"k": "design" , "v":0.05},{"k": "processor" , "v": 0}]; </script> <script type="text/javascript"> google.load('visualization', '1.0', {'packages':['corechart']}); google.setonloadcallback(drawchart); function drawchart() { var data = new google.visualization.datatable(); data.addcolumn('string', 'feature'); data.addcolumn('number', 'positive'); var f,n; for(var = 0 ; i<mydata.length;i++){ f = mydata[i].k n = mydata[i].v data.addrows([[f,n]]) } var options = {'title':'pie chart', 'width':800, 'height':700}; var chart = new google.visualization.piechart(document.getelementbyid('chart_div')); chart.draw(data, options); } </script> </head> <body> <div id="chart_div"></div> </body> </html>
Comments
Post a Comment