【问题标题】:Morris chart not showing the last xkey value with MVCMorris 图表未显示 MVC 的最后一个 xkey 值
【发布时间】:2016-07-18 17:38:05
【问题描述】:

我的莫里斯图没有显示最后一个 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
    });

【问题讨论】:

    标签: javascript asp.net-mvc morris.js


    【解决方案1】:

    这也发生在我身上。

    我不确定 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); },
        ...
    });
    

    【讨论】:

    • 您好,谢谢!很好的解决方案,我刚刚更改了行 var formattedDay = day + '-' + (month
    • 太棒了。很高兴我能帮上忙!
    • gridTextSize 为我做了!
    【解决方案2】:

    这是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/

    【讨论】:

    • 您好,谢谢!我已经检查过 adriennetacke 的答案是否正确,因为我更喜欢日期短于您在解决方案中提出的角度。在仪表板的设计和平衡方面,我认为它也更好。还是谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    相关资源
    最近更新 更多