3 if (typeof exports === 'object') {
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);
13 factory(angular, Chart);
15 }(function (angular, Chart) {
18 Chart.defaults.global.responsive = true;
19 Chart.defaults.global.multiTooltipTemplate = '<%if (datasetLabel){%><%=datasetLabel%>: <%}%><%= value %>';
21 Chart.defaults.global.colours = [
23 '#DCDCDC', // light grey
28 '#4D5360' // dark grey
31 var usingExcanvas = typeof window.G_vmlCanvasManager === 'object' &&
32 window.G_vmlCanvasManager !== null &&
33 typeof window.G_vmlCanvasManager.initElement === 'function';
35 if (usingExcanvas) Chart.defaults.global.animation = false;
37 return angular.module('chart.js', [])
38 .provider('ChartJs', ChartJsProvider)
39 .factory('ChartJsFactory', ['ChartJs', '$timeout', ChartJsFactory])
40 .directive('chartBase', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory(); }])
41 .directive('chartLine', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('Line'); }])
42 .directive('chartBar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('Bar'); }])
43 .directive('chartRadar', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('Radar'); }])
44 .directive('chartDoughnut', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('Doughnut'); }])
45 .directive('chartPie', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('Pie'); }])
46 .directive('chartPolarArea', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('PolarArea'); }]);
49 * Wrapper for chart.js
50 * Allows configuring chart js using the provider
52 * angular.module('myModule', ['chart.js']).config(function(ChartJsProvider) {
53 * ChartJsProvider.setOptions({ responsive: true });
54 * ChartJsProvider.setOptions('Line', { responsive: false });
57 function ChartJsProvider () {
61 getOptions: function (type) {
62 var typeOptions = type && options[type] || {};
63 return angular.extend({}, options, typeOptions);
68 * Allow to set global options during configuration
70 this.setOptions = function (type, customOptions) {
71 // If no type was specified set option for the global object
72 if (! customOptions) {
74 options = angular.extend(options, customOptions);
77 // Set options for the specific chart
78 options[type] = angular.extend(options[type] || {}, customOptions);
81 this.$get = function () {
86 function ChartJsFactory (ChartJs, $timeout) {
87 return function chart (type) {
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]);
117 if (usingExcanvas) window.G_vmlCanvasManager.initElement(elem[0]);
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;
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');
137 // Order of setting "watch" matter
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;
145 if (canUpdateChart(newVal, oldVal)) return updateChart(chart, newVal, scope, elem);
149 createChart(chartType);
152 scope.$watch('series', resetChart, true);
153 scope.$watch('labels', resetChart, true);
154 scope.$watch('options', resetChart, true);
155 scope.$watch('colours', resetChart, true);
157 scope.$watch('chartType', function (newVal, oldVal) {
158 if (isEmpty(newVal)) return;
159 if (angular.equals(newVal, oldVal)) return;
160 if (chart) chart.destroy();
164 scope.$on('$destroy', function () {
165 if (chart) chart.destroy();
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;
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();
178 createChart(chartType);
181 function createChart (type) {
182 if (isResponsive(type, scope) && elem[0].clientHeight === 0 && container.clientHeight === 0) {
183 return $timeout(function () {
187 if (! scope.data || ! scope.data.length) return;
188 scope.getColour = typeof scope.getColour === 'function' ? scope.getColour : getRandomColour;
189 var 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 || [], colours) :
193 getData(scope.labels, scope.data, 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);
199 cvs.onclick = scope.click ? getEventHandler(scope, chart, 'click', false) : angular.noop;
200 cvs.onmousemove = scope.hover ? getEventHandler(scope, chart, 'hover', true) : angular.noop;
202 if (scope.legend && scope.legend !== 'false') setLegend(elem, chart);
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);
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;
228 function sum (carry, val) {
232 function getEventHandler (scope, chart, action, triggerOnlyOnChange) {
233 var lastState = null;
234 return function (evt) {
235 var atEvent = chart.getPointsAtEvent || chart.getBarsAtEvent || chart.getSegmentsAtEvent;
237 var activePoints = atEvent.call(chart, evt);
238 if (triggerOnlyOnChange === false || angular.equals(lastState, activePoints) === false) {
239 lastState = activePoints;
240 scope[action](activePoints, evt);
247 function getColours (type, scope) {
248 var notEnoughColours = false;
249 var colours = angular.copy(scope.colours ||
250 ChartJs.getOptions(type).colours ||
251 Chart.defaults.global.colours
253 while (colours.length < scope.data.length) {
254 colours.push(scope.getColour());
255 notEnoughColours = true;
257 // mutate colours in this case as we don't want
258 // the colours to change on each refresh
259 if (notEnoughColours) scope.colours = colours;
260 return colours.map(convertColour);
263 function convertColour (colour) {
264 if (typeof colour === 'object' && colour !== null) return colour;
265 if (typeof colour === 'string' && colour[0] === '#') return getColour(hexToRgb(colour.substr(1)));
266 return getRandomColour();
269 function getRandomColour () {
270 var colour = [getRandomInt(0, 255), getRandomInt(0, 255), getRandomInt(0, 255)];
271 return getColour(colour);
274 function getColour (colour) {
276 fillColor: rgba(colour, 0.2),
277 strokeColor: rgba(colour, 1),
278 pointColor: rgba(colour, 1),
279 pointStrokeColor: '#fff',
280 pointHighlightFill: '#fff',
281 pointHighlightStroke: rgba(colour, 0.8)
285 function getRandomInt (min, max) {
286 return Math.floor(Math.random() * (max - min + 1)) + min;
289 function rgba (colour, alpha) {
291 // rgba not supported by IE8
292 return 'rgb(' + colour.join(',') + ')';
294 return 'rgba(' + colour.concat(alpha).join(',') + ')';
298 // Credit: http://stackoverflow.com/a/11508164/1190235
299 function hexToRgb (hex) {
300 var bigint = parseInt(hex, 16),
301 r = (bigint >> 16) & 255,
302 g = (bigint >> 8) & 255,
308 function getDataSets (labels, data, series, colours) {
311 datasets: data.map(function (item, i) {
312 return angular.extend({}, colours[i], {
320 function getData (labels, data, colours) {
321 return labels.map(function (label, i) {
322 return angular.extend({}, colours[i], {
325 color: colours[i].strokeColor,
326 highlight: colours[i].pointHighlightStroke
331 function setLegend (elem, chart) {
332 var $parent = elem.parent(),
333 $oldLegend = $parent.find('chart-legend'),
334 legend = '<chart-legend>' + chart.generateLegend() + '</chart-legend>';
335 if ($oldLegend.length) $oldLegend.replaceWith(legend);
336 else $parent.append(legend);
339 function updateChart (chart, values, scope, elem) {
340 if (Array.isArray(scope.data[0])) {
341 chart.datasets.forEach(function (dataset, i) {
342 (dataset.points || dataset.bars).forEach(function (dataItem, j) {
343 dataItem.value = values[i][j];
347 chart.segments.forEach(function (segment, i) {
348 segment.value = values[i];
352 scope.$emit('update', chart);
353 if (scope.legend && scope.legend !== 'false') setLegend(elem, chart);
356 function isEmpty (value) {
358 (Array.isArray(value) && ! value.length) ||
359 (typeof value === 'object' && ! Object.keys(value).length);
362 function isResponsive (type, scope) {
363 var options = angular.extend({}, Chart.defaults.global, ChartJs.getOptions(type), scope.options);
364 return options.responsive;