如何让谷歌互动中的图表角(UI事件)?(How to make interactive Google

2019-10-18 12:51发布

我不知道是否有处理的角谷歌图表API事件的好办法?

(没有足够的口碑,使这些引用正确的链接:)

我使用[bouil.github.io/angular-google-chart/]

并试图端口[github.com/angular-ui/ui-map/blob/master/ui-map.js]

的处理与[github.com/angular-ui/ui-utils/blob/master/modules/event/event.js]事件的方式。


这里是表示非交互图的plunker:

http://plnkr.co/edit/RTx8UgZdEvDmYoPUu4Xf

我想发生的是把foo() - 在选择图表中由方法(显示警报)将被调用。


模板:

<div data-google-chart chart="chart" class="chart" ui-event="{'chart-select':'foo()'}">

指示:

angular.module('googlechart.directives', ['ui.event']).directive('googleChart',     ['$timeout', '$window', function ($timeout, $window) {
    return {
        restrict: 'A',
        scope: {
            chart: '=chart'
        },
        link: function ($scope, $elm, $attr) {
            var eventNames="select ready onmouseover animationfinish error onmouseout";          

         /* ... Set up the chart and chartwrapper. */

            google.visualization.events.addListener($scope.chartWrapper, 'ready', function () {
                bindChartEvents($scope, eventNames, $scope.chartWrapper.getChart(), $elm);
            });

        }
    };
    function bindChartEvents(scope, eventsStr, chart, element) {
      angular.forEach(eventsStr.split(' '), function (eventName) {
          google.visualization.events.addListener(chart, eventName, function (event) {
              var newEventName = 'chart-' + eventName;
              element.triggerHandler(newEventName, event);
              if (!scope.$$phase){
                  scope.$apply();
              }
          });
      });
  }
}]);

Answer 1:

我放弃了UI事件的一部分,并直接注入事件监听器给我传递到指令图表对象。

function bindChartEvents(scope) {
   if(typeof $scope.chart.methods !== "undefined" && typeof $scope.chart.methods.select  !== "undefined"){
        $scope.selectListener = google.visualization.events.addListener($scope.chartWrapper, "select", function (event) {
            $scope.chart.methods.select($scope.chartWrapper.getChart().getSelection(), event);
        });
    }
  });


文章来源: How to make interactive Google Charts in Angular (UI-event)?