angular.easypiechart.js 9.8 KB

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