Skip to content

Instantly share code, notes, and snippets.

@notionparallax
Last active August 29, 2015 13:56
Show Gist options
  • Save notionparallax/9044181 to your computer and use it in GitHub Desktop.
Save notionparallax/9044181 to your computer and use it in GitHub Desktop.
This is a limited unicode-histogram for use in a Google Spreadsheet.
function sparkHist(inputRange){
//this is what we have to work with from the unicode block elements
//▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟
//▁▂▃▄▅▆▇█ is the useful bit
var bars=["░","▁","▂","▃","▄","▅","▆","▇","█"];
//var inputRange = [10,10,8,10,10,10,10,10,1,1,,8,5,10,'name@email.com',,,5,10,7,10,10,1,1,8,7,,7,8,9,9,4,10,,,5,,10,8,8,5,8,10,8,,8,2,10,8,8,8,10,10,8,10,10,5,10,10,5,9,10,8,8,5,,5,,,,,,,,,,10,10,10,9,1,4,8,5,7,,,,8,5,8,,,];
var filtered = filterNonNum(inputRange[0]);//Logger.log("filtered").log(filtered);
var binned = bin(filtered); //Logger.log(binned);
var vals = valueArray(binned); //Logger.log(vals);
var most = Math.max.apply(null, vals); //Logger.log(most);
var least = Math.min.apply(null, vals); //Logger.log(least);
var range = most-least; //Logger.log("**");
var stepSize = (bars.length-1)/range; //Logger.log(stepSize);
var sparkBar=[];
for (i in binned){
sparkBar.push(bars[Math.floor(binned[i]*stepSize)]);
}
var hist = sparkBar.join("");
//Logger.log(hist);
return hist;
}
function filterNonNum(array){
//tried this with a map, but it puts nulls in when it should skip
var newArray = [];
for(i in array){
var x = array[i];
if(x !== ""){
var res = Number(x);
if (isNaN(res) == false){
newArray.push(res);
}
}
}
return newArray;
}
function bin(values){
var bins = {1:0,
2:0,
3:0,
4:0,
5:0,
6:0,
7:0,
8:0,
9:0,
10:0};
for(i in values){
var v = values[i];
bins[v]++;
}
return bins;
}
function valueArray(dataObject){
var dataArray = new Array;
for(var o in dataObject) {
dataArray.push(dataObject[o]);
}
return dataArray;
}
@notionparallax
Copy link
Author

It takes a row of values between 1 and 10, bins them and then uses unicode blocks to represent them.

▁░░░▃░▁▅▁█

█▄▁▁▂▃▇▆▂░
▁▄▁▃▃▂▂█▂░
█░░░░░░░░░
█░▃▂█▁▅▁░▁
█▁▂░▁░░░░░
▂░░░░▄▆█▅░
█░▁░░▁▁░░░
█▁▂▁▁░▁▁░░
█░░░░░░░░░
█░░░░░░░░░
█▂▂▁▁░▁░░░
█░░░░░░░░░
▄░▁▁▃░█▅▁▁
▆▂▁░▃▁▄▇█▁
█▁░░▁░▂▁░░
█▅▄▆▁▂▅░▁▁
▇▆█▅▅▃▄▅▂░
█░░░░░░░░░
▂█▃▂▂▂▄█▂

TODO:

  • Make it take arbitrary values
  • Make it have a controllable number of bins
  • Look into how spark tweets are written

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment