HighCharts:如何使用回流以允许在更改大小后自动调整大小

HighCharts: How to use reflow to allow auto-resize after changing size

在我们的 Angular 应用中,我们使用 highcarts-ng for our HighCharts 实现。

这是图表最大化和最小化功能,它有效:

function expandChartPanel() {
    vm.chartMaxed = !vm.chartMaxed;

    viewHeader = ScopeFactory.getScope('viewHeader');
    highChart  = ScopeFactory.getScope('highChart');

    var chart = highChart.chartObject;
    var highChartContainer       = document.getElementById("highchart-container");
    var highChartContainerWidth  = document.getElementById('highchart-container').clientWidth;
    var highChartContainerHeight = document.getElementById('highchart-container').clientHeight;

    var windowWidth  = window.innerWidth;
    var windowHeight = window.innerHeight;

    if (vm.chartMaxed) {

        vs.savedWidth  = highChartContainerWidth;
        vs.savedHeight = highChartContainerHeight;

        console.log('savedWidth  = ', vs.savedWidth);
        console.log('savedHeight = ', vs.savedHeight);

        root.chartExpanded          = true;
        viewHeader.vh.chartExpanded = true;
        highChart.highChartMax      = true;

        highChartContainerHeight = document.getElementById('highchart-container').clientHeight;
        windowWidth  = window.innerWidth;
        windowHeight = window.innerHeight;

        highChart.chartConfig.size.width  = windowWidth;
        highChart.chartConfig.size.height = windowHeight - 220;

        chart.setSize(windowWidth, windowHeight - 220);
    }
    else {
        root.chartExpanded          = false;
        viewHeader.vh.chartExpanded = false;
        highChart.highChartMax      = false;

        highChart.chartConfig.size.width  = vs.savedWidth;
        highChart.chartConfig.size.height = vs.savedHeight;

        chart.setSize(vs.savedWidth, vs.savedHeight);
    }

    highChart.restoreChartSize();
}

回流功能如下:

function restoreChartSize() {
    console.log('restoreChartSize');
    if (!vs.chartObject.reflowNow) {
        vs.chartObject.reflowNow = vs.chartObject.reflowNow = function() {
            this.containerHeight = this.options.chart.height || window.window.HighchartsAdapter.adapterRun(this.renderTo, 'height');
            this.containerWidth  = this.options.chart.width  || window.window.HighchartsAdapter.adapterRun(this.renderTo, 'width');
            this.setSize(this.containerWidth, this.containerHeight, true);
            this.hasUserSize = null;
        }
     }

     vs.chartObject.reflowNow();
}

上面的回流功能在这个 jsFiddle 中完美运行,但在我们的应用程序中却不行。

The full Gist file of our HighChartsDirective file.

单击“最大化”后,图表将扩展到浏览器的完整大小 window,但在拖动以调整浏览器大小后 window,我调用了 restoreChartSize 函数,激活回流。

但是图表的大小不会自动调整为 100% 100%,它会返回到之前的图表大小:(

最大化之前:

最大化函数后:

调整浏览器大小后 window:

window.onresize = function(event) {
    console.log('window resizing...');
    highChart = ScopeFactory.getScope('highChart');
    highChart.restoreChartSize();
    console.log('highChart.chartConfig = ', highChart.chartConfig);
};

^ 回到较小的静态尺寸,而不是自动尺寸 100%

您可以通过向图表添加一个新方法来实现此目的,该方法将手动触发回流,如下所示:

chart.reflowNow = function(){
    this.containerHeight = this.options.chart.height || window.window.HighchartsAdapter.adapterRun(this.renderTo, 'height');
    this.containerWidth = this.options.chart.width || window.window.HighchartsAdapter.adapterRun(this.renderTo, 'width');
    this.setSize(this.containerWidth, this.containerHeight, false);
    this.hasUserSize = null;
}

然后,每当您想使用 setSize() 摆脱手动调整大小时,只需调用 chart.reflow()

这是一个工作示例:jsFiddle

引用自:github-issue

UPDATE for ng-highcharts users

要在使用 ng-highcharts 库时执行此操作,您可以简单地在具有 highcharts-ng 依赖项的控制器中拉出图表对象并添加 reflowNow 函数,如下所示:

var chart = this.chartConfig.getHighcharts();
chart.reflowreflowNow = function (){ ... }

这也是 ng-highcharts 作者推荐的拉出图表以执行自定义作业的方法 here and this fiddle

我最终找到了一个替代解决方案,这是我唯一可以开始工作的方法,而且它实际上非常简单直接。如果其他人正在为此寻找解决方案,这里是指向对我有用并解决了问题的资源的链接。

您只需将其添加到您的图表配置对象中,与 config.series 或 config.options 处于同一级别。评论引用了信息,但对我有用的实际解决方案使用 $timeout 0 秒,这里

*为了使用highcharts-ng显然

http://plnkr.co/edit/14x7gfQAlHw12XZVhWm0?p=preview

$scope.chartConfigObject = {

    // function to trigger reflow in bootstrap containers
    // see: http://jsfiddle.net/pgbc988d/ and https://github.com/pablojim/highcharts-ng/issues/211
    func: function(chart) {
        $timeout(function() {
            chart.reflow();
            //The below is an event that will trigger all instances of charts to reflow
            //$scope.$broadcast('highchartsng.reflow');
        }, 0);
    }
};