Class: Rubyvis::Layout::Stack

Inherits:
Rubyvis::Layout show all
Defined in:
lib/rubyvis/layout/stack.rb

Instance Attribute Summary collapse

Attributes inherited from Panel

#_canvas, #children, #root

Attributes inherited from Mark

#_properties, #binds, #child_index, #parent, #proto, #root, #scale, #scene, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rubyvis::Layout

Stack, attr_accessor_dsl

Methods inherited from Panel

#add, #anchor, #bind, #build_instance, #children_inspect, #panel_build_implied, #to_svg, #type

Methods inherited from Bar

#type, #width

Methods inherited from Mark

#add, #anchor, attr_accessor_dsl, #bind, #build, #build_instance, #build_properties, #context, #context_apply, #context_clear, #cousin, #delete_index, #event, #extend, index, #index, index=, #index=, #index_defined?, #instance, #instances, #margin, #mark_anchor, #mark_bind, #mark_build_implied, #mark_build_instance, #properties, properties, property_method, #property_value, #render, scene, scene=, #sibling, stack, stack=, #type

Constructor Details

#initializeStack

Returns a new instance of Stack.



14
15
16
17
18
19
20
21
22
# File 'lib/rubyvis/layout/stack.rb', line 14

def initialize
  super
  @none=lambda {nil}
  @prop = {"t"=> @none, "l"=> @none, "r"=> @none, "b"=> @none, "w"=> @none, "h"=> @none}
  @values=nil
  @_x=lambda {return 0}
  @_y=lambda {return 0}
  @_values=Rubyvis.identity
end

Instance Attribute Details

#_valuesObject

Returns the value of attribute _values.



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

def _values
  @_values
end

#_xObject

Returns the value of attribute _x.



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

def _x
  @_x
end

#_yObject

Returns the value of attribute _y.



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

def _y
  @_y
end

#propObject

Returns the value of attribute prop.



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

def prop
  @prop
end

Class Method Details

.defaultsObject



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

def self.defaults
  Stack.new.extend(Layout.defaults).orient("bottom-left").offset("zero").layers([[]])
end

Instance Method Details

#build_implied(s) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/rubyvis/layout/stack.rb', line 50

def build_implied(s)
  # puts "Build stack" if $DEBUG
  panel_build_implied(s)
  data = s.layers
  n = data.size
  m = nil
  orient = s.orient
  if orient =~/^(top|bottom)\b/
    horizontal=true
  else
    horizontal=false
  end
  h = self.parent.send(horizontal ? "height" : "width")
  x = []
  y = []
  dy = []
  
  #
  # Iterate over the data, evaluating the values, x and y functions. The
  # context in which the x and y psuedo-properties are evaluated is a
  # pseudo-mark that is a grandchild of this layout.
  #
  stack = Rubyvis::Mark.stack
  
  o = OpenStruct.new({:parent=> OpenStruct.new({:parent=> self})})
  stack.unshift(nil)
  values = []
  n.times {|i|
  dy[i] = []
  y[i] = []
  o.parent.index = i
  stack[0] = data[i]
  values[i] = self._values.js_apply(o.parent, stack);
  m = values[i].size if (i==0) 
  stack.unshift(nil)
  m.times {|j|
    stack[0] = values[i][j]
    o.index = j
    x[j] = self._x.js_apply(o, stack) if i==0
    dy[i][j] = self._y.js_apply(o, stack)
  }
  stack.shift()
  }
  stack.shift()
  
  # order
  _index=nil
  case (s.order) 
  when "inside-out" 
    max  = dy.map {|v| Rubyvis.max.index(v) }
    map  = pv.range(n).sort {|a,b| return max[a] - max[b]}
    sums = dy.map {|v| Rubyvis.sum(v)}
    top = 0
    bottom = 0
    tops = []
    bottoms = []
    n.times {|i|
      j = map[i]
      if (top < bottom) 
        top += sums[j];
        tops.push(j);
      else
        bottom += sums[j];
        bottoms.push(j);
      end
    }
    _index = bottoms.reverse+tops
    
  when "reverse"
    _index = Rubyvis.range(n - 1, -1, -1)
  else
    _index = Rubyvis.range(n)
  end
  
  #/* offset */
  case (s.offset) 
  when "silohouette"
    m.times {|j|
      o = 0;
      n.times {|i| 
        o += dy[i][j]
      }
      y[_index[0]][j] = (h - o) / 2.0;
    }
  
  when "wiggle"
    o = 0;
    n.times {|i|  o += dy[i][0] }
    
    y[_index[0]][0] = o = (h - o) / 2.0
    
    (1...m).each  {|j|
      s1 = 0
      s2 = 0
      dx = x[j] - x[j - 1]
      n.times {|i| s1 += dy[i][j]}
      n.times {|i|
        
        s3 = (dy[_index[i]][j] - dy[_index[i]][j - 1]) / (2.0 * dx)
        i.times {|k|
          s3 += (dy[_index[k]][j] - dy[_index[k]][j - 1]) / dx.to_f
        }
        s2 += s3 * dy[_index[i]][j]
      }
      o -= (s1!=0) ? s2 / s1.to_f * dx : 0
      y[_index[0]][j] = o
      
    }
  when "expand"
    m.times {|j|
      y[_index[0]][j] = 0
      
      k = 0
      n.times {|i|k += dy[i][j]}
      if (k!=0) 
        k = h / k.to_f
        n.times {|i| dy[i][j] *= k}
      else 
        k = h / n.to_f
        n.times { dy[i][j] = k}
      end
    }
  else
    m.times {|j| y[_index[0]][j] = 0}
  end
  
  # Propagate the offset to the other series. */
  m.times {|j|
  o = y[_index[0]][j]
  (1...n).each {|i|
    
    o += dy[_index[i - 1]][j]
    y[_index[i]][j] = o
  }
  }
  
  # /* Find the property definitions for dynamic substitution. */
  
  i = orient.index("-")
  pdy = horizontal ? "h" : "w"
  px = i < 0 ? (horizontal ? "l" : "b") : orient[i + 1,1]
  py = orient[0,1]
  
  @values=values
  
  @prop.each {|k,v|
    @prop[k]=@none
  }
  # puts "stack: x:#{px}, y:#{py}, dy:#{pdy}" if $DEBUG
  @prop[px] =lambda {|i1,j| x[j]}
  @prop[py] =lambda {|i1,j| y[i1][j]}
  @prop[pdy]=lambda {|i1,j| dy[i1][j]}  
end

#layerObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rubyvis/layout/stack.rb', line 204

def layer
  that=self
  value = Rubyvis::Mark.new().data(lambda { that.values[self.parent.index] }).top(proxy("t")).left(proxy("l")).right(proxy("r")).
    bottom(proxy("b")).
  width(proxy("w")).
  height(proxy("h"))
  
  class << value
    def that=(v)
      @that = v
    end
    def add(type)
      that  = @that
      that.add( Rubyvis.Panel ).data(lambda { that.layers() }).add(type).extend( self )
    end
  end
  value.that=self
  return value
end

#proxy(name) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/rubyvis/layout/stack.rb', line 41

def proxy(name)
  that=self
  return lambda {
    a=that.prop[name].js_call(self, self.parent.index, self.index);
    puts "proxy(#{name}): #{a}" if $DEBUG
    a
  }
end

#values(f = nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/rubyvis/layout/stack.rb', line 31

def values(f=nil)
  if f.nil?
    @values
  else
    @_values=Rubyvis.functor(f)
    return self
  end
end

#x(f) ⇒ Object



23
24
25
26
# File 'lib/rubyvis/layout/stack.rb', line 23

def x(f)
  @_x=Rubyvis.functor(f)
  return self
end

#y(f) ⇒ Object



27
28
29
30
# File 'lib/rubyvis/layout/stack.rb', line 27

def y(f)
  @_y=Rubyvis.functor(f)
  return self
end