jquery.easy-pie-chart.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Generated by CoffeeScript 1.4.0
  2. /*
  3. Easy pie chart is a jquery plugin to display simple animated pie charts for only one value
  4. Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
  5. and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
  6. Built on top of the jQuery library (http://jquery.com)
  7. @source: http://github.com/rendro/easy-pie-chart/
  8. @autor: Robert Fleischmann
  9. @version: 1.0.1
  10. Inspired by: http://dribbble.com/shots/631074-Simple-Pie-Charts-II?list=popular&offset=210
  11. Thanks to Philip Thrasher for the jquery plugin boilerplate for coffee script
  12. */
  13. (function() {
  14. (function($) {
  15. $.easyPieChart = function(el, options) {
  16. var addScaleLine, animateLine, drawLine, easeInOutQuad, renderBackground, renderScale, renderTrack,
  17. _this = this;
  18. this.el = el;
  19. this.$el = $(el);
  20. this.$el.data("easyPieChart", this);
  21. this.init = function() {
  22. var percent;
  23. _this.options = $.extend({}, $.easyPieChart.defaultOptions, options);
  24. percent = parseInt(_this.$el.data('percent'), 10);
  25. _this.percentage = 0;
  26. _this.canvas = $("<canvas width='" + _this.options.size + "' height='" + _this.options.size + "'></canvas>").get(0);
  27. _this.$el.append(_this.canvas);
  28. if (typeof G_vmlCanvasManager !== "undefined" && G_vmlCanvasManager !== null) {
  29. G_vmlCanvasManager.initElement(_this.canvas);
  30. }
  31. _this.ctx = _this.canvas.getContext('2d');
  32. if (window.devicePixelRatio > 1.5) {
  33. $(_this.canvas).css({
  34. width: _this.options.size,
  35. height: _this.options.size
  36. });
  37. _this.canvas.width *= 2;
  38. _this.canvas.height *= 2;
  39. _this.ctx.scale(2, 2);
  40. }
  41. _this.ctx.translate(_this.options.size / 2, _this.options.size / 2);
  42. _this.$el.addClass('easyPieChart');
  43. _this.$el.css({
  44. width: _this.options.size,
  45. height: _this.options.size,
  46. lineHeight: "" + _this.options.size + "px"
  47. });
  48. _this.update(percent);
  49. return _this;
  50. };
  51. this.update = function(percent) {
  52. if (_this.options.animate === false) {
  53. return drawLine(percent);
  54. } else {
  55. return animateLine(_this.percentage, percent);
  56. }
  57. };
  58. renderScale = function() {
  59. var i, _i, _results;
  60. _this.ctx.fillStyle = _this.options.scaleColor;
  61. _this.ctx.lineWidth = 1;
  62. _results = [];
  63. for (i = _i = 0; _i <= 24; i = ++_i) {
  64. _results.push(addScaleLine(i));
  65. }
  66. return _results;
  67. };
  68. addScaleLine = function(i) {
  69. var offset;
  70. offset = i % 6 === 0 ? 0 : _this.options.size * 0.017;
  71. _this.ctx.save();
  72. _this.ctx.rotate(i * Math.PI / 12);
  73. _this.ctx.fillRect(_this.options.size / 2 - offset, 0, -_this.options.size * 0.05 + offset, 1);
  74. return _this.ctx.restore();
  75. };
  76. renderTrack = function() {
  77. var offset;
  78. offset = _this.options.size / 2 - _this.options.lineWidth / 2;
  79. if (_this.options.scaleColor !== false) {
  80. offset -= _this.options.size * 0.08;
  81. }
  82. _this.ctx.beginPath();
  83. _this.ctx.arc(0, 0, offset, 0, Math.PI * 2, true);
  84. _this.ctx.closePath();
  85. _this.ctx.strokeStyle = _this.options.trackColor;
  86. _this.ctx.lineWidth = _this.options.lineWidth;
  87. return _this.ctx.stroke();
  88. };
  89. renderBackground = function() {
  90. if (_this.options.scaleColor !== false) {
  91. renderScale();
  92. }
  93. if (_this.options.trackColor !== false) {
  94. return renderTrack();
  95. }
  96. };
  97. drawLine = function(percent) {
  98. var offset;
  99. renderBackground();
  100. _this.ctx.strokeStyle = $.isFunction(_this.options.barColor) ? _this.options.barColor(percent) : _this.options.barColor;
  101. _this.ctx.lineCap = _this.options.lineCap;
  102. _this.ctx.lineWidth = _this.options.lineWidth;
  103. offset = _this.options.size / 2 - _this.options.lineWidth / 2;
  104. if (_this.options.scaleColor !== false) {
  105. offset -= _this.options.size * 0.08;
  106. }
  107. _this.ctx.save();
  108. _this.ctx.rotate(-Math.PI / 2);
  109. _this.ctx.beginPath();
  110. _this.ctx.arc(0, 0, offset, 0, Math.PI * 2 * percent / 100, false);
  111. _this.ctx.stroke();
  112. return _this.ctx.restore();
  113. };
  114. animateLine = function(from, to) {
  115. var currentStep, fps, steps;
  116. fps = 30;
  117. steps = fps * _this.options.animate / 1000;
  118. currentStep = 0;
  119. _this.options.onStart.call(_this);
  120. _this.percentage = to;
  121. if (_this.animation) {
  122. clearInterval(_this.animation);
  123. _this.animation = false;
  124. }
  125. return _this.animation = setInterval(function() {
  126. _this.ctx.clearRect(-_this.options.size / 2, -_this.options.size / 2, _this.options.size, _this.options.size);
  127. renderBackground.call(_this);
  128. drawLine.call(_this, [easeInOutQuad(currentStep, from, to - from, steps)]);
  129. currentStep++;
  130. if ((currentStep / steps) > 1) {
  131. clearInterval(_this.animation);
  132. _this.animation = false;
  133. return _this.options.onStop.call(_this);
  134. }
  135. }, 1000 / fps);
  136. };
  137. easeInOutQuad = function(t, b, c, d) {
  138. var easeIn, easing;
  139. easeIn = function(t) {
  140. return Math.pow(t, 2);
  141. };
  142. easing = function(t) {
  143. if (t < 1) {
  144. return easeIn(t);
  145. } else {
  146. return 2 - easeIn((t / 2) * -2 + 2);
  147. }
  148. };
  149. t /= d / 2;
  150. return c / 2 * easing(t) + b;
  151. };
  152. return this.init();
  153. };
  154. $.easyPieChart.defaultOptions = {
  155. barColor: '#ef1e25',
  156. trackColor: '#f2f2f2',
  157. scaleColor: '#dfe0e0',
  158. lineCap: 'round',
  159. size: 110,
  160. lineWidth: 3,
  161. animate: false,
  162. onStart: $.noop,
  163. onStop: $.noop
  164. };
  165. $.fn.easyPieChart = function(options) {
  166. return $.each(this, function(i, el) {
  167. var $el;
  168. $el = $(el);
  169. if (!$el.data('easyPieChart')) {
  170. return $el.data('easyPieChart', new $.easyPieChart(el, options));
  171. }
  172. });
  173. };
  174. return void 0;
  175. })(jQuery);
  176. }).call(this);