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