Highcharts图表在窗口大小调整时无法正确调整大小

17

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

自动调整大小的工作示例可在http://jsfiddle.net/2gtpA/找到。

我做了一个无法工作的示例来复制这个问题,可以在http://jsfiddle.net/4rrZw/2/中看到。

注意:您必须多次调整框架大小才能复制此问题(较小>较大>较小应该可以)

我认为这主要取决于您声明容器的方式...将HTML代码粘贴为highcharts的JS代码似乎与此无关...(两个示例的情况相同)。

<table style="width:100%;">
  <tr>
    <td style="width:50%;">
        <div id="container" style="height: 50%"></div>
    </td>
    <td style="width:50%;"></td>
  </tr>
</table>
3个回答

22

您可以避免使用JavaScript进行调整大小,而是依靠CSS来实现。这可以通过将HighChart渲染到具有以下内容的div中来实现:

position: absolute;
width: 100%;

要确保容器的高度合适,您需要将这个绝对定位的元素包裹在另一个已明确设置高度的元素中:

position: relative;
width: 100%;
height: 400px;
将HighChart渲染到绝对定位元素中,图表将对父元素宽度的减小做出响应。

谢谢!这解决了 https://github.com/highcharts/highcharts/issues/6427 这个问题,其中 highcharts 在 flex 容器内无法正确调整大小。 - thinkOfaNumber

19

我将你的fiddle分叉为可工作的解决方案 @ http://jsfiddle.net/AxyNp/ 其中最重要的代码片段如下:

$(window).resize(function() {
    height = chart.height
    width = $("#chartRow").width() / 2
    chart.setSize(width, height, doAnimation = true);
});

这个解决方案涉及到这篇SO帖子的答案。

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


3
/**
 * 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);
});

对于Bootstrap标签页

$('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);
                    }
                }
            },

HTML代码

<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> 

在 jsfiddle 上查看示例

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接