Highcharts - Hidden charts don't get re-size properly

后端 未结 3 1444
遇见更好的自我
遇见更好的自我 2020-12-05 23:35

I currently have a 3 tabbed page. Each tab is a div that is set to display: hidden when not selected. In these tabs I have a Grid system created with Susy (comp

3条回答
  •  抹茶落季
    2020-12-06 00:09

    /**
     * Adjust size for hidden charts
     * @param chart highcharts
     */
    function adjustGraph(chart) {
        try {
            if (typeof (chart === 'undefined' || chart === null) && this instanceof jQuery) { // if no obj chart and the context is set
                this.find('.chart-container:visible').each(function () { // for only visible charts container in the curent context
                    $container = $(this); // context container
                    $container.find('div[id^="chart-"]').each(function () { // for only chart
                        $chart = $(this).highcharts(); // cast from JQuery to highcharts obj
                        $chart.setSize($container.width(), $chart.chartHeight, doAnimation = true); // adjust chart size with animation transition
                    });
                });
            } else {
                chart.setSize($('.chart-container:visible').width(), chart.chartHeight, doAnimation = true); // if chart is set, adjust
            }
        }
        catch (err) {
            // do nothing
        }
    }
    

    usage

    $(window).resize(function () {
            if (this.resizeTO) clearTimeout(this.resizeTO);
            this.resizeTO = setTimeout(function () {
                // resizeEnd call function with pass context body
                adjustGraph.call($('body'));
    
            }, 500);
        });
    

    for bootstrap tab

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var isChart = $(this).attr('data-chart');
                var target = $(this).attr('href');
                if (isChart) {
                    // call functio inside context target
                    adjustGraph.call($(target));
                }
            });
    
    
    
      
    

    on chart

    new Highcharts.Chart({
                chart: {
                    renderTo: 'chart-bar',
                    defaultSeriesType: 'column',
                    zoomType: 'xy',
                    backgroundColor: null,
                    events: {
                        load: function (event) {
                            adjustGraph(this);
                        }
                    }
                },
    

    html code

    div class="tab-pane" id="charts">
        

    See on jsfiddle http://jsfiddle.net/davide_vallicella/LuxFd/2/

提交回复
热议问题