ng-click不使用chart.js

时间:2016-02-01 11:48:40

标签: javascript angularjs charts

我用chartjs创建了一个饼图, 但是当我点击特定切片时,不会执行点击事件。

查看:

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

控制器:

'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

3 个答案:

答案 0 :(得分:9)

我研究了你的问题并正在阅读 @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

答案 1 :(得分:5)

使用html onclick功能并使用任何ID

访问范围

实施例

    <?php
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader($url);
$page_sheet = 1;
if(count($data->sheets[$page_sheet][cells])>0) // checking sheet not empty
{

    $rows = count($data->sheets[$page_sheet][cells]);

    for($j=5;$j<=count($data->sheets[$page_sheet][cells]);$j++) // loop used to get each row of the sheet
    {
        $data->sheets[$page_sheet][cells][$j][1];
        $rowid = $data->sheets[$page_sheet][cells][$j][1];
        $phone = $data->sheets[$page_sheet][cells][$j][2];
        $season = $data->sheets[$page_sheet][cells][$j][3];
        $row = $data->sheets[$page_sheet][cells][$j][4];
        $name = $data->sheets[$page_sheet][cells][$j][5];

       echo "$name<br />";
    }
}

答案 2 :(得分:3)

如果您使用angular-chart,请使用chart-click = "chartClick"

我认为chart.js会覆盖点击事件,并且不会将其传递给角度,因此您无法触发ng-click事件。

您可以尝试使用angular.element('#myPieChart')在外部绑定点击事件,但我不确定。