PageSourceSearch

https://www.buildingstack.com/wp-content/themes/Divi/includes/builder/scripts/jquery.easypiechart.js

js buildingstack.com collected 2026-10-01 15:50:19 UTC 9,532 bytes, 362 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.