Class: Charty::Backends::UnicodePlot

Inherits:
Object
  • Object
show all
Defined in:
lib/charty/backends/unicode_plot.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepareObject



9
10
11
# File 'lib/charty/backends/unicode_plot.rb', line 9

def prepare
  require 'unicode_plot'
end

Instance Method Details

#bar(bar_pos, _group_names, values, colors, _orient, **kwargs) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/charty/backends/unicode_plot.rb', line 19

def bar(bar_pos, _group_names, values, colors, _orient, **kwargs)
  @figure = {
    type: :bar,
    bar_pos: bar_pos,
    values: values,
  }
end

#begin_figureObject



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

def begin_figure
  @figure = nil
  @layout = {}
end

#box_plot(plot_data, positions, orient:, **kwargs) ⇒ Object



27
28
29
# File 'lib/charty/backends/unicode_plot.rb', line 27

def box_plot(plot_data, positions, orient:, **kwargs)
  @figure = { type: :box, data: plot_data, orient: orient }
end

#disable_xaxis_gridObject



51
52
53
# File 'lib/charty/backends/unicode_plot.rb', line 51

def disable_xaxis_grid
  # do nothing
end

#render(**kwargs) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/charty/backends/unicode_plot.rb', line 55

def render(**kwargs)
  plot = case @figure[:type]
          when :bar
            ::UnicodePlot.barplot(@layout[:xtick_labels], @figure[:values], xlabel: @layout[:xlabel])
          when :box
            xlabel = if @figure[:orient] == :v
                       @layout[:ylabel]
                     else
                       @layout[:xlabel]
                     end
            ::UnicodePlot.boxplot(@layout[:xtick_labels], @figure[:data], xlabel: xlabel)
          end
  sio = StringIO.new
  class << sio
    def tty?; true; end
  end
  plot.render(sio)
  sio.string
end

#set_xlabel(label) ⇒ Object



31
32
33
# File 'lib/charty/backends/unicode_plot.rb', line 31

def set_xlabel(label)
  @layout[:xlabel] = label
end

#set_xlim(min, max) ⇒ Object



47
48
49
# File 'lib/charty/backends/unicode_plot.rb', line 47

def set_xlim(min, max)
  @layout[:xlim] = [min, max]
end

#set_xtick_labels(values) ⇒ Object



43
44
45
# File 'lib/charty/backends/unicode_plot.rb', line 43

def set_xtick_labels(values)
  @layout[:xtick_labels] = values
end

#set_xticks(values) ⇒ Object



39
40
41
# File 'lib/charty/backends/unicode_plot.rb', line 39

def set_xticks(values)
  @layout[:xticks] = values
end

#set_ylabel(label) ⇒ Object



35
36
37
# File 'lib/charty/backends/unicode_plot.rb', line 35

def set_ylabel(label)
  @layout[:ylabel] = label
end