gpt4 book ai didi

jquery - X 轴上的图表线 morris.js 格式日期

转载 作者:行者123 更新时间:2023-12-03 22:46:32 28 4
gpt4 key购买 nike

我使用 morris.js 折线图来显示不同的系列值。

enter image description here

我想格式化我的 X 轴,这样我就可以放置 3 个字符的星期几,而不是年份。

示例:

  • 2016-05-10 应为 THU-05-10

这是我的实际代码:

Morris.Line({
element: 'linechart',
data: data,
lineColors: ['#819C79', '#fc8710', '#FF6541', '#A4ADD3', '#766B56'],
xkey: 'period',
ykeys: ['park1','park2','park3','park4','park5'],
labels: ['PARK 1', ''PARK 2', ''PARK 3', ''PARK 4', ''PARK 5'],
xLabels: 'day',
xLabelAngle: 45,
resize: true
});

有什么线索吗?

最佳答案

添加xLabelFormat将日期转换为所需的格式。尝试以下代码片段中的 xLabelFormat 函数:

Morris.Line({
element: 'chart',
data: [
{ period: '2016-05-10', park1: 200, park2: 200, park3: 50, park4: 10, park5: 0 },
{ period: '2016-05-11', park1: 15, park2: 275, park3: 5, park4: 60, park5: 0 },
{ period: '2016-05-12', park1: 80, park2: 20, park3: 30, park4: 30, park5: 0 },
{ period: '2016-05-13', park1: 100, park2: 200, park3: 250, park4: 50, park5: 0 },
{ period: '2016-05-14', park1: 50, park2: 60, park3: 20, park4: 10, park5: 0 },
{ period: '2016-05-15', park1: 75, park2: 65, park3: 10, park4: 60, park5: 0 },
{ period: '2016-05-16', park1: 175, park2:95, park3: 110, park4: 30, park5: 0 },
{ period: '2016-05-17', park1: 150, park2:95, park3: 90, park4: 111, park5: 0 },
{ period: '2016-05-18', park1: 120, park2:95, park3: 60, park4: 47, park5: 0 },
{ period: '2016-05-19', park1: 60, park2:95, park3: 50, park4: 231, park5: 0 },
{ period: '2016-05-20', park1: 10, park2:95, park3: 100, park4: 80, park5: 0 }
],
lineColors: ['#819C79', '#fc8710', '#FF6541', '#A4ADD3', '#766B56'],
xkey: 'period',
ykeys: ['park1','park2','park3','park4','park5'],
labels: ['PARK 1', 'PARK 2', 'PARK 3', 'PARK 4', 'PARK 5'],
xLabels: 'day',
xLabelAngle: 45,
xLabelFormat: function (d) {
var weekdays = new Array(7);
weekdays[0] = "SUN";
weekdays[1] = "MON";
weekdays[2] = "TUE";
weekdays[3] = "WED";
weekdays[4] = "THU";
weekdays[5] = "FRI";
weekdays[6] = "SAT";

return weekdays[d.getDay()] + '-' +
("0" + (d.getMonth() + 1)).slice(-2) + '-' +
("0" + (d.getDate())).slice(-2);
},
resize: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/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>
<link href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css" rel="stylesheet"/>

<div id="chart"></div>

关于jquery - X 轴上的图表线 morris.js 格式日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com