|
5 | 5 | module.exports = factory( |
6 | 6 | typeof angular !== 'undefined' ? angular : require('angular'), |
7 | 7 | typeof Chart !== 'undefined' ? Chart : require('chart.js')); |
8 | | - } else if (typeof define === 'function' && define.amd) { |
| 8 | + } else if (typeof define === 'function' && define.amd) { |
9 | 9 | // AMD. Register as an anonymous module. |
10 | 10 | define(['angular', 'chart'], factory); |
11 | 11 | } else { |
12 | 12 | // Browser globals |
13 | 13 | if (typeof angular === 'undefined') { |
14 | | - throw new Error('AngularJS framework needs to be included, see https://angularjs.org/'); |
| 14 | + throw new Error('AngularJS framework needs to be included, see https://angularjs.org/'); |
15 | 15 | } else if (typeof Chart === 'undefined') { |
16 | 16 | throw new Error('Chart.js library needs to be included, see http://jtblin.github.io/angular-chart.js/'); |
17 | 17 | } |
|
35 | 35 | '#4D5360' // dark grey |
36 | 36 | ]; |
37 | 37 |
|
38 | | - var useExcanvas = typeof window.G_vmlCanvasManager === 'object' && |
| 38 | + const useExcanvas = typeof window.G_vmlCanvasManager === 'object' && |
39 | 39 | window.G_vmlCanvasManager !== null && |
40 | 40 | typeof window.G_vmlCanvasManager.initElement === 'function'; |
41 | 41 |
|
|
44 | 44 | return angular.module('chart.js', []) |
45 | 45 | .provider('ChartJs', ChartJsProvider) |
46 | 46 | .factory('ChartJsFactory', ['ChartJs', '$timeout', ChartJsFactory]) |
47 | | - .directive('chartBase', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory(); }]) |
48 | | - .directive('chartLine', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('line'); }]) |
49 | | - .directive('chartBar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('bar'); }]) |
50 | | - .directive('chartHorizontalBar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('horizontalBar'); }]) |
51 | | - .directive('chartRadar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('radar'); }]) |
52 | | - .directive('chartDoughnut', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('doughnut'); }]) |
53 | | - .directive('chartPie', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('pie'); }]) |
54 | | - .directive('chartPolarArea', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('polarArea'); }]) |
55 | | - .directive('chartBubble', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('bubble'); }]) |
| 47 | + .directive('chartBase', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory()]) |
| 48 | + .directive('chartLine', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('line')]) |
| 49 | + .directive('chartBar', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('bar')]) |
| 50 | + .directive('chartHorizontalBar', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('horizontalBar')]) |
| 51 | + .directive('chartRadar', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('radar')]) |
| 52 | + .directive('chartDoughnut', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('doughnut')]) |
| 53 | + .directive('chartPie', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('pie')]) |
| 54 | + .directive('chartPolarArea', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('polarArea')]) |
| 55 | + .directive('chartBubble', ['ChartJsFactory', (ChartJsFactory) => new ChartJsFactory('bubble')]) |
56 | 56 | .name; |
57 | 57 |
|
58 | 58 | /** |
|
65 | 65 | * }))) |
66 | 66 | */ |
67 | 67 | function ChartJsProvider () { |
68 | | - var options = { responsive: true }; |
69 | | - var ChartJs = { |
| 68 | + let options = { responsive: true }; |
| 69 | + const ChartJs = { |
70 | 70 | Chart: Chart, |
71 | | - getOptions: function (type) { |
72 | | - var typeOptions = type && options[type] || {}; |
| 71 | + getOptions: (type) => { |
| 72 | + const typeOptions = type && options[type] || {}; |
73 | 73 | return angular.extend({}, options, typeOptions); |
74 | 74 | } |
75 | 75 | }; |
|
90 | 90 | angular.merge(ChartJs.Chart.defaults, options); |
91 | 91 | }; |
92 | 92 |
|
93 | | - this.$get = function () { |
94 | | - return ChartJs; |
95 | | - }; |
| 93 | + this.$get = () => ChartJs; |
96 | 94 | } |
97 | 95 |
|
98 | 96 | function ChartJsFactory (ChartJs, $timeout) { |
|
123 | 121 | scope.$watch('chartDatasetOverride', watchOther, true); |
124 | 122 | scope.$watch('chartType', watchType, false); |
125 | 123 |
|
126 | | - scope.$on('$destroy', function () { |
127 | | - destroyChart(scope); |
128 | | - }); |
| 124 | + scope.$on('$destroy', () => destroyChart(scope)); |
129 | 125 |
|
130 | | - scope.$on('$resize', function () { |
| 126 | + scope.$on('$resize', () => { |
131 | 127 | if (scope.chart) scope.chart.resize(); |
132 | 128 | }); |
133 | 129 |
|
|
136 | 132 | destroyChart(scope); |
137 | 133 | return; |
138 | 134 | } |
139 | | - var chartType = type || scope.chartType; |
| 135 | + const chartType = type || scope.chartType; |
140 | 136 | if (! chartType) return; |
141 | 137 |
|
142 | 138 | if (scope.chart && canUpdateChart(newVal, oldVal)) |
|
148 | 144 | function watchOther (newVal, oldVal) { |
149 | 145 | if (isEmpty(newVal)) return; |
150 | 146 | if (angular.equals(newVal, oldVal)) return; |
151 | | - var chartType = type || scope.chartType; |
| 147 | + const chartType = type || scope.chartType; |
152 | 148 | if (! chartType) return; |
153 | 149 |
|
154 | 150 | // chart.update() doesn't work for series and labels |
|
166 | 162 | }; |
167 | 163 |
|
168 | 164 | function createChart (type, scope, elem) { |
169 | | - var options = getChartOptions(type, scope); |
| 165 | + const options = getChartOptions(type, scope); |
170 | 166 | if (! hasData(scope) || ! canDisplay(type, scope, elem, options)) return; |
171 | 167 |
|
172 | | - var cvs = elem[0]; |
173 | | - var ctx = cvs.getContext('2d'); |
| 168 | + const cvs = elem[0]; |
| 169 | + const ctx = cvs.getContext('2d'); |
174 | 170 |
|
175 | 171 | scope.chartGetColor = getChartColorFn(scope); |
176 | | - var data = getChartData(type, scope); |
| 172 | + const data = getChartData(type, scope); |
177 | 173 | // Destroy old chart if it exists to avoid ghost charts issue |
178 | 174 | // https://github.com/jtblin/angular-chart.js/issues/187 |
179 | 175 | destroyChart(scope); |
|
190 | 186 | function canUpdateChart (newVal, oldVal) { |
191 | 187 | if (newVal && oldVal && newVal.length && oldVal.length) { |
192 | 188 | return Array.isArray(newVal[0]) ? |
193 | | - newVal.length === oldVal.length && newVal.every(function (element, index) { |
194 | | - return element.length === oldVal[index].length; }) : |
| 189 | + newVal.length === oldVal.length && newVal.every((element, index) => element.length === oldVal[index].length) : |
195 | 190 | oldVal.reduce(sum, 0) > 0 ? newVal.length === oldVal.length : false; |
196 | 191 | } |
197 | 192 | return false; |
|
202 | 197 | } |
203 | 198 |
|
204 | 199 | function getEventHandler (scope, action, triggerOnlyOnChange) { |
205 | | - var lastState = { |
206 | | - point: void 0, |
207 | | - points: void 0 |
| 200 | + const lastState = { |
| 201 | + point: undefined, |
| 202 | + points: undefined |
208 | 203 | }; |
209 | 204 | return function (evt) { |
210 | | - var atEvent = scope.chart.getElementAtEvent || scope.chart.getPointAtEvent; |
211 | | - var atEvents = scope.chart.getElementsAtEvent || scope.chart.getPointsAtEvent; |
| 205 | + const atEvent = scope.chart.getElementAtEvent || scope.chart.getPointAtEvent; |
| 206 | + const atEvents = scope.chart.getElementsAtEvent || scope.chart.getPointsAtEvent; |
212 | 207 | if (atEvents) { |
213 | | - var points = atEvents.call(scope.chart, evt); |
214 | | - var point = atEvent ? atEvent.call(scope.chart, evt)[0] : void 0; |
| 208 | + const points = atEvents.call(scope.chart, evt); |
| 209 | + const point = atEvent ? atEvent.call(scope.chart, evt)[0] : undefined; |
215 | 210 |
|
216 | 211 | if (triggerOnlyOnChange === false || |
217 | 212 | (! angular.equals(lastState.points, points) && ! angular.equals(lastState.point, point)) |
|
225 | 220 | } |
226 | 221 |
|
227 | 222 | function getColors (type, scope) { |
228 | | - var colors = angular.copy(scope.chartColors || |
| 223 | + const colors = angular.copy(scope.chartColors || |
229 | 224 | ChartJs.getOptions(type).chartColors || |
230 | 225 | Chart.defaults.global.colors |
231 | 226 | ); |
232 | | - var notEnoughColors = colors.length < scope.chartData.length; |
| 227 | + const notEnoughColors = colors.length < scope.chartData.length; |
233 | 228 | while (colors.length < scope.chartData.length) { |
234 | 229 | colors.push(scope.chartGetColor()); |
235 | 230 | } |
|
250 | 245 | } |
251 | 246 |
|
252 | 247 | function getRandomColor () { |
253 | | - var color = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)]; |
| 248 | + const color = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)]; |
254 | 249 | return getColor(color); |
255 | 250 | } |
256 | 251 |
|
257 | 252 | function getColor (color) { |
258 | | - var alpha = color[3] || 1; |
| 253 | + const alpha = color[3] || 1; |
259 | 254 | color = color.slice(0, 3); |
260 | 255 | return { |
261 | 256 | backgroundColor: rgba(color, 0.2), |
|
273 | 268 |
|
274 | 269 | function rgba (color, alpha) { |
275 | 270 | // rgba not supported by IE8 |
276 | | - return useExcanvas ? 'rgb(' + color.join(',') + ')' : 'rgba(' + color.concat(alpha).join(',') + ')'; |
| 271 | + return useExcanvas ? `rgb(${color.join(',')})` : `rgba(${color.concat(alpha).join(',')})`; |
277 | 272 | } |
278 | 273 |
|
279 | 274 | // Credit: http://stackoverflow.com/a/11508164/1190235 |
280 | 275 | function hexToRgb (hex) { |
281 | | - var bigint = parseInt(hex, 16), |
282 | | - r = (bigint >> 16) & 255, |
283 | | - g = (bigint >> 8) & 255, |
284 | | - b = bigint & 255; |
| 276 | + const bigint = parseInt(hex, 16); |
| 277 | + const r = (bigint >> 16) & 255; |
| 278 | + const g = (bigint >> 8) & 255; |
| 279 | + const b = bigint & 255; |
285 | 280 |
|
286 | 281 | return [r, g, b]; |
287 | 282 | } |
288 | 283 |
|
289 | 284 | function rgbStringToRgb (color) { |
290 | | - var match = color.match(/^rgba?\(([\d,.]+)\)$/); |
| 285 | + const match = color.match(/^rgba?\(([\d,.]+)\)$/); |
291 | 286 | if (! match) throw new Error('Cannot parse rgb value'); |
292 | 287 | color = match[1].split(','); |
293 | 288 | return color.map(Number); |
|
302 | 297 | } |
303 | 298 |
|
304 | 299 | function getChartData (type, scope) { |
305 | | - var colors = getColors(type, scope); |
| 300 | + const colors = getColors(type, scope); |
306 | 301 | return Array.isArray(scope.chartData[0]) ? |
307 | 302 | getDataSets(scope.chartLabels, scope.chartData, scope.chartSeries || [], colors, scope.chartDatasetOverride) : |
308 | 303 | getData(scope.chartLabels, scope.chartData, colors, scope.chartDatasetOverride); |
|
311 | 306 | function getDataSets (labels, data, series, colors, datasetOverride) { |
312 | 307 | return { |
313 | 308 | labels: labels, |
314 | | - datasets: data.map(function (item, i) { |
315 | | - var dataset = angular.extend({}, colors[i], { |
| 309 | + datasets: data.map((item, i) => { |
| 310 | + const dataset = angular.extend({}, colors[i], { |
316 | 311 | label: series[i], |
317 | 312 | data: item |
318 | 313 | }); |
|
325 | 320 | } |
326 | 321 |
|
327 | 322 | function getData (labels, data, colors, datasetOverride) { |
328 | | - var dataset = { |
| 323 | + const dataset = { |
329 | 324 | labels: labels, |
330 | 325 | datasets: [{ |
331 | 326 | data: data, |
332 | | - backgroundColor: colors.map(function (color) { |
333 | | - return color.pointBackgroundColor; |
334 | | - }), |
335 | | - hoverBackgroundColor: colors.map(function (color) { |
336 | | - return color.backgroundColor; |
337 | | - }) |
| 327 | + backgroundColor: colors.map((color) => color.pointBackgroundColor), |
| 328 | + hoverBackgroundColor: colors.map((color) => color.backgroundColor) |
338 | 329 | }] |
339 | 330 | }; |
340 | 331 | if (datasetOverride) { |
|
354 | 345 |
|
355 | 346 | function updateChart (values, scope) { |
356 | 347 | if (Array.isArray(scope.chartData[0])) { |
357 | | - scope.chart.data.datasets.forEach(function (dataset, i) { |
| 348 | + scope.chart.data.datasets.forEach((dataset, i) => { |
358 | 349 | dataset.data = values[i]; |
359 | 350 | }); |
360 | 351 | } else { |
|
374 | 365 | function canDisplay (type, scope, elem, options) { |
375 | 366 | // TODO: check parent? |
376 | 367 | if (options.responsive && elem[0].clientHeight === 0) { |
377 | | - $timeout(function () { |
| 368 | + $timeout(() => { |
378 | 369 | createChart(type, scope, elem); |
379 | 370 | }, 50, false); |
380 | 371 | return false; |
|
0 commit comments