Highcharts add export as csv

2019-07-12 11:04发布

问题:

I am using the highcharts js library and i want to add the feature of exporting as csv.

I have added the option in the file modules/exporting.js but i dont know what to do next.

Can anyone give me a hand?

回答1:

Here is an example on how to modify the buttons. You can add a custom button with your csv export and then in the server side just create a string and export it.



回答2:

You could make your Javascript call a PHP function (assuming you use it) that generates a CSV based on whatever parameters you include in the url (none here).

$sql = "SELECT * FROM `table` ORDER BY `id` ASC";
$result = mysql_query($sql) or die(mysql_error());

$csv = '';

while ($row = mysql_fetch_assoc($result)) {
    $csv .= $row["one"];
    $csv .= "," . $row["two"];
    $csv .= "," . $row["three"];
    $csv .= "\n";
}

header("Content-Type: application/csv") ;
echo $csv;
?>

This method could be used for other server-side languages.



回答3:

This worked for me. Just add this to the constructor. It's a modification of Elzo's post. I'm using highstock, but they should work similar.

exporting: {
buttons: {
    contextButton: {
        menuItems: [{
                text: 'Export to PNG (small)',
                onclick: function() {
                    this.exportChart({
                        width: 250
                    });
                }
            }, {
                text: 'Export to PNG (large)',
                onclick: function() {
                    this.exportChart(); // 800px by default
                }
            }, {
                text: 'Export to CSV',
                onclick: function() {
                    window.open('/getcsv/loc/' + loc + '/ser/' + ser + '/reg/' + reg);
                }
            },
            null
        ]
    }
}
},