Search code examples
javascripthighchartsjquery-ui-tabsautoresize

Highcharts in JQuery Tabs - Resizing hidden tab code stops resizing visible tab


I solved one problem and created another.

I have 2 Highchart graphs in a jQuery UI tab. Initially the chart in the HIDDEN tab wouldn't resize its width when the browser window is resized. I fixed that with this code:

$( "#jQTab" ).on( 
            "tabsactivate", 
            function( event, ui ) {
               if(ui.newPanel[0].id == "jQTab_1")   { var chart = $('#container_1').highcharts();                      
                                                      chart.update({  chart: { renderTo: 'jQTab_1', width: $('#jQTab_1').width() } });
               }    
               if(ui.newPanel[0].id == "jQTab_2")   { var chart = $('#container_2').highcharts();                      
                                                      chart.update({  chart: { renderTo: 'jQTab_2', width: $('#jQTab_2').width() } });
               }    
             }
);

But this created a second problem. Now the VISIBLE chart does not change its width when the browser window's width is changed.

Here's the complete code:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Highcharts Resizing Issues in JQuery Tabs</title>

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <script src="https://code.highcharts.com/highcharts.js"></script>
  <script src="https://code.highcharts.com/highcharts-more.js"></script>
  <script src="https://code.highcharts.com/modules/exporting.js"></script>

  <script>     
  $(function () {    

$( "#jQTab" ).tabs();

var chart_1 = Highcharts.chart('container_1', {        title: {            text: 'Chart.update'        },        subtitle: {            text: 'Plain'        },        xAxis: {            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']        },        series: [{            type: 'column',            colorByPoint: true,            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],            showInLegend: false        }]    });
var chart_2 = Highcharts.chart('container_2', {        title: {            text: 'Chart.update'        },        subtitle: {            text: 'Plain'        },        xAxis: {            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']        },        series: [{            type: 'column',            colorByPoint: true,            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],            showInLegend: false        }]    });


$( "#jQTab" ).on( 
            "tabsactivate", 
            function( event, ui ) {
               if(ui.newPanel[0].id == "jQTab_1")   { var chart = $('#container_1').highcharts();                      
                                                      chart.update({  chart: { renderTo: 'jQTab_1', width: $('#jQTab_1').width() } });
               }    
               if(ui.newPanel[0].id == "jQTab_2")   { var chart = $('#container_2').highcharts();                      
                                                      chart.update({  chart: { renderTo: 'jQTab_2', width: $('#jQTab_2').width() } });
               }    
             }
);

});
</script>

</head>


<body>
 <div id="jQTab">
  <ul>
    <li><a href="#jQTab_1">Tab 1</a></li>
    <li><a href="#jQTab_2">Tab 2</a></li>
  </ul>
  <div id="jQTab_1"> <div id="container_1"></div></div>
  <div id="jQTab_2"> <div id="container_2"></div></div>
</div>
</body>
</html>

Solution

  • You can use chart.reflow() instead of setting width explicitly.

    $("#jQTab").on(
      "tabsactivate",
      function(event, ui) {
        if (ui.newPanel[0].id == "jQTab_1") {
          var chart = $('#container_1').highcharts();
          chart.reflow();
        }
        if (ui.newPanel[0].id == "jQTab_2") {
          var chart = $('#container_2').highcharts();
          chart.reflow();
        }
      }
    );
    

    example: https://jsfiddle.net/w2b1zgz1/