chart.js Angular 控制器中的图表单击传递参数
chart.js chart-click pass parameter in Angular controller
我正在使用 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);
};
}]);
如果您仍然需要帮助,对于最新版本,您将获得标签:
$scope.onClick = function (points, evt) {
console.log(points[0]._view.label);
}
以下是我在单击栏时获取数据的解决方案
$scope.clickBar = (points, evt, element) => {
if (element != undefined) {
console.log(points,evt,element);
}
};
我正在使用 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);
};
}]);
如果您仍然需要帮助,对于最新版本,您将获得标签:
$scope.onClick = function (points, evt) {
console.log(points[0]._view.label);
}
以下是我在单击栏时获取数据的解决方案
$scope.clickBar = (points, evt, element) => {
if (element != undefined) {
console.log(points,evt,element);
}
};