在我们的Angular应用程序中,我们为HighCharts实现使用了highcarts-ng。
以下是图表最大化和最小化功能,该功能有效:
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。
单击最大化后,图表将展开到浏览器窗口的完整大小,但在拖动以调整浏览器窗口的大小后,我调用restoreChartSize函数,该函数将激活回流。
然而,图表的大小不会自动调整为100% 100%,它会恢复到图表的前一个大小:(
在最大化之前:

在最大化功能之后:

现在,调整浏览器窗口的大小后:
window.onresize = function(event) {
console.log('window resizing...');
highChart = ScopeFactory.getScope('highChart');
highChart.restoreChartSize();
console.log('highChart.chartConfig = ', highChart.chartConfig);
};

^返回到较小的静态大小,而不是自动调整大小100%
发布于 2015-08-01 04:48:04
您可以通过向图表添加一个新方法来执行此操作,该方法将手动触发回流,如下所示:
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
面向ng-highcharts用户的
更新
为了在使用ng-highcharts库时做到这一点,您可以简单地在控制器中拉出具有highcharts-ng依赖的图表对象,并添加reflowNow函数,如下所示:
var chart = this.chartConfig.getHighcharts();
chart.reflowreflowNow = function (){ ... }这也是由ng-highcharts的作者here和此fiddle推荐的拉出图表以执行自定义作业的方法。
发布于 2016-03-11 02:38:55
我最终找到了另一种解决方案,这是我唯一能做的事情,而且它实际上非常简单和直接。如果其他人正在寻找解决这个问题的方法,这里有一些有用的资源链接,它们为我解决了这个问题。
您可以简单地将其添加到与config.series或config.options相同级别的图表配置对象中。注释引用了信息,但对我有效的实际解决方案使用的是0秒的$timeout,这里
用于使用 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);
}
};https://stackoverflow.com/questions/31754511
复制相似问题