【问题标题】:Google Charts API CalendarChartGoogle Charts API CalendarChart
【发布时间】:2020-03-31 13:34:47
【问题描述】:

我正在使用 Google Charts API 进行数据可视化。我想在图表上检索所选日期的日期。

我检索到的内容很奇怪。我在图表上使用了文档推荐的 selectHandler。

  function selectHandler() {

    console.log(chart.getSelection()[0]);

  }

  google.visualization.events.addListener(chart, 'select', selectHandler);

点击以下日期后,我正在控制台上读取以下输出:

22/04/2020 : 1587513600000

19/06/2020 : 1592524800000

这是什么日期格式? 谁能帮我?

【问题讨论】:

    标签: charts google-api google-visualization google-calendar-api


    【解决方案1】:

    这是表示为自 Unix 纪元以来的毫秒数的日期。
    它与 getTime() method 在日期对象上返回的值相同。

    您可以将从选择中收到的值传递给日期构造函数以获取日期...

    var selection = chart.getSelection();
    if (selection.length > 0) {
      var selectedDate = new Date(selection[0].date);
    }
    

    请参阅以下工作 sn-p...

    google.charts.load('current', {
      packages: ['calendar']
    }).then(function () {
      var dataTable = new google.visualization.DataTable();
      dataTable.addColumn({ type: 'date', id: 'Date' });
      dataTable.addColumn({ type: 'number', id: 'Won/Loss' });
      dataTable.addRows([
        [ new Date(2012, 3, 13), 37032 ],
        [ new Date(2012, 3, 14), 38024 ],
        [ new Date(2012, 3, 15), 38024 ],
        [ new Date(2012, 3, 16), 38108 ],
        [ new Date(2012, 3, 17), 38229 ],
        [ new Date(2013, 9, 4), 38177 ],
        [ new Date(2013, 9, 5), 38705 ],
        [ new Date(2013, 9, 12), 38210 ],
        [ new Date(2013, 9, 13), 38029 ],
        [ new Date(2013, 9, 19), 38823 ],
        [ new Date(2013, 9, 23), 38345 ],
        [ new Date(2013, 9, 24), 38436 ],
        [ new Date(2013, 9, 30), 38447 ]
      ]);
    
      var chart = new google.visualization.Calendar(document.getElementById('calendar_basic'));
    
      var options = {
       title: "Red Sox Attendance",
       height: 350,
      };
    
      function selectHandler() {
        var selection = chart.getSelection();
        if (selection.length > 0) {
          var selectedDate = new Date(selection[0].date);
          console.log(selectedDate);
        }
      }
    
      google.visualization.events.addListener(chart, 'select', selectHandler);
    
      chart.draw(dataTable, options);
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="calendar_basic"></div>

    注意:您应该在访问内容之前检查选择的长度。
    取消选择日期时也会调用 select 事件。
    在这种情况下,选择数组将为空......

    【讨论】:

    • 谢谢!我已经启动并运行了..保重!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多