forked from benpickles/peity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
491 lines (410 loc) · 16.2 KB
/
index.html
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<title>Peity • progressive <svg> pie charts</title>
<link href="docs/style.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="jquery.peity.js"></script>
<script>
$(function() {
// Just the defaults.
$("span.pie").peity("pie")
$(".line").peity("line")
$(".bar").peity("bar")
$(".bar-colours-1").peity("bar", {
fill: ["red", "green", "blue"]
})
$(".bar-colours-2").peity("bar", {
fill: function(value) {
return value > 0 ? "green" : "red"
}
})
$(".bar-colours-3").peity("bar", {
fill: function(_, i, all) {
var g = parseInt((i / all.length) * 255)
return "rgb(255, " + g + ", 0)"
}
})
$(".pie-colours-1").peity("pie", {
fill: ["cyan", "magenta", "yellow", "black"]
})
$(".pie-colours-2").peity("pie", {
fill: function(_, i, all) {
var g = parseInt((i / all.length) * 255)
return "rgb(255, " + g + ", 0)"
}
})
// Using data attributes
$(".pie span").peity("pie")
// Simple evented example.
$("select").change(function() {
var text = $(this).val() + "/" + 5
$(this)
.siblings("span.graph")
.text(text)
.change()
$("#notice").text("Chart updated: " + text)
}).change()
$("span.graph").peity("pie")
// Updating charts.
var updatingChart = $(".updating-chart").peity("line", { width: 64 })
setInterval(function() {
var random = Math.round(Math.random() * 10)
var values = updatingChart.text().split(",")
values.shift()
values.push(random)
updatingChart
.text(values.join(","))
.change()
}, 1000)
})
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-117680-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="container">
<h1>Peity</h1>
<iframe src="http://ghbtns.com/github-btn.html?user=benpickles&repo=peity&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" width="110" height="20"></iframe>
<iframe src="http://ghbtns.com/github-btn.html?user=benpickles&repo=peity&type=fork&count=true"
allowtransparency="true" frameborder="0" scrolling="0" width="95" height="20"></iframe>
<p>Peity (sounds like deity) is a simple <a href="http://jquery.com/">jQuery</a>
plugin that converts an element's content into a simple <code><svg></code>
mini pie <span class="pie">2/5</span>
line <span class="line">5,3,9,6,5,9,7,3,5,2</span>
or bar chart <span class="bar">5,3,9,6,5,9,7,3,5,2</span> and is
compatible with any browser that supports <code><svg></code>:
Chrome, Firefox, IE9+, Opera, Safari.</p>
<nav>
<p>
Read about <a href="#pie-charts">pie charts</a>,
<a href="#line-charts">line charts</a>,
<a href="#bar-charts">bar charts</a>,
<a href="#dynamic-colours">setting colours dynamically</a>,
<a href="#updating-charts">updating charts</a>,
<a href="#custom-charts">custom charts</a>,
<a href="#data-attributes">data-* attributes</a> and
<a href="#events">events</a>,
<a href="#defaults">default settings</a>.
</p>
</nav>
<h2>Download <span class="version">version 2.0.1</span></h2>
<dl>
<dt>Uncompressed 7.1Kb</dt>
<dd><a href="jquery.peity.js">jquery.peity.js</a></dd>
<dt>Minified 3.3Kb (+gzipped 1.5Kb)</dt>
<dd><a href="jquery.peity.min.js">jquery.peity.min.js</a></dd>
<dt>Source</dt>
<dd><a href="https://github.com/benpickles/peity">github.com/benpickles/peity</a></dd>
</dl>
<a href="https://github.com/benpickles/peity" id="fork-me"><img src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
<h2 id="pie-charts">Pie Charts</h2>
<p>Simply call <code>peity("pie")</code> on a jQuery selection. There are
two subtly different pie chart semantics, a "/" delimiter is assumed to
mean "three out of five" and only the first two values will be drawn,
otherwise all of the values are included in the chart and the total is the
sum of all values.</p>
<p>You can also pass <code>delimiter</code>, <code>diameter</code>,
<code>fill</code>, <code>height</code> and <code>width</code> options.
Setting a diameter is a shortcut for setting the width and height to the
same value.</p>
<p>
<span class="pie">1/5</span>
<span class="pie">226/360</span>
<span class="pie">0.52/1.561</span>
<span class="pie">1,4</span>
<span class="pie">226,134</span>
<span class="pie">0.52,1.041</span>
<span class="pie">1,2,3,2,2</span>
</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><span class="pie">1/5</span>
<span class="pie">226/360</span>
<span class="pie">0.52/1.561</span>
<span class="pie">1,4</span>
<span class="pie">226,134</span>
<span class="pie">0.52,1.041</span>
<span class="pie">1,2,3,2,2</span></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">$("span.pie").peity("pie")</code></pre>
</div>
<h2 id="line-charts">Line Charts</h2>
<p>Line charts work on a comma-separated list of digits. Line charts can
take the following options: <code>delimiter</code>, <code>fill</code>,
<code>height</code>, <code>max</code>, <code>min</code>,
<code>stroke</code>, <code>strokeWidth</code> and <code>width</code>.</p>
<p>
<span class="line">5,3,9,6,5,9,7,3,5,2</span>
<span class="line">5,3,2,-1,-3,-2,2,3,5,2</span>
<span class="line">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><span class="line">5,3,9,6,5,9,7,3,5,2</span>
<span class="line">5,3,2,-1,-3,-2,2,3,5,2</span>
<span class="line">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">$(".line").peity("line")</code></pre>
</div>
<h2 id="bar-charts">Bar Charts</h2>
<p>Bar charts work in the same way as line charts and take the following
options: <code>delimiter</code>, <code>fill</code>, <code>gap</code>,
<code>height</code>, <code>max</code>, <code>min</code> and
<code>width</code>.</p>
<p>
<span class="bar">5,3,9,6,5,9,7,3,5,2</span>
<span class="bar">5,3,2,-1,-3,-2,2,3,5,2</span>
<span class="bar">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><span class="bar">5,3,9,6,5,9,7,3,5,2</span>
<span class="bar">5,3,2,-1,-3,-2,2,3,5,2</span>
<span class="bar">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">$(".bar").peity("bar")</code></pre>
</div>
<h2 id="dynamic-colours">Setting Colours Dynamically</h2>
<p>Bar and pie chart colours can be defined dynamically based on the values
of the chart. When passing an array its values are cycled, when passing a
function it is called once for each value allowing you to define each
bar's colour. The callback is invoked with the value, its index, and the
full array of values - the same arguments as the callback for
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description">Array#forEach</a></code>.</p>
<p>
<span class="bar-colours-1">5,3,9,6,5,9,7,3,5,2</span>
<span class="bar-colours-2">5,3,2,-1,-3,-2,2,3,5,2</span>
<span class="bar-colours-3">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
<span class="pie-colours-1">4,7,6,5</span>
<span class="pie-colours-2">5,3,9,6,5</span>
</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><span class="bar-colours-1">5,3,9,6,5,9,7,3,5,2</span>
<span class="bar-colours-2">5,3,2,-1,-3,-2,2,3,5,2</span>
<span class="bar-colours-3">0,-3,-6,-4,-5,-4,-7,-3,-5,-2</span>
<span class="pie-colours-1">4,7,6,5</span>
<span class="pie-colours-2">5,3,9,6,5</span></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">$(".bar-colours-1").peity("bar", {
fill: ["red", "green", "blue"]
})
$(".bar-colours-2").peity("bar", {
fill: function(value) {
return value > 0 ? "green" : "red"
}
})
$(".bar-colours-3").peity("bar", {
fill: function(_, i, all) {
var g = parseInt((i / all.length) * 255)
return "rgb(255, " + g + ", 0)"
}
})
$(".pie-colours-1").peity("pie", {
fill: ["cyan", "magenta", "yellow", "black"]
})
$(".pie-colours-2").peity("pie", {
fill: function(_, i, all) {
var g = parseInt((i / all.length) * 255)
return "rgb(255, " + g + ", 0)"
}
})</code></pre>
</div>
<h2 id="updating-charts">Updating Charts</h2>
<p>Charts can be updated by changing the the jQuery selection's text content
and calling <code>change()</code> on it. The chart will be redrawn with
the same options that were originally passed to it.</p>
<p>
<span class="updating-chart">5,3,9,6,5,9,7,3,5,2,5,3,9,6,5,9,7,3,5,2</span>
</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><span class="updating-chart">5,3,9,6,5,9,7,3,5,2,5,3,9,6,5,9,7,3,5,2</span></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">var updatingChart = $(".updating-chart").peity("line", { width: 64 })
setInterval(function() {
var random = Math.round(Math.random() * 10)
var values = updatingChart.text().split(",")
values.shift()
values.push(random)
updatingChart
.text(values.join(","))
.change()
}, 1000)</code></pre>
</div>
<h2 id="custom-charts">Custom Charts</h2>
<p>You can easily add your own custom chart by registering it with
Peity with name, defaults and the draw function which receives an options
object.</p>
<pre><code class="javascript">$.fn.peity.add("custom", {
fill: "#FFCC00"
}, function(opts) {
...
}
)</code></pre>
<h2 id="data-attributes">data-* attributes</h2>
<p>If you use data attributes, Peity will apply their values to the defaults
with the same names automatically. Options passed to the
<code>peity()</code> function take precedence over data-* attributes.</p>
<p class="pie">
<span data-diameter="40" data-fill='["red", "#eeeeee"]' >1/7</span>
<span data-diameter="36" data-fill='["orange", "#eeeeee"]'>2/7</span>
<span data-diameter="32" data-fill='["yellow", "#eeeeee"]'>3/7</span>
<span data-diameter="28" data-fill='["green", "#eeeeee"]' >4/7</span>
<span data-diameter="24" data-fill='["blue", "#eeeeee"]' >5/7</span>
<span data-diameter="20" data-fill='["indigo", "#eeeeee"]'>6/7</span>
<span data-diameter="16" data-fill='["violet", "#eeeeee"]'>7/7</span>
</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><span data-diameter="40" data-fill='["red", "#eeeeee"]' >1/7</span>
<span data-diameter="36" data-fill='["orange", "#eeeeee"]'>2/7</span>
<span data-diameter="32" data-fill='["yellow", "#eeeeee"]'>3/7</span>
<span data-diameter="28" data-fill='["green", "#eeeeee"]' >4/7</span>
<span data-diameter="24" data-fill='["blue", "#eeeeee"]' >5/7</span>
<span data-diameter="20" data-fill='["indigo", "#eeeeee"]'>6/7</span>
<span data-diameter="16" data-fill='["violet", "#eeeeee"]'>7/7</span></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">$(".pie span").peity("pie")</code></pre>
</div>
<h2 id="events">Events</h2>
<p>Peity adds a "change" event trigger to your graph elements, so if you
update their data your can regenerate one or more charts by triggering
<code>change()</code> on them.</p>
<ul>
<li>
<span class="graph"></span>
<select>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4" selected>4</option>
<option value="5">5</option>
</select>
</li>
<li>
<span class="graph"></span>
<select>
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</li>
<li>
<span class="graph"></span>
<select>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</li>
</ul>
<p id="notice">Nothing's happened yet.</p>
<div class="example">
<h4>HTML</h4>
<pre><code class="html"><ul>
<li>
<span class="graph"></span>
<select>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4" selected>4</option>
<option value="5">5</option>
</select>
</li>
<li>
<span class="graph"></span>
<select>
<option value="0">0</option>
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</li>
<li>
<span class="graph"></span>
<select>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</li>
</ul>
<p id="notice">Nothing's happened yet.</p></code></pre>
<h4>JavaScript</h4>
<pre><code class="javascript">$("select").change(function() {
$(this)
.siblings("span.graph")
.text($(this).val() + "/" + 5).change()
}).change()
var chartUpdate = function(event, value, max) {
$("#notice").text(
"Chart updated: " + value + "/" + max
)
}
$("span.graph")
.peity("pie")
.bind("change.peity", chartUpdate)</code></pre>
</div>
<h2 id="defaults">Default Settings</h2>
<p>Defaults can be overridden globally like so:</p>
<pre><code class="javascript">$.fn.peity.defaults.pie = {
delimiter: null,
diameter: 16,
fill: ["<span style="background:#ff9900">#ff9900</span>", "<span style="background:#fff4dd">#fff4dd</span>", "<span style="background:#ffd592">#ffd592</span>"],
height: null,
width: null
}
$.fn.peity.defaults.line = {
delimiter: ",",
fill: "<span style="background:#c6d9fd">#c6d9fd</span>",
height: 16,
max: null,
min: 0,
stroke: "<span style="background:#4d89f9">#4d89f9</span>",
strokeWidth: 1,
width: 32
}
$.fn.peity.defaults.bar = {
delimiter: ",",
fill: ["<span style="background:#4d89f9">#4d89f9</span>"],
gap: 1,
height: 16,
max: null,
min: 0,
width: 32
}</code></pre>
<footer>
<p>
© 2009-2014 <a href="http://benpickles.com/">Ben Pickles</a>.
<a href="https://github.com/benpickles/peity/blob/master/MIT-LICENCE">MIT LICENCE</a>.
</p>
</footer>
</div>
</body>
</html>