Class: Charty::Rubyplot

Inherits:
PlotterAdapter show all
Defined in:
lib/charty/backends/rubyplot.rb

Constant Summary collapse

Name =
"rubyplot"

Instance Method Summary collapse

Methods inherited from PlotterAdapter

create, inherited

Constructor Details

#initializeRubyplot

Returns a new instance of Rubyplot


7
8
9
# File 'lib/charty/backends/rubyplot.rb', line 7

def initialize
  @plot = ::Rubyplot
end

Instance Method Details

#label(x, y) ⇒ Object


11
12
# File 'lib/charty/backends/rubyplot.rb', line 11

def label(x, y)
end

#plot(plot, context) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/charty/backends/rubyplot.rb', line 34

def plot(plot, context)
  # case
  # when plot.respond_to?(:xlim)
  #   plot.xlim(context.range_x.begin, context.range_x.end)
  #   plot.ylim(context.range_y.begin, context.range_y.end)
  # when plot.respond_to?(:set_xlim)
  #   plot.set_xlim(context.range_x.begin, context.range_x.end)
  #   plot.set_ylim(context.range_y.begin, context.range_y.end)
  # end

  figure = ::Rubyplot::Figure.new
  axes = figure.add_subplot 0,0
  axes.title = context.title if context.title
  axes.x_title = context.xlabel if context.xlabel
  axes.y_title = context.ylabel if context.ylabel

  case context.method
  when :bar
    context.series.each do |data|
      axes.bar! do |p|
        p.data(data.xs.to_a)
        p.label = data.label
      end
    end
    figure
  when :barh
    raise NotImplementedError
  when :box_plot
    raise NotImplementedError
  when :bubble
    context.series.each do |data|
      axes.bubble! do |p|
        p.data(data.xs.to_a, data.ys.to_a, data.zs.to_a)
        p.label = data.label if data.label
      end
    end
    figure
  when :curve
    context.series.each do |data|
      axes.line! do |p|
        p.data(data.xs.to_a, data.ys.to_a)
        p.label = data.label if data.label
      end
    end
    figure
  when :scatter
    context.series.each do |data|
      axes.scatter! do |p|
        p.data(data.xs.to_a, data.ys.to_a)
        p.label = data.label if data.label
      end
    end
    figure
  when :error_bar
    # refs. https://github.com/SciRuby/rubyplot/issues/26
    raise NotImplementedError
  when :hist
    raise NotImplementedError
  end
end

#render(context, filename = "") ⇒ Object


29
30
31
32
# File 'lib/charty/backends/rubyplot.rb', line 29

def render(context, filename="")
  FileUtils.mkdir_p(File.dirname(filename))
  plot(@plot, context).write(filename)
end

#render_layout(layout) ⇒ Object


18
19
20
21
22
23
24
25
26
27
# File 'lib/charty/backends/rubyplot.rb', line 18

def render_layout(layout)
  (fig, axes) = *@plot.subplots(nrows: layout.num_rows, ncols: layout.num_cols)
  layout.rows.each_with_index do |row, y|
    row.each_with_index do |cel, x|
      plot = layout.num_rows > 1 ? axes[y][x] : axes[x]
      plot(plot, cel)
    end
  end
  @plot.show
end

#series=(series) ⇒ Object


14
15
16
# File 'lib/charty/backends/rubyplot.rb', line 14

def series=(series)
  @series = series
end