Morris 图表未显示带有 MVC 的最后一个 xkey 值

Morris chart not showing the last xkey value with MVC

我的 Morris Chart 没有显示最后一个 xkey 值:

知道为什么吗?

我的数据是:

[{"Date":"2016-07-17","Average":0.0},{"Date":"2016-07-16","Average":0.0},{"Date":"2016-07-15","Average":4.125},{"Date":"2016-07-14","Average":0.0},{"Date":"2016-07-13","Average":0.0},{"Date":"2016-07-12","Average":0.0},{"Date":"2016-07-11","Average":0.0}]

观点:

<script>
    var surveyLastDaysChartData = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.SurveyLastDaysChartData));
</script>

 <div class="col-lg-6">
      <div class="card-box">
          <h4 class="header-title m-t-0">Média dos últimos 7 dias</h4>
          <div id="dsb-survey-last-days-chart" style="height: 217px;"></div>
      </div>
 </div><!-- end col -->

构建它的脚本:

var _surveyLastDaysChartId = "dsb-survey-last-days-chart";

Morris.Line({
        // ID of the element in which to draw the chart.
        element: _surveyLastDaysChartId,
        // Chart data records -- each entry in this array corresponds to a point on the chart.
        data: surveyLastDaysChartData,
        // The name of the data record attribute that contains x-values.
        xkey: 'Date',
        // A list of names of data record attributes that contain y-values.
        ykeys: ['Average'],
        // Labels for the ykeys -- will be displayed when you hover over the chart.
        labels: ['Média'],
        resize: true,
        hideHover: 'auto',
        ymax: 5
    });

当标签太长时,这是 Morris.js 的默认行为。您可以使用 xLabelAngle、is 和与水平方向的角度来绘制 x 轴标签:

Morris.Line({
        // ID of the element in which to draw the chart.
        element: _surveyLastDaysChartId,
        // Chart data records -- each entry in this array corresponds to a point on the chart.
        data: surveyLastDaysChartData,
        // The name of the data record attribute that contains x-values.
        xkey: 'Date',
        // A list of names of data record attributes that contain y-values.
        ykeys: ['Average'],
        // Labels for the ykeys -- will be displayed when you hover over the chart.
        labels: ['Média'],
        resize: true,
        hideHover: 'auto',
        xLabelAngle: 60, //<-- add this
        ymax: 5
    });

演示: https://jsfiddle.net/iRbouh/hpq42x7b/

我也遇到过这种情况。

我不确定 Morris 是如何计算它的元素的,但有时,当它超过宽度时,它会切断 x 轴上的值。

我能够修复它的方法(尽管它是一种 hack)是使用他们的 gridTextSize 选项并将其更改为较小的字体大小。

示例:

Morris.Line({
    ...
    gridTextSize: 10,
    ...
});

另一种选择,如果您的应用允许您缩短日期,它会使用他们的 xLabelFormat 选项将您的日期解析为更小的格式。

示例:

var display_date = function(d) {
    var month = d.getMonth() + 1,
        day   = d.getDate();

    var formattedDay = month + '-' + day

    return formattedDay; // Return "M-DD" format for date
}

Morris.Line({
    ...
    xLabelFormat: function(x) { return display_date(x); },
    ...
});