romer8 / crazysearch

0 stars 0 forks source link

Add download CSV to the plotly options #7

Closed romer8 closed 4 years ago

romer8 commented 4 years ago

code is available here: https://github.com/plotly/plotly.js/issues/2171

In the meanwhile, here is a function I use that provides this feature. I hope it's helpful:

modeBarButtonsToAdd: [{ name: 'downloadCsv', title: 'Download data as csv', icon: Plotly.Icons.disk, click: function(){ var csvData = []; var header = ['X'] //main header. for (var j = 0; j < numSeries; j++){ header.push(y_names[j]); } csvData.push(header); var header = [x_title] //subheader for (var j = 0; j < numSeries; j++){ header.push(y_titles[j]); } csvData.push(header); for (var i = 0; i < data.length; i++){ //data var line = [data[i][x_name]]; for (var j = 0; j < numSeries; j++){ line.push(data[i][y_names[j]]); } csvData.push(line); } var csvFile = csvData.map(e=>e.map(a=>'"'+((a||"").toString().replace(/"/gi,'""'))+'"').join(",")).join("\r\n"); //quote all fields, escape quotes by doubling them. var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' }); var link = document.createElement("a"); var url = URL.createObjectURL(blob); link.setAttribute("href", url); link.setAttribute("download", charttitle.replace(/[^a-z0-9.-]/gi,'_') + ".csv"); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } }],

romer8 commented 4 years ago

The graph now has the ability to download each one of the different graphs: Scatter,Pie, Whisker and Plots, and Bars