angular.easypiechart.js 9.6 KB

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