谷歌图表显示错误的月份 [英] Google Charts displays wrong month
问题描述
我有一个显示正确行的谷歌折线图;但日期的注释完全关闭了一个月。 json数据有正确的日期;但不知何故,谷歌图表转换它:
有人知道为什么会发生这种情况吗?
使用下面的日期构造函数时,没有错误,显示正确的月份
,月份基于零 ...
日期(年,月,日,小时,分钟,秒,mill)
请参阅以下代码片段...
console.log(new Date(2016,0,1)); //< - Janconsole.log(new Date(2016,1,1)); //< - Febconsole.log(new Date(2016,11,1)); // < - Dec
下面是另一个演示片段使用json与谷歌图表...
google.charts.load('当前',{callback:function(){var data = new google.visualization.DataTable({cols:[{label:Date,type:date}],rows:[ {c:[{v:Date(2016,0,28,15,0,0)}]},{c:[{v:Date(2016,1,28 ,15,0,0)}]},{c:[{v:Date(2016,2,28,15,0,0)}]},{c:[ v:Date(2016,3,28,15,0,0)}]},{c:[{v:Date(2016,4,28,15,0,0) }},{c:[{v:Date(2016,5,28,15,0,0)}]},{c:[{v:Date 2016,6,28,15,0,0)}]},{c:[{v:Date(2016,7,28,15,0,0)}]},{ c:[{v:Date(2016,8,28,15,0,0)}]},{c:[{v:Date(2016 ,9,28,15,0,0)}]},{c:[{v:Date(2016,10,28,15,0,0)}]},{c :[{v:Date(2016,11,28,15,0,0)}]},]}); var chart = new google.visualization.Table(document.getElementById('chart_div')); chart.draw(数据); },packages:['table']});
b
$ b
<?php
$ date1 = new DateTime(); $($)$ date2 =Date(。date_format($ date1,'Y')。,。((int)date_format($ date1,'m') - 1)。,.date_format($ date1,'d')。,.date_format($ date1,'H')。,.date_format($ date1,'i')。,.date_format($ date1,'s')。 );
echo $ date2;
?>
这里是一个PHP小提琴来测试上面的代码片段......
I've got a google line chart which shows the correct line; but the annotation of the Date is off by one month exactly. The json data has the correct date; but somehow google charts transforms it:
Anybody an idea why this happens?
no mistake, the correct month is being displayed
when using the following date constructor, the months are zero based...
Date(year, month, day, hour, min, sec, mill)
see following snippet...
console.log(new Date(2016, 0, 1)); // <-- Jan
console.log(new Date(2016, 1, 1)); // <-- Feb
console.log(new Date(2016, 11, 1)); // <-- Dec
following is another snippet to demonstrate using json with google charts...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable({
"cols": [
{"label": "Date", "type": "date"}
],
"rows": [
{"c": [{"v": "Date(2016,0,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,1,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,2,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,3,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,4,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,5,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,6,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,7,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,8,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,9,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,10,28,15,0,0)"}]},
{"c": [{"v": "Date(2016,11,28,15,0,0)"}]},
]
});
var chart = new google.visualization.Table(document.getElementById('chart_div'));
chart.draw(data);
},
packages:['table']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
following is a php snippet to create the json date using the above constructor
<?php
$date1 = new DateTime();
$date2 = "Date(".date_format($date1, 'Y').", ".((int) date_format($date1, 'm') - 1).", ".date_format($date1, 'd').", ".date_format($date1, 'H').", ".date_format($date1, 'i').", ".date_format($date1, 's').")";
echo $date2;
?>
here is a php fiddle to test the above snippet...
这篇关于谷歌图表显示错误的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!