How to download csv with fusion graphs in codeigniter

I am trying to execute the following function to create a CSV for download, but I see that I can only get CSV data in alert.

function ExportMyChart(type) {
    var chartObj = getChartFromId('myChartIdAmount4');
    if( chartObj.hasRendered() ){
        if(type === 'CSV'){  
            alert(chartObj.getDataAsCSV());
        }else{
            chartObj.exportChart({ exportAtClient: '1',  exportFormat: type, exportAction: 'download' }); 
        }
    }
}

chartObj.exportChartdoesn't work for CSV, is there any way to make it work for CSV because it works for PDF, JPEG ?. I would appreciate any help with this. Thank.

+2
source share
2 answers

You can export the CSV as a download file by encoding the CSV string and using the loading methods for the anchor createElement tag object. See the code below, which is a small modification of your implementation.

jsFiddle, FusionCharts V 3.3.1

var exportMyChart = function (type) {
    var chartObj = FusionCharts('myChartIdAmount4');

    if (chartObj.hasRendered()) {
        if(type === 'CSV'){  
            var a = document.createElement('a');
            a.href = 'data:attachment/csv,' + encodeURIComponent(chartObj.getDataAsCSV());
            a.target = '_blank';
            a.download = 'export.csv';
            document.body.appendChild(a);
            a.click();
        }
        else{
            chartObj.exportChart({ exportAtClient: '1',  exportFormat: type, exportAction: 'download' });
        }
    }
    else{
        alert("What are you trying to export?");
    }
}
+2

Nishikant,

FusionCharts CSV- , /pdf.

, , CSV , , ( ), CSV.

0

All Articles