Created
March 14, 2012 18:11
-
-
Save nsfmc/2038321 to your computer and use it in GitHub Desktop.
Junkify an inline data series on your webpage
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 junkify = function( someClass ){ | |
// requires underscore.js | |
var sparks = document.getElementsByClassName( someClass ); | |
_(sparks).each(function(e,i){ | |
var range = 16; | |
var origContent = e.innerHTML; | |
e.title = "data: "+ origContent; | |
var d = origContent.split(","), | |
max = _.max(d), min = _.min(d), | |
dataRange = max-min; | |
// an array like ["0","1",...,"e","f,","g"] | |
var cj = _.range(17).map(function(e){return e.toString(17)}) | |
// remap data to start at "0" and end at "g" | |
var rescaled = _(d).map(function(e){ | |
var idx = Math.ceil((e - min) * (range / dataRange) ); | |
return cj[idx] | |
}) | |
// reassemble the string and add chartjunk's junkjunk classname | |
var graphText = rescaled.join("") | |
e.innerHTML = graphText; | |
var cns = _.uniq( (e.className + " junkjunk").split(" ") ).join(" ") | |
e.className = cns; | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment