Module: UnicodePlot

Defined in:
lib/unicode_plot/plot.rb,
lib/unicode_plot/canvas.rb,
lib/unicode_plot/barplot.rb,
lib/unicode_plot/version.rb,
lib/unicode_plot/lineplot.rb,
lib/unicode_plot/renderer.rb,
lib/unicode_plot/ascii_canvas.rb,
lib/unicode_plot/braille_canvas.rb,
lib/unicode_plot/styled_printer.rb,
lib/unicode_plot/value_transformer.rb

Defined Under Namespace

Modules: BorderMaps, BorderPrinter, StyledPrinter, ValueTransformer, Version Classes: AsciiCanvas, Barplot, BrailleCanvas, Canvas, GridCanvas, Lineplot, Plot, Renderer

Constant Summary collapse

VERSION =
"0.0.1"
BORDER_MAP =
{
  solid:   BorderMaps::BORDER_SOLID,
  barplot: BorderMaps::BORDER_BARPLOT,
}.freeze

Class Method Summary collapse

Class Method Details

.barplot(*args, width: Barplot::DEFAULT_WIDTH, color: Barplot::DEFAULT_COLOR, symbol: Barplot::DEFAULT_SYMBOL, border: :barplot, xscale: nil, xlabel: nil, data: nil, **kw) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/unicode_plot/barplot.rb', line 73

module_function def barplot(*args,
                            width: Barplot::DEFAULT_WIDTH,
                            color: Barplot::DEFAULT_COLOR,
                            symbol: Barplot::DEFAULT_SYMBOL,
                            border: :barplot,
                            xscale: nil,
                            xlabel: nil,
                            data: nil,
                            **kw)
  case args.length
  when 0
    data = Hash(args[0] || data)
    keys = data.keys.map(&:to_s)
    heights = data.values
  when 2
    keys = Array(args[0])
    heights = Array(args[1])
  else
    raise ArgumentError, "invalid arguments"
  end

  xlabel ||= ValueTransformer.transform_name(xscale)
  plot = Barplot.new(heights, width, color, symbol, xscale,
                     border: border, xlabel: xlabel,
                     **kw)
  keys.each_with_index do |key, i|
    plot.annotate_row!(:l, i, key)
  end

  plot
end

.lineplot(*args, canvas: :braille, color: :auto, name: "", **kw) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/unicode_plot/lineplot.rb', line 167

module_function def lineplot(*args,
                             canvas: :braille,
                             color: :auto,
                             name: "",
                             **kw)
  case args.length
  when 1
    # y only
    y = Array(args[0])
    x = Array(1 .. y.length)
  when 2
    # x and y
    x = Array(args[0])
    y = Array(args[1])
  else
    raise ArgumentError, "wrong number of arguments"
  end

  case x[0]
  when Time, Date
    if x[0].is_a? Time
      d = x.map(&:to_f)
    else
      origin = Date.new(1, 1, 1)
      d = x.map {|xi| xi - origin }
    end
    plot = lineplot(d, y, canvas: canvas, color: color, name: name, **kw)
    xmin, xmax = x.minmax
    plot.annotate!(:bl, xmin.to_s, color: :light_black)
    plot.annotate!(:br, xmax.to_s, color: :light_black)
    plot
  else
    plot = Lineplot.new(x, y, canvas, **kw)
    lineplot!(plot, x, y, color: color, name: name)
  end
end

.lineplot!(plot, *args, color: :auto, name: "") ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/unicode_plot/lineplot.rb', line 204

module_function def lineplot!(plot,
                              *args,
                              color: :auto,
                              name: "")
  case args.length
  when 1
    # y only
    y = Array(args[0])
    x = Array(1 .. y.length)
  when 2
    # x and y
    x = Array(args[0])
    y = Array(args[1])

    if x.length == 1 && y.length == 1
      # intercept and slope
      intercept = x[0]
      slope = y[0]
      xmin = plot.origin_x
      xmax = plot.origin_x + plot.plot_width
      ymin = plot.origin_y
      ymax = plot.origin_y + plot.plot_height
      x = [xmin, xmax]
      y = [intercept + xmin*slope, intercept + xmax*slope]
    end
  else
    raise ArgumentError, "wrong number of arguments"
  end

  case x[0]
  when Time, Date
    if x[0].is_a? Time
      d = x.map(&:to_f)
    else
      origin = Date.new(1, 1, 1)
      d = x.map {|xi| xi - origin }
    end
    lineplot!(plot, d, y, color: color, name: name)
  else
    color = color == :auto ? plot.next_color : color
    plot.annotate!(:r, name.to_s, color: color) unless name.nil? || name == ""
    plot.lines!(x, y, color)
  end
  plot
end