amCharts将unixtime转换为可读日期

时间:2017-03-01 02:45:59

标签: angularjs parsing amcharts

我想使用amCharts显示堆积区域图表,其他任何工作都可以,但是日期不正确解析。

"dataProvider": [{
    "date": 1482192000,
    "cars": 1587,
    "motorcycles": 650,
    "bicycles": 121
}

上述数据包中名为 date 的属性无法转换为“DD / MM / YYYY”等可读日期

最后,图表必须显示所选月份的30天 这是我的 CodePen
CodePen Stacked Area Chart

1 个答案:

答案 0 :(得分:1)

您的数据和设置都不正确。这是一个错误列表以及如何修复它们

1)dataDateFormat用于解析字符串日期,而不是格式化它们。由于您使用的是unix时间戳,因此根本不需要此属性,因此您可以将其删除。

2)你的unix时间戳也必须以毫秒为单位才能生效。秒数会给你无效的时间。

3)您的数据必须按升序排序,才能正确呈现。您的数据目前处于混合状态。

至于你的其他问题:

要设置日期格式,您必须将categoryAxis中的dateFormats array设置为所需的格式字符串described here。对于DD / MM / YYYY:

"categoryAxis": {
  // other properties omitted:
  "dateFormats": [{period:'fff',format:'JJ:NN:SS'},
    {period:'ss',format:'JJ:NN:SS'},
    {period:'mm',format:'JJ:NN'},
    {period:'hh',format:'JJ:NN'},
    {period:'DD',format:'DD/MM/YYYY'}, //you may need to change the entries for 'WW' and 'MM' as well, depending on the amount of visible data
    {period:'WW',format:'MMM DD'},
    {period:'MM',format:'MMM'},
    {period:'YYYY',format:'YYYY'}]
  // ...
}

要自动缩放图表加载,您可以添加类似于AmCharts网站上演示的rendered事件,并调用任何缩放方法,例如:

"listeners": [{
  "event": "rendered",
  "method": function(e) {
    // different zoom methods can be used - zoomToIndexes, zoomToDates, zoomToCategoryValues
    e.chart.zoomToDates(new Date(2017, 1, 1), new Date(2017, 1, 15));
  }
}]

这是一个更新的codepen,其中包含上述所有修复程序here

相关问题