forked from benpickles/peity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.peity.js
261 lines (217 loc) · 6.42 KB
/
jquery.peity.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Peity jQuery plugin version 1.2.0
// (c) 2013 Ben Pickles
//
// http://benpickles.github.io/peity
//
// Released under MIT license.
(function($, document, Math, devicePixelRatio) {
var canvasSupported = document.createElement("canvas").getContext
var peity = $.fn.peity = function(type, options) {
if (canvasSupported) {
this.each(function() {
var $this = $(this)
var chart = $this.data("peity")
if (chart) {
if (type) chart.type = type
$.extend(chart.opts, options)
chart.draw()
} else {
var defaults = peity.defaults[type]
var data = {}
$.each($this.data(), function(name, value) {
if (name in defaults) data[name] = value
})
var opts = $.extend({}, defaults, data, options)
var chart = new Peity($this, type, opts)
chart.draw()
$this
.change(function() { chart.draw() })
.data("peity", chart)
}
});
}
return this;
};
var Peity = function($el, type, opts) {
this.$el = $el
this.type = type
this.opts = opts
}
var PeityPrototype = Peity.prototype
PeityPrototype.colours = function() {
var colours = this.opts.colours
var func = colours
if (!$.isFunction(func)) {
func = function(_, i) {
return colours[i % colours.length]
}
}
return func
}
PeityPrototype.draw = function() {
peity.graphers[this.type].call(this, this.opts)
}
PeityPrototype.prepareCanvas = function(width, height) {
var canvas = this.canvas
var $canvas
if (canvas) {
this.context.clearRect(0, 0, canvas.width, canvas.height)
$canvas = $(canvas)
} else {
$canvas = $("<canvas>").css({
height: height,
width: width
}).addClass("peity").data("peity", this)
this.canvas = canvas = $canvas[0]
this.context = canvas.getContext("2d")
this.$el.hide().after(canvas)
}
canvas.height = $canvas.height() * devicePixelRatio
canvas.width = $canvas.width() * devicePixelRatio
return canvas
}
PeityPrototype.values = function() {
return $.map(this.$el.text().split(this.opts.delimiter), function(value) {
return parseFloat(value)
})
}
peity.defaults = {}
peity.graphers = {}
peity.register = function(type, defaults, grapher) {
this.defaults[type] = defaults
this.graphers[type] = grapher
}
peity.register(
'pie',
{
colours: ["#ff9900", "#fff4dd", "#ffc66e"],
delimiter: null,
diameter: 16
},
function(opts) {
if (!opts.delimiter) {
var delimiter = this.$el.text().match(/[^0-9\.]/)
opts.delimiter = delimiter ? delimiter[0] : ","
}
var values = this.values()
if (opts.delimiter == "/") {
var v1 = values[0]
var v2 = values[1]
values = [v1, v2 - v1]
}
var i = 0
var length = values.length
var sum = 0
for (; i < length; i++) {
sum += values[i]
}
var canvas = this.prepareCanvas(opts.width || opts.diameter, opts.height || opts.diameter)
var context = this.context
var width = canvas.width
var height = canvas.height
var radius = Math.min(width, height) / 2
var pi = Math.PI
var colours = this.colours()
context.save()
context.translate(width / 2, height / 2)
context.rotate(-pi / 2)
for (i = 0; i < length; i++) {
var value = values[i]
var slice = (value / sum) * pi * 2
context.beginPath()
context.moveTo(0, 0)
context.arc(0, 0, radius, 0, slice, false)
context.fillStyle = colours.call(this, value, i, values)
context.fill()
context.rotate(slice)
}
context.restore()
}
)
peity.register(
"line",
{
colour: "#c6d9fd",
strokeColour: "#4d89f9",
strokeWidth: 1,
delimiter: ",",
height: 16,
max: null,
min: 0,
width: 32
},
function(opts) {
var values = this.values()
if (values.length == 1) values.push(values[0])
var max = Math.max.apply(Math, values.concat([opts.max]));
var min = Math.min.apply(Math, values.concat([opts.min]))
var canvas = this.prepareCanvas(opts.width, opts.height)
var context = this.context
var width = canvas.width
var height = canvas.height
var xQuotient = width / (values.length - 1)
var yQuotient = height / (max - min)
var coords = [];
var i;
context.beginPath();
context.moveTo(0, height + (min * yQuotient))
for (i = 0; i < values.length; i++) {
var x = i * xQuotient
var y = height - (yQuotient * (values[i] - min))
coords.push({ x: x, y: y });
context.lineTo(x, y);
}
context.lineTo(width, height + (min * yQuotient))
context.fillStyle = opts.colour;
context.fill();
if (opts.strokeWidth) {
context.beginPath();
context.moveTo(0, coords[0].y);
for (i = 0; i < coords.length; i++) {
context.lineTo(coords[i].x, coords[i].y);
}
context.lineWidth = opts.strokeWidth * devicePixelRatio;
context.strokeStyle = opts.strokeColour;
context.stroke();
}
}
);
peity.register(
'bar',
{
colours: ["#4D89F9"],
delimiter: ",",
height: 16,
max: null,
min: 0,
spacing: devicePixelRatio,
width: 32
},
function(opts) {
var values = this.values()
var max = Math.max.apply(Math, values.concat([opts.max]));
var min = Math.min.apply(Math, values.concat([opts.min]))
var canvas = this.prepareCanvas(opts.width, opts.height)
var context = this.context
var width = canvas.width
var height = canvas.height
var yQuotient = height / (max - min)
var space = opts.spacing
var xQuotient = (width + space) / values.length
var colours = this.colours()
for (var i = 0; i < values.length; i++) {
var value = values[i]
var y = height - (yQuotient * (value - min))
var h
if (value == 0) {
if (min >= 0 || max > 0) y -= 1
h = 1
} else {
h = yQuotient * values[i]
}
context.fillStyle = colours.call(this, value, i, values)
context.fillRect(i * xQuotient, y, xQuotient - space, h)
}
}
);
})(jQuery, document, Math, window.devicePixelRatio || 1);