Search code examples
plotly

How to save plotly image to local file with javascript


What's proper way to save plotly graph to local file with javascript?

img_png.attr("src", url);
Plotly.toImage(gd,{format:'png',height:400,width:400});

Above code from official document save it to element, I would like to save it to local disk directly!


Solution

  • You'll get an image encode with base64 string from Plotly.toImage(), and you can convert base64 string to image in many ways. (For example like this page.)

    Assume the HTML code is like this:

    <div id="chart">
      <!-- chart here -->
    </div>
    

    Then the JavaScript code should be like this:

    Plotly.toImage('chart', { format: 'png', width: 800, height: 600 }).then(
        function (dataUrl) {
        // use the dataUrl
    })