RobinL RobinL - 4 months ago 572
Javascript Question

Highcharts - redraw() vs. new Highcharts.chart

I'm struggling to understand the correct way to update a highcharts chart. Supposing I have rendered a chart, and then I want to update it in some way. For instance, I may want to change the values of the data series, or I may want to enable dataLabels.

At the moment the only way I can figure out how to do this is to alter the chart options, and use

new Highcharts.chart
to tell highcharts to redraw.

However, I'm wondering whether this may be overkill and it might be possible to alter the chart 'in situ', without having to start from scratch with
new Highcharts.chart
. I notice there is a
method, but I can't seem to get it to work.

Any help is very much appreciated.



Sample code is as follows and at the bottom there is a jsFiddle

$(document).ready(function() {

chartOptions = {
chart: {
renderTo: 'container',
type: 'area',
series: [{
data: [1,2,3]

chart1 = new Highcharts.Chart(chartOptions);

chartOptions.series[0].data= [10,5,2];
chart1 = new Highcharts.Chart(chartOptions);

//The following seems to have no effect
chart1.series[0].data = [2,4,4];



For any future viewers of this question, it's worth noting there is no method to hide and show dataLabels. The following shows how to do it:



The setData method itself will call the redraw method