gpt4 book ai didi

javascript - ng-click 不适用于 chart.js

转载 作者:可可西里 更新时间:2023-11-01 01:31:57 25 4
gpt4 key购买 nike

我用 chartjs 创建了一个饼图,但是当我点击一个特定的切片时,点击事件没有发生。

查看:

<div ng-controller="ProjectsController">
<a>Projects</a>
<br>
</br>
<canvas id="myPieChart" width="600" height="600" ng-click="chartClick()"></canvas>

Controller :

'use strict';

angular.module('Progs')

.controller('ProjectsController', ['$scope', '$rootScope', '$http', '$window', '$state',
function ($scope, $rootScope, $http, $window, $state) {
//...
$scope.chartClick = function (event) {
console.log('chartClick');
//console.log("segment: " + $scope.chart.getSegmentsAtEvent(event));
}
//...
}

我的代码有什么问题?

请注意:我没有使用 angular-chart.js

最佳答案

我研究了您的问题并正在阅读 @MohammadMansoor 的回答,我尝试为您实现解决方案。

(function(angular) {
var app = angular.module("app", []);
app.controller("ChartCtrl", ['$scope', function($scope) {
$scope.hello = "Hello, World";
$scope.canvas = document.getElementById("myPieChart");
$scope.ctx = $scope.canvas.getContext("2d");
$scope.data = [{
value: 300,
color: "#F7464A",
highlight: "#FF5A5E",
label: "Red"
}, {
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
}, {
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
}];
$scope.myPieChart = new Chart($scope.ctx).Pie($scope.data,{});
$scope.chartClick = function (event) {
console.log('chartClick');
console.log($scope.myPieChart.getSegmentsAtEvent(event));
}
$scope.canvas.onclick = $scope.chartClick;
}]);
})(angular);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>

<div ng-app="app" ng-controller="ChartCtrl">
<h1>{{hello}}</h1>
<a>Projects</a>
<br>
<canvas id="myPieChart" width="600" height="600"></canvas>
</div>

这里是plunk对于相同的实现

关于javascript - ng-click 不适用于 chart.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com