Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 redraw() method, but I can't seem to get it to work.

Any help is very much appreciated.

Thanks,

Robin

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]; chart1.redraw();  });​ 

http://jsfiddle.net/sUXsu/18/

[edit]:

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: http://jsfiddle.net/supertrue/tCF8Y/

like image 844
RobinL Avatar asked Dec 31 '12 09:12

RobinL


People also ask

How do you redraw a Highchart graph?

To redraw the chart you can simply use redraw() method (https://api.highcharts.com/class-refere ... art#redraw). You might also want to check update() method (https://api.highcharts.com/class-refere ... art#update).

Is it possible to pass multiple series in a chart in Highcharts?

Yes, you can.

How do I destroy Highcharts?

destroy () - Removes the chart and purges memory. This method should be called before writing a new chart into the same container. It is called internally on window unload to prevent leaks. var hc_options = { chart: { renderTo: 'container' }, series: [{ name: 'USD to EUR', data: usdeur }] }; var chart=new Highcharts.

How do I remove a credit from Highcharts?

highcharts Credits Removing "highcharts.com" Logo Highchart by default puts a credits label in the lower right corner of the chart. This can be removed using credits option in your chart settings. will remove the highcharts.com logo.


2 Answers

chart.series[0].setData(data,true);

The setData method itself will call the redraw method

like image 166
karthik - LK Avatar answered Oct 19 '22 06:10

karthik - LK


you have to call set and add functions on chart object before calling redraw.

chart.xAxis[0].setCategories([2,4,5,6,7], false);  chart.addSeries({     name: "acx",     data: [4,5,6,7,8] }, false);  chart.redraw(); 
like image 31
waqas Avatar answered Oct 19 '22 06:10

waqas