CmdUtils.CreateCommand({ name: "sparkword", description: "Graphs the current selection, turning it into a textual sparkline.", takes: {"data": noun_arb_text}, // really this is Aza Raskin's code with a small change from me author: {name: "Brian Ewins", email:"brian.ewins@gmail.com"}, license: "MIT", help: "Select a set of numbers -- in a table or otherwise -- and use this command to graph them as a sparkline. Don't worry about non-numbers getting in there. It'll handle them.", _cleanData: function( string ) { var dirtyData = string.split(/\W/); var data = []; for(var i=0; i max ) max = data[i]; } // Use 1/8 as baseline var chars="\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588"; var scale = (max == min) ? 1 : 7./(max - min); var sparkword = ""; for (var i = 0; i < data.length; i++) { var h = Math.round((data[i] - min) * scale); sparkword += chars.charAt(h); } return sparkword; }, preview: function(pblock, input) { var img = this._dataToSparkline( input.text ); if( !img ) jQuery(pblock).text( "Requires numbers to graph." ); else jQuery(pblock).empty().append( img ).height( "15px" ); }, execute: function( input ) { var img = this._dataToSparkline( input.text ); if( img ) CmdUtils.setSelection( img ); } });