與此相同的問題我自己奮鬥的時間比我應該有,我終於發現了Chartjs文檔中的印刷精美的http://www.chartjs.org/docs/#scales-time-units的unitStepSize參數需要在時間子選項中定義。
我正在做:
scales: {
xAxes: [{
type: "time",
time: {
unit: 'day',
displayFormats: {
day: 'MMM DD',
},
},
ticks: {
fontColor: "black",
fontSize: 12,
fontStyle: "normal",
fontFamily: "Segoe UI",
},
unitStepSize: 7,
}],
yAxes: yAxes
},
何時應該已經做:
scales: {
xAxes: [{
type: "time",
time: {
unit: 'day',
displayFormats: {
day: 'MMM DD',
},
unitStepSize: 7,
},
ticks: {
fontColor: "black",
fontSize: 12,
fontStyle: "normal",
fontFamily: "Segoe UI",
},
}],
yAxes: yAxes
},
在更廣的範圍內:(注意eleChart1是圖表的畫布DOM元素,而Y軸,數據集和ChartName是在示例之外構建的對象/變量。)
var Chart1 = new Chart(eleChart1, {
type: 'line',
lineWidth: 15,
data: {
datasets: datasets,
},
options: {
showPointLabels: true,
legend: {
display: true,
//position: "bottom"
},
title: {
display: true,
text: ChartName,
fontSize: 18,
fontStyle: "bold",
fontFamily: "Segoe UI"
},
scales: {
xAxes: [{
type: "time",
time: {
unit: 'day',
displayFormats: {
day: 'MMM DD',
},
unitStepSize: 7,
},
ticks: {
fontColor: "black",
fontSize: 12,
fontStyle: "normal",
fontFamily: "Segoe UI",
},
}],
yAxes: yAxes
},
}
});
底線:一旦我把時間對象的unitStepSize選項,它按預期工作。