2016-05-22 5 views
2

私は、mysqlからデータを取得するGoogleのチャートを使って簡単な折れ線グラフを作成しようとしていました。私のjsonは動作しますが、完全なコーディングで呼び出すと、 "c.getTimezoneOffsetは関数ではありません"と表示されます。ここでなぜ "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> 
+0

自分で 'c.getTimezoneOffset'を呼び出していますか?' c'はPHPスクリプトから注入したjsonオブジェクトか、エラーはgoogle libから来ていますか? – michaJlS

+0

エラー – Pro

答えて

0

問題がtime(...)への呼び出しであるように見えます。代わりに新しいDateオブジェクト(例:Date(...))をインスタンス化し、すべてが期待どおりに機能するように見えます。

Example Codepen here

私は、AJAX呼び出しをスキップしてちょうど直接あなたの例JSONを使用するようにdrawChart()を修正。 latest version of Google Charts APIを使用するように呼び出しを更新しました。

関連する問題