chartjs / chartjs-plugin-zoom

Zoom and pan plugin for Chart.js
MIT License
586 stars 326 forks source link

Zoom go to Infinity on any event on chart #818

Open M-t-t-h opened 3 months ago

M-t-t-h commented 3 months ago

Hy,

I get a unexpected zoom on any event on chart. After analyse, the function getZoomLevel return 'Infinity'.

I tried many way to resolve it with set min/max, check datas, set X, set Y... not solutions.

here my simpliest JS :


/*********************************************************************/
/***************************** macDChart *****************************/
/*********************************************************************/
var ctx_chart = document.querySelector('#macd_chart_ADAUSD_OneHour').getContext('2d');
ctx_chart.canvas.width = 1000;
ctx_chart.canvas.height = 250;

var datas = [{x:'2024-05-05 22:00:00',y:0.000414969070996751},{x:'2024-05-05 23:00:00',y:0.00027615726125757}, ... },{x:'2024-05-06 04:00:00',y:-0.000250683582229627}];

var chart_ADAUSD_OneHour_macd = new Chart(ctx_chart, {
    type: 'line',
    data: {
        datasets: [
            {
                label: 'MACD',
                data: datas.filter(function (item) {
                    return item.y !== null;
                }), 
                borderColor: 'blue',
                backgroundColor: 'rgba(0, 0, 255, 0.2)',
                borderWidth: 0.5,
                pointRadius: 0,
                fill: false,
            }
        ]
    },
    options:
    {
   plugins:
        {
            zoom:
            {
                pan: { enabled: true, mode: 'x' },
                sync: {
                    enabled: true,
                    group: 'my-charts'
                },
                zoom: {
                    wheel: { enabled: true, modifierKey: 'ctrl' }, pinch: { enabled: true, modifierKey: 'ctrl' }, drag: { enabled: true, modifierKey: 'ctrl' }, mode: 'x',
                }
            }
        },
    },
    scales:
    {
        x: { type: 'time', distribution: 'series', time: { unit: 'day' }, },
        y: {
            type: 'linear',
            display: true, position: 'left',
            title: { display: true, text: 'Price'},
        }
    }
});

I attache you the image of the graph at load VS. on load : graph_at_load after any event : graph_after_event

Thanks in advance for your help.