Poru
Poru

Reputation: 8360

Highcharts - Redraw empty chart

I have a rendered Highcharts chart on a website and I need to empty it after a certain time. Now I tried that with such code but the chart doesn't empty itself/nothing changes...

var chart = new Highcharts.Chart({
// Chart settings
});

// Some other JS
function emptyChart(chart) {
    chart.series = [];
    chart.redraw();
}

// Some code and a function executes this function after some time
emptyChart(chart);

I also don't get any error in the Firebug console or somewhere else, just nothing happens...

Upvotes: 1

Views: 4904

Answers (5)

Sebastian Bochan
Sebastian Bochan

Reputation: 37578

You can also use setData([])

http://api.highcharts.com/highcharts#Series.setData()

Upvotes: 1

Jb Drucker
Jb Drucker

Reputation: 992

A clean way to do this :

function emptyChart(chart) {
    while(chart.series.length !=0) {
        chart.series[0].hide();
        chart.series[0].remove();
    }
}

If you want your axis to disappear as well, use the "showEmpty: false" option (check xAxis.showEmpty )

Upvotes: 4

Poru
Poru

Reputation: 8360

I had this problem already a second time and now I finally found a simple but currently only-working solution: Just create a new, empty Highchart:

function emptyChart(chart) {
    chart = new Highcharts.Chart(/* chartOptions */);
}

Upvotes: 2

Jashwant
Jashwant

Reputation: 28995

I guess, you want something like this,

chart.series[0].data = [];
chart.redraw();

Upvotes: 2

blearn
blearn

Reputation: 1208

Just a guess, but try changing to chart.series = {};?

Upvotes: 1

Related Questions