blob: e9a27f13c6450dd68bd1d00646ab0400e50eb6b7 [file] [log] [blame]
Matteo Scandolocc571402016-05-03 15:13:59 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 3/24/16.
7 */
8
9(function () {
10 'use strict';
11
12 angular.module('xos.uiComponents')
13 /**
14 * @ngdoc directive
15 * @name xos.uiComponents.directive:xosSmartPie
16 * @restrict E
17 * @description The xos-table directive
18 * @param {Object} config The configuration for the component,
19 * it is composed by the name of an angular [$resource](https://docs.angularjs.org/api/ngResource/service/$resource)
20 * and a field name that is used to group the data.
21 * ```
22 * {
23 resource: 'Users',
24 groupBy: 'fieldName',
25 classes: 'my-custom-class',
Matteo Scandolo02d40342016-05-03 15:45:28 -070026 labelFormatter: (labels) => {
27 // here you can format your label,
28 // you should return an array with the same order
29 return labels;
30 }
Matteo Scandolocc571402016-05-03 15:13:59 -070031 }
32 * ```
33 * @scope
34 * @example
35 */
36 .directive('xosSmartPie', function(){
37 return {
38 restrict: 'E',
39 scope: {
40 config: '='
41 },
42 template: `
43 <canvas
44 class="chart chart-pie {{vm.config.classes}}"
45 chart-data="vm.data" chart-labels="vm.labels">
46 </canvas>
47 `,
48 bindToController: true,
49 controllerAs: 'vm',
50 controller: function($injector, _){
Matteo Scandolo02d40342016-05-03 15:45:28 -070051
Matteo Scandolocc571402016-05-03 15:13:59 -070052 this.Resource = $injector.get(this.config.resource);
53
54 const getData = () => {
55 this.Resource.query().$promise
56 .then((res) => {
57
58 if(!res[0]){
59 return;
60 }
61
62 // group data
63 let grouped = _.groupBy(res, this.config.groupBy);
64 this.data = _.reduce(Object.keys(grouped), (data, group) => data.concat(grouped[group].length), []);
65
66 // create labels
Matteo Scandolo02d40342016-05-03 15:45:28 -070067 this.labels = angular.isFunction(this.config.labelFormatter) ? this.config.labelFormatter(Object.keys(grouped)) : Object.keys(grouped);
Matteo Scandolocc571402016-05-03 15:13:59 -070068 });
69 }
70
71 getData();
72 }
73 };
74 });
75})();