Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 1 | (function (factory) { |
| 2 | 'use strict'; |
| 3 | if (typeof exports === 'object') { |
| 4 | // Node/CommonJS |
| 5 | module.exports = factory( |
| 6 | typeof angular !== 'undefined' ? angular : require('angular'), |
| 7 | typeof Chart !== 'undefined' ? Chart : require('chart.js')); |
| 8 | } else if (typeof define === 'function' && define.amd) { |
| 9 | // AMD. Register as an anonymous module. |
| 10 | define(['angular', 'chart'], factory); |
| 11 | } else { |
| 12 | // Browser globals |
| 13 | factory(angular, Chart); |
| 14 | } |
| 15 | }(function (angular, Chart) { |
| 16 | 'use strict'; |
| 17 | |
| 18 | Chart.defaults.global.responsive = true; |
| 19 | Chart.defaults.global.multiTooltipTemplate = '<%if (datasetLabel){%><%=datasetLabel%>: <%}%><%= value %>'; |
| 20 | |
| 21 | Chart.defaults.global.colours = [ |
| 22 | '#97BBCD', // blue |
| 23 | '#DCDCDC', // light grey |
| 24 | '#F7464A', // red |
| 25 | '#46BFBD', // green |
| 26 | '#FDB45C', // yellow |
| 27 | '#949FB1', // grey |
| 28 | '#4D5360' // dark grey |
| 29 | ]; |
| 30 | |
| 31 | var usingExcanvas = typeof window.G_vmlCanvasManager === 'object' && |
| 32 | window.G_vmlCanvasManager !== null && |
| 33 | typeof window.G_vmlCanvasManager.initElement === 'function'; |
| 34 | |
| 35 | if (usingExcanvas) Chart.defaults.global.animation = false; |
| 36 | |
| 37 | return angular.module('chart.js', []) |
| 38 | .provider('ChartJs', ChartJsProvider) |
| 39 | .factory('ChartJsFactory', ['ChartJs', '$timeout', ChartJsFactory]) |
| 40 | .directive('chartBase', function (ChartJsFactory) { return new ChartJsFactory(); }) |
| 41 | .directive('chartLine', function (ChartJsFactory) { return new ChartJsFactory('Line'); }) |
| 42 | .directive('chartBar', function (ChartJsFactory) { return new ChartJsFactory('Bar'); }) |
| 43 | .directive('chartRadar', function (ChartJsFactory) { return new ChartJsFactory('Radar'); }) |
| 44 | .directive('chartDoughnut', function (ChartJsFactory) { return new ChartJsFactory('Doughnut'); }) |
| 45 | .directive('chartPie', function (ChartJsFactory) { return new ChartJsFactory('Pie'); }) |
| 46 | .directive('chartPolarArea', function (ChartJsFactory) { return new ChartJsFactory('PolarArea'); }); |
| 47 | |
| 48 | /** |
| 49 | * Wrapper for chart.js |
| 50 | * Allows configuring chart js using the provider |
| 51 | * |
| 52 | * angular.module('myModule', ['chart.js']).config(function(ChartJsProvider) { |
| 53 | * ChartJsProvider.setOptions({ responsive: true }); |
| 54 | * ChartJsProvider.setOptions('Line', { responsive: false }); |
| 55 | * }))) |
| 56 | */ |
| 57 | function ChartJsProvider () { |
| 58 | var options = {}; |
| 59 | var ChartJs = { |
| 60 | Chart: Chart, |
| 61 | getOptions: function (type) { |
| 62 | var typeOptions = type && options[type] || {}; |
| 63 | return angular.extend({}, options, typeOptions); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Allow to set global options during configuration |
| 69 | */ |
| 70 | this.setOptions = function (type, customOptions) { |
| 71 | // If no type was specified set option for the global object |
| 72 | if (! customOptions) { |
| 73 | customOptions = type; |
| 74 | options = angular.extend(options, customOptions); |
| 75 | return; |
| 76 | } |
| 77 | // Set options for the specific chart |
| 78 | options[type] = angular.extend(options[type] || {}, customOptions); |
| 79 | }; |
| 80 | |
| 81 | this.$get = function () { |
| 82 | return ChartJs; |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | function ChartJsFactory (ChartJs, $timeout) { |
| 87 | return function chart (type) { |
| 88 | return { |
| 89 | restrict: 'CA', |
| 90 | scope: { |
| 91 | data: '=?', |
| 92 | labels: '=?', |
| 93 | options: '=?', |
| 94 | series: '=?', |
| 95 | colours: '=?', |
| 96 | getColour: '=?', |
| 97 | chartType: '=', |
| 98 | legend: '@', |
| 99 | click: '=?', |
| 100 | hover: '=?', |
| 101 | |
| 102 | chartData: '=?', |
| 103 | chartLabels: '=?', |
| 104 | chartOptions: '=?', |
| 105 | chartSeries: '=?', |
| 106 | chartColours: '=?', |
| 107 | chartLegend: '@', |
| 108 | chartClick: '=?', |
| 109 | chartHover: '=?' |
| 110 | }, |
| 111 | link: function (scope, elem/*, attrs */) { |
| 112 | var chart, container = document.createElement('div'); |
| 113 | container.className = 'chart-container'; |
| 114 | elem.replaceWith(container); |
| 115 | container.appendChild(elem[0]); |
| 116 | |
| 117 | if (usingExcanvas) window.G_vmlCanvasManager.initElement(elem[0]); |
| 118 | |
| 119 | ['data', 'labels', 'options', 'series', 'colours', 'legend', 'click', 'hover'].forEach(deprecated); |
| 120 | function aliasVar (fromName, toName) { |
| 121 | scope.$watch(fromName, function (newVal) { |
| 122 | if (typeof newVal === 'undefined') return; |
| 123 | scope[toName] = newVal; |
| 124 | }); |
| 125 | } |
| 126 | /* provide backward compatibility to "old" directive names, by |
| 127 | * having an alias point from the new names to the old names. */ |
| 128 | aliasVar('chartData', 'data'); |
| 129 | aliasVar('chartLabels', 'labels'); |
| 130 | aliasVar('chartOptions', 'options'); |
| 131 | aliasVar('chartSeries', 'series'); |
| 132 | aliasVar('chartColours', 'colours'); |
| 133 | aliasVar('chartLegend', 'legend'); |
| 134 | aliasVar('chartClick', 'click'); |
| 135 | aliasVar('chartHover', 'hover'); |
| 136 | |
| 137 | // Order of setting "watch" matter |
| 138 | |
| 139 | scope.$watch('data', function (newVal, oldVal) { |
| 140 | if (! newVal || ! newVal.length || (Array.isArray(newVal[0]) && ! newVal[0].length)) return; |
| 141 | var chartType = type || scope.chartType; |
| 142 | if (! chartType) return; |
| 143 | |
| 144 | if (chart) { |
| 145 | if (canUpdateChart(newVal, oldVal)) return updateChart(chart, newVal, scope, elem); |
| 146 | chart.destroy(); |
| 147 | } |
| 148 | |
| 149 | createChart(chartType); |
| 150 | }, true); |
| 151 | |
| 152 | scope.$watch('series', resetChart, true); |
| 153 | scope.$watch('labels', resetChart, true); |
| 154 | scope.$watch('options', resetChart, true); |
| 155 | scope.$watch('colours', resetChart, true); |
| 156 | |
| 157 | scope.$watch('chartType', function (newVal, oldVal) { |
| 158 | if (isEmpty(newVal)) return; |
| 159 | if (angular.equals(newVal, oldVal)) return; |
| 160 | if (chart) chart.destroy(); |
| 161 | createChart(newVal); |
| 162 | }); |
| 163 | |
| 164 | scope.$on('$destroy', function () { |
| 165 | if (chart) chart.destroy(); |
| 166 | }); |
| 167 | |
| 168 | function resetChart (newVal, oldVal) { |
| 169 | if (isEmpty(newVal)) return; |
| 170 | if (angular.equals(newVal, oldVal)) return; |
| 171 | var chartType = type || scope.chartType; |
| 172 | if (! chartType) return; |
| 173 | |
| 174 | // chart.update() doesn't work for series and labels |
| 175 | // so we have to re-create the chart entirely |
| 176 | if (chart) chart.destroy(); |
| 177 | |
| 178 | createChart(chartType); |
| 179 | } |
| 180 | |
| 181 | function createChart (type) { |
| 182 | if (isResponsive(type, scope) && elem[0].clientHeight === 0 && container.clientHeight === 0) { |
| 183 | return $timeout(function () { |
| 184 | createChart(type); |
| 185 | }, 50, false); |
| 186 | } |
| 187 | if (! scope.data || ! scope.data.length) return; |
| 188 | scope.getColour = typeof scope.getColour === 'function' ? scope.getColour : getRandomColour; |
| 189 | scope.colours = getColours(type, scope); |
| 190 | var cvs = elem[0], ctx = cvs.getContext('2d'); |
| 191 | var data = Array.isArray(scope.data[0]) ? |
| 192 | getDataSets(scope.labels, scope.data, scope.series || [], scope.colours) : |
| 193 | getData(scope.labels, scope.data, scope.colours); |
| 194 | var options = angular.extend({}, ChartJs.getOptions(type), scope.options); |
| 195 | chart = new ChartJs.Chart(ctx)[type](data, options); |
| 196 | scope.$emit('create', chart); |
| 197 | |
| 198 | // Bind events |
| 199 | cvs.onclick = scope.click ? getEventHandler(scope, chart, 'click', false) : angular.noop; |
| 200 | cvs.onmousemove = scope.hover ? getEventHandler(scope, chart, 'hover', true) : angular.noop; |
| 201 | |
| 202 | if (scope.legend && scope.legend !== 'false') setLegend(elem, chart); |
| 203 | } |
| 204 | |
| 205 | function deprecated (attr) { |
| 206 | if (typeof console !== 'undefined' && ChartJs.getOptions().env !== 'test') { |
| 207 | var warn = typeof console.warn === 'function' ? console.warn : console.log; |
| 208 | if (!! scope[attr]) { |
| 209 | warn.call(console, '"%s" is deprecated and will be removed in a future version. ' + |
| 210 | 'Please use "chart-%s" instead.', attr, attr); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | }; |
| 216 | }; |
| 217 | |
| 218 | function canUpdateChart (newVal, oldVal) { |
| 219 | if (newVal && oldVal && newVal.length && oldVal.length) { |
| 220 | return Array.isArray(newVal[0]) ? |
| 221 | newVal.length === oldVal.length && newVal.every(function (element, index) { |
| 222 | return element.length === oldVal[index].length; }) : |
| 223 | oldVal.reduce(sum, 0) > 0 ? newVal.length === oldVal.length : false; |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | function sum (carry, val) { |
| 229 | return carry + val; |
| 230 | } |
| 231 | |
| 232 | function getEventHandler (scope, chart, action, triggerOnlyOnChange) { |
| 233 | var lastState = null; |
| 234 | return function (evt) { |
| 235 | var atEvent = chart.getPointsAtEvent || chart.getBarsAtEvent || chart.getSegmentsAtEvent; |
| 236 | if (atEvent) { |
| 237 | var activePoints = atEvent.call(chart, evt); |
| 238 | if (triggerOnlyOnChange === false || angular.equals(lastState, activePoints) === false) { |
| 239 | lastState = activePoints; |
| 240 | scope[action](activePoints, evt); |
| 241 | scope.$apply(); |
| 242 | } |
| 243 | } |
| 244 | }; |
| 245 | } |
| 246 | |
| 247 | function getColours (type, scope) { |
| 248 | var colours = angular.copy(scope.colours || |
| 249 | ChartJs.getOptions(type).colours || |
| 250 | Chart.defaults.global.colours |
| 251 | ); |
| 252 | while (colours.length < scope.data.length) { |
| 253 | colours.push(scope.getColour()); |
| 254 | } |
| 255 | return colours.map(convertColour); |
| 256 | } |
| 257 | |
| 258 | function convertColour (colour) { |
| 259 | if (typeof colour === 'object' && colour !== null) return colour; |
| 260 | if (typeof colour === 'string' && colour[0] === '#') return getColour(hexToRgb(colour.substr(1))); |
| 261 | return getRandomColour(); |
| 262 | } |
| 263 | |
| 264 | function getRandomColour () { |
| 265 | var colour = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)]; |
| 266 | return getColour(colour); |
| 267 | } |
| 268 | |
| 269 | function getColour (colour) { |
| 270 | return { |
| 271 | fillColor: rgba(colour, 0.2), |
| 272 | strokeColor: rgba(colour, 1), |
| 273 | pointColor: rgba(colour, 1), |
| 274 | pointStrokeColor: '#fff', |
| 275 | pointHighlightFill: '#fff', |
| 276 | pointHighlightStroke: rgba(colour, 0.8) |
| 277 | }; |
| 278 | } |
| 279 | |
| 280 | function getRandomInt (min, max) { |
| 281 | return Math.floor(Math.random() * (max - min + 1)) + min; |
| 282 | } |
| 283 | |
| 284 | function rgba (colour, alpha) { |
| 285 | if (usingExcanvas) { |
| 286 | // rgba not supported by IE8 |
| 287 | return 'rgb(' + colour.join(',') + ')'; |
| 288 | } else { |
| 289 | return 'rgba(' + colour.concat(alpha).join(',') + ')'; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // Credit: http://stackoverflow.com/a/11508164/1190235 |
| 294 | function hexToRgb (hex) { |
| 295 | var bigint = parseInt(hex, 16), |
| 296 | r = (bigint >> 16) & 255, |
| 297 | g = (bigint >> 8) & 255, |
| 298 | b = bigint & 255; |
| 299 | |
| 300 | return [r, g, b]; |
| 301 | } |
| 302 | |
| 303 | function getDataSets (labels, data, series, colours) { |
| 304 | return { |
| 305 | labels: labels, |
| 306 | datasets: data.map(function (item, i) { |
| 307 | return angular.extend({}, colours[i], { |
| 308 | label: series[i], |
| 309 | data: item |
| 310 | }); |
| 311 | }) |
| 312 | }; |
| 313 | } |
| 314 | |
| 315 | function getData (labels, data, colours) { |
| 316 | return labels.map(function (label, i) { |
| 317 | return angular.extend({}, colours[i], { |
| 318 | label: label, |
| 319 | value: data[i], |
| 320 | color: colours[i].strokeColor, |
| 321 | highlight: colours[i].pointHighlightStroke |
| 322 | }); |
| 323 | }); |
| 324 | } |
| 325 | |
| 326 | function setLegend (elem, chart) { |
| 327 | var $parent = elem.parent(), |
| 328 | $oldLegend = $parent.find('chart-legend'), |
| 329 | legend = '<chart-legend>' + chart.generateLegend() + '</chart-legend>'; |
| 330 | if ($oldLegend.length) $oldLegend.replaceWith(legend); |
| 331 | else $parent.append(legend); |
| 332 | } |
| 333 | |
| 334 | function updateChart (chart, values, scope, elem) { |
| 335 | if (Array.isArray(scope.data[0])) { |
| 336 | chart.datasets.forEach(function (dataset, i) { |
| 337 | (dataset.points || dataset.bars).forEach(function (dataItem, j) { |
| 338 | dataItem.value = values[i][j]; |
| 339 | }); |
| 340 | }); |
| 341 | } else { |
| 342 | chart.segments.forEach(function (segment, i) { |
| 343 | segment.value = values[i]; |
| 344 | }); |
| 345 | } |
| 346 | chart.update(); |
| 347 | scope.$emit('update', chart); |
| 348 | if (scope.legend && scope.legend !== 'false') setLegend(elem, chart); |
| 349 | } |
| 350 | |
| 351 | function isEmpty (value) { |
| 352 | return ! value || |
| 353 | (Array.isArray(value) && ! value.length) || |
| 354 | (typeof value === 'object' && ! Object.keys(value).length); |
| 355 | } |
| 356 | |
| 357 | function isResponsive (type, scope) { |
| 358 | var options = angular.extend({}, Chart.defaults.global, ChartJs.getOptions(type), scope.options); |
| 359 | return options.responsive; |
| 360 | } |
| 361 | } |
| 362 | })); |