Module: Rubyvis

Defined in:
lib/rubyvis.rb,
lib/rubyvis/mark.rb,
lib/rubyvis/nest.rb,
lib/rubyvis/scale.rb,
lib/rubyvis/format.rb,
lib/rubyvis/layout.rb,
lib/rubyvis/mark/bar.rb,
lib/rubyvis/mark/dot.rb,
lib/rubyvis/property.rb,
lib/rubyvis/internals.rb,
lib/rubyvis/mark/area.rb,
lib/rubyvis/mark/line.rb,
lib/rubyvis/mark/rule.rb,
lib/rubyvis/scale/log.rb,
lib/rubyvis/transform.rb,
lib/rubyvis/mark/image.rb,
lib/rubyvis/mark/label.rb,
lib/rubyvis/mark/panel.rb,
lib/rubyvis/mark/wedge.rb,
lib/rubyvis/color/color.rb,
lib/rubyvis/format/date.rb,
lib/rubyvis/mark/anchor.rb,
lib/rubyvis/color/colors.rb,
lib/rubyvis/layout/stack.rb,
lib/rubyvis/scale/linear.rb,
lib/rubyvis/sceneelement.rb,
lib/rubyvis/format/number.rb,
lib/rubyvis/scale/ordinal.rb,
lib/rubyvis/scene/svg_bar.rb,
lib/rubyvis/scene/svg_dot.rb,
lib/rubyvis/scene/svg_area.rb,
lib/rubyvis/scene/svg_line.rb,
lib/rubyvis/scene/svg_rule.rb,
lib/rubyvis/scene/svg_image.rb,
lib/rubyvis/scene/svg_label.rb,
lib/rubyvis/scene/svg_panel.rb,
lib/rubyvis/scene/svg_wedge.rb,
lib/rubyvis/scale/quantitative.rb,
lib/rubyvis/scene/svg_scene.rb

Defined Under Namespace

Modules: AreaPrototype, Colors, Format, LinePrototype, Scale, SvgScene Classes: Anchor, Area, Bar, Color, Dot, Image, Label, Layout, Line, Mark, Nest, NestedArray, Panel, Property, Rule, SceneElement, Transform, Wedge

Constant Summary collapse

VERSION =

Rubyvis version

'0.1.7'
API_VERSION =

Protovis API on which current Rubyvis is based

'3.3'
Infinity =
1.0 / 0
@@id =

:section: /pv-internals.js

0

Class Method Summary collapse

Class Method Details

.Anchor(target) ⇒ Object

Alias por Rubyvis::Anchor.new(target)



3
4
5
# File 'lib/rubyvis/mark/anchor.rb', line 3

def self.Anchor(target)
  Rubyvis::Anchor.new(target)
end

.AreaObject

Alias for Rubyvis::Area



3
4
5
# File 'lib/rubyvis/mark/area.rb', line 3

def self.Area
  Rubyvis::Area
end

.BarObject

Alias for Rubyvis::Bar



3
4
5
# File 'lib/rubyvis/mark/bar.rb', line 3

def self.Bar
  Rubyvis::Bar
end

.blend(arrays) ⇒ Object



46
47
48
49
# File 'lib/rubyvis/internals.rb', line 46

def self.blend(arrays)
  # I love Ruby expresivness
  arrays.inject([]) {|ac,v| ac+v}
end

.childObject



63
64
65
# File 'lib/rubyvis.rb', line 63

def self.child
  lambda {|*args| self.child_index}
end

.clear_documentObject



69
70
71
72
# File 'lib/rubyvis.rb', line 69

def self.clear_document
  @document=REXML::Document.new
  @document.add_element("document")
end

.color(format) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubyvis/color/color.rb', line 3

def self.color(format)
  return format.rgb if format.respond_to? :rgb
  if (format =~/([a-z]+)\((.*)\)/)
    m2 = $2.split(",")
    a = 1
    if ['hsla','rgba'].include? $1
      a = m2[3].to_f
      return Color.transparent if (a==0)
    end

    if ['hsla','hsl'].include? $1
      h=m2[0].to_f
      s=m2[0].to_f.quo(100)
      l=m2[0].to_f.quo(100)
      return Color::Hsl.new(h,s,l,a).rgb()
    end

    if ['rgba','rgb'].include? $1
      parse=lambda {|c|
        f=c.to_f
        return (c[c.size-1]=='%') ? (f*2.55).round : f
      }
      r=parse.call(m2[0])
      g=parse.call(m2[1])
      b=parse.call(m2[2])
      return Rubyvis.rgb(r,g,b,a)
    end
  end
  
  named = Rubyvis::Color.names[format.to_sym]


  return Rubyvis.color(named) if (named)

  # Hexadecimal colors: #rgb and #rrggbb. */
  if (format[0,1]== "#")
    if (format.size == 4)
      r = format[1,1]; r += r
      g = format[2,1]; g +=g
      b = format[3,1]; b +=b
    elsif (format.size == 7)
      r = format[1,2]
      g = format[3,2]
      b = format[5,2]
    end
    return Rubyvis.rgb(r.to_i(16), g.to_i(16), b.to_i(16), 1);
  end

  # Otherwise, pass-through unsupported colors. */
  return Rubyvis::Color.new(format, 1);
end

.colors(*args) ⇒ Object



6
7
8
9
10
# File 'lib/rubyvis/color/colors.rb', line 6

def self.colors(*args)
  scale=Rubyvis::Scale.ordinal
  scale.range(*args)
  scale
end

.ColorsObject

Alias for Rubyvis::Colors



3
4
5
# File 'lib/rubyvis/color/colors.rb', line 3

def self.Colors
  Rubyvis::Colors
end

.cross(a, b) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/rubyvis/internals.rb', line 37

def self.cross(a,b)
  array = [];
  a.each {|x|
    b.each {|y|
      array.push([x,y])
    }
  }
  array
end

.degrees(radians) ⇒ Object



224
225
226
# File 'lib/rubyvis/internals.rb', line 224

def self.degrees(radians)
  ((180.0) / Math::PI)*radians
end

.deviation(array, f = nil) ⇒ Object



200
201
202
# File 'lib/rubyvis/internals.rb', line 200

def self.deviation(array,f=nil)
  Math::sqrt(self.variance(array,f) / (array.size.to_f-1))
end

.documentObject



73
74
75
76
77
78
79
# File 'lib/rubyvis.rb', line 73

def self.document
  if @document.nil?
    @document=REXML::Document.new
    @document.add_element("document")
  end
  @document.root
end

.DotObject

Alias por Rubyvis::Dot



3
4
5
# File 'lib/rubyvis/mark/dot.rb', line 3

def self.Dot
  Rubyvis::Dot
end

.FormatObject



2
3
4
# File 'lib/rubyvis/format.rb', line 2

def self.Format
  Rubyvis::Format
end

.functor(f) ⇒ Object

Return a value as a Proc.



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

def self.functor(f)
  (f.is_a? Proc) ? f : lambda {f}
end

.idObject

Return a unique id each time



5
6
7
# File 'lib/rubyvis/internals.rb', line 5

def self.id
  @@id+=1
end

.identityObject

Returns the passed-in argument, x; the identity function. This method is provided for convenience since it is used as the default behavior for a number of property functions.

Parameters:

  • a value.

Returns:

  • the value x.



43
44
45
# File 'lib/rubyvis.rb', line 43

def self.identity
  lambda {|x,*args| x}
end

.ImageObject



2
3
4
# File 'lib/rubyvis/mark/image.rb', line 2

def self.Image
  Rubyvis::Image
end

.indexObject

Returns self.index. This method is provided for convenience for use with scales. For example, to color bars by their index, say:

<pre>.fill_style(Rubyvis::Colors.category10().by(Rubyvis.index))</pre>

This method is equivalent to lambda {self.index}, but more succinct. Note that the index property is also supported for accessor functions with Rubyvis.max, Rubyvis.min and other array utility methods.



59
60
61
# File 'lib/rubyvis.rb', line 59

def self.index
  lambda {|*args| self.index}
end

.keys(map) ⇒ Object

:section: /data/Objects.js



229
230
231
# File 'lib/rubyvis/internals.rb', line 229

def self.keys(map)
  map.keys
end

.LabelObject

Alias for Rubyvis::Label



3
4
5
# File 'lib/rubyvis/mark/label.rb', line 3

def self.Label
  Rubyvis::Label
end

.LayoutObject



2
3
4
# File 'lib/rubyvis/layout.rb', line 2

def self.Layout
  Rubyvis::Layout
end

.LineObject

Alias for Rubyvis::Line



3
4
5
# File 'lib/rubyvis/mark/line.rb', line 3

def self.Line
  Rubyvis::Line
end

.log(x, b) ⇒ Object



203
204
205
# File 'lib/rubyvis/internals.rb', line 203

def self.log(x,b)
  Math::log(x).quo(Math::log(b))
end

.log_adjusted(x, b) ⇒ Object



209
210
211
212
213
214
# File 'lib/rubyvis/internals.rb', line 209

def self.log_adjusted(x,b)
  x if x.is_a? Float and !x.finite?
  negative=x<0
  x += (b - x) / b.to_f if (x < b)
  negative ? -Rubyvis.log(x, b) : Rubyvis.log(x, b);
end

.log_ceil(x, b) ⇒ Object



218
219
220
# File 'lib/rubyvis/internals.rb', line 218

def self.log_ceil(x,b)
  (x > 0) ? b ** (Rubyvis.log(x, b)).ceil : -(b ** -(-Rubyvis.log(-x, b)).ceil);
end

.log_floor(x, b) ⇒ Object



215
216
217
# File 'lib/rubyvis/internals.rb', line 215

def self.log_floor(x,b)
  (x>0)  ? b**(Rubyvis.log(x,b).floor) : b**(-(-Rubyvis.log(-x,b)).floor)
end

.log_symmetric(x, b) ⇒ Object



206
207
208
# File 'lib/rubyvis/internals.rb', line 206

def self.log_symmetric(x,b)
  (x == 0) ? 0 : ((x < 0) ? -Rubyvis.log(-x, b) : Rubyvis.log(x, b));
end

.map(array, f = nil) ⇒ Object

A private variant of Array.map that supports the index property

:call-seq:

self.map(Array)
self.map(array,proc)


23
24
25
26
27
28
29
30
31
32
# File 'lib/rubyvis/internals.rb', line 23

def self.map(array, f=nil)
  if f
    array.size.times.map {|i|
      o=o_index(i)
      f.js_call(o, array[i])
    }
  else
    array.dup
  end
end

.max(array, f = nil) ⇒ Object



164
165
166
167
# File 'lib/rubyvis/internals.rb', line 164

def self.max(array, f=nil)
  return array.size-1 if f==Rubyvis.index
  f ? Rubyvis.map(array, f).max : array.max
end

.max_index(array, f = nil) ⇒ Object



168
169
170
171
172
# File 'lib/rubyvis/internals.rb', line 168

def self.max_index(array,f=nil)
  a2=Rubyvis.map(array,f)
  max=a2.max
  a2.index(max)
end

.mean(array, f = nil) ⇒ Object



183
184
185
# File 'lib/rubyvis/internals.rb', line 183

def self.mean(array, f=nil)
  Rubyvis.sum(array,f).quo(array.size)
end

.median(array, f = nil) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/rubyvis/internals.rb', line 186

def self.median(array,f=nil)
  return (array.length - 1).quo(2) if (f == Rubyvis.index)
  array = Rubyvis.map(array, f).sort
  return array[array.size.quo(2).floor] if (array.length % 2>0)
  i = array.size.quo(2);
  return (array[i - 1] + array[i]).quo(2);
end

.min(array, f = nil) ⇒ Object



174
175
176
177
# File 'lib/rubyvis/internals.rb', line 174

def self.min(array, f=nil)
  return array.size-1 if f==Rubyvis.index
  f ? Rubyvis.map(array, f).min : array.min
end

.min_index(array, f = nil) ⇒ Object



178
179
180
181
182
# File 'lib/rubyvis/internals.rb', line 178

def self.min_index(array,f=nil)
  a2=Rubyvis.map(array,f)
  min=a2.min
  a2.index(min)
end

.natural_orderObject



84
85
86
# File 'lib/rubyvis/internals.rb', line 84

def self.natural_order()
  lambda {|a,b| a<=>b}
end

.nest(array) ⇒ Object

Returns a Nest operator for the specified array. This is a convenience factory method, equivalent to Nest.new(array).

Parameters:

  • an array of elements to nest.

See Also:



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

def self.nest(array)
  Nest.new(array)
end

.normalize(array, f = nil) ⇒ Object



60
61
62
63
64
# File 'lib/rubyvis/internals.rb', line 60

def self.normalize(array,f=nil)
  norm=Rubyvis.map(array,f)
  sum=Rubyvis.sum(norm)
  norm.map {|x| x.quo(sum)}
end

.numerate(keys, f = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rubyvis/internals.rb', line 72

def self.numerate(keys, f=nil)
  f=Rubyvis.identity if f.nil?
  m = {}
  keys.each_with_index {|x,i|
    o=o_index(i)
    m[f.js_call(o,x)]=i
  }
  m
end

.o_index(i) ⇒ Object



65
66
67
# File 'lib/rubyvis/internals.rb', line 65

def self.o_index(i)
  o=OpenStruct.new :index=>i
end

.PanelObject

Alias for Rubyvis::Panel



3
4
5
# File 'lib/rubyvis/mark/panel.rb', line 3

def self.Panel
  Rubyvis::Panel
end

.parentObject



66
67
68
# File 'lib/rubyvis.rb', line 66

def self.parent
  lambda {|*args| self.parent.index}
end

.permute(array, indexes, f = nil) ⇒ Object



68
69
70
71
# File 'lib/rubyvis/internals.rb', line 68

def self.permute(array,indexes, f=nil)
  f=Rubyvis.identity if f.nil?
  indexes.map {|i| o=o_index(i); f.js_call(o, array[i])}
end

.radians(degrees) ⇒ Object



221
222
223
# File 'lib/rubyvis/internals.rb', line 221

def self.radians(degrees)
  (Math::PI/180.0)*degrees
end

.random(*arguments) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/rubyvis/internals.rb', line 143

def self.random(*arguments)
  start,stop,step=arguments
  if (arguments.size == 1)
    stop = start;
    start = 0;
  end
  step||= 1;
  return step ? ((rand() * (stop - start).quo(step)).floor * step + start) : (rand() * (stop - start) + start);
end

.range(*arguments) ⇒ Object

:section: /data/Numbers.js



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
# File 'lib/rubyvis/internals.rb', line 116

def self.range(*arguments)
  start, stop, step=arguments
  if (arguments.size == 1)
    stop = start;
    start = 0;
  end
  step||= 1
  raise "range must be finite" if ((stop - start) / step.to_f).infinite?
  array = []
  i = 0
  stop = stop- (stop - start) * 1e-10 #// floating point precision!
  j = start + step * i
  if (step < 0)
    while (j > stop)
      array.push(j)
      i+=1
      j = start + step * i
    end
  else
    while (j < stop)
      array.push(j)
      i+=1
      j = start + step * i
    end
  end
  array
end

.repeat(array, n = 2) ⇒ Object



34
35
36
# File 'lib/rubyvis/internals.rb', line 34

def self.repeat(array, n=2)
  array*n
end

.reverse_orderObject



87
88
89
# File 'lib/rubyvis/internals.rb', line 87

def self.reverse_order()
  lambda {|a,b| -(a<=>b)}
end

.rgb(r, g, b, a = 1) ⇒ Object



55
56
57
# File 'lib/rubyvis/color/color.rb', line 55

def self.rgb(r,g,b,a=1)
  Rubyvis::Color::Rgb.new(r,g,b,a)
end

.RuleObject

Alias for Rubyvis::Rule



3
4
5
# File 'lib/rubyvis/mark/rule.rb', line 3

def self.Rule
  Rubyvis::Rule
end

.ScaleObject



2
3
4
# File 'lib/rubyvis/scale.rb', line 2

def self.Scale
  Rubyvis::Scale
end

.SceneObject



16
17
18
# File 'lib/rubyvis/scene/svg_scene.rb', line 16

def self.Scene
  Rubyvis::SvgScene
end

.search(array, value, f = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubyvis/internals.rb', line 91

def self.search(array, value, f=nil)
  f = Rubyvis.identity if (f.nil?)
  low = 0
  high = array.size - 1;
  while (low <= high)
    mid = (low + high) >> 1
    midValue = f.call(array[mid]);
    if (midValue < value)
      low = mid + 1;
    elsif (midValue > value)
      high = mid - 1;
    else
      return mid;
    end
  end
  return -low - 1;
end

.search_index(array, value, f = nil) ⇒ Object



108
109
110
111
# File 'lib/rubyvis/internals.rb', line 108

def self.search_index(array,value,f=nil)
  i=Rubyvis.search(array,value,f)
  (i < 0 ) ? (-i-1) : i;
end

.sum(array, f = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rubyvis/internals.rb', line 153

def self.sum(array,f=nil)
  if f.nil?
    array.inject(0) {|ac,v| ac+v}
  else
    i=0
    array.inject(0) {|ac,v|
      o=o_index(i);i+=1;
      ac+f.js_call(o,v);
    }
  end
end

.to_svgObject



80
81
82
83
84
# File 'lib/rubyvis.rb', line 80

def self.to_svg
  s=""
  @document.write(s)
  s
end

.TransformObject



2
3
4
# File 'lib/rubyvis/transform.rb', line 2

def self.Transform
  Rubyvis::Transform
end

.transpose(matrix) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/rubyvis/internals.rb', line 50

def self.transpose(matrix)
  out=[]
  matrix.size.times do |i|
    matrix[i].size.times do |j|
      out[j]||=Array.new
      out[j][i]=matrix[i][j]
    end
  end
  out
end

.uniq(array, f = nil) ⇒ Object



81
82
83
# File 'lib/rubyvis/internals.rb', line 81

def self.uniq(array, f=nil  )
  self.map(array,f).uniq
end

.variance(array, f = nil) ⇒ Object

Sum of square, really



194
195
196
197
198
199
# File 'lib/rubyvis/internals.rb', line 194

def self.variance(array,f=nil)
  return 0 if array.size==1 or array.uniq.size==1
  ar=(f.nil?) ? array : Rubyvis.map(array,f)
  mean=Rubyvis.mean(ar)
  ar.inject(0) {|ac,v| ac+(v-mean)**2}
end

.WedgeObject

Alias for Rubyvis::Wedge



3
4
5
# File 'lib/rubyvis/mark/wedge.rb', line 3

def self.Wedge
  Rubyvis::Wedge
end