how to download csv with fusion charts in codeigniter

冷暖自知 提交于 2019-12-24 04:24:08

问题


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.


回答1:


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?");
    }
}



回答2:


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.



来源:https://stackoverflow.com/questions/18589865/how-to-download-csv-with-fusion-charts-in-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!