23a4b3612c8e6501b26bbbbc967f5dfd2304d056
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnCommons / mwtnCommons-module / src / main / resources / mwtnCommons / bower_components_patches / angular-chart.js.patch
1 /*!
2  * angular-chart.js - An angular.js wrapper for Chart.js
3  * http://jtblin.github.io/angular-chart.js/
4  * Version: 1.1.1
5  *
6  * Copyright 2016 Jerome Touffe-Blin
7  * Released under the BSD-2-Clause license
8  * https://github.com/jtblin/angular-chart.js/blob/master/LICENSE
9  */
10 (function (factory) {
11   'use strict';
12   if (typeof exports === 'object') {
13     // Node/CommonJS
14     module.exports = factory(
15       typeof angular !== 'undefined' ? angular : require('angular'),
16       typeof Chart !== 'undefined' ? Chart : require('chart.js'));
17   }  else if (typeof define === 'function' && define.amd) {
18     // AMD. Register as an anonymous module.
19     define(['angular', 'app/mwtnCommons/bower_components/chart.js/dist/Chart'], factory);
20   } else {
21     // Browser globals
22     if (typeof angular === 'undefined') {
23         throw new Error('AngularJS framework needs to be included, see https://angularjs.org/');
24     } else if (typeof Chart === 'undefined') {
25       throw new Error('Chart.js library needs to be included, see http://jtblin.github.io/angular-chart.js/');
26     }
27     factory(angular, Chart);
28   }
29 }(function (angular, Chart) {
30   'use strict';
31
32   Chart.defaults.global.multiTooltipTemplate = '<%if (datasetLabel){%><%=datasetLabel%>: <%}%><%= value %>';
33   Chart.defaults.global.tooltips.mode = 'label';
34   Chart.defaults.global.elements.line.borderWidth = 2;
35   Chart.defaults.global.elements.rectangle.borderWidth = 2;
36   Chart.defaults.global.legend.display = false;
37   Chart.defaults.global.colors = [
38     '#97BBCD', // blue
39     '#DCDCDC', // light grey
40     '#F7464A', // red
41     '#46BFBD', // green
42     '#FDB45C', // yellow
43     '#949FB1', // grey
44     '#4D5360'  // dark grey
45   ];
46
47   var useExcanvas = typeof window.G_vmlCanvasManager === 'object' &&
48     window.G_vmlCanvasManager !== null &&
49     typeof window.G_vmlCanvasManager.initElement === 'function';
50
51   if (useExcanvas) Chart.defaults.global.animation = false;
52
53   return angular.module('chart.js', [])
54     .provider('ChartJs', ChartJsProvider)
55     .factory('ChartJsFactory', ['ChartJs', '$timeout', ChartJsFactory])
56     .directive('chartBase', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory(); }])
57     .directive('chartLine', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('line'); }])
58     .directive('chartBar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('bar'); }])
59     .directive('chartHorizontalBar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('horizontalBar'); }])
60     .directive('chartRadar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('radar'); }])
61     .directive('chartDoughnut', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('doughnut'); }])
62     .directive('chartPie', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('pie'); }])
63     .directive('chartPolarArea', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('polarArea'); }])
64     .directive('chartBubble', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('bubble'); }])
65     .name;
66
67   /**
68    * Wrapper for chart.js
69    * Allows configuring chart js using the provider
70    *
71    * angular.module('myModule', ['chart.js']).config(function(ChartJsProvider) {
72    *   ChartJsProvider.setOptions({ responsive: false });
73    *   ChartJsProvider.setOptions('Line', { responsive: true });
74    * })))
75    */
76   function ChartJsProvider () {
77     var options = { responsive: true };
78     var ChartJs = {
79       Chart: Chart,
80       getOptions: function (type) {
81         var typeOptions = type && options[type] || {};
82         return angular.extend({}, options, typeOptions);
83       }
84     };
85
86     /**
87      * Allow to set global options during configuration
88      */
89     this.setOptions = function (type, customOptions) {
90       // If no type was specified set option for the global object
91       if (! customOptions) {
92         customOptions = type;
93         options = angular.merge(options, customOptions);
94       } else {
95         // Set options for the specific chart
96         options[type] = angular.merge(options[type] || {}, customOptions);
97       }
98
99       angular.merge(ChartJs.Chart.defaults, options);
100     };
101
102     this.$get = function () {
103       return ChartJs;
104     };
105   }
106
107   function ChartJsFactory (ChartJs, $timeout) {
108     return function chart (type) {
109       return {
110         restrict: 'CA',
111         scope: {
112           chartGetColor: '=?',
113           chartType: '=',
114           chartData: '=?',
115           chartLabels: '=?',
116           chartOptions: '=?',
117           chartSeries: '=?',
118           chartColors: '=?',
119           chartClick: '=?',
120           chartHover: '=?',
121           chartDatasetOverride: '=?'
122         },
123         link: function (scope, elem/*, attrs */) {
124           if (useExcanvas) window.G_vmlCanvasManager.initElement(elem[0]);
125
126           // Order of setting "watch" matter
127           scope.$watch('chartData', watchData, true);
128           scope.$watch('chartSeries', watchOther, true);
129           scope.$watch('chartLabels', watchOther, true);
130           scope.$watch('chartOptions', watchOther, true);
131           scope.$watch('chartColors', watchOther, true);
132           scope.$watch('chartDatasetOverride', watchOther, true);
133           scope.$watch('chartType', watchType, false);
134
135           scope.$on('$destroy', function () {
136             destroyChart(scope);
137           });
138
139           scope.$on('$resize', function () {
140             if (scope.chart) scope.chart.resize();
141           });
142
143           function watchData (newVal, oldVal) {
144             if (! newVal || ! newVal.length || (Array.isArray(newVal[0]) && ! newVal[0].length)) {
145               destroyChart(scope);
146               return;
147             }
148             var chartType = type || scope.chartType;
149             if (! chartType) return;
150
151             if (scope.chart && canUpdateChart(newVal, oldVal))
152               return updateChart(newVal, scope);
153
154             createChart(chartType, scope, elem);
155           }
156
157           function watchOther (newVal, oldVal) {
158             if (isEmpty(newVal)) return;
159             if (angular.equals(newVal, oldVal)) return;
160             var chartType = type || scope.chartType;
161             if (! chartType) return;
162
163             // chart.update() doesn't work for series and labels
164             // so we have to re-create the chart entirely
165             createChart(chartType, scope, elem);
166           }
167
168           function watchType (newVal, oldVal) {
169             if (isEmpty(newVal)) return;
170             if (angular.equals(newVal, oldVal)) return;
171             createChart(newVal, scope, elem);
172           }
173         }
174       };
175     };
176
177     function createChart (type, scope, elem) {
178       var options = getChartOptions(type, scope);
179       if (! hasData(scope) || ! canDisplay(type, scope, elem, options)) return;
180
181       var cvs = elem[0];
182       var ctx = cvs.getContext('2d');
183
184       scope.chartGetColor = getChartColorFn(scope);
185       var data = getChartData(type, scope);
186       // Destroy old chart if it exists to avoid ghost charts issue
187       // https://github.com/jtblin/angular-chart.js/issues/187
188       destroyChart(scope);
189
190       scope.chart = new ChartJs.Chart(ctx, {
191         type: type,
192         data: data,
193         options: options
194       });
195       scope.$emit('chart-create', scope.chart);
196       bindEvents(cvs, scope);
197     }
198
199     function canUpdateChart (newVal, oldVal) {
200       if (newVal && oldVal && newVal.length && oldVal.length) {
201         return Array.isArray(newVal[0]) ?
202         newVal.length === oldVal.length && newVal.every(function (element, index) {
203           return element.length === oldVal[index].length; }) :
204           oldVal.reduce(sum, 0) > 0 ? newVal.length === oldVal.length : false;
205       }
206       return false;
207     }
208
209     function sum (carry, val) {
210       return carry + val;
211     }
212
213     function getEventHandler (scope, action, triggerOnlyOnChange) {
214       var lastState = {
215         point: void 0,
216         points: void 0
217       };
218       return function (evt) {
219         var atEvent = scope.chart.getElementAtEvent || scope.chart.getPointAtEvent;
220         var atEvents = scope.chart.getElementsAtEvent || scope.chart.getPointsAtEvent;
221         if (atEvents) {
222           var points = atEvents.call(scope.chart, evt);
223           var point = atEvent ? atEvent.call(scope.chart, evt)[0] : void 0;
224
225           if (triggerOnlyOnChange === false ||
226             (! angular.equals(lastState.points, points) && ! angular.equals(lastState.point, point))
227           ) {
228             lastState.point = point;
229             lastState.points = points;
230             scope[action](points, evt, point);
231           }
232         }
233       };
234     }
235
236     function getColors (type, scope) {
237       var colors = angular.copy(scope.chartColors ||
238         ChartJs.getOptions(type).chartColors ||
239         Chart.defaults.global.colors
240       );
241       var notEnoughColors = colors.length < scope.chartData.length;
242       while (colors.length < scope.chartData.length) {
243         colors.push(scope.chartGetColor());
244       }
245       // mutate colors in this case as we don't want
246       // the colors to change on each refresh
247       if (notEnoughColors) scope.chartColors = colors;
248       return colors.map(convertColor);
249     }
250
251     function convertColor (color) {
252       // Allows RGB and RGBA colors to be input as a string: e.g.: "rgb(159,204,0)", "rgba(159,204,0, 0.5)"
253       if (typeof color === 'string' && color[0] === 'r') return getColor(rgbStringToRgb(color));
254       // Allows hex colors to be input as a string.
255       if (typeof color === 'string' && color[0] === '#') return getColor(hexToRgb(color.substr(1)));
256       // Allows colors to be input as an object, bypassing getColor() entirely
257       if (typeof color === 'object' && color !== null) return color;
258       return getRandomColor();
259     }
260
261     function getRandomColor () {
262       var color = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)];
263       return getColor(color);
264     }
265
266     function getColor (color) {
267       var alpha = color[3] || 1;
268       color = color.slice(0, 3);
269       return {
270         backgroundColor: rgba(color, 0.2),
271         pointBackgroundColor: rgba(color, alpha),
272         pointHoverBackgroundColor: rgba(color, 0.8),
273         borderColor: rgba(color, alpha),
274         pointBorderColor: '#fff',
275         pointHoverBorderColor: rgba(color, alpha)
276       };
277     }
278
279     function getRandomInt (min, max) {
280       return Math.floor(Math.random() * (max - min + 1)) + min;
281     }
282
283     function rgba (color, alpha) {
284       // rgba not supported by IE8
285       return useExcanvas ? 'rgb(' + color.join(',') + ')' : 'rgba(' + color.concat(alpha).join(',') + ')';
286     }
287
288     // Credit: http://stackoverflow.com/a/11508164/1190235
289     function hexToRgb (hex) {
290       var bigint = parseInt(hex, 16),
291         r = (bigint >> 16) & 255,
292         g = (bigint >> 8) & 255,
293         b = bigint & 255;
294
295       return [r, g, b];
296     }
297
298     function rgbStringToRgb (color) {
299       var match = color.match(/^rgba?\(([\d,.]+)\)$/);
300       if (! match) throw new Error('Cannot parse rgb value');
301       color = match[1].split(',');
302       return color.map(Number);
303     }
304
305     function hasData (scope) {
306       return scope.chartData && scope.chartData.length;
307     }
308
309     function getChartColorFn (scope) {
310       return typeof scope.chartGetColor === 'function' ? scope.chartGetColor : getRandomColor;
311     }
312
313     function getChartData (type, scope) {
314       var colors = getColors(type, scope);
315       return Array.isArray(scope.chartData[0]) ?
316         getDataSets(scope.chartLabels, scope.chartData, scope.chartSeries || [], colors, scope.chartDatasetOverride) :
317         getData(scope.chartLabels, scope.chartData, colors, scope.chartDatasetOverride);
318     }
319
320     function getDataSets (labels, data, series, colors, datasetOverride) {
321       return {
322         labels: labels,
323         datasets: data.map(function (item, i) {
324           var dataset = angular.extend({}, colors[i], {
325             label: series[i],
326             data: item
327           });
328           if (datasetOverride && datasetOverride.length >= i) {
329             angular.merge(dataset, datasetOverride[i]);
330           }
331           return dataset;
332         })
333       };
334     }
335
336     function getData (labels, data, colors, datasetOverride) {
337       var dataset = {
338         labels: labels,
339         datasets: [{
340           data: data,
341           backgroundColor: colors.map(function (color) {
342             return color.pointBackgroundColor;
343           }),
344           hoverBackgroundColor: colors.map(function (color) {
345             return color.backgroundColor;
346           })
347         }]
348       };
349       if (datasetOverride) {
350         angular.merge(dataset.datasets[0], datasetOverride);
351       }
352       return dataset;
353     }
354
355     function getChartOptions (type, scope) {
356       return angular.extend({}, ChartJs.getOptions(type), scope.chartOptions);
357     }
358
359     function bindEvents (cvs, scope) {
360       cvs.onclick = scope.chartClick ? getEventHandler(scope, 'chartClick', false) : angular.noop;
361       cvs.onmousemove = scope.chartHover ? getEventHandler(scope, 'chartHover', true) : angular.noop;
362     }
363
364     function updateChart (values, scope) {
365       if (Array.isArray(scope.chartData[0])) {
366         scope.chart.data.datasets.forEach(function (dataset, i) {
367           dataset.data = values[i];
368         });
369       } else {
370         scope.chart.data.datasets[0].data = values;
371       }
372
373       scope.chart.update();
374       scope.$emit('chart-update', scope.chart);
375     }
376
377     function isEmpty (value) {
378       return ! value ||
379         (Array.isArray(value) && ! value.length) ||
380         (typeof value === 'object' && ! Object.keys(value).length);
381     }
382
383     function canDisplay (type, scope, elem, options) {
384       // TODO: check parent?
385       if (options.responsive && elem[0].clientHeight === 0) {
386         $timeout(function () {
387           createChart(type, scope, elem);
388         }, 50, false);
389         return false;
390       }
391       return true;
392     }
393
394     function destroyChart(scope) {
395       if(! scope.chart) return;
396       scope.chart.destroy();
397       scope.$emit('chart-destroy', scope.chart);
398     }
399   }
400 }));