javascript - retrieve data from mongodb and link it with google charts -
i'd retrieve data database (mongodb) , data i'd draw à pie chart using google charts. in first case have collection called user , each user has boolean attribute called (paid), i'd count how many paid users , free users , basing on results draw pie chart percentage of each kind of users
this try tried long time i'dont arrive show chart,
thanks :)
<?php $connection = new mongoclient(); $collection = $connection->pato->user; $cursor = $collection->find(); $cursor = $collection->find(array("paid"=>true)); $x=$cursor->count(); $cursor = $collection->find(array("paid"=>false)); $y=$cursor->count(); $myurl[]="['option','value']"; $myurl[]=[['free users', $y],['paid users', $x]]; ?> <html> <head> <!--load ajax api--> <body> <script type="text/javascript" src="https://www.google.com/jsapi"></script> </head> <script type="text/javascript"> google.load('visualization', '1', {'packages':['corechart']}); google.setonloadcallback(drawchart); function drawchart() { var data=google.visualization.arraytodatatable([ <?echo(implode(",",$myurl));?> ]); // set chart options var options = {'title':'user statistics', 'width':400, 'height':300}; // instantiate , draw our chart, passing in options. chart = new google.visualization.piechart(document.getelementbyid('userstatsdiv')); google.visualization.events.addlistener(chart, 'select', selecthandler); chart.draw(data, options); }</script> <body> <div id="usagestatsdiv" style="width:700; height:500"></div> </body> </html>
you have syntax error in php, , fixing that, data structure not quite right visualization api. try this:
$myurl = array( array('option', 'value'), array('free users', $y), array('paid users', $x) );
and in javascript:
var data = google.visualization.arraytodatatable(<?php echo json_encode($myurl, json_numeric_check); ?>);
using json_encode
allows transition php arrays javascript arrays , objects without worrying syntax of array , object construction.
Comments
Post a Comment