简体   繁体   English

渲染后在 Angular-Charts.js 中设置范围

[英]Set Scope in Angular-Charts.js after rendering

Hi im currently trying to show multiple charts with angular-charts.js framework.嗨,我目前正在尝试使用 angular-charts.js 框架显示多个图表。 My problem is that i need the same scale on each chart.我的问题是我需要在每个图表上使用相同的比例。 At the moment each chart has its own scale based upon the data displayed.目前,根据显示的数据,每个图表都有自己的比例。 I can not predict the data before the charts are rendered.我无法在呈现图表之前预测数据。 So I need something like a Callback function...所以我需要一个回调函数之类的东西......

Is there any solution?有什么解决办法吗?

Using Angular 1.6.1使用 Angular 1.6.1

EDIT:编辑:

$scope.options = {
                  scales: {
                    yAxes: [{
                            ticks: { min: 0, max: },
                              }]
                  }
                };

Markup:标记:

<div ng-repeat="i in getNumberOfSprints(numberOfSprints) track by $index">
                <h3>
                    Sprint {{$index+1}}
                </h3>
                <div ng-controller="MyCtrl">
                    <canvas class="chart-bar"
                      chart-data="GetData($index)"
                      chart-dataset-override="datasetOverride" chart-options="options">
                    </canvas> 
                </div>
            </div>

GetData calculates Data for each chart based on index GetData 根据索引计算每个图表的数据

I finally found a solution for my Problems.我终于找到了解决我的问题的方法。

I used a parent-controller to store my scale variable and in my Child Controllers a $watch expression to rerender each chart.我使用父控制器来存储我的比例变量,并在我的子控制器中使用 $watch 表达式来重新呈现每个图表。

Parent:家长:

$scope.max = 0;

    $scope.setMax = function(val){
        $scope.max = Math.max($scope.max, val);
        console.log($scope.max);
    }

Child:孩子:

$scope.$watch('max',function(){
    ...
});

When you change a chart's option after it was render, you need to call the update function.当您在渲染后更改图表的选项时,您需要调用更新函数。

.update(duration, lazy) .更新(持续时间,懒惰)

Triggers an update of the chart.触发图表更新。 This can be safely called after replacing the entire data object.这可以在替换整个数据对象后安全地调用。 This will update all scales, legends, and then re-render the chart.这将更新所有比例、图例,然后重新渲染图表。

Documentation 文档

Example:例子:

// duration is the time for the animation of the redraw in milliseconds
// lazy is a boolean. if true, the animation can be interrupted by other animations
    
// Would update the first dataset's value of 'March' to be 50
myLineChart.data.datasets[0].data[2] = 50; 
    
// Calling update now animates the position of March from 90 to 50.
myLineChart.update(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM