morris.js - 月份名称(2014年1月)而不是2014-01

时间:2014-06-11 11:38:52

标签: morris.js

我想将我的x标签显示为" 2014年1月",而不是" 2014-01"。

我的数据的json看起来像这样:

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

2 个答案:

答案 0 :(得分: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
});

答案 1 :(得分:1)

如果是条形图,请注意,因为xLabelFormat参数中的对象不是Date!

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进行测试