0

I am trying to combine googlecharts with ngTable.

This is my chart: http://plnkr.co/edit/RB2G4zjwaTKrrrRVdyFz?p=preview. The data for the chart, I want to get them from this table: http://bazalt-cms.com/ng-table/example/21

To explain it better, in plnkr I've manually entered the data of the chart based to the example of ngTable (grouping by role: admin 4, user 12, moderator 7).

  • (a) group.data.length -> provides me each 4, 12, 7
  • (b) group.value -> provides me each admin, user, moderator

The big question is: how can I pass this data to the chart? how can I explain chart.draw($scope.data, options); it should take my data from (a) and (b) to create the chart.

ng-User
  • 243
  • 1
  • 6
  • 18

1 Answers1

0

I think you should pass the data through a service.. here's my version of what you are doing: http://plnkr.co/edit/56TAdPTMt5nwnMwhErQM?p=preview

app.controller('MyCtrl', function($scope,data) {
      var columns = [{ 'string' :'Topping' }, {'number' : 'Slices'}],
          rows = [[ 'User', 12 ], ['Administrator', 4], ['Moderator', 7]];

      $scope.data = data.get(columns,rows);
    });

    app.service('data',function() {
      return {
        get : function(_columns,_rows) {
          var _data = new google.visualization.DataTable();
          for(var i=0;i < _columns.length;i++){
        for(k in _columns[i]) {
            _data.addColumn(k,_columns[i][k]);
        }
          }
          _data.addRows(_rows);
          return _data;
        }
      }
    });

    app.directive('chart', function() {
        return {
          restrict: 'A',
          link: function($scope, $elm, $attrs) {
            var chart,data,
              // Set chart options
              options = {'title':'ROLE',
                           'width':400,
                           'height':300};
            $scope.$watch(function() {
              return JSON.stringify($scope[$attrs.chart]);
            },
            function(_d) {
              if(typeof _d !== 'undefined') {
                data = $scope[$attrs.chart]
                // Instantiate and draw our chart, passing in some options.
                chart = new google.visualization.PieChart($elm[0]);
                chart.draw(data, options);
              }
            });
          }
      }
    });
koolunix
  • 1,995
  • 16
  • 25
  • Here's an even better version: http://plnkr.co/edit/k2YyoiPYwDpXnQcTi4V1?p=preview – koolunix Feb 08 '14 at 00:05
  • Hi. thanks for your help. But how can I manipulate the datas of the chart dynamically? In practical the table will be dynamically. So the chart should get dynamic data not static. – ng-User Feb 08 '14 at 12:02
  • With this example-> http://plnkr.co/edit/k2YyoiPYwDpXnQcTi4V1?p=preview, this call-> "$scope.data = data.get();" in the controller represents some other area where you are getting data, maybe ajax, and could be run multiple times (you have not specified how you want this to update). As the object $scope.data changes, the $scope.$watch function in directive chart will rerun, and regenerate your chart – koolunix Feb 08 '14 at 20:24