You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chartjs-plugin-zoom drag选项失效:与streaming插件联用异常

解决Chart.js实时流与拖拽缩放冲突问题

问题现象

同时使用Chart.js、chartjs-plugin-zoom和chartjs-plugin-streaming插件时,拖拽选框可显示选中区域,但无法完成预期缩放:

  • 预期:选中区域后仅显示该区域,x/y轴适配调整,实时滚动停止
  • 实际:选中区域随图表自动更新移动,x轴未适配缩放范围,非选中区域数据被隐藏

问题根源

chartjs-plugin-streaming的实时刷新机制会自动调整x轴范围以保持最新数据可见,这会覆盖chartjs-plugin-zoom的缩放结果,导致缩放后的视图被实时更新“拉回”。

解决方案

通过以下配置调整,在缩放时暂停实时更新并锁定轴范围:

1. 添加缩放回调,切换轴类型并锁定范围

在zoom插件配置中添加缩放完成回调,将x轴从实时类型切换为普通时间轴,禁用自动滚动并锁定缩放后的范围。

2. 控制实时数据推送

在onRefresh函数中添加判断,仅在未缩放状态下更新数据。

修改后的完整配置

// 全局变量标记是否处于缩放状态
let isZoomed = false;

const options = {
    maintainAspectRatio: false,
    interaction:{
        intersect: false 
    },
    plugins: {
        zoom: {
            pan: {
                enabled: true,
                mode: 'xy',
                modifierKey: 'ctrl'
            },
            zoom: {
                mode: 'xy',
                wheel: {
                    enabled: true
                },
                drag: {
                    enabled: true,
                    backgroundColor: 'rgba(225,225,225,0.5)'
                },
                // 缩放完成时触发的回调
                onZoomComplete: ({ chart }) => {
                    isZoomed = true;
                    // 将x轴从实时类型切换为普通时间轴
                    chart.scales.x.options.type = 'time';
                    // 移除实时配置,避免自动刷新调整轴范围
                    delete chart.scales.x.options.realtime;
                    // 锁定x轴当前缩放后的范围
                    chart.scales.x.options.min = chart.scales.x.min;
                    chart.scales.x.options.max = chart.scales.x.max;
                    chart.update();
                }
            }
        },
        annotation: {
            annotations: {
                box1: {
                    type: 'box',
                    yMin: 0.70,
                    yMax: 0.90,
                    backgroundColor: 'rgba(204, 204, 255, 0.25)',
                    borderColor: 'rgba(204, 204, 255, 0.25)'
                },
                box2: {
                    type: 'box',
                    yMin: 0.30,
                    yMax: 0.50,
                    backgroundColor: 'rgba(1, 121, 111, 0.25)',
                    borderColor: 'rgba(1, 121, 111, 0.25)'
                },
                line: {
                    type: 'line',
                    scaleID: 'x',
                    value: Date.now(),
                    borderColor: 'black',
                    borderWidth: 3,
                }
            }
        }
    },
    scales: {
        x: {
            type: 'realtime',
            realtime: {
                refresh : 5000, // 5 seconds
                ttl: 172800000, //2 days
                duration: 7200000, //2 hours
                delay: -3600000, // - 1 hour
                frameRate: 0.20,
                onRefresh: chart => {
                    // 仅在未缩放状态下更新数据和时间线
                    if (!isZoomed) {
                        const line = chart.options.plugins.annotation.annotations.line;
                        chart.data.datasets.forEach(dataset => {
                            if(dataset !== chart.data.datasets[1]){
                                dataset.data.push({
                                    x: Date.now(),
                                    y: Math.random()
                                });
                            }
                        });
                        line.value = Date.now();
                    }
                }
            }
        },
        y:{}
    }
}

补充说明

  • 如果需要恢复实时滚动,可添加按钮触发重置逻辑:将x轴类型改回realtime,恢复原配置并设置isZoomed = false
  • 确保已正确引入chartjs-adapter-date-fns或其他时间适配器,否则time类型轴无法正常工作

内容的提问来源于stack exchange,提问作者Roberto Marchesi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 02:27:37