Dixy Xavier
Dixy Xavier

Reputation: 677

Save a plot as png file in react-plotly.js?

I am using react-plotly.js library to plot histogram. I need to download the graph as a png file. I saw a document on how to do it in plotly.js library. Can someone help me to figure out how to do it in react-plotly.js library. Thanks in advance :)

Upvotes: 2

Views: 2349

Answers (2)

user3837006
user3837006

Reputation: 41

if you have list of plotly charts in react inside an element, having id as "chart-container", then use the following code to download the graph as png, which can be called on any button click event.

let ele = document.getElementById('chart-container').getElementsByClassName('modebar');
for (var i = 0; i < ele.length; i++) {
    ele[i].getElementsByClassName('modebar-btn')[0].click();
}

Upvotes: 0

Sujatha Girijala
Sujatha Girijala

Reputation: 1209

just use this method to save png file

downloadGraph(fileName) {
if(this.graphPlotted) {
  Plotly.downloadImage(this.graphPlotted, {format: 'png', filename: fileName})
}

}

Upvotes: 3

Related Questions