javascript - how to make google chart moving and update dynamically -
i don't know how make google chart moving, , asking again out there .i need badly.. want make google chart moving database because database collecting data automatically.i want chart move automatic update without refreshing it. following javascript code:
<!doctype html> <html> <head> <!-- external libs--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://www.gstatic.com/charts/loader.js"></script> <script src="jquery.min.js"></script> <!-- example script --> <script> google.charts.load('current', { callback: drawchart, packages: ['corechart'] }); // onload callback function drawchart() { // jsonp request var jsondata = $.ajax({ url: 'livedata.php', datatype: 'json', }).done(function (results) { var data = new google.visualization.datatable(jsondata); data.addcolumn('date', 'time_stamp'); data.addcolumn('number', 'ph'); data.addcolumn('number', 'moist'); $.each(results, function (i, row) { data.addrow([ new date(row.time_stamp), parsefloat(row.ph), parsefloat(row.moist) ]); }); var chart = new google.visualization.areachart($('#chart').get(0)); chart.draw(data, { title: 'soil analysis', curvetype: 'function', displayannotations: true //legend: { position: 'bottom' } //pointsize: 10 }); }); } drawchart(); setinterval(drawchart, 10000); // load chart lib // call drawchart once google charts loaded // google.setonloadcallback(drawchart); </script> </head> <body> <div id="chart" style="width: 80%;height:380px"></div> </body> </html>
i dont know if affect chart making move following php json code:
$query = "select readingid, moist, ph , time_stamp soilreading"; $result = mysqli_query($connection, $query); $data_points = array(); while($row = mysqli_fetch_array($result)){ $wholedate = date('c',strtotime($row['time_stamp'])); $monthnum = date('m',strtotime($row['time_stamp'])); $daynum = date('d', strtotime($row['time_stamp'])); $yearnum = date('y', strtotime($row['time_stamp'])); $dateobj = datetime::createfromformat('!m', $monthnum); $monthname = $dateobj->format('f'); if(($monthnum == "9")&&($yearnum == "2017")){ if (array_key_exists($daynum, $data_points)) { $data_points[$daynum]->ph += $row['ph'] / $data_points[$daynum]->ph = count($row['ph']); $data_points[$daynum]->moist += $row['moist']; }else{ //$data_points[$daynum]->ts = $yearnum."".$monthname ."".$daynum; // $data_points[$daynum]->ts = $yearnum."".$monthnum."".$daynum; $data_points[$daynum]->time_stamp =$wholedate; //$data_points[$daynum]->ts = $wholedate; $data_points[$daynum]->ph = $row['ph'] / $data_points[$daynum]->ph = count($row['ph']); $data_points[$daynum]->moist = $row['moist']; } } } $jsonresult = json_encode(array_values($data_points)); echo $jsonresult;
what's point in using jsondata variable? looks it's not required @ all. try this:
$.ajax({ url: 'livedata.php', datatype: 'json', }).done(function (results) { var data = new google.visualization.datatable();
also make use of google.setonloadcallback(drawchart);
tried. ensure drawchart run after google api loaded.
Comments
Post a Comment