为什么会出现 "c.getTimezoneOffset 不是一个函数" 的错误?

6

我试图使用Google图表制作一个简单的折线图,从mysql中检索数据。我的json已经可以工作了,但是当我尝试在完整的代码中调用它时,它会显示“c.getTimezoneOffset不是函数”。

这是我的json php代码

<?php
include("config.php");

$query = "SELECT * FROM reading";
$sql = mysqli_query($con, $query);

if (!$sql) {
die("Error running $sql: " . mysql_error());
}

$results = array(
'cols' => array (
array('label' => 'Date', 'type' => 'datetime'),
array('label' => 'API', 'type' => 'number')
),
'rows' => array()
);
while($row = mysqli_fetch_assoc($sql)) {

$year=date("Y", strtotime($row['time']));
$month=date("m", strtotime($row['time']));
$day=date("d", strtotime($row['time']));
$hour=date("H", strtotime($row['time']));
$minute=date("i", strtotime($row['time']));
$second=date("s", strtotime($row['time']));

$results['rows'][] = array('c' => array(
array('v' => "time($year, $month, $day, $hour, $minute, $second)"),
array('v' => $row['api'])
));
}
$json = json_encode($results, JSON_NUMERIC_CHECK);
echo $json;
?>

这是我的JSON数据:
{"cols":[{"label":"Date","type":"datetime"},
"label":"API","type":"number"}],
"rows":[{"c":[{"v":"time(2016, 05, 22, 12, 24, 26)"},{"v":26}]},
{"c":[{"v":"time(2016, 05, 22, 12, 24, 41)"},{"v":26}]},
{"c":[{"v":"time(2016, 05, 22, 12, 24, 56)"},{"v":27}]}]}

这是我的HTML代码

<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {
$.ajax({
url: 'getData.php',
dataType: 'json',
success: function (jsonData) {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
});
}

</script>
</head>

<body>
<div id="chart_div"></div>
</body>
</html>

你是自己调用 c.getTimezoneOffset 的吗?而且 c 是从 PHP 脚本注入的 JSON 对象,还是这个错误来自于谷歌库? - michaJlS
2个回答

0

看起来问题出在对 time(...) 的调用上;我将它们修改为实例化新的 Date 对象(例如 Date(...)),现在一切都像预期的那样工作。

这里是示例 Codepen

我修改了 drawChart() 以跳过 ajax 调用,并直接使用您的示例 JSON;我还更新了调用以使用 Google Charts API 的最新版本


0

我遇到了同样的错误,不得不修改我的JSON以生成一个看起来像这样的字符串:

"c":[{"v":"Date(2018,9,16)","f":null}

这意味着我的代码必须解析出年、月和日,以便将它们拼回去。我正在使用JSP,但希望这能帮助到某些人:

createDateObj.put("v", "Date("+ createYear + "," + createMonth + "," + createDay + ")");

不要简单地使用从mySQL返回的日期,它看起来像这样:

createDateObj.put("v", createDate);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接