编程语言
首页 > 编程语言> > javascript-Angular控制器中的chart.js chart-click传递参数

javascript-Angular控制器中的chart.js chart-click传递参数

作者:互联网

我正在使用chart.js / angular-chart.js在饼图中显示一些数据.

我需要一些功能,当用户单击图表饼图内的标签时,单击事件将复制到所选图表值上.

我可以通过以下操作触发事件:

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

这是我的标记:

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

知道如何将一些参数传递给该事件以实际获取值吗?

编辑

完整的控制器:

.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);
                    });
                }
            })
    }
])

解决方法:

你可以这样做,

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

控制器:

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

标签:angularjs,chart-js,charts,angular-chart,javascript
来源: https://codeday.me/bug/20191026/1937458.html