admin管理员组文章数量:1355103
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);
});
}
})
}
])
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);
});
}
})
}
])
Share
Improve this question
edited Feb 2, 2019 at 21:37
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Oct 31, 2016 at 19:20
LazialeLaziale
8,23548 gold badges155 silver badges271 bronze badges
3 Answers
Reset to default 3You 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
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);
}
};
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);
}
本文标签: javascriptchartjs chartclick pass parameter in Angular controllerStack Overflow
版权声明:本文标题:javascript - chart.js chart-click pass parameter in Angular controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744036501a2579869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论