File: stacked_charts.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download | duplicates (3)
1
2
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
# = Stacked Area
# This example uses the Stack layout to stack areas one over another.
$:.unshift(File.dirname(__FILE__)+"/../../lib")
require 'rubyvis'

data = pv.range(4).map {|i|
  pv.range(0, 10, 0.1).map {|x|
    OpenStruct.new({:x=> x, :y=> Math.sin(x) + rand() * 0.5 + 2})
  }
}

w = 400
h = 200

x = pv.Scale.linear(0, 9.9).range(0, w)
y = pv.Scale.linear(0, 14).range(0, h)

#/* The root panel. */
vis = pv.Panel.new()
    .width(w)
    .height(h)
    .bottom(20)
    .left(20)
    .right(10)
    .top(5)
#/* X-axis and ticks. */
vis.add(pv.Rule)
    .data(x.ticks())
    .visible(lambda {|d| d!=0})
    .left(x)
    .bottom(-5)
    .height(5)
  .anchor("bottom").add(pv.Label)
  .text(x.tick_format)
#/* Y-axis and ticks. */
vis.add(pv.Rule)
    .data(y.ticks(3))
    .bottom(y)
    .strokeStyle(lambda {|d|  d!=0 ? "rgba(128,128,128,.2)" : "#000"})
  .anchor("left").add(pv.Label)
  .text(y.tick_format)
#/* The stack layout. */
vis.add(pv.Layout.Stack)
    .layers(data)
    .x(lambda {|d| x.scale(d.x)})
    .y(lambda {|d| y.scale(d.y)})
  .layer.add(pv.Area)

vis.render();
puts vis.to_svg