module Rubyvis

Rubyvis

Ruby port of Protovis

Public Class Methods

Anchor(target) click to toggle source

Alias por Rubyvis::Anchor.new

# File lib/rubyvis/mark/anchor.rb, line 3
def self.Anchor(target)
  Rubyvis::Anchor.new(target)
end
Area() click to toggle source

Alias for Rubyvis::Area

# File lib/rubyvis/mark/area.rb, line 3
def self.Area
  Rubyvis::Area
end
Bar() click to toggle source

Alias for Rubyvis::Bar

# File lib/rubyvis/mark/bar.rb, line 3
def self.Bar
  Rubyvis::Bar
end
Dot() click to toggle source

Alias por Rubyvis::Dot

# File lib/rubyvis/mark/dot.rb, line 3
def self.Dot
  Rubyvis::Dot
end
Format() click to toggle source
# File lib/rubyvis/format.rb, line 2
def self.Format
  Rubyvis::Format
end
Image() click to toggle source
# File lib/rubyvis/mark/image.rb, line 2
def self.Image
  Rubyvis::Image
end
Label() click to toggle source

Alias for Rubyvis::Label

# File lib/rubyvis/mark/label.rb, line 3
def self.Label
  Rubyvis::Label
end
Layout() click to toggle source
# File lib/rubyvis/layout.rb, line 2
def self.Layout
  Rubyvis::Layout
end
Line() click to toggle source

Alias for Rubyvis::Line

# File lib/rubyvis/mark/line.rb, line 3
def self.Line
  Rubyvis::Line
end
Panel() click to toggle source

Alias for Rubyvis::Panel

# File lib/rubyvis/mark/panel.rb, line 3
def self.Panel
  Rubyvis::Panel
end
Rule() click to toggle source

Alias for Rubyvis::Rule

# File lib/rubyvis/mark/rule.rb, line 3
def self.Rule
  Rubyvis::Rule
end
Scale() click to toggle source
# File lib/rubyvis/scale.rb, line 2
def self.Scale
  Rubyvis::Scale
end
Transform() click to toggle source
# File lib/rubyvis/transform.rb, line 2
def self.Transform
  Rubyvis::Transform
end
Wedge() click to toggle source

Alias for Rubyvis::Wedge

# File lib/rubyvis/mark/wedge.rb, line 3
def self.Wedge
  Rubyvis::Wedge
end
color(format) click to toggle source
Returns the Rubyvis::Color for the specified color format string. Colors
may have an associated opacity, or alpha channel. 
Color formats are specified by CSS Color Modular Level 3, 
using either in RGB or HSL color space. For example:<ul>

<li>#f00 // #rgb
<li>#ff0000 // #rrggbb
<li>rgb(255, 0, 0)
<li>rgb(100%, 0%, 0%)
<li>hsl(0, 100%, 50%)
<li>rgba(0, 0, 255, 0.5)
<li>hsla(120, 100%, 50%, 1)
<%rul>

The SVG 1.0 color keywords names are also supported, such as "aliceblue"
and "yellowgreen". The "transparent" keyword is supported for fully-
transparent black.

<p>If the <tt>format</tt> argument is already an instance of <tt>Color<%rtt>,
the argument is returned with no further processing.

* see <a href="http://www.w3.org/TR/SVG/types.html#ColorKeywords">SVG color
keywords<%ra>
* see <a href="http://www.w3.org/TR/css3-color/">CSS3 color module</a>

/

# File lib/rubyvis/color/color.rb, line 27
def self.color(format)
  return format.rgb if format.respond_to? :rgb
  if (format =~%r([a-z]+)\((.*)\)/)
    color_type,color_data=$1,$2
    m2 = color_data.split(",")
    a = 1
    if ['hsla','rgba'].include? color_type
      a = m2[3].to_f
      return Color.transparent if (a==0)
    end

    if ['hsla','hsl'].include? color_type
      h=m2[0].to_f
      s=m2[1].to_f / 100.0
      l=m2[2].to_f / 100.0
      return Color::Hsl.new(h,s,l,a).rgb
    end
   
    if ['rgba','rgb'].include? color_type
      parse=lambda {|c|
        (c[c.size-1,1]=='%') ? (c.to_f*2.55).round : c.to_i
      }
      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
flatten(map) click to toggle source

Returns a {@link pv.Flatten} operator for the specified map. This is a convenience factory method, equivalent to new pv.Flatten(map).

# File lib/rubyvis/flatten.rb, line 4
def self.flatten(map)
  Flatten.new(map)
end
histogram(data,f=nil) click to toggle source
# File lib/rubyvis/histogram.rb, line 2
def self.histogram(data,f=nil)
  Rubyvis::Histogram.new(data,f)
end
hsl(h,s,l,a=1) click to toggle source
# File lib/rubyvis/color/color.rb, line 82
def self.hsl(h,s,l,a=1)
  Rubyvis::Color::Hsl.new(h,s,l,a)
end
nest(array) click to toggle source

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

@see Rubyvis::Nest @param {array} array an array of elements to nest. @returns {Nest} a nest operator for the specified array.

# File lib/rubyvis/nest.rb, line 10
def self.nest(array)
  Nest.new(array)
end
rgb(r,g,b,a=1) click to toggle source

Constructs a new RGB color with the specified channel values.

# File lib/rubyvis/color/color.rb, line 79
def self.rgb(r,g,b,a=1)
  Rubyvis::Color::Rgb.new(r,g,b,a)
end
vector(x,y) click to toggle source

Returns a {@link pv.Vector} for the specified x and y coordinate. This is a convenience factory method, equivalent to new pv.Vector(x, y).

@see pv.Vector @param {number} x the x coordinate. @param {number} y the y coordinate. @returns {pv.Vector} a vector for the specified coordinates.

# File lib/rubyvis/vector.rb, line 11
def self.vector(x,y) 
  Rubyvis::Vector.new(x,y)
end

/data/Arrays.js

↑ top

Public Class Methods

blend(arrays) click to toggle source
# File lib/rubyvis/internals.rb, line 53
def self.blend(arrays)
  # I love Ruby expresivness
  arrays.inject([]) {|ac,v| ac+v}
end
cross(a,b) click to toggle source
# File lib/rubyvis/internals.rb, line 44
def self.cross(a,b)
  array = [];
  a.each {|x|
    b.each {|y|
      array.push([x,y])
    }
  }
  array
end
map(Array) click to toggle source
map(array,proc)

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

# 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
natural_order() click to toggle source
# File lib/rubyvis/internals.rb, line 91
def self.natural_order()
  lambda {|a,b| a<=>b}
end
normalize(array,f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 67
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) click to toggle source
# File lib/rubyvis/internals.rb, line 79
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) click to toggle source
# File lib/rubyvis/internals.rb, line 72
def self.o_index(i)
  OpenStruct.new :index=>i
end
permute(array,indexes, f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 75
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
repeat(array, n=2) click to toggle source

Concatenates the specified array with itself n times. For example, +repeat([1, 2])+ returns [1, 2, 1, 2].

  • @param {array} a an array.

  • @param {number} [n] the number of times to repeat; defaults to two.

  • @returns {array} an array that repeats the specified array.

# File lib/rubyvis/internals.rb, line 41
def self.repeat(array, n=2)
  array*n
end
reverse_order() click to toggle source
# File lib/rubyvis/internals.rb, line 94
def self.reverse_order()
  lambda {|a,b| -(a<=>b)}
end
search_index(array,value,f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 115
def self.search_index(array,value,f=nil)
  i=Rubyvis.search(array,value,f)
  (i < 0 ) ? (-i-1) : i;
end
transpose(matrix) click to toggle source
# File lib/rubyvis/internals.rb, line 57
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 ) click to toggle source
# File lib/rubyvis/internals.rb, line 88
def self.uniq(array, f=nil  )
  self.map(array,f).uniq
end

/data/Numbers.js

↑ top

Public Class Methods

degrees(radians) click to toggle source
# File lib/rubyvis/internals.rb, line 232
def self.degrees(radians)
  ((180.0) / Math::PI)*radians
end
deviation(array,f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 208
def self.deviation(array,f=nil)
  Math::sqrt(self.variance(array,f) / (array.size.to_f-1))
end
log(x,b) click to toggle source
# File lib/rubyvis/internals.rb, line 211
def self.log(x,b)
  Math::log(x).quo(Math::log(b))
end
log_adjusted(x,b) click to toggle source
# File lib/rubyvis/internals.rb, line 217
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) click to toggle source
# File lib/rubyvis/internals.rb, line 226
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) click to toggle source
# File lib/rubyvis/internals.rb, line 223
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) click to toggle source
# File lib/rubyvis/internals.rb, line 214
def self.log_symmetric(x,b)
  (x == 0) ? 0 : ((x < 0) ? -Rubyvis.log(-x, b) : Rubyvis.log(x, b));
end
max(array, f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 172
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) click to toggle source
# File lib/rubyvis/internals.rb, line 176
def self.max_index(array,f=nil)
  a2=Rubyvis.map(array,f)
  max=a2.max
  a2.index(max)
end
mean(array, f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 191
def self.mean(array, f=nil)
  Rubyvis.sum(array,f).quo(array.size)
end
median(array,f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 194
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) click to toggle source
# File lib/rubyvis/internals.rb, line 182
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) click to toggle source
# File lib/rubyvis/internals.rb, line 186
def self.min_index(array,f=nil)
  a2=Rubyvis.map(array,f)
  min=a2.min
  a2.index(min)
end
radians(degrees) click to toggle source
# File lib/rubyvis/internals.rb, line 229
def self.radians(degrees)
  (Math::PI/180.0)*degrees
end
random(*arguments) click to toggle source
# File lib/rubyvis/internals.rb, line 150
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) click to toggle source
# File lib/rubyvis/internals.rb, line 123
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.to_f - start.to_f) / 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
sum(array, f=nil) click to toggle source
# File lib/rubyvis/internals.rb, line 160
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
variance(array,f=nil) click to toggle source

Sum of square, really

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

/data/Objects.js

↑ top

Constants

Infinity

You actually can do it! snipplr.com/view/2137/uses-for-infinity-in-ruby/

PROTOVIS_API_VERSION

Protovis API on which current Rubyvis is based

VERSION

Rubyvis version

Public Class Methods

dict(keys, f) click to toggle source

Returns a map constructed from the specified keys, using the function f to compute the value for each key. The single argument to the value function is the key. The callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

<p>For example, this expression creates a map from strings to string length:

<pre>pv.dict([“one”, “three”, “seventeen”], function(s) s.length)</pre>

The returned value is {one: 3, three: 5, seventeen: 9}. Accessor functions can refer to this.index.

  • @param {array} keys an array.

  • @param {function} f a value function.

  • @returns a map from keys to values.

# File lib/rubyvis/internals.rb, line 259
def self.dict(keys, f)
  m = {}
  keys.size.times do |i|
    unless keys[i].nil?
      k=keys[i] 
      o=o_index(i)
      m[k]=f.js_call(o,k)
    end
  end
  m
end
keys(map) click to toggle source
# File lib/rubyvis/internals.rb, line 237
def self.keys(map)
  map.keys
end

/pv-internals.js

↑ top

Public Class Methods

functor(f) click to toggle source

Return a proc wrapping specific constant

# File lib/rubyvis/internals.rb, line 9
def self.functor(f)
  (f.is_a? Proc) ? f : lambda {f}
end
id() click to toggle source

Returns a locally-unique positive id.

# File lib/rubyvis/internals.rb, line 5
def self.id
  @@id+=1
end

basic methods

↑ top

Public Class Methods

child() click to toggle source
# File lib/rubyvis.rb, line 97
def self.child
  lambda {|*args| self.child_index}
end
has_nokogiri?() click to toggle source
# File lib/rubyvis.rb, line 55
def self.has_nokogiri?
  if @@nokogiri.nil?
    begin
      require 'nokogiri'
      @@nokogiri=true
    rescue LoadError
      @@nokogiri=false
    end
  end
  @@nokogiri
end
identity() click to toggle source

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.

@param [Object] x, a value. @return [Object] the value x.

# File lib/rubyvis.rb, line 52
def self.identity
  lambda {|x,*args| x}
end
index() click to toggle source

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(::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 {@link ::max}, {@link ::min} and other array utility methods.

@see Rubyvis::Scale @see Rubyvis::Mark#index

# File lib/rubyvis.rb, line 93
def self.index
  lambda {|*args| self.index}
end
nokogiri_document(v=nil) click to toggle source
# File lib/rubyvis.rb, line 75
def self.nokogiri_document(v=nil)
  if !v.nil?
    @@nokogiri_document=v
  end
  @@nokogiri_document
end
parent() click to toggle source
# File lib/rubyvis.rb, line 100
def self.parent
  lambda {|*args| self.parent.index}
end
xml_engine() click to toggle source
# File lib/rubyvis.rb, line 66
def self.xml_engine
  if has_nokogiri? and !$rubyvis_no_nokogiri
    :nokogiri
  else
    :rexml
  end
end

colors/Colors.js

↑ top

Public Class Methods

Colors() click to toggle source

Alias for Rubyvis::Colors

# File lib/rubyvis/color/colors.rb, line 21
def self.Colors
  Rubyvis::Colors
end
colors(*args) click to toggle source

Returns a new categorical color encoding using the specified colors. The arguments to this method are an array of colors; see ::color. For example, to create a categorical color encoding using the species attribute:

<pre>::colors(“red”, “green”, “blue”).by(lambda{|d| d.species})</pre>

The result of this expression can be used as a fill- or stroke-style property. This assumes that the data’s species attribute is a string.

# File lib/rubyvis/color/colors.rb, line 36
def self.colors(*args)
  scale=Rubyvis::Scale.ordinal
  scale.range(*args)
  scale
end
dom(map) click to toggle source

Returns a Rubyvis::Dom operator for the given map. This is a convenience factory method, equivalent to Rubyvis::Dom.new. To apply the operator and retrieve the root node, call Rubyvis::Dom#root to retrieve all nodes flattened, use Rubyvis::Dom#nodes

@see pv.Dom @param map a map from which to construct a DOM. @returns {pv.Dom} a DOM operator for the specified map.

# File lib/rubyvis/dom.rb, line 10
def self.dom(map) 
  Rubyvis::Dom.new(map)
end
nodes(values) click to toggle source

Given a flat array of values, returns a simple DOM with each value wrapped by a node that is a child of the root node.

# File lib/rubyvis/dom.rb, line 335
def self.nodes(values)
  root=Rubyvis::Dom::Node.new
  values.each do |v,i|
    root.append_child(Rubyvis::Dom::Node.new(v))
  end
  root.nodes()
end

colors/Ramp.js

↑ top

Public Class Methods

ramp(*arguments) click to toggle source

Returns a linear color ramp from the specified start color to the specified end color. The color arguments may be specified either as strings or as Rubyvis::Color. This is equivalent to:

<pre> Rubyvis::Scale.linear().domain(0, 1).range(…)</pre>

# File lib/rubyvis/color/colors.rb, line 11
def self.ramp(*arguments)
  #start, _end, dummy = arguments
  scale = Rubyvis.Scale.linear
  scale.range(*arguments)
  scale
end