php - 谷歌图表查询mysql
问题描述
我一直在尝试使用 json 在 php 和 javascript 之间传输信息,但不起作用。查询没问题,我检查一下名为“dia”的查询结果是一个日期,“importe”是一个数字(浮点数)
任何想法 ?谢谢您的帮助
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi" > </script>
</head>
<body>
<div id="line_chart" style="width: 100%; height: 500px"></div>
<?php
$consulta = "SELECT fecha as dia,SUM(importe) as importe FROM tfg.reservas WHERE fecha < '2019-05-31' AND fecha > '2019-05-20' GROUP BY DAY(fecha) asc;";
$json = array();
$resultado = mysqli_query($conexion,$consulta);
if ($resultado->num_rows > 0) {
while($fila = mysqli_fetch_array($resultado)) {
$dataRow = array(
$fila['dia'],
$fila['importe'],
);
array_push($json, $dataRow);
}
}
$jsonstring = json_encode($json);
?>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart()
{
var data = google.visualization.arrayToDataTable([
[{type: 'date', label: 'dia'}, {type: 'number', label: 'importe'}]
]);
data.addRows(<?= $jsonstring ?>);
var options = {
title:'Sensors Data',
legend:{position:'bottom'},
chartArea:{width:'95%', height:'65%'}
};
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
chart.draw(data, options);
}
</script>
</body>
</html>
解决方案
推荐阅读
- python - 如何在 Python 中从 .secrets 文件中调用凭证
- azure - 通过 Proxy .net core 连接 Azure 存储
- powershell - 使用 powershell 脚本为所有用户安装字体作为快捷方式
- arrays - 选择器“indexPathForRow:inSection:inSection:”没有已知的类方法
- html - 将文本宽度优化为弹性盒装卡片
- node.js - Node.js 堆栈大小静默崩溃
- docker - 通过特定端口连接到 docker tomcat 容器
- node.js - 无法使用 AWS EC2 实例上的端口 25 从 nodejs nodemailer 发送 gmail 地址的邮件
- docker - 对象引用未设置为行 app.UseDiscoveryClient() 处的对象实例;在 .NET Core API 的 Startup.cs 文件中
- java - Java Bank Account 类,用于跟踪特定时间范围内发生的交易