1

Hi im currently trying to show multiple charts with angular-charts.js framework. 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

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

6
  • you tried services and factories ?? and create a single object of chart and create object aray and assign the same property of scale to each chart with angular.each Commented Jan 25, 2017 at 9:06
  • You should have better separate your architecture, between rendering and updating date. First, render your chart, then update with your data to chart. You can decide your trigger for updating data to chart. Commented Jan 25, 2017 at 9:07
  • Data for each chart is calculated by a function in a service. I can use a property for the scale but changes are not assumend by the chatrs Commented Jan 25, 2017 at 9:08
  • Add your code - other than that, the only advice we can give you is calculate your numbers first, and draw the charts afterwards. Commented Jan 25, 2017 at 9:10
  • I added the code for my problem Commented Jan 25, 2017 at 10:01

2 Answers 2

1

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.

Parent:

$scope.max = 0;

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

Child:

$scope.$watch('max',function(){
    ...
});
Sign up to request clarification or add additional context in comments.

Comments

0

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(); 

2 Comments

I tried this solution but for some reason Im not able to access my chart-element properly. Is there a way to get not just the dom-element, but the Charts-Object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.