2014-06-11 99 views
2

例如,我想將我的x標籤顯示爲「2014年1月」,而不是「2014-01」。morris.js - 月份名稱(2014年1月)而不是2014-01

我的數據的JSON看起來是這樣的:

[ 
    { 
    "product1": 2, 
    "product2": 0, 
    "period": "2013-05" 
    }, 
... 
+1

有誰知道爲什麼** xLabelFormat **和** ** DATEFORMAT不能在**柱狀圖中可以使用**?以上代碼不起作用... – Heart1010

+1

xLabelFormat或dateFormat既不適用於BAR圖表,也不適用於LINE(至少現在)。這裏有一個關於它的GitHub線程:https://github.com/morrisjs/morris.js/issues/494 – ammills01

回答

10

OP找到了解決辦法使用xLabelFormatdateFormat

Morris.Line({ 
    element: 'morris-line-chart', 
    data: <?php echo $morris_line_json; ?>, 
    xkey: 'period', 
    ykeys: ['product1', 'product2'], 
    labels: ['Product 1', 'Product 2'], 
    hideHover: 'auto', 
    xLabelAngle: 70, 
    xLabelFormat: function (x) { 
     var IndexToMonth = [ "Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez" ]; 
     var month = IndexToMonth[ x.getMonth() ]; 
     var year = x.getFullYear(); 
     return year + ' ' + month; 
    }, 
    dateFormat: function (x) { 
     var IndexToMonth = [ "Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez" ]; 
     var month = IndexToMonth[ new Date(x).getMonth() ]; 
     var year = new Date(x).getFullYear(); 
     return year + ' ' + month; 
    }, 
    resize: true 
}); 
+0

將'return year +''+ month;'改爲'return month +''+ year;'to得到'2013年3月'格式。感謝您發佈答案。 – sg552

0

條形圖的情況下,要小心,因爲對象在xLabelFormat的參數中不是日期!

xLabelFormat: function (x) { 
    x.src; // this is all the data 
    x.src.date; // this is probably what you are looking for 
       // depends how you provided it, but it's probably a string and not a Date object 
} 

測試了0.5.1

相關問題