: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Lightweight plugin to render simple, animated and retina optimized pie charts
* @author Robert Fleischmann <rendro87@gmail.com> (http://robert-fleischmann.de)
(function(root, factory) {
if(typeof exports === 'object') {
module.exports = factory(require('jquery'));
else if(typeof define === 'function' && define.amd) {
define(['jquery'], factory);
* Renderer to render the chart on a canvas object
* @param {DOMElement} el DOM element to host the canvas (root of the plugin)
* @param {object} options options object of the plugin
var CanvasRenderer = function(el, options) {
var canvas = document.createElement('canvas');
if (typeof(G_vmlCanvasManager) !== 'undefined') {
G_vmlCanvasManager.initElement(canvas);
var ctx = canvas.getContext('2d');
canvas.width = canvas.height = options.size;
// canvas on retina devices
if (window.devicePixelRatio > 1) {
scaleBy = window.devicePixelRatio;
canvas.style.width = canvas.style.height = [options.size, 'px'].join('');
canvas.width = canvas.height = options.size * scaleBy;
ctx.scale(scaleBy, scaleBy);
// move 0,0 coordinates to the center
ctx.translate(options.size / 2, options.size / 2);
ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);
var radius = (options.size - options.lineWidth) / 2;
if (options.scaleColor && options.scaleLength) {
radius -= options.scaleLength + 2; // 2 is the distance between scale and bar
Date.now = Date.now || function() {
* Draw a circle around the center of the canvas
* @param {strong} color Valid CSS color string
* @param {number} lineWidth Width of the line in px
* @param {number} percent Percentage to draw (float between -1 and 1)
var drawCircle = function(color, lineWidth, percent, alpha ) {
percent = Math.min(Math.max(-1, percent || 0), 1);
var isNegative = percent <= 0 ? true : false;
ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, isNegative);
ctx.lineWidth = lineWidth;
* Draw the scale of the chart
var drawScale = function() {
ctx.fillStyle = options.scaleColor;
for (var i = 24; i > 0; --i) {
length = options.scaleLength;
length = options.scaleLength * 0.6;
offset = options.scaleLength - length;
ctx.fillRect(-options.size/2 + offset, 0, length, 1);
ctx.rotate(Math.PI / 12);
* Request animation frame wrapper with polyfill
* @return {function} Request animation frame method or timeout fallback
var reqAnimationFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.setTimeout(callback, 1000 / 60);
* Draw the background of the plugin including the scale and the track
var drawBackground = function() {
if(options.scaleColor) drawScale();
if(options.trackColor) drawCircle(options.trackColor, options.lineWidth, 1, options.trackAlpha );
this.getCanvas = function() {
* Canvas 2D context 'ctx' accessor
this.getCtx = function() {
* Clear the complete canvas
this.clear = function() {
ctx.clearRect(options.size / -2, options.size / -2, options.size, options.size);
* Draw the complete chart
* @param {number} percent Percent shown by the chart between -100 and 100
this.draw = function(percent) {
// do we need to render a background
if (!!options.scaleColor || !!options.trackColor) {
// getImageData and putImageData are supported
if (ctx.getImageData && ctx.putImageData) {
cachedBackground = ctx.getImageData(0, 0, options.size * scaleBy, options.size * scaleBy);
ctx.putImageData(cachedBackground, 0, 0);
ctx.lineCap = options.lineCap;
// if barcolor is a function execute it and pass the percent as a value
if (typeof(options.barColor) === 'function') {
color = options.barColor(percent);
color = options.barColor;
drawCircle(color, options.lineWidth, percent / 100, options.barAlpha );
* Animate from some percent to some other percentage
* @param {number} from Starting percentage
* @param {number} to Final percentage
this.animate = function(from, to) {
var startTime = Date.now();
options.onStart(from, to);
var animation = function() {
var process = Math.min(Date.now() - startTime, options.animate.duration);
var currentValue = options.easing(this, process, from, to - from, options.animate.duration);
options.onStep(from, to, currentValue);
if (process >= options.animate.duration) {
options.onStop(from, to);
reqAnimationFrame(animation);
reqAnimationFrame(animation);
var EasyPieChart = function(el, opts) {
easing: function (x, t, b, c, d) { // more can be found here: http://gsgd.co.uk/sandbox/jquery/easing/
return c / 2 * t * t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
onStart: function(from, to) {
onStep: function(from, to, currentValue) {
onStop: function(from, to) {
// detect present renderer
if (typeof(CanvasRenderer) !== 'undefined') {
defaultOptions.renderer = CanvasRenderer;
} else if (typeof(SVGRenderer) !== 'undefined') {
defaultOptions.renderer = SVGRenderer;
throw new Error('Please load either the SVG- or the CanvasRenderer');
* Initialize the plugin by creating the options object and initialize rendering
// merge user options into default options
for (var i in defaultOptions) {
if (defaultOptions.hasOwnProperty(i)) {
options[i] = opts && typeof(opts[i]) !== 'undefined' ? opts[i] : defaultOptions[i];
if (typeof(options[i]) === 'function') {
options[i] = options[i].bind(this);
// check for jQuery easing
if (typeof(options.easing) === 'string' && typeof(jQuery) !== 'undefined' && jQuery.isFunction(jQuery.easing[options.easing])) {
options.easing = jQuery.easing[options.easing];
options.easing = defaultOptions.easing;
// process earlier animate option to avoid bc breaks
if (typeof(options.animate) === 'number') {
duration: options.animate,
if (typeof(options.animate) === 'boolean' && !options.animate) {
this.renderer = new options.renderer(el, options);
this.renderer.draw(currentValue);
if (el.dataset && el.dataset.percent) {
this.update(parseFloat(el.dataset.percent));
} else if (el.getAttribute && el.getAttribute('data-percent')) {
this.update(parseFloat(el.getAttribute('data-percent')));
* Update the value of the chart
* @param {number} newValue Number between 0 and 100
* @return {object} Instance of the plugin for method chaining
this.update = function(newValue) {
newValue = parseFloat(newValue);
if (options.animate.enabled) {
this.renderer.animate(currentValue, newValue);
this.renderer.draw(newValue);
* @return {object} Instance of the plugin for method chaining
this.disableAnimation = function() {
options.animate.enabled = false;
* @return {object} Instance of the plugin for method chaining
this.enableAnimation = function() {
options.animate.enabled = true;
$.fn.easyPieChart = function(options) {
return this.each(function() {
if (!$.data(this, 'easyPieChart')) {
instanceOptions = $.extend({}, options, $(this).data());
$.data(this, 'easyPieChart', new EasyPieChart(this, instanceOptions));