easy_as_pie 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ // Generated by CoffeeScript 1.3.3
2
+
3
+ /*
4
+ Easy pie chart is a jquery plugin to display simple animated pie charts for only one value
5
+
6
+ Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
7
+ and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
8
+
9
+ Built on top of the jQuery library (http://jquery.com)
10
+
11
+ @source: http://github.com/rendro/easy-pie-chart/
12
+ @autor: Robert Fleischmann
13
+ @version: 1.0.1
14
+
15
+ Inspired by: http://dribbble.com/shots/631074-Simple-Pie-Charts-II?list=popular&offset=210
16
+ Thanks to Philip Thrasher for the jquery plugin boilerplate for coffee script
17
+ */
18
+
19
+
20
+ (function() {
21
+
22
+ (function($) {
23
+ $.easyPieChart = function(el, options) {
24
+ var addScaleLine, animateLine, drawLine, easeInOutQuad, renderBackground, renderScale, renderTrack,
25
+ _this = this;
26
+ this.el = el;
27
+ this.$el = $(el);
28
+ this.$el.data("easyPieChart", this);
29
+ this.init = function() {
30
+ var percent;
31
+ _this.options = $.extend({}, $.easyPieChart.defaultOptions, options);
32
+ percent = parseInt(_this.$el.data('percent'), 10);
33
+ _this.percentage = 0;
34
+ _this.canvas = $("<canvas width='" + _this.options.size + "' height='" + _this.options.size + "'></canvas>").get(0);
35
+ _this.$el.append(_this.canvas);
36
+ if (typeof G_vmlCanvasManager !== "undefined" && G_vmlCanvasManager !== null) {
37
+ G_vmlCanvasManager.initElement(_this.canvas);
38
+ }
39
+ _this.ctx = _this.canvas.getContext('2d');
40
+ _this.ctx.translate(_this.options.size / 2, _this.options.size / 2);
41
+ _this.$el.addClass('easyPieChart');
42
+ _this.$el.css({
43
+ width: _this.options.size,
44
+ height: _this.options.size,
45
+ lineHeight: "" + _this.options.size + "px"
46
+ });
47
+ _this.update(percent);
48
+ return _this;
49
+ };
50
+ this.update = function(percent) {
51
+ if (_this.options.animate === false) {
52
+ return drawLine(percent);
53
+ } else {
54
+ return animateLine(_this.percentage, percent);
55
+ }
56
+ };
57
+ renderScale = function() {
58
+ var i, _i, _results;
59
+ _this.ctx.fillStyle = _this.options.scaleColor;
60
+ _this.ctx.lineWidth = 1;
61
+ _results = [];
62
+ for (i = _i = 0; _i <= 24; i = ++_i) {
63
+ _results.push(addScaleLine(i));
64
+ }
65
+ return _results;
66
+ };
67
+ addScaleLine = function(i) {
68
+ var offset;
69
+ offset = i % 6 === 0 ? 0 : _this.options.size * 0.017;
70
+ _this.ctx.save();
71
+ _this.ctx.rotate(i * Math.PI / 12);
72
+ _this.ctx.fillRect(_this.options.size / 2 - offset, 0, -_this.options.size * 0.05 + offset, 1);
73
+ return _this.ctx.restore();
74
+ };
75
+ renderTrack = function() {
76
+ var offset;
77
+ offset = _this.options.size / 2 - _this.options.lineWidth / 2;
78
+ if (_this.options.scaleColor !== false) {
79
+ offset -= _this.options.size * 0.08;
80
+ }
81
+ _this.ctx.beginPath();
82
+ _this.ctx.arc(0, 0, offset, 0, Math.PI * 2, true);
83
+ _this.ctx.closePath();
84
+ _this.ctx.strokeStyle = _this.options.trackColor;
85
+ _this.ctx.lineWidth = _this.options.lineWidth;
86
+ return _this.ctx.stroke();
87
+ };
88
+ renderBackground = function() {
89
+ if (_this.options.scaleColor !== false) {
90
+ renderScale();
91
+ }
92
+ if (_this.options.trackColor !== false) {
93
+ return renderTrack();
94
+ }
95
+ };
96
+ drawLine = function(percent) {
97
+ var offset;
98
+ renderBackground();
99
+ _this.ctx.strokeStyle = $.isFunction(_this.options.barColor) ? _this.options.barColor(percent) : _this.options.barColor;
100
+ _this.ctx.lineCap = _this.options.lineCap;
101
+ offset = _this.options.size / 2 - _this.options.lineWidth / 2;
102
+ if (_this.options.scaleColor !== false) {
103
+ offset -= _this.options.size * 0.08;
104
+ }
105
+ _this.ctx.save();
106
+ _this.ctx.rotate(-Math.PI / 2);
107
+ _this.ctx.beginPath();
108
+ _this.ctx.arc(0, 0, offset, 0, Math.PI * 2 * percent / 100, false);
109
+ _this.ctx.stroke();
110
+ return _this.ctx.restore();
111
+ };
112
+ animateLine = function(from, to) {
113
+ var currentStep, fps, steps;
114
+ fps = 30;
115
+ steps = fps * _this.options.animate / 1000;
116
+ currentStep = 0;
117
+ _this.options.onStart.call(_this);
118
+ _this.percentage = to;
119
+ if (_this.animation) {
120
+ clearInterval(_this.animation);
121
+ _this.animation = false;
122
+ }
123
+ return _this.animation = setInterval(function() {
124
+ _this.ctx.clearRect(-_this.options.size / 2, -_this.options.size / 2, _this.options.size, _this.options.size);
125
+ renderBackground.call(_this);
126
+ drawLine.call(_this, [easeInOutQuad(currentStep, from, to - from, steps)]);
127
+ currentStep++;
128
+ if ((currentStep / steps) > 1) {
129
+ clearInterval(_this.animation);
130
+ _this.animation = false;
131
+ return _this.options.onStop.call(_this);
132
+ }
133
+ }, 1000 / fps);
134
+ };
135
+ easeInOutQuad = function(t, b, c, d) {
136
+ t /= d / 2;
137
+ if (t < 1) {
138
+ return c / 2 * t * t + b;
139
+ } else {
140
+ return -c / 2 * ((--t) * (t - 2) - 1) + b;
141
+ }
142
+ };
143
+ return this.init();
144
+ };
145
+ $.easyPieChart.defaultOptions = {
146
+ barColor: '#ef1e25',
147
+ trackColor: '#f2f2f2',
148
+ scaleColor: '#dfe0e0',
149
+ lineCap: 'round',
150
+ size: 110,
151
+ lineWidth: 3,
152
+ animate: false,
153
+ onStart: $.noop,
154
+ onStop: $.noop
155
+ };
156
+ $.fn.easyPieChart = function(options) {
157
+ return $.each(this, function(i, el) {
158
+ var $el;
159
+ $el = $(el);
160
+ if (!$el.data('easyPieChart')) {
161
+ return $el.data('easyPieChart', new $.easyPieChart(el, options));
162
+ }
163
+ });
164
+ };
165
+ return void 0;
166
+ })(jQuery);
167
+
168
+ }).call(this);
@@ -0,0 +1,152 @@
1
+ ###
2
+ Easy pie chart is a jquery plugin to display simple animated pie charts for only one value
3
+
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
+
7
+ Built on top of the jQuery library (http://jquery.com)
8
+
9
+ @source: http://github.com/rendro/easy-pie-chart/
10
+ @autor: Robert Fleischmann
11
+ @version: 1.0.1
12
+
13
+ Inspired by: http://dribbble.com/shots/631074-Simple-Pie-Charts-II?list=popular&offset=210
14
+ Thanks to Philip Thrasher for the jquery plugin boilerplate for coffee script
15
+ ###
16
+
17
+ (($) ->
18
+ $.easyPieChart = (el, options) ->
19
+
20
+ @el = el
21
+ @$el = $ el
22
+ @$el.data "easyPieChart", @
23
+
24
+ @init = =>
25
+ @options = $.extend {}, $.easyPieChart.defaultOptions, options
26
+
27
+ #get relevant data
28
+ percent = parseInt @$el.data('percent'), 10
29
+ @percentage = 0
30
+
31
+ #create canvas element and set the origin to the center
32
+ @canvas = $("<canvas width='#{@options.size}' height='#{@options.size}'></canvas>").get(0)
33
+ @$el.append @canvas
34
+ G_vmlCanvasManager.initElement @canvas if G_vmlCanvasManager?
35
+ @ctx = @canvas.getContext '2d'
36
+ @ctx.translate @options.size/2, @options.size/2
37
+
38
+ @$el.addClass 'easyPieChart'
39
+ @$el.css {
40
+ width: @options.size
41
+ height: @options.size
42
+ lineHeight: "#{@options.size}px"
43
+ }
44
+
45
+ @update percent
46
+ @
47
+
48
+ @update = (percent) =>
49
+ if @options.animate == false
50
+ drawLine percent
51
+ else
52
+ animateLine @percentage, percent
53
+
54
+ renderScale = =>
55
+ @ctx.fillStyle = @options.scaleColor
56
+ @ctx.lineWidth = 1
57
+ addScaleLine i for i in [0..24]
58
+
59
+ addScaleLine = (i) =>
60
+ offset = if i%6==0 then 0 else @options.size*0.017
61
+ @ctx.save()
62
+ @ctx.rotate i * Math.PI / 12
63
+ @ctx.fillRect @options.size/2-offset, 0, -@options.size*0.05+offset, 1
64
+ @ctx.restore()
65
+
66
+ renderTrack = =>
67
+ offset = @options.size/2-@options.lineWidth/2
68
+ offset -= @options.size*0.08 if @options.scaleColor != false
69
+
70
+ @ctx.beginPath()
71
+ @ctx.arc 0, 0, offset, 0, Math.PI * 2, true
72
+ @ctx.closePath()
73
+ @ctx.strokeStyle = @options.trackColor
74
+ @ctx.lineWidth = @options.lineWidth
75
+ @ctx.stroke()
76
+
77
+ renderBackground = =>
78
+ do renderScale if @options.scaleColor != false
79
+ do renderTrack if @options.trackColor != false
80
+
81
+ drawLine = (percent) =>
82
+ do renderBackground
83
+
84
+ @ctx.strokeStyle = if $.isFunction @options.barColor then @options.barColor percent else @options.barColor
85
+ @ctx.lineCap = @options.lineCap
86
+
87
+ offset = @options.size/2-@options.lineWidth/2
88
+ offset -= @options.size*0.08 if @options.scaleColor != false
89
+
90
+ @ctx.save()
91
+ @ctx.rotate -Math.PI/2
92
+ @ctx.beginPath()
93
+ @ctx.arc 0, 0, offset, 0, Math.PI * 2 * percent/100, false
94
+ @ctx.stroke()
95
+ @ctx.restore()
96
+
97
+ animateLine = (from, to) =>
98
+ fps = 30
99
+ steps = fps * @options.animate/1000
100
+ currentStep = 0
101
+
102
+ @options.onStart.call @
103
+ @percentage = to
104
+
105
+ if @animation
106
+ clearInterval @animation
107
+ @animation = false
108
+
109
+ @animation = setInterval =>
110
+ @ctx.clearRect -@options.size/2, -@options.size/2, @options.size, @options.size
111
+ renderBackground.call @
112
+ drawLine.call @, [easeInOutQuad currentStep, from, to-from, steps]
113
+
114
+ currentStep++
115
+
116
+ if (currentStep/steps) > 1
117
+ clearInterval @animation
118
+ @animation = false
119
+ @options.onStop.call @
120
+
121
+ , 1000/fps
122
+
123
+ #t=time;b=beginning value;c=change in value;d=duration
124
+ easeInOutQuad = (t, b, c, d) ->
125
+ t /= d/2
126
+ if ((t) < 1)
127
+ c/2*t*t + b
128
+ else
129
+ -c/2 * ((--t)*(t-2) - 1) + b
130
+
131
+ @init()
132
+
133
+ $.easyPieChart.defaultOptions =
134
+ barColor: '#ef1e25'
135
+ trackColor: '#f2f2f2'
136
+ scaleColor: '#dfe0e0'
137
+ lineCap: 'round'
138
+ size: 110
139
+ lineWidth: 3
140
+ animate: false
141
+ onStart: $.noop
142
+ onStop: $.noop
143
+
144
+ $.fn.easyPieChart = (options) ->
145
+ $.each @, (i, el) ->
146
+ $el = ($ el)
147
+
148
+ unless $el.data 'easyPieChart'
149
+ $el.data 'easyPieChart', new $.easyPieChart el, options
150
+
151
+ undefined
152
+ )(jQuery)
@@ -0,0 +1,10 @@
1
+ .easyPieChart {
2
+ position: relative;
3
+ text-align: center;
4
+ }
5
+
6
+ .easyPieChart canvas {
7
+ position: absolute;
8
+ top: 0;
9
+ left: 0;
10
+ }
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_as_pie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0.5'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0.5'
94
+ description: Makes it easy to add Pie charts to Rails 3+ apps
95
+ email: kmandrup@gmail.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - LICENSE.txt
100
+ - README.md
101
+ files:
102
+ - .document
103
+ - .rspec
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - easy_as_pie.gemspec
111
+ - lib/easy_as_pie.rb
112
+ - lib/easy_as_pie/engine.rb
113
+ - lib/easy_as_pie/view_helper.rb
114
+ - spec/easy_as_pie_spec.rb
115
+ - spec/spec_helper.rb
116
+ - vendor/assets/javascripts/excanvas.js
117
+ - vendor/assets/javascripts/jquery.easy-pie-chart.js
118
+ - vendor/assets/javascripts/jquery.easy-pie-chart.js.coffee
119
+ - vendor/assets/stylesheets/jquery.easy-pie-chart.css
120
+ homepage: http://github.com/kristianmandrup/easy_as_pie
121
+ licenses:
122
+ - MIT
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ segments:
134
+ - 0
135
+ hash: 169199517746559933
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.24
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Rails asset wrapper for jQuery easy-pie-chart
148
+ test_files: []