Chartjs-plugin-zoom 插件不会改变 x 轴标签

Cra*_*min 1 javascript chart.js chartjs-plugin-zoom

我正在使用 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
                                }
                            }
                        }
                    };
Run Code Online (Sandbox Code Playgroud)

如果需要,我可以提供更多代码,但我想错误可能包含在配置中。我试图改变zoom.mode成为,'x'但没有奏效。

Cra*_*min 5

万一其他人出现这种情况,我想出了一个非常不直观的解决方案。

第一个问题是在 chart.js 中处理标签的方式,因为它们被视为类别而不是我认为的 x 数据。因此,首先您必须以这种格式将数据作为坐标传递:(如本文档所示:https ://www.chartjs.org/docs/latest/charts/line.html )

data: [{
    x: 10,
    y: 20
}, {
    x: 15,
    y: 10
}]
Run Code Online (Sandbox Code Playgroud)

并删除标签变量。

但是,尽管文档中有说明,但这不适用于折线图。为了解决这个问题,您可以设置:type: 'scatter'并在数据集内写入showLine: true 这将生成一个线图,其中自动生成 x 标签并且缩放将完美工作。

我还认为有性能提升,这是一个很好的奖励。