-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsparkline.js
52 lines (41 loc) · 1.34 KB
/
sparkline.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
/*
* sparkline
* https://github.com/shiwano/sparkline
*
* Copyright (c) 2013 Shogo Iwano
* Licensed under the MIT license.
*/
(function(window) {
'use strict';
var sparkline,
ticks = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
function lshift(n, bits) {
return Math.floor(n) * Math.pow(2, bits);
}
sparkline = function(numbers, options) {
options = options || {};
var max = typeof options.max === 'number' ? options.max : Math.max.apply(null, numbers),
min = typeof options.min === 'number' ? options.min : Math.min.apply(null, numbers),
html = typeof options.html === 'boolean' ? options.html : false,
results = [],
f, i;
f = Math.floor(lshift(max - min, 8) / (ticks.length - 1));
if (f < 1) { f = 1; }
for (i = 0; i < numbers.length; i++) {
var value = ticks[Math.floor(lshift(numbers[i] - min, 8) / f)];
if (html) {
value = '<span title="' + numbers[i] + '">' + value + '</span>';
}
results.push(value);
}
return results.join('');
};
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = sparkline;
} else {
window.sparkline = sparkline;
if (typeof define === 'function' && define.amd) {
define('sparkline', [], function () { return sparkline; });
}
}
})(this);