jquery.easypiechart.min.js.older 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Generated by CoffeeScript 1.6.3
  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.2.3
  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. $.easyPieChart = function(el, options) {
  15. var addScaleLine, animateLine, drawLine, easeInOutQuad, rAF, renderBackground, renderScale, renderTrack,
  16. _this = this;
  17. this.el = el;
  18. this.$el = $(el);
  19. this.$el.data("easyPieChart", this);
  20. this.init = function() {
  21. var percent, scaleBy;
  22. _this.options = $.extend({}, $.easyPieChart.defaultOptions, options);
  23. percent = parseInt(_this.$el.data('percent'), 10);
  24. _this.percentage = 0;
  25. _this.canvas = $("<canvas width='" + _this.options.size + "' height='" + _this.options.size + "'></canvas>").get(0);
  26. _this.$el.append(_this.canvas);
  27. if (typeof G_vmlCanvasManager !== "undefined" && G_vmlCanvasManager !== null) {
  28. G_vmlCanvasManager.initElement(_this.canvas);
  29. }
  30. _this.ctx = _this.canvas.getContext('2d');
  31. if (window.devicePixelRatio > 1) {
  32. scaleBy = window.devicePixelRatio;
  33. $(_this.canvas).css({
  34. width: _this.options.size,
  35. height: _this.options.size
  36. });
  37. _this.canvas.width *= scaleBy;
  38. _this.canvas.height *= scaleBy;
  39. _this.ctx.scale(scaleBy, scaleBy);
  40. }
  41. _this.ctx.translate(_this.options.size / 2, _this.options.size / 2);
  42. _this.ctx.rotate(_this.options.rotate * Math.PI / 180);
  43. _this.$el.addClass('easyPieChart');
  44. _this.$el.css({
  45. width: _this.options.size,
  46. height: _this.options.size,
  47. lineHeight: "" + _this.options.size + "px"
  48. });
  49. _this.update(percent);
  50. return _this;
  51. };
  52. this.update = function(percent) {
  53. percent = parseFloat(percent) || 0;
  54. if (_this.options.animate === false) {
  55. drawLine(percent);
  56. } else {
  57. animateLine(_this.percentage, percent);
  58. }
  59. return _this;
  60. };
  61. renderScale = function() {
  62. var i, _i, _results;
  63. _this.ctx.fillStyle = _this.options.scaleColor;
  64. _this.ctx.lineWidth = 1;
  65. _results = [];
  66. for (i = _i = 0; _i <= 24; i = ++_i) {
  67. _results.push(addScaleLine(i));
  68. }
  69. return _results;
  70. };
  71. addScaleLine = function(i) {
  72. var offset;
  73. offset = i % 6 === 0 ? 0 : _this.options.size * 0.017;
  74. _this.ctx.save();
  75. _this.ctx.rotate(i * Math.PI / 12);
  76. _this.ctx.fillRect(_this.options.size / 2 - offset, 0, -_this.options.size * 0.05 + offset, 1);
  77. _this.ctx.restore();
  78. };
  79. renderTrack = function() {
  80. var offset;
  81. offset = _this.options.size / 2 - _this.options.lineWidth / 2;
  82. if (_this.options.scaleColor !== false) {
  83. offset -= _this.options.size * 0.08;
  84. }
  85. _this.ctx.beginPath();
  86. _this.ctx.arc(0, 0, offset, 0, Math.PI * 2, true);
  87. _this.ctx.closePath();
  88. _this.ctx.strokeStyle = _this.options.trackColor;
  89. _this.ctx.lineWidth = _this.options.lineWidth;
  90. _this.ctx.stroke();
  91. };
  92. renderBackground = function() {
  93. if (_this.options.scaleColor !== false) {
  94. renderScale();
  95. }
  96. if (_this.options.trackColor !== false) {
  97. renderTrack();
  98. }
  99. };
  100. drawLine = function(percent) {
  101. var offset;
  102. renderBackground();
  103. _this.ctx.strokeStyle = $.isFunction(_this.options.barColor) ? _this.options.barColor(percent) : _this.options.barColor;
  104. _this.ctx.lineCap = _this.options.lineCap;
  105. _this.ctx.lineWidth = _this.options.lineWidth;
  106. offset = _this.options.size / 2 - _this.options.lineWidth / 2;
  107. if (_this.options.scaleColor !== false) {
  108. offset -= _this.options.size * 0.08;
  109. }
  110. _this.ctx.save();
  111. _this.ctx.rotate(-Math.PI / 2);
  112. _this.ctx.beginPath();
  113. _this.ctx.arc(0, 0, offset, 0, Math.PI * 2 * percent / 100, false);
  114. _this.ctx.stroke();
  115. _this.ctx.restore();
  116. };
  117. rAF = (function() {
  118. return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) {
  119. return window.setTimeout(callback, 1000 / 60);
  120. };
  121. })();
  122. animateLine = function(from, to) {
  123. var anim, startTime;
  124. _this.options.onStart.call(_this);
  125. _this.percentage = to;
  126. Date.now || (Date.now = function() {
  127. return +(new Date);
  128. });
  129. startTime = Date.now();
  130. anim = function() {
  131. var currentValue, process;
  132. process = Date.now() - startTime;
  133. if (process < _this.options.animate) {
  134. rAF(anim);
  135. }
  136. _this.ctx.clearRect(-_this.options.size / 2, -_this.options.size / 2, _this.options.size, _this.options.size);
  137. renderBackground.call(_this);
  138. currentValue = [easeInOutQuad(process, from, to - from, _this.options.animate)];
  139. _this.options.onStep.call(_this, currentValue);
  140. drawLine.call(_this, currentValue);
  141. if (process >= _this.options.animate) {
  142. return _this.options.onStop.call(_this, currentValue, to);
  143. }
  144. };
  145. rAF(anim);
  146. };
  147. easeInOutQuad = function(t, b, c, d) {
  148. var easeIn, easing;
  149. easeIn = function(t) {
  150. return Math.pow(t, 2);
  151. };
  152. easing = function(t) {
  153. if (t < 1) {
  154. return easeIn(t);
  155. } else {
  156. return 2 - easeIn((t / 2) * -2 + 2);
  157. }
  158. };
  159. t /= d / 2;
  160. return c / 2 * easing(t) + b;
  161. };
  162. return this.init();
  163. };
  164. $.easyPieChart.defaultOptions = {
  165. barColor: '#ef1e25',
  166. trackColor: '#f2f2f2',
  167. scaleColor: '#dfe0e0',
  168. lineCap: 'round',
  169. rotate: 0,
  170. size: 110,
  171. lineWidth: 3,
  172. animate: false,
  173. onStart: $.noop,
  174. onStop: $.noop,
  175. onStep: $.noop
  176. };
  177. $.fn.easyPieChart = function(options) {
  178. return $.each(this, function(i, el) {
  179. var $el, instanceOptions;
  180. $el = $(el);
  181. if (!$el.data('easyPieChart')) {
  182. instanceOptions = $.extend({}, options, $el.data());
  183. return $el.data('easyPieChart', new $.easyPieChart(el, instanceOptions));
  184. }
  185. });
  186. };
  187. return void 0;
  188. })(jQuery);