Rubyvis - Example: Bar & Column Charts

This simple bar chart is constructed using a bar mark. A linear scale is used to compute the width of the bar, while an ordinal scale sets the top position and height for the categorical dimension. Next, rules and labels are added for reference values.
Bars can be used in a variety of ways. For instance, they can be stacked or grouped to show multiple data series, or arranged as vertical columns rather than bars.

require 'rubyvis'

data = pv.range(10).map {|d| rand + 0.1 }

#/* Sizing and scales. *
w = 400
h = 250
x = pv.Scale.linear(0, 1.1).range(0, w)
y = pv.Scale.ordinal(pv.range(10)).split_banded(0, h, 4/5.0)

#/* The root panel. */
vis =

#/* The bars. */
bar = vis.add(pv.Bar)
    .top(lambda { y.scale(self.index)})

#/* The value label. */
    .text(lambda {|d| "%0.1f" % d})

#/* The variable label. */
    .text(lambda { "ABCDEFGHIJK"[self.index,1]});
#/* X-axis ticks. */
    .stroke_style(lambda {|d|  d!=0 ? "rgba(255,255,255,.3)" : "#000"})

puts vis.to_svg