pie-chart.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //Anonymous sely-executing function
  2. (function (root, factory) {
  3. factory(root.jQuery);
  4. }(this, function ($) {
  5. var CanvasRenderer = function (element, options) {
  6. var cachedBackground;
  7. var canvas = document.createElement('canvas');
  8. element.appendChild(canvas);
  9. var ctx = canvas.getContext('2d');
  10. canvas.width = canvas.height = options.size;
  11. // move 0,0 coordinates to the center
  12. ctx.translate(options.size / 2, options.size / 2);
  13. // rotate canvas -90deg
  14. ctx.rotate((-1 / 2 + options.rotate / 180) * Math.PI);
  15. var radius = (options.size - options.lineWidth) / 2;
  16. Date.now = Date.now || function () {
  17. //convert to milliseconds
  18. return +(new Date());
  19. };
  20. var drawCircle = function (color, lineWidth, percent) {
  21. percent = Math.min(Math.max(-1, percent || 0), 1);
  22. var isNegative = percent <= 0 ? true : false;
  23. ctx.beginPath();
  24. ctx.arc(0, 0, radius, 0, Math.PI * 2 * percent, isNegative);
  25. ctx.strokeStyle = color;
  26. ctx.lineWidth = lineWidth;
  27. ctx.stroke();
  28. };
  29. /**
  30. * Return function request animation frame method or timeout fallback
  31. */
  32. var reqAnimationFrame = (function () {
  33. return window.requestAnimationFrame ||
  34. window.webkitRequestAnimationFrame ||
  35. window.mozRequestAnimationFrame ||
  36. function (callback) {
  37. window.setTimeout(callback, 1000 / 60);
  38. };
  39. }());
  40. /**
  41. * Draw the background of the plugin track
  42. */
  43. var drawBackground = function () {
  44. if (options.trackColor) drawCircle(options.trackColor, options.lineWidth, 1);
  45. };
  46. /**
  47. * Clear the complete canvas
  48. */
  49. this.clear = function () {
  50. ctx.clearRect(options.size / -2, options.size / -2, options.size, options.size);
  51. };
  52. /**
  53. * Draw the complete chart
  54. * param percent Percent shown by the chart between -100 and 100
  55. */
  56. this.draw = function (percent) {
  57. if (!!options.trackColor) {
  58. // getImageData and putImageData are supported
  59. if (ctx.getImageData && ctx.putImageData) {
  60. if (!cachedBackground) {
  61. drawBackground();
  62. cachedBackground = ctx.getImageData(0, 0, options.size, options.size);
  63. } else {
  64. ctx.putImageData(cachedBackground, 0, 0);
  65. }
  66. } else {
  67. this.clear();
  68. drawBackground();
  69. }
  70. } else {
  71. this.clear();
  72. }
  73. ctx.lineCap = options.lineCap;
  74. // draw bar
  75. drawCircle(options.barColor, options.lineWidth, percent / 100);
  76. }.bind(this);
  77. this.animate = function (from, to) {
  78. var startTime = Date.now();
  79. var animation = function () {
  80. var process = Math.min(Date.now() - startTime, options.animate.duration);
  81. var currentValue = options.easing(this, process, from, to - from, options.animate.duration);
  82. this.draw(currentValue);
  83. //Show the number at the center of the circle
  84. options.onStep(from, to, currentValue);
  85. reqAnimationFrame(animation);
  86. }.bind(this);
  87. reqAnimationFrame(animation);
  88. }.bind(this);
  89. };
  90. var pieChart = function (element, userOptions) {
  91. var defaultOptions = {
  92. barColor: '#ef1e25',
  93. trackColor: '#f9f9f9',
  94. lineCap: 'round',
  95. lineWidth: 3,
  96. size: 100,
  97. rotate: 0,
  98. animate: {
  99. duration: 1000,
  100. enabled: true
  101. },
  102. easing: function (x, t, b, c, d) {//copy from jQuery easing animate
  103. t = t / (d / 2);
  104. if (t < 1) {
  105. return c / 2 * t * t + b;
  106. }
  107. return -c / 2 * ((--t) * (t - 2) - 1) + b;
  108. },
  109. onStep: function (from, to, currentValue) {
  110. return;
  111. },
  112. renderer: CanvasRenderer//Maybe SVGRenderer more later
  113. };
  114. var options = {};
  115. var currentValue = 0;
  116. var init = function () {
  117. this.element = element;
  118. this.options = options;
  119. // merge user options into default options
  120. for (var i in defaultOptions) {
  121. if (defaultOptions.hasOwnProperty(i)) {
  122. options[i] = userOptions && typeof(userOptions[i]) !== 'undefined' ? userOptions[i] : defaultOptions[i];
  123. if (typeof(options[i]) === 'function') {
  124. options[i] = options[i].bind(this);
  125. }
  126. }
  127. }
  128. // check for jQuery easing, use jQuery easing first
  129. if (typeof(options.easing) === 'string' && typeof(jQuery) !== 'undefined' && jQuery.isFunction(jQuery.easing[options.easing])) {
  130. options.easing = jQuery.easing[options.easing];
  131. } else {
  132. options.easing = defaultOptions.easing;
  133. }
  134. // create renderer
  135. this.renderer = new options.renderer(element, options);
  136. // initial draw
  137. this.renderer.draw(currentValue);
  138. if (element.getAttribute && element.getAttribute('data-percent')) {
  139. var newValue = parseFloat(element.getAttribute('data-percent'));
  140. if (options.animate.enabled) {
  141. this.renderer.animate(currentValue, newValue);
  142. } else {
  143. this.renderer.draw(newValue);
  144. }
  145. currentValue = newValue;
  146. }
  147. }.bind(this)();
  148. };
  149. $.fn.pieChart = function (options) {
  150. //Iterate all the dom to draw the pie-charts
  151. return this.each(function () {
  152. if (!$.data(this, 'pieChart')) {
  153. var userOptions = $.extend({}, options, $(this).data());
  154. $.data(this, 'pieChart', new pieChart(this, userOptions));
  155. }
  156. });
  157. };
  158. }));