如何在angularjs中一起使用angular-gridster和highcharts-ng指令

时间:2015-01-15 14:31:55

标签: javascript angularjs gridster highcharts-ng

我正在使用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指令能够自我渲染之前设置网格宽度和高度。请帮忙。

1 个答案:

答案 0 :(得分:1)

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

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

希望它可以帮助别人。