Morris.Line js 不能将几年更改为几个月



我正试图使用Morris.js源代码创建一个折线图,但我希望在图表中输入月份,而不是年份。现在的问题是,当我更改years字段时,图表不再工作。我在下面添加了两个示例,第一个是工作年版本,第二个是非工作月版本。

如果有人能看到我哪里出了问题,或者我错过了什么,那将是非常有帮助的。

提前谢谢。

图表年份

new Morris.Line({
// ID of the element in which to draw the chart.
element: 'monthlyReport',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: [{
year: '2008',
value: 20
},
{
year: '2009',
value: 10
},
{
year: '2010',
value: 5
},
{
year: '2011',
value: 5
},
{
year: '2012',
value: 20
}
],
// The name of the data record attribute that contains x-values.
xkey: 'year',
// A list of names of data record attributes that contain y-values.
ykeys: ['value'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value']
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="monthlyReport" style="height: 500px;width:50%;margin-left:25%;margin-bottom:100px;"></div>

图表月份

new Morris.Line({
// ID of the element in which to draw the chart.
element: 'monthlyReport',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: [{
month: 'Jan',
value: 20
},
{
month: 'Feb',
value: 10
},
{
month: 'Mar',
value: 5
},
{
month: 'Apr',
value: 5
},
{
month: 'May',
value: 20
}
],
// The name of the data record attribute that contains x-values.
xkey: 'month',
// A list of names of data record attributes that contain y-values.
ykeys: ['value'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Value']
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="monthlyReport" style="height: 500px;width:50%;margin-left:25%;margin-bottom:100px;"></div>

您可以尝试将选项parseTime设置为false

这是因为键值必须是数字。对于year,您使用的是数值(2008-2012(,但在第二个示例中,您为每个month使用一个字符串。用数字替换键值,它就会起作用。

使用以下指南配置Morris.line以显示您的月份名称:https://morrisjs.github.io/morris.js/lines.html

最新更新