gpt4 book ai didi

php - 无法从 JSON 数据显示 Google Gauge

转载 作者:行者123 更新时间:2023-11-29 00:10:18 26 4
gpt4 key购买 nike

我正在尝试通过 JSON 编码从 MySQL 数据库中的数据中获取我网站上的一些谷歌图表。检查了几个论坛后,我的 json 编码数据看起来格式正确,但 google.visualization.DataTable 部分似乎没有做任何事情。

我的主要代码如下:

<script type="text/javascript" src="jsapi.js"></script>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript">google.load('visualization', '1', {'packages':['corechart', 'table', 'gauge']});</script>
<script type="text/javascript">

function dataSelect(option1) {

var option2 = "";

// create graphs

switch (option1){

case "logged":

option2 = "logged";

// Get Logged Graph Data

var jsonLoggedGauges = $.ajax({
url: "getLoggedGaugeData.php",
dataType: "json",
async: false
}).responseText;

//document.write(jsonLoggedGauges);
var gaugeData = new google.visualization.DataTable(jsonLoggedGauges);


// Draw Gauges

var gaugeChart = new google.visualization.PieChart(document.getElementById('gauge_div'));
var gaugeOptions = {width: 400, height: 120, redFrom: 90, redTo: 100, yellowFrom:75, yellowTo: 90, minorTicks: 5};
gaugeChart.draw(gaugeData, gaugeOptions);

break;
}}
</script>

我的getLoggedGaugeData.php如下:

<?php include 'dbconnect.php';

$result = $dbhandle->query("SELECT currentTemp, feelsLike, windSpeed, humidity, pressure FROM `logged` ORDER BY dateTime DESC LIMIT 1");
$table = array();
$table['cols'][] = array('id' => "", 'label' => "Label", pattern => "", 'type' => 'string');
$table['cols'][] = array('id' => "", 'label' => "Value", pattern => "", 'type' => 'number');

while ($nt = $result->fetch_assoc())
{
$table['rows'][]['c'] = array( array('v' => 'Current Temp'), array('v' => $nt['currentTemp']));
$table['rows'][]['c'] = array( array('v' => 'Feels Like'), array('v' => $nt['feelsLike']));
$table['rows'][]['c'] = array( array('v' => 'Wind Speed'), array('v' => $nt['windSpeed']));
$table['rows'][]['c'] = array( array('v' => 'Humidity'), array('v' => $nt['humidity']));
$table['rows'][]['c'] = array( array('v' => 'Pressure'), array('v' => $nt['pressure']));
}

$jsonTable = json_encode($table, JSON_NUMERIC_CHECK);
echo $jsonTable;
?>

如果我从 document.write(jsonLoggedGauges) 中删除//为了激活它,我按如下方式取回数据,看起来是正确的:

{"cols":[{"id":"","label":"Label","pattern":"","type":"string"},  {"id":"","label":"Value","pattern":"","type":"number"}],"rows":[{"c":[{"v":"Current Temp"},{"v":15.3}]},{"c":[{"v":"Feels Like"},{"v":13.5}]},{"c":[{"v":"Wind Speed"},{"v":5}]},{"c":[{"v":"Humidity"},{"v":68}]},{"c":[{"v":"Pressure"},{"v":1002.1}]}]}

任何建议将不胜感激,因为我完全不知道为什么它不起作用。

最佳答案

希望它对你有用:

PHP MySQL Google Chart JSON - Complete Example

Google一般会返回JSONP格式的数据

关于php - 无法从 JSON 数据显示 Google Gauge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25487297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com