faulpin
faulpin

Reputation: 21

Highcharts add export as csv

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?

Upvotes: 2

Views: 5930

Answers (3)

jake_astub
jake_astub

Reputation: 340

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
        ]
    }
}
},

Upvotes: 0

Elzo Valugi
Elzo Valugi

Reputation: 27856

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.

Upvotes: 1

Michael Robinson
Michael Robinson

Reputation: 29498

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.

Upvotes: 0

Related Questions