3

I'm using chart.js/angular-chart.js to display some data in pie charts.

I need functionality where when a user will click on a label within the chart pie the click event will copy over the selected chart value.

I'm able to trigger the event by this action:

$scope.chartClick = function() {
               alert('ok');
           }

And this is the markup I have:

 <canvas id="pie" class="chart chart-pie"
                  chart-data="data" chart-labels="labels" display="true" chart-click="chartClick()" chart-options="options"></canvas> 

Any idea how I can pass some parameter to that event to actually get the value?

Edit

Full controller:

.controller('InvestorLtvReportController', [
    '$scope', '$http', '$state', function ($scope, $http, $state) {
        $scope.labels = [];
        $scope.data = [];
        $scope.options = {
            legend: {
                display: true,
                position: 'bottom',
                labels: {
                    fontColor: 'rgb(255, 99, 132)'
                }
            }
        };

        $scope.chartClick = function (points, evt) {
            console.log(points, evt);
        };

        $http({
                url: 'http://myapi/api/Manage/GetUserRole',
                method: "GET"
            }).success(function (data, status) {
                $scope.isInvestor = false;
                angular.forEach(data, function (value, key) {
                    if (value == 'Investor') {
                        $scope.isInvestor = true;
                    }
                });

                if (!$scope.isInvestor) {
                    $state.go('accountlogin');
                } else {
                    $http({
                        url: 'http://myapi/api/investor/GetInvestorLtvReport',
                        method: "GET"
                    }).success(function (data, status) {

                        angular.forEach(data, function (value, key) {
                            $scope.labels.push(value.Amortisation);
                            $scope.data.push(value.PercOfFund);
                        });
                    }).error(function (data, status) {
                        console.log(data);
                    });
                }
            })
    }
])

3 Answers 3

3

You can do this,

<canvas id="pie" chart-click="onClick" class="chart chart-pie"chart-data="data" chart-labels="labels"></canvas> 

Controller:

app.controller('AppCtrl', ['$scope', function($scope){
  $scope.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales"];
  $scope.data = [300, 500, 100];
   $scope.onClick = function (points, evt) {
    console.log(points, evt);
  };
}]); 

DEMO

Sign up to request clarification or add additional context in comments.

8 Comments

Hi @sajeetharan thx for your help, I'm getting undefined undefined although I have labels as you can see from the screenshots i.gyazo.com/946c26eea34b0c027f78426864547607.png
@Laziale did you check the demo i attached?
I see that it works fine thx, I'm getting the labels dynamically, do you think that might make a difference?
if you have the data in chart, it should work either way. Mark as answer if it has helped
I just updated my question with full controller code, can you pls check and let me know if I'm missing something, Thanks
|
2

A solution that worked for me to get the data when you click on a bar is the following

$scope.clickBar = (points, evt, element) => {
    if (element != undefined) {
        console.log(points,evt,element);
    }
};

Comments

1

If you still need help with that, for the recent version, you get the label with:

$scope.onClick = function (points, evt) {
    console.log(points[0]._view.label);
}

Comments

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.