2017-03-01 25 views
1

我想使用amCharts顯示堆積面積圖,其他任何工作都很好,但日期不正確。amCharts將unixtime轉換爲可讀的日期

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

屬性命名爲日期上以上的數據包不能被轉換爲可讀的日期,如「DD/MM/YYYY」

最後,圖表必須顯示所選月份的30天。
這是我CodePen
CodePen Stacked Area Chart

+0

它看起來像amCharts期望一個實際的Javascript日期對象。嘗試將unix時間戳更改爲日期:http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – stderr

+0

@stderr - Unix時間戳受支持,但它們需要在毫秒 – xorspark

回答

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'}] 
    // ... 
} 

要在圖表上的負載自動變焦,您可以類似於一個rendered事件添加到AmCharts網站上的演示如何做,並調用任何縮放方法,例如:

"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

+0

謝謝@xorspark,它的工作 –

相關問題