Path: blob/master/web-gui/buildyourownbotnet/assets/js/easy-pie-chart/dist/jquery.easypiechart.js
1293 views
/**!1* easy-pie-chart2* Lightweight plugin to render simple, animated and retina optimized pie charts3*4* @license5* @author Robert Fleischmann <[email protected]> (http://robert-fleischmann.de)6* @version 2.1.77**/89(function (root, factory) {10if (typeof define === 'function' && define.amd) {11// AMD. Register as an anonymous module unless amdModuleId is set12define(["jquery"], function (a0) {13return (factory(a0));14});15} else if (typeof exports === 'object') {16// Node. Does not work with strict CommonJS, but17// only CommonJS-like environments that support module.exports,18// like Node.19module.exports = factory(require("jquery"));20} else {21factory(jQuery);22}23}(this, function ($) {2425/**26* Renderer to render the chart on a canvas object27* @param {DOMElement} el DOM element to host the canvas (root of the plugin)28* @param {object} options options object of the plugin29*/30var CanvasRenderer = function(el, options) {31var cachedBackground;32var canvas = document.createElement('canvas');3334el.appendChild(canvas);3536if (typeof(G_vmlCanvasManager) === 'object') {37G_vmlCanvasManager.initElement(canvas);38}3940var ctx = canvas.getContext('2d');4142canvas.width = canvas.height = options.size;4344// canvas on retina devices45var scaleBy = 1;46if (window.devicePixelRatio > 1) {47scaleBy = window.devicePixelRatio;48canvas.style.width = canvas.style.height = [options.size, 'px'].join('');49canvas.width = canvas.height = options.size * scaleBy;50ctx.scale(scaleBy, scaleBy);51}5253// move 0,0 coordinates to the center54ctx.translate(options.size / 2, options.size / 2);5556// rotate canvas -90deg57ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);5859var radius = (options.size - options.lineWidth) / 2;60if (options.scaleColor && options.scaleLength) {61radius -= options.scaleLength + 2; // 2 is the distance between scale and bar62}6364// IE polyfill for Date65Date.now = Date.now || function() {66return +(new Date());67};6869/**70* Draw a circle around the center of the canvas71* @param {strong} color Valid CSS color string72* @param {number} lineWidth Width of the line in px73* @param {number} percent Percentage to draw (float between -1 and 1)74*/75var drawCircle = function(color, lineWidth, percent) {76percent = Math.min(Math.max(-1, percent || 0), 1);77var isNegative = percent <= 0 ? true : false;7879ctx.beginPath();80ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, isNegative);8182ctx.strokeStyle = color;83ctx.lineWidth = lineWidth;8485ctx.stroke();86};8788/**89* Draw the scale of the chart90*/91var drawScale = function() {92var offset;93var length;9495ctx.lineWidth = 1;96ctx.fillStyle = options.scaleColor;9798ctx.save();99for (var i = 24; i > 0; --i) {100if (i % 6 === 0) {101length = options.scaleLength;102offset = 0;103} else {104length = options.scaleLength * 0.6;105offset = options.scaleLength - length;106}107ctx.fillRect(-options.size/2 + offset, 0, length, 1);108ctx.rotate(Math.PI / 12);109}110ctx.restore();111};112113/**114* Request animation frame wrapper with polyfill115* @return {function} Request animation frame method or timeout fallback116*/117var reqAnimationFrame = (function() {118return window.requestAnimationFrame ||119window.webkitRequestAnimationFrame ||120window.mozRequestAnimationFrame ||121function(callback) {122window.setTimeout(callback, 1000 / 60);123};124}());125126/**127* Draw the background of the plugin including the scale and the track128*/129var drawBackground = function() {130if(options.scaleColor) drawScale();131if(options.trackColor) drawCircle(options.trackColor, options.trackWidth || options.lineWidth, 1);132};133134/**135* Canvas accessor136*/137this.getCanvas = function() {138return canvas;139};140141/**142* Canvas 2D context 'ctx' accessor143*/144this.getCtx = function() {145return ctx;146};147148/**149* Clear the complete canvas150*/151this.clear = function() {152ctx.clearRect(options.size / -2, options.size / -2, options.size, options.size);153};154155/**156* Draw the complete chart157* @param {number} percent Percent shown by the chart between -100 and 100158*/159this.draw = function(percent) {160// do we need to render a background161if (!!options.scaleColor || !!options.trackColor) {162// getImageData and putImageData are supported163if (ctx.getImageData && ctx.putImageData) {164if (!cachedBackground) {165drawBackground();166cachedBackground = ctx.getImageData(0, 0, options.size * scaleBy, options.size * scaleBy);167} else {168ctx.putImageData(cachedBackground, 0, 0);169}170} else {171this.clear();172drawBackground();173}174} else {175this.clear();176}177178ctx.lineCap = options.lineCap;179180// if barcolor is a function execute it and pass the percent as a value181var color;182if (typeof(options.barColor) === 'function') {183color = options.barColor(percent);184} else {185color = options.barColor;186}187188// draw bar189drawCircle(color, options.lineWidth, percent / 100);190}.bind(this);191192/**193* Animate from some percent to some other percentage194* @param {number} from Starting percentage195* @param {number} to Final percentage196*/197this.animate = function(from, to) {198var startTime = Date.now();199options.onStart(from, to);200var animation = function() {201var process = Math.min(Date.now() - startTime, options.animate.duration);202var currentValue = options.easing(this, process, from, to - from, options.animate.duration);203this.draw(currentValue);204options.onStep(from, to, currentValue);205if (process >= options.animate.duration) {206options.onStop(from, to);207} else {208reqAnimationFrame(animation);209}210}.bind(this);211212reqAnimationFrame(animation);213}.bind(this);214};215216var EasyPieChart = function(el, opts) {217var defaultOptions = {218barColor: '#ef1e25',219trackColor: '#f9f9f9',220scaleColor: '#dfe0e0',221scaleLength: 5,222lineCap: 'round',223lineWidth: 3,224trackWidth: undefined,225size: 110,226rotate: 0,227animate: {228duration: 1000,229enabled: true230},231easing: function (x, t, b, c, d) { // more can be found here: http://gsgd.co.uk/sandbox/jquery/easing/232t = t / (d/2);233if (t < 1) {234return c / 2 * t * t + b;235}236return -c/2 * ((--t)*(t-2) - 1) + b;237},238onStart: function(from, to) {239return;240},241onStep: function(from, to, currentValue) {242return;243},244onStop: function(from, to) {245return;246}247};248249// detect present renderer250if (typeof(CanvasRenderer) !== 'undefined') {251defaultOptions.renderer = CanvasRenderer;252} else if (typeof(SVGRenderer) !== 'undefined') {253defaultOptions.renderer = SVGRenderer;254} else {255throw new Error('Please load either the SVG- or the CanvasRenderer');256}257258var options = {};259var currentValue = 0;260261/**262* Initialize the plugin by creating the options object and initialize rendering263*/264var init = function() {265this.el = el;266this.options = options;267268// merge user options into default options269for (var i in defaultOptions) {270if (defaultOptions.hasOwnProperty(i)) {271options[i] = opts && typeof(opts[i]) !== 'undefined' ? opts[i] : defaultOptions[i];272if (typeof(options[i]) === 'function') {273options[i] = options[i].bind(this);274}275}276}277278// check for jQuery easing279if (typeof(options.easing) === 'string' && typeof(jQuery) !== 'undefined' && jQuery.isFunction(jQuery.easing[options.easing])) {280options.easing = jQuery.easing[options.easing];281} else {282options.easing = defaultOptions.easing;283}284285// process earlier animate option to avoid bc breaks286if (typeof(options.animate) === 'number') {287options.animate = {288duration: options.animate,289enabled: true290};291}292293if (typeof(options.animate) === 'boolean' && !options.animate) {294options.animate = {295duration: 1000,296enabled: options.animate297};298}299300// create renderer301this.renderer = new options.renderer(el, options);302303// initial draw304this.renderer.draw(currentValue);305306// initial update307if (el.dataset && el.dataset.percent) {308this.update(parseFloat(el.dataset.percent));309} else if (el.getAttribute && el.getAttribute('data-percent')) {310this.update(parseFloat(el.getAttribute('data-percent')));311}312}.bind(this);313314/**315* Update the value of the chart316* @param {number} newValue Number between 0 and 100317* @return {object} Instance of the plugin for method chaining318*/319this.update = function(newValue) {320newValue = parseFloat(newValue);321if (options.animate.enabled) {322this.renderer.animate(currentValue, newValue);323} else {324this.renderer.draw(newValue);325}326currentValue = newValue;327return this;328}.bind(this);329330/**331* Disable animation332* @return {object} Instance of the plugin for method chaining333*/334this.disableAnimation = function() {335options.animate.enabled = false;336return this;337};338339/**340* Enable animation341* @return {object} Instance of the plugin for method chaining342*/343this.enableAnimation = function() {344options.animate.enabled = true;345return this;346};347348init();349};350351$.fn.easyPieChart = function(options) {352return this.each(function() {353var instanceOptions;354355if (!$.data(this, 'easyPieChart')) {356instanceOptions = $.extend({}, options, $(this).data());357$.data(this, 'easyPieChart', new EasyPieChart(this, instanceOptions));358}359});360};361362363}));364365366