检索数据从mongodb和链接它与谷歌图表
retrieve data from mongodb and link it with google charts
我想从数据库(mongodb)检索数据,并与这些数据,我想用谷歌图表绘制
这是我的尝试,我尝试了很长时间,但我没有到达显示图表,
谢谢:)
<?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 the 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 and draw our chart, passing in some 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>
您在PHP中有一个语法错误,即使修复了这个错误,您的数据结构也不完全适合visualapi。试试这个:
$myurl = array(
array('Option', 'Value'),
array('Free users', $y),
array('Paid users', $x)
);
,然后在javascript中:
var data = google.visualization.arrayToDataTable(<?php echo json_encode($myurl, JSON_NUMERIC_CHECK); ?>);
使用json_encode
可以让你从PHP数组转换到javascript数组和对象,而不用担心数组和对象构造的语法。
相关文章:
- 谷歌公共日历链接没有'不工作(完整日历)
- HTML5 Shiv谷歌CDN链接不工作
- 打开带有多个标记的谷歌地图链接
- 谷歌将广告超链接映射到国家标记
- 如何获取显示在谷歌搜索右侧的图像链接
- 谷歌jQuery UI链接无法脱机工作
- 谷歌分析跟踪内部链接不正常工作的事件
- 如何在shopify中使用谷歌分析来跟踪未绑定的链接
- 当我们点击链接时,谷歌搜索会动态
- 谷歌地图 API - 指向地点详细信息页面的链接
- 给定带有位置“loc”的链接,搜索使用谷歌地图API从标记中提取纬度和经度
- 谷歌地图路线链接仅在iOS中显示路线预览
- 谷歌地图 点击链接时平移到中心
- 谷歌地图点击链接/选项卡更改标记位置地图
- 谷歌地图api通过点击信息窗口内的链接打开新的信息窗口
- 谷歌图表中的链接视图
- Rails4涡轮链接和谷歌分析
- 谷歌图书预览没有在安全的https上显示图书图像链接,但在http上工作.在Google Chrome和Touch浏览器
- AngularJS-谷歌Chrome扩展中的动态链接断开
- 谷歌表单:在文档的电子邮件中包含超链接'谷歌硬盘的位置