javascript - Google Charts with client side data population -
i've been looking @ possibilities populate chart google charts live client data. have tried solutions send requests back-end keep side separate client environment , api still undergoing development.
this example code standard pie chart
//js code <script type="text/javascript" src="https://www.google.com/jsapi"></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. function drawchart() { // create data table. var data = new google.visualization.datatable(); data.addcolumn('string', 'topping'); data.addcolumn('number', 'slices'); data.addrows([ ['mushrooms', 3], ['onions', 1], ['olives', 1], ['zucchini', 1], ['pepperoni', 2] ]); // set chart options var options = {'title':'how pizza ate last night', 'width':400, 'height':300}; // instantiate , draw our chart, passing in options. var chart = new google.visualization.piechart(document.getelementbyid('chart_div')); chart.draw(data, options); } </script> //html code <div id="chart_div"></div>
is there way populate google charts retrieving data table being displayed jquery or pure javascript? imagine this:
data.addrows([ [$('#textfield1').getattribute('value'), 3], [$('#textfield2').getattribute('value'), 1], [$('#textfield3').getattribute('value'), 1], [$('#textfield4').getattribute('value'), 1], [$('#textfield5').getattribute('value'), 2] ]);
does have experience attempting or know if possibility?
appreciate input!
with pure javascript, let suppose have above hardcoded datatable
rows
in html <table>
:
<table id="table"> <thead> <tr> <th>topping</th> <th>slices</th> </tr> </thead> <tbody> <tr><td>mushrooms</td><td>3</td></tr> <tr><td>onions</td><td>1</td></tr> <tr><td>olives</td><td>1</td></tr> <tr><td>zucchini</td><td>1</td></tr> <tr><td>pepperoni</td><td>2</td></tr> </tbody> </table>
then can read table , use source charts datatable
way :
... data.addcolumn('string', 'topping'); data.addcolumn('number', 'slices'); var tablerows = document.queryselectorall('#table tbody tr'); (var i=0;i<tablerows.length;i++) { data.addrow([ tablerows[i].cells[0].textcontent, parseint(tablerows[i].cells[1].textcontent) ]); } ...
see demo -> http://jsfiddle.net/aklf3gl9/
notice parseint(...)
important data inject on #2th column typeof number, otherwise visualization fail.
Comments
Post a Comment