Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 1 | /** |
| 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 Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 26 | labelFormatter: (labels) => { |
| 27 | // here you can format your label, |
| 28 | // you should return an array with the same order |
| 29 | return labels; |
| 30 | } |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 31 | } |
| 32 | * ``` |
| 33 | * @scope |
| 34 | * @example |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame^] | 35 | <example module="sampleSmartPie"> |
| 36 | <file name="index.html"> |
| 37 | <div ng-controller="SampleCtrl as vm"> |
| 38 | <xos-smart-pie config="vm.config"></xos-smart-pie> |
| 39 | </div> |
| 40 | </file> |
| 41 | <file name="script.js"> |
| 42 | angular.module('sampleSmartPie', ['xos.uiComponents', 'ngResource', 'ngMockE2E']) |
| 43 | .controller('SampleCtrl', function(){ |
| 44 | this.config = { |
| 45 | resource: 'SampleResource', |
| 46 | groupBy: 'category', |
| 47 | poll: 2, |
| 48 | labelFormatter: (labels) => { |
| 49 | return labels.map(l => l === '1' ? 'Active' : 'Banned'); |
| 50 | } |
| 51 | }; |
| 52 | }); |
| 53 | </file> |
| 54 | <file name="backend.js"> |
| 55 | angular.module('sampleSmartPie') |
| 56 | .run(function($httpBackend, _){ |
| 57 | let mock = [ |
| 58 | [ |
| 59 | {id: 1, first_name: 'Jon', last_name: 'Snow', category: 1}, |
| 60 | {id: 2, first_name: 'Danaerys', last_name: 'Targaryen', category: 2}, |
| 61 | {id: 3, first_name: 'Aria', last_name: 'Stark', category: 1}, |
| 62 | {id: 3, first_name: 'Tyrion', last_name: 'Lannister', category: 1} |
| 63 | ], |
| 64 | |
| 65 | [ |
| 66 | {id: 1, first_name: 'Jon', last_name: 'Snow', category: 1}, |
| 67 | {id: 2, first_name: 'Danaerys', last_name: 'Targaryen', category: 2}, |
| 68 | {id: 3, first_name: 'Aria', last_name: 'Stark', category: 2}, |
| 69 | {id: 3, first_name: 'Tyrion', last_name: 'Lannister', category: 2} |
| 70 | ], |
| 71 | |
| 72 | [ |
| 73 | {id: 1, first_name: 'Jon', last_name: 'Snow', category: 1}, |
| 74 | {id: 2, first_name: 'Danaerys', last_name: 'Targaryen', category: 2}, |
| 75 | {id: 3, first_name: 'Aria', last_name: 'Stark', category: 1}, |
| 76 | {id: 3, first_name: 'Tyrion', last_name: 'Lannister', category: 2} |
| 77 | ] |
| 78 | ]; |
| 79 | $httpBackend.whenGET('/test').respond(function(method, url, data, headers, params) { |
| 80 | return [200, mock[Math.round(Math.random() * 3)]]; |
| 81 | }); |
| 82 | }) |
| 83 | .factory('_', function($window){ |
| 84 | return $window._; |
| 85 | }) |
| 86 | .service('SampleResource', function($resource){ |
| 87 | return $resource('/test/:id', {id: '@id'}); |
| 88 | }) |
| 89 | </file> |
| 90 | </example> |
| 91 | |
| 92 | <example module="sampleSmartPiePoll"> |
| 93 | <file name="index.html"> |
| 94 | <div ng-controller="SampleCtrl as vm"> |
| 95 | <xos-smart-pie config="vm.config"></xos-smart-pie> |
| 96 | </div> |
| 97 | </file> |
| 98 | <file name="script.js"> |
| 99 | angular.module('sampleSmartPiePoll', ['xos.uiComponents', 'ngResource', 'ngMockE2E']) |
| 100 | .controller('SampleCtrl', function(){ |
| 101 | this.config = { |
| 102 | resource: 'SampleResource', |
| 103 | groupBy: 'category', |
| 104 | labelFormatter: (labels) => { |
| 105 | return labels.map(l => l === '1' ? 'North' : 'Dragon'); |
| 106 | } |
| 107 | }; |
| 108 | }); |
| 109 | </file> |
| 110 | <file name="backendPoll.js"> |
| 111 | angular.module('sampleSmartPiePoll') |
| 112 | .run(function($httpBackend, _){ |
| 113 | let datas = [ |
| 114 | {id: 1, first_name: 'Jon', last_name: 'Snow', category: 1}, |
| 115 | {id: 2, first_name: 'Danaerys', last_name: 'Targaryen', category: 2}, |
| 116 | {id: 3, first_name: 'Aria', last_name: 'Stark', category: 1} |
| 117 | ]; |
| 118 | |
| 119 | $httpBackend.whenGET('/test').respond(200, datas) |
| 120 | }) |
| 121 | .factory('_', function($window){ |
| 122 | return $window._; |
| 123 | }) |
| 124 | .service('SampleResource', function($resource){ |
| 125 | return $resource('/test/:id', {id: '@id'}); |
| 126 | }) |
| 127 | </file> |
| 128 | </example> |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 129 | */ |
| 130 | .directive('xosSmartPie', function(){ |
| 131 | return { |
| 132 | restrict: 'E', |
| 133 | scope: { |
| 134 | config: '=' |
| 135 | }, |
| 136 | template: ` |
| 137 | <canvas |
| 138 | class="chart chart-pie {{vm.config.classes}}" |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame^] | 139 | chart-data="vm.data" chart-labels="vm.labels" |
| 140 | chart-legend="{{vm.config.legend}}"> |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 141 | </canvas> |
| 142 | `, |
| 143 | bindToController: true, |
| 144 | controllerAs: 'vm', |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame^] | 145 | controller: function($injector, $timeout, $interval, _){ |
Matteo Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 146 | |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 147 | this.Resource = $injector.get(this.config.resource); |
| 148 | |
| 149 | const getData = () => { |
| 150 | this.Resource.query().$promise |
| 151 | .then((res) => { |
| 152 | |
| 153 | if(!res[0]){ |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | // group data |
| 158 | let grouped = _.groupBy(res, this.config.groupBy); |
| 159 | this.data = _.reduce(Object.keys(grouped), (data, group) => data.concat(grouped[group].length), []); |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 160 | // create labels |
Matteo Scandolo | 02d4034 | 2016-05-03 15:45:28 -0700 | [diff] [blame] | 161 | this.labels = angular.isFunction(this.config.labelFormatter) ? this.config.labelFormatter(Object.keys(grouped)) : Object.keys(grouped); |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 162 | }); |
| 163 | } |
| 164 | |
| 165 | getData(); |
Matteo Scandolo | 68df51b | 2016-05-03 17:07:01 -0700 | [diff] [blame^] | 166 | |
| 167 | if(this.config.poll){ |
| 168 | $interval(() => {getData()}, this.config.poll * 1000) |
| 169 | } |
Matteo Scandolo | cc57140 | 2016-05-03 15:13:59 -0700 | [diff] [blame] | 170 | } |
| 171 | }; |
| 172 | }); |
| 173 | })(); |