Angularjs google Chart with dynamic data

Asked

Viewed 240 times

4

Example of how my code is

http://plnkr.co/edit/hMnKAzjbkQzHWjL5p6KX?p=previewangularjs

I receive the data from the webservice and organize this way.

var avaliacoes_descTipo = [];
var avaliacoes_quantidade = [];

angular.forEach(data, function(value, key) { 
                this.push(key + ': ' + value);
                avaliacoes_descTipo[key] = value.descTipo;
                avaliacoes_quantidade[key] = value.quantidade;
 }, log_avaliacoes);

However I am not able to put dynamic values, put an array, I do not know, so I put fixed, as the example below.

**{w: [
                     {V: avaliacoes_descTipo [1]},
                     {V: avaliacoes_quantidade [1]}
                 ]}
                 {w: [
                     {V: avaliacoes_descTipo [2]},
                     {V: avaliacoes_quantidade [2]}
                 ]}
]};**

I need help to get dynamic, by foreach, any way I don’t need to keep putting the content in it.?

1 answer

3


Try doing it this way:

app.controller('MainCtrl', function($scope) {
   var avaliacoes_descTipo = ["1", "12", "13"];
   var avaliacoes_quantidade = [1,2,3];
   var val = [];
   var i;
   var val1 = [];
$scope.avaliacoes = {};

for (i = 0; i < avaliacoes_descTipo.length; i++) {
  val = {c: [
    {v: avaliacoes_descTipo[i]},
    {v: avaliacoes_quantidade[i]}
  ]};

  val1.push(val);
}

$scope.avaliacoes.type = "PieChart";

$scope.onions = [
  {v: avaliacoes_descTipo[0]},
  {v: avaliacoes_quantidade[0]}
];

$scope.avaliacoes.data = {"cols": [
  {id: "t", label: "Topping", type: "string"},
  {id: "s", label: "Slices", type: "number"}
], "rows":
  val1

};

$scope.avaliacoes.options = {
  'title': '',
  'legend' : {'position': 'bottom'}
};

});
  • 1

    Great answer! worked perfectly! thank you, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.