Google chart redraw/scale on window resize

前端 未结 9 1324
甜味超标
甜味超标 2020-12-07 19:50

How do I redraw/rescale a google linechart on window resize?

9条回答
  •  盖世英雄少女心
    2020-12-07 20:37

    I personally prefer the following approach, if You can live with using addEventListener, and don't mind lack of support for IE < 9.

    var windowResizeTimer;
    window.addEventListener('resize', function(e){
        clearTimeout(windowResizeTimer);
        windowResizeTimer = setTimeout(function(){
            chart.draw(data, options);
        }, 750);
    });
    

    Note the use of the setTimeout() and clearTimeout() functions and the added delay of 750 milliseconds, which makes this slightly less intensive when multiple resize events fire in quick succession (which is often the case for browsers on desktop when resizing using a mouse).

提交回复
热议问题