如何在 AngularJS 中同时使用 Angular-gridster 和 Highcharts-NG 指令

how to use angular-gridster and highcharts-ng directives together in angularjs

本文关键字:Angular-gridster Highcharts-NG 指令 AngularJS      更新时间:2023-09-26

我正在使用angularjs-gridster(https://github.com/ManifestWebDesign/angular-gridster)和higharts-ng指令(https://github.com/pablojim/highcharts-ng/blob/master/README.md)

我正在尝试在网格单元格内生成这些高图表。我的问题是,即使我将图形抽屉功能放在$timeout服务中,高图表也会占用其默认宽度和高度(600px * 400px)。代码如下:

HTML

<div class="graph-list" gridster="gridsterOpts">
  <ul>
    <li gridster-item="graph.grid" class="graph-set" ng-repeat="graph in graphs | orderBy: 'number'">
      <highchart id="{{'graph' + graph.number}}" class="graph" config="graph.config"></highchart>
    </li>
  </ul>
</div>

JS:

// inside the graph-list div controller 
$scope.gridsterOpts = {
    colums: 4,
    rowHeight: 240,
    margins: [10,10],
    outerMargin: false,
    draggable: {
      enabled: false // whether dragging items is supported
    }
};
$scope.graphs = {}; //
$scope.somefunction(){ /* this function populates the graphs object */ }; 
function drawGraphs(){ /* this function populates the graph.config object by looping through all the graph objects */ }
$timeout(function(){
    drawGraphs(); 
});

我尝试在网格单元格的宽度和高度上创建手表,但它没有显示任何变化。我没有在 graph.config 选项中明确给出高图表宽度和高度,因为我在 highcharts-ng 文档中读到它默认采用父宽度和高度,但它没有发生。谁能指导我可能出现的问题。

在我看来,angularjs-gridster 插件无法在 highcharts 指令能够呈现自身之前设置网格宽度和高度。请帮忙。

我最终做到了。我需要在 highcharts-ng 文档中提供的 func() 选项中添加 chart.reflow() 方法(它只是调整图表大小而不是重新绘制图表,以便性能更好)。

graph.config = {
  options: { },
  series: [],
  func: function (chart) {
    $timeout(function(){
      chart.reflow();
    })
  } 
} 

希望它对其他人有所帮助。