Skip to main content

How to Save D3 Chart as an Image

D3.js is a powerful JavaScript library for data visualization. It enables you to create sophisticated charts and graphs from data sets. Most developers want to export the charts as images, which can be used in reports or presentations.

To save a D3 chart as an image, we need an external library. The SaveSvgAsPng library is a handy tool that can help save D3 charts as images. It relies on JavaScript promises, so any browsers that don’t natively support the standard Promise object will need to have a polyfill.

d3.select("#saveAsImage").on('click', function(){
    saveSvgAsPng(document.getElementsByTagName("svg")[0], "any_chart.png", {scale: 2, backgroundColor: "#000"});
})

You can set the background color to save the chart with that background color.

By continuing to use the site, you agree to the use of cookies.