Last active
August 29, 2015 14:05
-
-
Save rashidkpc/8bc62dea2df3639e99d2 to your computer and use it in GitHub Desktop.
Javascript unicode sparklines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sparkline = function(input) { | |
var numbers = input.split(/[\s,]+/); | |
var bar = [0,1,2,3,4,5,6].map(function (n) { return String.fromCharCode(9601+n);}); | |
var min = Math.min.apply(Math, numbers); | |
var max = Math.max.apply(Math, numbers); | |
var div = (max - min) / (bar.length - 1) | |
if (min === max) return Array(numbers.length).join(_.last(bar)) | |
return numbers.map(function (p) {return bar[Math.round(((p - min) / div))]}).join(''); | |
} | |
data = [ | |
'1 2 3 4 5 6 7 8 7 6 5 4 3 2 1', | |
'1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5' | |
]; | |
data.forEach(function (input) { | |
console.log('Input: ' + input); | |
console.log('Sparkline:' + sparkline(input)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment