Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.

Getting chart in controller. I'm trying to use chart='var', but 'var remains 'null' even after chart is drawn. #19

Open
vic64 opened this issue Nov 3, 2014 · 7 comments

Comments

@vic64
Copy link

vic64 commented Nov 3, 2014

No description provided.

@carlcraig
Copy link
Owner

Check this example (this is to implement some custom onclicks, but does show how to expose the chart object to your controller) http://jsfiddle.net/Lfmhcab3/4/

@beneshed
Copy link

+1 to vic64

@beneshed
Copy link

.controller('GaugeCtrl', ['$scope',function ($scope) {
    $scope.chart;
    $scope.chartClick = function chartClick(event){
      console.log(event);
      console.log($scope.chart.getSegmentsAtEvent( event ));
    }
$scope.data = {
filler
}
   $scope.globalGaugeOption = {
      segmentStrokeColor : "#fff",

      //Number - The width of each segment stroke
      segmentStrokeWidth : 2,

      //Number - The percentage of the chart that we cut out of the middle
      percentageInnerCutout : 70, // This is 0 for Pie charts

      //Number - Amount of animation steps
      animationSteps : 1,

      showScale: false,
      scaleShowLabels : false,

      //Boolean - Whether we animate the rotation of the Doughnut
      animateRotate : false
    }
}]);

partial

<canvas tc-chartjs-doughnut chart="chart" chart-data="data" chart-options="globalGaugeOption" ng-click=chartClick($event)" width="100" height="100"></canvas>

@beneshed
Copy link

console.log($scope.chart.getSegmentsAtEvent);

returns

undefined is not a function
    at chartClick (http://localhost/~bwaters/app/scripts/controllers/gauge.js:16:31)

@carlcraig
Copy link
Owner

Im not sure why that would be happening, i know that the getSegmentsAtEvent is only available in the latest version of Chart.js.

Could you double check that Chart.js is 1.0.0+ ?

If it is, would you be able to fork this example http://jsfiddle.net/Lfmhcab3/4/ and try to create this problem so I can take a look?

@beneshed
Copy link

I found the problem was bower installs the wrong chart js. I replaced with the current stable version from github and it worked just fine.

On Nov 13, 2014, at 6:00 PM, Carl Craig notifications@github.com wrote:

Im not sure why that would be happening, i know that the getSegmentsAtEvent is only available in the latest version of Chart.js.

Could you double check that Chart.js is 1.0.0+ ?

If it is, would you be able to fork this example http://jsfiddle.net/Lfmhcab3/4/ and try to create this problem so I can take a look?


Reply to this email directly or view it on GitHub.

@carlcraig carlcraig changed the title I'm trying to use chart='var', but 'var remains 'null' even after chart is drawn. Getting chart in controller. I'm trying to use chart='var', but 'var remains 'null' even after chart is drawn. Jun 13, 2015
@carlcraig carlcraig added bug and removed bug labels Jun 13, 2015
@ghost
Copy link

ghost commented Aug 31, 2015

@carlcraig: I discovered that there is a problem with the scope visibility.
In general, parent scope cannot access the child scope of a contained directive, so in some scenarios it failed to pass the chart object back up just by putting it on the scope, like you do now.

If you wish to expose the chart object successfully at all cases I suggest you fire an event that anyone outside tc-angular-chartj can catch.
Something like:

if (exposeChart) {
    $scope.chart = chartObj;
    $rootScope.$broadcast('eventName', chartObj);
}

To solve this issue for current version, create a dedicated controller for the chart and put it on the wrapper html element as described in the example above. Something like this:

<div ng-controller="ChartController">
    <canvas tc-chartjs-line chart-options="options" chart-data="data" auto-legend chart="chart"></canvas>
</div>

Then declare $scope.chart variable inside ChartController along with the rest of the code for the chart (declare data, options, etc) and hopefully it will solve the problem.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants