class Rubyspark::Spark
Public Class Methods
new(data = [])
click to toggle source
a new instance of RubySpark::Spark
@param data [Array] An array of numbers to build the graph from
# File lib/rubyspark.rb, line 7 def initialize data = [] @data = [] data.cycle(1) { |x| @data.push(x.to_f) } graphit end
Public Instance Methods
compute(x, distance)
click to toggle source
Compute x’s location in relation to the max
@param x [Fixnum] data point from input array @param distance [Fixnum] the max/size of ticks array @return [Fixnum] the position on the ticks array for the value of x
# File lib/rubyspark.rb, line 18 def compute x, distance (x / distance).round - 1 end
graphit()
click to toggle source
Graphs data, takes data and maps it against flat map of ticks @return [String] sparkline output in STDOUT
# File lib/rubyspark.rb, line 24 def graphit ticks = %w(▁ ▂ ▃ ▄ ▅ ▆ ▇) distance = @data.max.to_f / ticks.size puts @data.map { |x| compute(x, distance) <= 0 ? ticks[0] : ticks[compute(x, distance)] }.join('') end