Sometimes, we want to redraw or scale Google chart on window resize with JavaScript.
In this article, we’ll look at how to redraw or scale Google chart on window resize with JavaScript.
How to redraw or scale Google chart on window resize with JavaScript?
To redraw or scale Google chart on window resize with JavaScript, we can redraw the chart when we resize the screen.
For instance, we write
const resize = () => {
const chart = new google.visualization.LineChart(
document.getElementById("chart_div")
);
chart.draw(data, options);
};
window.onload = resize;
window.onresize = resize;
to create the resize function.
In it, we create a LineChart object with the container element of the chart.
And then we call draw to draw the line chart.
Then we set window.onload and window.onresize to resize to call it when the page loads and when we resize the page respectively.
Conclusion
To redraw or scale Google chart on window resize with JavaScript, we can redraw the chart when we resize the screen.