Method: Rubyvis::Nest#map

Defined in:
lib/rubyvis/nest.rb

#mapObject

Returns a hierarchical map of values. Each key adds one level to the hierarchy. With only a single key, the returned map will have a key for each distinct value of the key function; the correspond value with be an array of elements with that key value. If a second key is added, this will be a nested map. For example:

<pre>Rubyvis.nest(yields)

.key(function(d) d.variety)
.key(function(d) d.site)
.map()</pre>

returns a map m such that m[variety][site] is an array, a subset of yields, with each element having the given variety and site.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rubyvis/nest.rb', line 109

def map
  #i=0
  map={} 
  values=[]
  @array.each_with_index {|x,j|
    m=map
    (@keys.size-1).times {|i|
      k=@keys[i].call(x)
      m[k]={} if (!m[k])
      m=m[k]
    }
    k=@keys.last.call(x)
    if(!m[k])
      a=[]
      values.push(a)
      m[k]=a
    end
    m[k].push(x)
  }
  if(self.order)
    values.each_with_index {|v,vi|
      values[vi].sort!(&self.order)
    }
  end
  map
end