【发布时间】:2017-09-16 16:29:21
【问题描述】:
我正在使用以下代码使用图表 js 插件生成折线图,其中我面临 x 轴值重复两次的问题。
function newDate(days) {
return moment().add(days, 'd').format('MM/DD');
}
var painChartContext = document.getElementById('pain_chart');
var painChart = new Chart(painChartContext, {
type: 'line',
data: {
labels: [newDate(-7), newDate(-6), newDate(-5), newDate(-4), newDate(-3), newDate(-2), newDate(-1)],
datasets: [{
label: "Pain",
data: [8, 9, 7, 3, 10, 3, 4],
fill: false,
borderColor: '#b71705',
spanGaps: true
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
time: {
displayFormats: {
'millisecond': 'MM/DD',
'second': 'MM/DD',
'minute': 'MM/DD',
'hour': 'MM/DD',
'day': 'MM/DD',
'week': 'MM/DD',
'month': 'MM/DD',
'quarter': 'MM/DD',
'year': 'MM/DD',
},
min: '04/13',
max: '04/19'
}
}],
},
}
});
正如我上面提到的,这给了我下面的图表,其中 x 轴日期值重复了两次。
同样在我的控制台中,我收到了关于时刻 js 的警告消息;
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
我认为是因为这行代码;
function newDate(days) {
return moment().add(days, 'd').format('MM/DD');
}
请任何人为此提供解决方案,谢谢。
【问题讨论】:
标签: javascript momentjs chart.js