我是 Chart.js 的初学者,我正在尝试在创建图表时创建点击事件。
我试图在单击栏时访问它的所有元素,但现在,onClick 方法甚至无法正常运行。图表呈现得很好 - 因为在代码中创建了画布,所以不需要任何 html。我将不胜感激帮助以解决此问题
controller($state, $scope, $rootScope) {
$scope.myChart;
Run Code Online (Sandbox Code Playgroud)
....
$scope.go = function myFunc() {
document.getElementById("chartContainer").innerHTML = ' ';
document.getElementById("chartContainer").innerHTML = '<canvas style="margin-top: 10px; padding-top: 20px; height:90% ; background-color: ' + vm.options.backgroundColor + '; " id="myChart" click="onClick"></canvas>';
var ctx = document.getElementById("myChart").getContext('2d');
render($scope.myChart, ctx, vm.options.barColor, vm.options.backgroundColor, labelVal, value);
};
$scope.onClick = function (evt) {
console.log("Testing");
};
}
var render = function createChart(myChart, ctx, barColor, backgroundColor, labels, values) {
myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: …Run Code Online (Sandbox Code Playgroud)