网站搜索

javascript - 如何在 ChartJS 中跳过 x 轴标签


我在这个话题上找到的唯一东西是这个,但它并不能真正帮助解决问题。

所以我有一张像这样的数据表:

如您所见,数据集中未设置的值被截断。这是因为这段代码将所有内容填充为undefined:

let dataSet: any = {
   label: user.userName,
   backgroundColor: 'rgba(0, 0, 0, 0)',
   borderColor: this.getRandomColor(),
   data: new Array(this.chart.data.labels.length).fill(undefined)
}

之后我用这段代码填充数据:

data.forEach((d)=>{
   let tmpDate: Date = new Date(d.date * 1000)
   let insertIndex: number = this.chart.data.labels.indexOf(this.getDateString(tmpDate))
   dataSet.data.splice(insertIndex, 1, d.data) // d.data is a number
})

this.chart.data.datasets.push(dataSet)
this.chart.update()

我希望它能够跳过未定义的值,并在图表上创建一条平滑的曲线。

答案#1

文档中的 spanGaps 选项应该是你正在寻找的。