Sha256: 059eeccbc5199be5e4472d7c294edef1c3f58aab8e6b942d176e68af3a132068

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

# = Line and Step Chart
# Line charts are often used to visualize time series data, encoding the value of a variable over time using position. Typically, linear interpolation is used between samples. However, in some cases, the data does not vary smoothly, but instead changes in response to discrete events.
# Using the interpolate property, it is easy to change this behavior for lines and areas. Rubyvis also supports various nonlinear interpolation methods, including cardinal spline, Catmull-Rom spline, B-spline, and monotone cubic.

$:.unshift(File.dirname(__FILE__)+"/../lib")
require 'rubyvis'

data = pv.range(0, 10, 0.2).map {|x| 
  OpenStruct.new({:x=> x, :y=> Math.sin(x) + rand() + 1.5})
}


vis = pv.Panel.new().width(200).height(200);


w = 400
h = 200
x = pv.Scale.linear(data, lambda {|d| d.x}).range(0, w)
  
y = pv.Scale.linear(0, 4).range(0, h);

#/* The root panel. */
vis = pv.Panel.new()
    .width(w)
    .height(h)
    .bottom(20)
    .left(20)
    .right(10)
    .top(5);

#/* X-axis ticks. */
vis.add(pv.Rule)
    .data(x.ticks())
    .visible(lambda {|d|  d > 0})
    .left(x)
    .strokeStyle("#eee")
  .add(pv.Rule)
    .bottom(-5)
    .height(5)
    .strokeStyle("#000")
  .anchor("bottom").add(pv.Label)
  .text(x.tick_format)

#/* Y-axis ticks. */
vis.add(pv.Rule)
    .data(y.ticks(5))
    .bottom(y)
    .strokeStyle(lambda {|d| d!=0 ? "#eee" : "#000"})
  .anchor("left").add(pv.Label)
  .text(y.tick_format);

#/* The line. */
vis.add(pv.Line)
    .data(data)
    .interpolate("step-after")
    .left(lambda {|d| x.scale(d.x)})
    .bottom(lambda {|d| y.scale(d.y)})
    .lineWidth(3);

vis.render();


    

puts vis.to_svg

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubyvis-0.3.3 examples/line_and_step.rb
rubyvis-0.3.2 examples/line_and_step.rb
rubyvis-0.3.1 examples/line_and_step.rb
rubyvis-0.3.0 examples/line_and_step.rb
rubyvis-0.2.2 examples/line_and_step.rb
rubyvis-0.2.1 examples/line_and_step.rb
rubyvis-0.2.0 examples/line_and_step.rb
rubyvis-0.1.7 examples/line_and_step.rb
rubyvis-0.1.6 examples/line_and_step.rb