Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/easy-pie-chart/dist/angular.easypiechart.js
1293 views
1
/**!
2
* easy-pie-chart
3
* Lightweight plugin to render simple, animated and retina optimized pie charts
4
*
5
* @license
6
* @author Robert Fleischmann <[email protected]> (http://robert-fleischmann.de)
7
* @version 2.1.7
8
**/
9
10
(function (root, factory) {
11
if (typeof define === 'function' && define.amd) {
12
// AMD. Register as an anonymous module unless amdModuleId is set
13
define(["angular"], function (a0) {
14
return (factory(a0));
15
});
16
} else if (typeof exports === 'object') {
17
// Node. Does not work with strict CommonJS, but
18
// only CommonJS-like environments that support module.exports,
19
// like Node.
20
module.exports = factory(require("angular"));
21
} else {
22
factory(angular);
23
}
24
}(this, function (angular) {
25
26
(function (angular) {
27
28
'use strict';
29
30
return angular.module('easypiechart', [])
31
32
.directive('easypiechart', [function() {
33
return {
34
restrict: 'AE',
35
require: '?ngModel',
36
scope: {
37
percent: '=',
38
options: '='
39
},
40
link: function (scope, element, attrs) {
41
42
scope.percent = scope.percent || 0;
43
44
/**
45
* default easy pie chart options
46
* @type {Object}
47
*/
48
var options = {
49
barColor: '#ef1e25',
50
trackColor: '#f9f9f9',
51
scaleColor: '#dfe0e0',
52
scaleLength: 5,
53
lineCap: 'round',
54
lineWidth: 3,
55
size: 110,
56
rotate: 0,
57
animate: {
58
duration: 1000,
59
enabled: true
60
}
61
};
62
scope.options = angular.extend(options, scope.options);
63
64
var pieChart = new EasyPieChart(element[0], options);
65
66
scope.$watch('percent', function(newVal, oldVal) {
67
pieChart.update(newVal);
68
});
69
}
70
};
71
}]);
72
73
})(angular);
74
75
/**
76
* Renderer to render the chart on a canvas object
77
* @param {DOMElement} el DOM element to host the canvas (root of the plugin)
78
* @param {object} options options object of the plugin
79
*/
80
var CanvasRenderer = function(el, options) {
81
var cachedBackground;
82
var canvas = document.createElement('canvas');
83
84
el.appendChild(canvas);
85
86
if (typeof(G_vmlCanvasManager) === 'object') {
87
G_vmlCanvasManager.initElement(canvas);
88
}
89
90
var ctx = canvas.getContext('2d');
91
92
canvas.width = canvas.height = options.size;
93
94
// canvas on retina devices
95
var scaleBy = 1;
96
if (window.devicePixelRatio > 1) {
97
scaleBy = window.devicePixelRatio;
98
canvas.style.width = canvas.style.height = [options.size, 'px'].join('');
99
canvas.width = canvas.height = options.size * scaleBy;
100
ctx.scale(scaleBy, scaleBy);
101
}
102
103
// move 0,0 coordinates to the center
104
ctx.translate(options.size / 2, options.size / 2);
105
106
// rotate canvas -90deg
107
ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);
108
109
var radius = (options.size - options.lineWidth) / 2;
110
if (options.scaleColor && options.scaleLength) {
111
radius -= options.scaleLength + 2; // 2 is the distance between scale and bar
112
}
113
114
// IE polyfill for Date
115
Date.now = Date.now || function() {
116
return +(new Date());
117
};
118
119
/**
120
* Draw a circle around the center of the canvas
121
* @param {strong} color Valid CSS color string
122
* @param {number} lineWidth Width of the line in px
123
* @param {number} percent Percentage to draw (float between -1 and 1)
124
*/
125
var drawCircle = function(color, lineWidth, percent) {
126
percent = Math.min(Math.max(-1, percent || 0), 1);
127
var isNegative = percent <= 0 ? true : false;
128
129
ctx.beginPath();
130
ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, isNegative);
131
132
ctx.strokeStyle = color;
133
ctx.lineWidth = lineWidth;
134
135
ctx.stroke();
136
};
137
138
/**
139
* Draw the scale of the chart
140
*/
141
var drawScale = function() {
142
var offset;
143
var length;
144
145
ctx.lineWidth = 1;
146
ctx.fillStyle = options.scaleColor;
147
148
ctx.save();
149
for (var i = 24; i > 0; --i) {
150
if (i % 6 === 0) {
151
length = options.scaleLength;
152
offset = 0;
153
} else {
154
length = options.scaleLength * 0.6;
155
offset = options.scaleLength - length;
156
}
157
ctx.fillRect(-options.size/2 + offset, 0, length, 1);
158
ctx.rotate(Math.PI / 12);
159
}
160
ctx.restore();
161
};
162
163
/**
164
* Request animation frame wrapper with polyfill
165
* @return {function} Request animation frame method or timeout fallback
166
*/
167
var reqAnimationFrame = (function() {
168
return window.requestAnimationFrame ||
169
window.webkitRequestAnimationFrame ||
170
window.mozRequestAnimationFrame ||
171
function(callback) {
172
window.setTimeout(callback, 1000 / 60);
173
};
174
}());
175
176
/**
177
* Draw the background of the plugin including the scale and the track
178
*/
179
var drawBackground = function() {
180
if(options.scaleColor) drawScale();
181
if(options.trackColor) drawCircle(options.trackColor, options.trackWidth || options.lineWidth, 1);
182
};
183
184
/**
185
* Canvas accessor
186
*/
187
this.getCanvas = function() {
188
return canvas;
189
};
190
191
/**
192
* Canvas 2D context 'ctx' accessor
193
*/
194
this.getCtx = function() {
195
return ctx;
196
};
197
198
/**
199
* Clear the complete canvas
200
*/
201
this.clear = function() {
202
ctx.clearRect(options.size / -2, options.size / -2, options.size, options.size);
203
};
204
205
/**
206
* Draw the complete chart
207
* @param {number} percent Percent shown by the chart between -100 and 100
208
*/
209
this.draw = function(percent) {
210
// do we need to render a background
211
if (!!options.scaleColor || !!options.trackColor) {
212
// getImageData and putImageData are supported
213
if (ctx.getImageData && ctx.putImageData) {
214
if (!cachedBackground) {
215
drawBackground();
216
cachedBackground = ctx.getImageData(0, 0, options.size * scaleBy, options.size * scaleBy);
217
} else {
218
ctx.putImageData(cachedBackground, 0, 0);
219
}
220
} else {
221
this.clear();
222
drawBackground();
223
}
224
} else {
225
this.clear();
226
}
227
228
ctx.lineCap = options.lineCap;
229
230
// if barcolor is a function execute it and pass the percent as a value
231
var color;
232
if (typeof(options.barColor) === 'function') {
233
color = options.barColor(percent);
234
} else {
235
color = options.barColor;
236
}
237
238
// draw bar
239
drawCircle(color, options.lineWidth, percent / 100);
240
}.bind(this);
241
242
/**
243
* Animate from some percent to some other percentage
244
* @param {number} from Starting percentage
245
* @param {number} to Final percentage
246
*/
247
this.animate = function(from, to) {
248
var startTime = Date.now();
249
options.onStart(from, to);
250
var animation = function() {
251
var process = Math.min(Date.now() - startTime, options.animate.duration);
252
var currentValue = options.easing(this, process, from, to - from, options.animate.duration);
253
this.draw(currentValue);
254
options.onStep(from, to, currentValue);
255
if (process >= options.animate.duration) {
256
options.onStop(from, to);
257
} else {
258
reqAnimationFrame(animation);
259
}
260
}.bind(this);
261
262
reqAnimationFrame(animation);
263
}.bind(this);
264
};
265
266
var EasyPieChart = function(el, opts) {
267
var defaultOptions = {
268
barColor: '#ef1e25',
269
trackColor: '#f9f9f9',
270
scaleColor: '#dfe0e0',
271
scaleLength: 5,
272
lineCap: 'round',
273
lineWidth: 3,
274
trackWidth: undefined,
275
size: 110,
276
rotate: 0,
277
animate: {
278
duration: 1000,
279
enabled: true
280
},
281
easing: function (x, t, b, c, d) { // more can be found here: http://gsgd.co.uk/sandbox/jquery/easing/
282
t = t / (d/2);
283
if (t < 1) {
284
return c / 2 * t * t + b;
285
}
286
return -c/2 * ((--t)*(t-2) - 1) + b;
287
},
288
onStart: function(from, to) {
289
return;
290
},
291
onStep: function(from, to, currentValue) {
292
return;
293
},
294
onStop: function(from, to) {
295
return;
296
}
297
};
298
299
// detect present renderer
300
if (typeof(CanvasRenderer) !== 'undefined') {
301
defaultOptions.renderer = CanvasRenderer;
302
} else if (typeof(SVGRenderer) !== 'undefined') {
303
defaultOptions.renderer = SVGRenderer;
304
} else {
305
throw new Error('Please load either the SVG- or the CanvasRenderer');
306
}
307
308
var options = {};
309
var currentValue = 0;
310
311
/**
312
* Initialize the plugin by creating the options object and initialize rendering
313
*/
314
var init = function() {
315
this.el = el;
316
this.options = options;
317
318
// merge user options into default options
319
for (var i in defaultOptions) {
320
if (defaultOptions.hasOwnProperty(i)) {
321
options[i] = opts && typeof(opts[i]) !== 'undefined' ? opts[i] : defaultOptions[i];
322
if (typeof(options[i]) === 'function') {
323
options[i] = options[i].bind(this);
324
}
325
}
326
}
327
328
// check for jQuery easing
329
if (typeof(options.easing) === 'string' && typeof(jQuery) !== 'undefined' && jQuery.isFunction(jQuery.easing[options.easing])) {
330
options.easing = jQuery.easing[options.easing];
331
} else {
332
options.easing = defaultOptions.easing;
333
}
334
335
// process earlier animate option to avoid bc breaks
336
if (typeof(options.animate) === 'number') {
337
options.animate = {
338
duration: options.animate,
339
enabled: true
340
};
341
}
342
343
if (typeof(options.animate) === 'boolean' && !options.animate) {
344
options.animate = {
345
duration: 1000,
346
enabled: options.animate
347
};
348
}
349
350
// create renderer
351
this.renderer = new options.renderer(el, options);
352
353
// initial draw
354
this.renderer.draw(currentValue);
355
356
// initial update
357
if (el.dataset && el.dataset.percent) {
358
this.update(parseFloat(el.dataset.percent));
359
} else if (el.getAttribute && el.getAttribute('data-percent')) {
360
this.update(parseFloat(el.getAttribute('data-percent')));
361
}
362
}.bind(this);
363
364
/**
365
* Update the value of the chart
366
* @param {number} newValue Number between 0 and 100
367
* @return {object} Instance of the plugin for method chaining
368
*/
369
this.update = function(newValue) {
370
newValue = parseFloat(newValue);
371
if (options.animate.enabled) {
372
this.renderer.animate(currentValue, newValue);
373
} else {
374
this.renderer.draw(newValue);
375
}
376
currentValue = newValue;
377
return this;
378
}.bind(this);
379
380
/**
381
* Disable animation
382
* @return {object} Instance of the plugin for method chaining
383
*/
384
this.disableAnimation = function() {
385
options.animate.enabled = false;
386
return this;
387
};
388
389
/**
390
* Enable animation
391
* @return {object} Instance of the plugin for method chaining
392
*/
393
this.enableAnimation = function() {
394
options.animate.enabled = true;
395
return this;
396
};
397
398
init();
399
};
400
401
402
}));
403
404