angularjs - Chart.js with factory and webservices (Ionic) -
i'm trying use ionic framework angularjs build mobile application.
i'm adding data web services in piechart chart.js.
my factory :
.factory('srvstatproduit', function($http) { var baseurl = ''; var statpro = []; //var baseurl = 'http://webstock/api/'; //var cmdall = $http.get(baseurl + '/api/stat/detail'); return { all: function() { return $http.get(baseurl + '/api/stat/detail'); } }; })
my controller :
.controller("piectrl", function ($scope,$http, srvstatproduit, $stateparams) { srvstatproduit.all().success(function(data) { $scope.statpros = data; console.log(data); //exemple pie chart chart.js $scope.labels = ["download sales", "in-store sales", "mail-order sales"]; $scope.data = [data.qte]; });
my html:
<div ng-controller="piectrl"> <div> <canvas id="pie" class="chart chart-pie" legend="true" data="data" labels="labels"></canvas> </div> </div>
i don't know how can use data web services. if use ng-repeat
in html, if call data in controller.
edit :
first, reply.
in controller have added :
.controller("piectrl", function ($scope,$http, srvstatproduit, $stateparams) { srvstatproduit.all().success(function(data) { $scope.statpro = data; var x = new array(); (var = 1, len = $scope.statpro.length; < len; i++) { $scope.data = [data[i].qte]; $scope.labels = [$scope.statpro[i].lib_prod]; } });
});
my pie chart add data web services last data of table.
how can ?
thank you
last edit :
the solution
.controller("piectrl", function ($scope,$http, srvstatproduit, $stateparams) { srvstatproduit.all().success(function(data) { $scope.statpro = data; var myarray = []; var mylib = []; (var = 1, len = $scope.statpro.length; < len; i++) { console.log(i); myarray.push($scope.statpro[i].qte); mylib.push([$scope.statpro[i].lib_prod]); } $scope.data = myarray; $scope.labels = mylib; });
});
thank ^^
so if using $scope.statpros hold data , data array of objects add lables statpros. each object has statpros.data object , statpros.labels this:
[{data: {//data object}, labels: {//label object}}, {data: {//data object}, labels: {//label object}}]
in html can do
<div ng-repeat="item in statpros"> <canvas id="pie" class="chart chart-pie" legend="true" data="item.data" labels="item.labels"></canvas> </div>
Comments
Post a Comment