jquery.easypiechart.js 9.4 KB

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