All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.assets.rjzjh.highcharts.modules.export-excel.js Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/**
 * A small plugin for getting the CSV of a categorized chart
 */
(function (Highcharts) {
    
    
    var each = Highcharts.each;
    Highcharts.Chart.prototype.getCSV = function () {
        var columns = [],
            line,
            tempLine,
            csv = "", 
            row,
            col,
            options = (this.options.exporting || {}).csv || {},

            // Options
            dateFormat = options.dateFormat || '%Y-%m-%d %H:%M:%S',
            itemDelimiter = options.itemDelimiter || ',', // use ';' for direct import to Excel
            lineDelimiter = options.lineDelimeter || '\n';

        each (this.series, function (series) {
            if (series.options.includeInCSVExport !== false) {              
                if (series.xAxis) {
                        var xData = series.xData.slice(),
                        xTitle = 'X values';
	                    if (series.xAxis.isDatetimeAxis) {
	                        xData = Highcharts.map(xData, function (x) {
	                            return Highcharts.dateFormat(dateFormat, x)
	                        });
	                        xTitle = 'DateTime';
	                    } else if (series.xAxis.categories) {
	                        xData = Highcharts.map(xData, function (x) {
	                            return Highcharts.pick(series.xAxis.categories[x], x);
	                        });
	                        xTitle = 'Category';
	                    }
                    columns.push(xData);
                    columns[columns.length - 1].unshift(xTitle);
                }
                columns.push(series.yData.slice());
                columns[columns.length - 1].unshift(series.name);
            }
        });

        // Transform the columns to CSV
        for (row = 0; row < columns[0].length; row++) {
            line = [];
            for (col = 0; col < columns.length; col++) {
                line.push(columns[col][row]);
            }
            csv += line.join(itemDelimiter) + lineDelimiter;
        }

        return csv;
    };

    // Now we want to add "Download CSV" to the exporting menu. We post the CSV
    // to a simple PHP script that returns it with a content-type header as a 
    // downloadable file.
    // The source code for the PHP script can be viewed at 
    // https://raw.github.com/highslide-software/highcharts.com/master/studies/csv-export/csv.php
    if (Highcharts.getOptions().exporting) {
        Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
            text: 'Download Excel',
            onclick: function () {
            	//this.exportChart({csv: this.getCSV()});
                Highcharts.post(this.options.exporting.url, {
                    csv: this.getCSV()
                });
            }
        });
    }
}(Highcharts));




© 2015 - 2024 Weber Informatics LLC | Privacy Policy