Responsive Highcharts not sizing correctly until window resize

前端 未结 12 1412
北荒
北荒 2021-01-30 16:06

I\'m using Highcharts within Zurb\'s Foundation framework for a class project. I have three charts within a section tab. One is within a 12-column div, the other two are on the

相关标签:
12条回答
  • 2021-01-30 16:44

    You can add an event listener so your chart resizes every time it redraws:

    mychart = new Highcharts.stockChart('div',   {
        chart: {
            events: {
                redraw: function(e) {
                    mychart.reflow();
                }
            }
        },
    
    0 讨论(0)
  • 2021-01-30 16:44

    only this worked for me

    $(window).resize(function(){ 
        $scope.chartConfig.getChartObj().reflow() 
    });
    
    0 讨论(0)
  • 2021-01-30 16:47
    $(Highcharts.charts).each(function(i,chart){
        var height = chart.renderTo.clientHeight; 
        var width = chart.renderTo.clientWidth; 
        chart.setSize(width, height);
        });
    

    it works for me

    0 讨论(0)
  • 2021-01-30 16:49

    I got this from another answer, so give thegreyspot some credit in this question.

    solution: call $(window).resize(); after the graph is loaded (or when you fill it with data)

    0 讨论(0)
  • 2021-01-30 16:52

    In pure javascript multiple charts in single page resize issue on window resize

     window.onresize = function() {
        //- Remove empty charts if you are using multiple charts in single page 
        //- there may be empty charts
        Highcharts.charts = Highcharts.charts.filter(function(chart){
            return chart !== undefined;
        });
    
        Highcharts.charts.forEach(function(chart) {
            var height = chart.renderTo.chartHeight;
            //- If you want to preserve the actual height and only want to update 
            //- the width comment the above line.
            //- var height = chart.chartHeight; 
            var width = chart.renderTo.clientWidth;
            //- The third args is an animation set to true. 
            chart.setSize(width, height, true);
        });
    };
    

    We need this css code as well

    .highcharts-container {
        width: 100%;
    }
    .highcharts-container svg {
        width: 100%;
        display: flex;
    }
    
    0 讨论(0)
  • 2021-01-30 16:58

    for me, it needed to take some delay and trigger the window resize.

    window.setTimeout(function(){ $(window).trigger('resize'); }, 500);
    
    0 讨论(0)
提交回复
热议问题