[英]Why doesn't Google Charts work with this date format?
I am using PHP and SQL to generate charts using Google Charts. 我正在使用PHP和SQL使用Google Charts生成图表。
I have a particular chart that I've started but Google Charts doesn't seem to like the date format that is returned by SQL. 我已经启动了一个特定的图表,但是Google图表似乎不喜欢SQL返回的日期格式。
Code 码
<?php
include('core/connection.php');
$stid = oci_parse($conn, "
SELECT EntryDate AS \"Date\", COUNT(OrderNo) AS \"Orders\"
FROM Orders
WHERE EntryDate > '01-MAY-2014'
AND CustomerNo = 1
GROUP BY EntryDate
");
oci_execute($stid);
$table = array();
$table['cols'] = array(
array('label' => 'Date', 'type' => 'date'),
array('label' => 'Orders', 'type' => 'number')
);
$rows = array();
while($row = oci_fetch_assoc($stid)) {
$temp = array();
$date = new \DateTime($row['Date']);
$dateStringForGoogleCharts = $date->format("Y/m/d H:i");
$temp[] = array('v' => (int) $dateStringForGoogleCharts);
$temp[] = array('v' => (int) $row['Orders']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
oci_close($conn);
?>
<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() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
width: 1600,
height: 800
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
Please can someone provide an example using this code on what I must do to get Google Charts to accept this date format? 请有人提供使用此代码的示例,说明我必须执行哪些操作才能使Google图表接受此日期格式?
I have updated the code to generate the date in the format that Google Charts expects (thanks for the answer), but now when visiting the page I see an error message in a red box saying undefined is not a function
. 我已经更新了代码,以Google图表期望的格式(感谢您的回答)生成日期,但是现在访问页面时,我在红色框中看到一条错误消息,指出
undefined is not a function
。
I do this successfully with the following JS: 我使用以下JS成功做到了这一点:
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Date');
data.addColumn('number', 'Value');
data.addRows(<?=$json?>);
var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
chart.draw(data, {displayAnnotations: true, colors: ['blue', 'red']});
}
And, server-side, after getting the data from the database in PHP I use DateTime to format the string for the JSON, like this: 而且,在服务器端,用PHP从数据库中获取数据后,我使用DateTime格式化JSON的字符串,如下所示:
$date = new \DateTime($row['date']);
$dateStringForGoogleCharts = $date->format("Y/m/d H:i");
With the key bit being the format of "Y/m/d H:i" - that's how Google Charts like it. 关键是“ Y / m / d H:i”的格式-这就是Google图表喜欢的格式。
My advice is to convert the date format coming from your database to this format using DateTime . 我的建议是使用DateTime将来自数据库的日期格式转换为这种格式。 I seem to remember some trial and error getting to a format accepted by the API, but that format definately works.
我似乎还记得尝试并尝试过API接受的某种格式的错误,但是这种格式肯定可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.