how to download csv with fusion charts in codeigniter
Asked Answered
S

2

2

I have been trying below function to generate CSV for download, but what I am seeing is that I can get CSV data only 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.exportChart is not working for CSV, is there any way i can make it work for CSV as it work for PDF, JPEG ?. I would appreciate any help on this. Thanks.

Sargent answered 3/9, 2013 at 10:24 Comment(7)
am not sure your logic (as per the above code) is to fire alert when the type is CSV. So when it is csv the else condition will not execute. Am I missing anything?Automat
if(type ==='CSV') because chartObj.exportChart({ exportAtClient: '1', exportFormat: type, exportAction: 'download' }); gives error if I use it for CSVSargent
exportChart and getDataAsCSV is completely separate methods for separate purpose.readAutomat
and somehow exportChart fails to work with CSV, any thought why is it soSargent
It is because exporting is made for image or PDF formats. CSV is not an export format, it is simply getting data from chart as CSV string.Sagamore
@Sargent that is what I exactly wanted to explain. :)Automat
BTW there is nothing to do with CodeIgniter.Resigned
R
2

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

See my jsFiddle which uses 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?");
    }
}
Resigned answered 4/9, 2013 at 5:42 Comment(1)
Thanks a lot, it did saved lots of time.Sargent
B
0

Nishikant,

FusionCharts does not support CSV data as a downloadable option as it does with exporting of images/pdf.

So instead of showing the CSV in alert window you can store it in a variable then you can POST this to your server side handler (you'll have to create it) which will return it as CSV file.

Berkow answered 4/9, 2013 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.