我使用chart.js模块来绘制一些数据,并使用chartjs-plugin-zoom来实现缩放和平移,但是,尽管缩放可以工作,但是x轴上的标签不会因为任何原因而改变。我见过类似的问题,但它们都是处理时间序列数据的,因此建议没有帮助。
这是缩小后的图:

这是放大后的图像:

需要注意的关键是y轴上的标签是如何改变的,而x轴上的标签并没有改变。图表的相关配置变量如下:
const config3 = {
type: 'line',
data: {
labels: [I ran out of chars but this is just a very long list of numbers in this format: 1,2,3,4,5],
datasets: [
{
label: "",
backgroundColor: '#'+Math.floor(Math.random()*16777215).toString(16),
borderColor: '#0071BC',
data: [I ran out of chars but this is just a very long list of numbers in this format: 1,2,3,4,5],
fill: false,
borderWidth: 1,
},
],
},
options: {
responsive: true,
title: {
display: true,
text: 'Peak: -1.2188'
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Frequency (Hz)'
},
ticks:{
autoSkip: true,
maxTicksLimit: 20
},
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Amplitude'
}
}],
},
plugins:{
zoom: {
pan: {
enabled: true,
mode: 'xy',
speed: 20,
threshold: 10,
},
zoom: {
enabled: true,
drag: false,
mode: "xy",
speed: 0.1,
// sensitivity: 0.1,
}
}
},
elements: {
point:{
radius: 0
}
}
}
};如果需要,我可以提供更多代码,但我认为错误可能包含在配置中。我尝试将zoom.mode更改为'x',但这不起作用。
发布于 2020-08-06 04:20:36
以防其他人出现这种情况,我想出了一个非常不直观的解决方案。
第一个问题是在chart.js中处理标签的方式,因为它们被视为类别,而不是我所认为的x数据。因此,首先您必须以这种格式将数据作为坐标传递:(如本文档所示:https://www.chartjs.org/docs/latest/charts/line.html)
data: [{
x: 10,
y: 20
}, {
x: 15,
y: 10
}]并删除labels变量。
但是,无论文档如何说明,这都不适用于折线图。要解决这个问题,您可以设置:type: 'scatter',并在数据集内写入showLine: true,这将生成一个线状图,其中自动生成x标签,并且缩放将完美地工作。
我也认为有一个性能提升,这是一个很好的奖励。
https://stackoverflow.com/questions/63271287
复制相似问题