Sean
Sean

Reputation: 223

Render Highcharts after user updated labels

I am trying to create a chart builder. I have a user inputs for Title and location of the legend. I want a user to type in a title and when the click off (blur) the chart to be updated with what they typed.

The problem is the chart renders the first time, however i can never get the char to render again. Here is a quick summary of the code.

$('#legendlocation-select').blur(function updateChartLegend(){
console.log($('#legendlocation-select').val());
if($('#legendlocation-select').val()==='none'){
    chartData.legend.enabled = 'false';
}else{
    chartData.legend.enabled = 'true';
    chartData.legend.align = $('#legendlocation-select').val();
}
renderChart();
});
function renderChart(){
if(chart == undefined){
    console.log("First Draw");
    chart = new Highcharts.Chart(chartData);
}else{
    console.log("Redraw");
    chart.destroy();
    chart = new Highcharts.Chart(chartData);
    chart.redraw();
}
};

The chart renders the first time and then is just a blank white area the second time. Any ideas? chartData is a variable with all the options in it that renders correctly.

here is a JSFiddle that shows the issue http://jsfiddle.net/zVjrb/3/

Upvotes: 5

Views: 13445

Answers (1)

Sean
Sean

Reputation: 223

Figured out the Issue. Detailed here: http://highslide.com/forum/viewtopic.php?f=12&t=15255

Basically the Highcharts.charts function clears the series data from the original options object.

This is my working code:

var chart;
var chartData = { /* you chart data goes here */ };
$('#legendlocation-select').blur(function updateChartLegend(){
  console.log($('#legendlocation-select').val());
  if($('#legendlocation-select').val()==='none'){
    chart.options.legend.enabled = false;
  }else{
    chart.options.legend.enabled = true;
    chart.options.legend.align = $('#legendlocation-select').val();
  }
  renderChart();
});

function renderChart(){
  //console.log(chartData);
  if(chart == undefined){
    console.log("First Draw");
    chart = new Highcharts.Chart(chartData);
  }else{
    console.log("Redraw");
    chart.options.chart.animation = false;
    chart = new Highcharts.Chart(chart.options);
    chart.render();
  }
};

Upvotes: 10

Related Questions