Class: Rubyvis::Histogram

Inherits:
Object show all
Defined in:
lib/rubyvis/histogram.rb

Defined Under Namespace

Modules: Bin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, f = nil) ⇒ Histogram

Returns a new instance of Histogram.



12
13
14
15
16
# File 'lib/rubyvis/histogram.rb', line 12

def initialize(data,f=nil)
  @data=data
  @f=f
  @frequency=true
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/rubyvis/histogram.rb', line 10

def data
  @data
end

#fObject (readonly)

Returns the value of attribute f.



11
12
13
# File 'lib/rubyvis/histogram.rb', line 11

def f
  @f
end

#frequencyObject

Returns the value of attribute frequency.



9
10
11
# File 'lib/rubyvis/histogram.rb', line 9

def frequency
  @frequency
end

Instance Method Details

#bins(ticks = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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