Black Lives Matter. Please consider donating to Black Girls Code today.

Zoom Events in JavaScript

How to bind callback functions to zoom events in D3.js-based JavaScript charts.


Building AI apps or dashboards with Plotly.js? Chart Studio enables 1-click export, editing and sharing of Plotly.js charts. If building a full-stack app with Plotly.js, you might save time by building with Dash instead. Schedule a Dash Enterprise demo.


New to Plotly?

Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

var graphDiv = document.getElementById('myDiv');

var d3 = Plotly.d3,
    N = 40,
    x = d3.range(N),
    y = d3.range(N).map( d3.random.normal() ),
    data = [ { x:x, y:y } ];
    layout = { title:'Click-drag to zoom' };

Plotly.newPlot(graphDiv, data, layout);

graphDiv.on('plotly_relayout',
    function(eventdata){  
        alert( 'ZOOM!' + '\n\n' +
            'Event data:' + '\n' + 
             JSON.stringify(eventdata) + '\n\n' +
            'x-axis start:' + eventdata['xaxis.range[0]'] + '\n' +
            'x-axis end:' + eventdata['xaxis.range[1]'] );
    });