Highcharts 高图don';在调整窗口大小时不能正确调整大小

Highcharts 高图don';在调整窗口大小时不能正确调整大小,highcharts,Highcharts,我的页面上有两个并排的图表,并且希望在调整窗口大小时调整它们的大小:它们的容器设置为50%的宽度,根据示例,这应该足够了 可以找到自动调整图表大小的工作示例@ 我为重现该问题而做的一个不起作用的示例可以看到@ 注意:您必须多次调整帧大小以重现问题(较小>较大>较小即可) 我认为这一切都取决于你申报集装箱的方式。。。将HTML代码粘贴为highcharts的JS代码似乎与此无关。。。(两个示例中相同) 我把你的小提琴交给了一个有效的解决方案@ 其中最重要的代码片段如下: $(window).res

我的页面上有两个并排的图表,并且希望在调整窗口大小时调整它们的大小:它们的容器设置为50%的宽度,根据示例,这应该足够了

可以找到自动调整图表大小的工作示例@

我为重现该问题而做的一个不起作用的示例可以看到@

注意:您必须多次调整帧大小以重现问题(较小>较大>较小即可)

我认为这一切都取决于你申报集装箱的方式。。。将HTML代码粘贴为highcharts的JS代码似乎与此无关。。。(两个示例中相同)


我把你的小提琴交给了一个有效的解决方案@ 其中最重要的代码片段如下:

$(window).resize(function() {
    height = chart.height
    width = $("#chartRow").width() / 2
    chart.setSize(width, height, doAnimation = true);
});
该解决方案与SO post的答案有关


看起来,要正确调整图表容器(和单元格)的大小,最好的办法是依靠自己的调整触发器。请注意,我在图表的选项中将回流设置为“false”,以删除现有触发器。

您可以避免使用javascript执行调整大小,而是依赖css。这可以通过将HighChart渲染为具有以下内容的div来实现:

position: absolute;
width: 100%;
为确保容器的高度适当,必须将此绝对定位的元素包装到另一个已显式设置高度的元素中:

position: relative;
width: 100%;
height: 400px;
通过将HighChart呈现为位置绝对元素,图表将响应父元素中的宽度缩减

/**
 * 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
    }
}
用法 用于引导选项卡 Html代码

图1/h3>
字符2
/**图表**/
字符3
/**图表**/
请参见JSFIDLE上的示例

谢谢!这解决了highcharts在flex容器中无法正确调整大小的问题。
/**
 * 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
    }
}
$(window).resize(function () {
    if (this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function () {
        // resizeEnd call function with pass context body
        adjustGraph.call($('body'));
    }, 500);
});
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    isChart = $(this).attr('data-chart');
    var target = $(this).attr('href');
    if (isChart) {
        // call functio inside context target
        adjustGraph.call($(target));
    }
});


<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
    <li class="active">
         <a href="#anagrafica" data-toggle="tab"><h5>Anagrafica</h5></a>
    </li>
    <li><a href="#consumi" data-toggle="tab" data-chart="1"><h5>Consumi</h5></a></li>
</ul>
    new Highcharts.Chart({
            chart: {
                renderTo: 'chart-bar',
                defaultSeriesType: 'column',
                zoomType: 'xy',
                backgroundColor: null,
                events: {
                    load: function (event) {
                        adjustGraph(this);
                    }
                }
            },
<div class="tab-pane" id="charts">
    <div class="row-fluid">
        <div class="span6 offset3">
            <div id="myCarousel" class="carousel slide">
                <!-- Carousel items -->
                <div class="carousel-inner chart-container">
                    <div class="active item">
                        <h3>Chart 1/h3>
                        <div id="bar-pod-annuale">
                           <div id="chart-bar" style="width:100%;margin: 0 auto"></div>
                        </div>
                    </div>
                    <div class="item">
                        <h3>Char 2</h3>
                        /** chart **/
                    </div>
                    <div class="item">
                        <h3>Char 3</h3>
                        /** chart **/
                    </div>
                </div>
                <!-- Carousel nav -->
                <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
                <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
            </div>
        </div>
    </div>
</div>