1

我正在使用 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'但没有奏效。

4

1 回答 1

6

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

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

data: [{
    x: 10,
    y: 20
}, {
    x: 15,
    y: 10
}]

并删除标签变量。

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

我还认为性能提升是一个不错的奖励。

于 2020-08-05T20:20:35.903 回答