Class: ML::Data::Plotter

Inherits:
Object
  • Object
show all
Defined in:
lib/data/plotter.rb

Overview

Plotting the data to svg

Instance Method Summary collapse

Constructor Details

#initialize(x_range = 100, y_range = 100, x_size = 100, y_size = 100) {|_self| ... } ⇒ Plotter

Initializer of plotter

Parameters:

  • x_range (Integer) (defaults to: 100)

    x value range

  • y_range (Integer) (defaults to: 100)

    y value range

  • x_size (Integer) (defaults to: 100)

    x plot size

  • y_size (Integer) (defaults to: 100)

    y plot size

Yields:

  • (_self)

Yield Parameters:



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
# File 'lib/data/plotter.rb', line 13

def initialize x_range = 100, y_range = 100, x_size = 100, y_size = 100
  @x_range = x_range
  @y_range = y_range
  @x_size = x_size
  @y_size = y_size

  @x = pv.Scale.linear(0, @x_range).range(0, @x_size)
  @y = pv.Scale.linear(0, @y_range).range(0, @y_size)

  @vis = pv.Panel.new.width(@x_size).height(@y_size)
           .bottom(20).left(20).right(10).top(5)
  
  @vis.add(pv.Rule).data(@y.ticks()).bottom(@y)
      .stroke_style(lambda {|d| d!=0 ? "#eee" : "#000"})
      .anchor("left").add(pv.Label)
      .visible(lambda {|d| d > 0 and d < x_range})
      .text(@y.tick_format)
   
  @vis.add(pv.Rule).data(@x.ticks()).left(@x)
      .stroke_style(lambda {|d| d!=0 ? "#eee" : "#000"})
      .anchor("bottom").add(pv.Label)
      .visible(lambda {|d| d > 0 and d < y_range})
      .text(@x.tick_format)

  yield(self) if block_given?
end

Instance Method Details

#dot(points, shape = "circle", color = "#000") ⇒ Object

Plotting points with shape and color

Parameters:

  • points (Array)

    points to plot

  • shape (String) (defaults to: "circle")

    shape of the points

  • color (String) (defaults to: "#000")

    color of the points



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/data/plotter.rb', line 45

def dot points, shape = "circle", color = "#000"
  # FIXME: dirty hack for instance_exec
  x = @x
  y = @y

  @vis.add(pv.Dot).data(points)         
      .left(lambda {|d| x.scale(d[0])})
      .bottom(lambda {|d| y.scale(d[1])})
      .shape(shape)
      .stroke_style(color)
end

#line(points, color = "#000") ⇒ Object

Plotting line with color

Parameters:

  • points (Array)

    2 points which represents line

  • color (String) (defaults to: "#000")

    color of the line



61
62
63
64
65
66
67
68
69
# File 'lib/data/plotter.rb', line 61

def line points, color = "#000"
  x = @x
  y = @y

  @vis.add(pv.Line).data(points)
      .left(lambda {|d| x.scale(d[0])})
      .bottom(lambda {|d| y.scale(d[1])})
      .stroke_style(color)
end

#to_svgString

Convert to svg

Returns:

  • (String)

    svg plot



74
75
76
77
# File 'lib/data/plotter.rb', line 74

def to_svg
  @vis.render
  @vis.to_svg
end