Google Charts显示错误的月份

时间:2016-11-29 09:38:31

标签: javascript charts google-visualization

我有一个谷歌折线图,显示正确的行;但是日期的注释完全偏离了一个月。 json数据具有正确的日期;但不知何故谷歌图表转换它: chart with wrong date

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:9)

没错,正在显示正确的月份

使用以下日期构造函数时,月份为零 ...

Date(year, month, day, hour, min, sec, mill)

请参阅以下摘录...



console.log(new Date(2016,  0, 1)); // <-- Jan
console.log(new Date(2016,  1, 1)); // <-- Feb
console.log(new Date(2016, 11, 1)); // <-- Dec
&#13;
&#13;
&#13;

以下是另一个演示使用json和谷歌图表的片段......

&#13;
&#13;
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']
});
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;

以下是使用上述构造函数

创建json日期的php片段
<?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来测试上面的代码段...