class Rubyvis::Histogram

Attributes

data[R]
f[R]
frequency[RW]

Public Class Methods

new(data,f=nil) click to toggle source
# File lib/rubyvis/histogram.rb, line 12
def initialize(data,f=nil)
  @data=data
  @f=f
  @frequency=true
end

Public Instance Methods

bins(ticks=nil) click to toggle source
# File lib/rubyvis/histogram.rb, line 17
def bins(ticks=nil)
  x=Rubyvis.map(data,f)
  bins=[]
  ticks||=Rubyvis::Scale.linear(x).ticks()
  # Initialize the bins
  (ticks.size-1).times {|i|
    bin=bins[i]=[]
    bin.extend Bin
    bin.x=ticks[i]
    bin.dx=ticks[i+1]-ticks[i]
    bin.y=0
  }
  x.size.times {|i|
    j=Rubyvis.search_index(ticks, x[i])-1
    bin=bins[ [0,[bins.size-1,j].min].max]
    bin.y+=1
    bin.push(data[i])
  }
  if !@frequency
    bins.each {|b|
      b.y=b.y/x.size.to_f
    }
  end
  bins
end