class UnicodePlot::Plot

Constants

COLOR_CYCLE
DEFAULT_BORDER
DEFAULT_MARGIN
DEFAULT_PADDING
DEFAULT_WIDTH

Attributes

border[R]
colors_deco[R]
colors_left[R]
colors_right[R]
decorations[R]
labels_left[R]
labels_right[R]
margin[R]
padding[R]
title[R]
xlabel[R]
ylabel[R]

Public Class Methods

new(title: nil, xlabel: nil, ylabel: nil, border: DEFAULT_BORDER, margin: DEFAULT_MARGIN, padding: DEFAULT_PADDING, labels: true) click to toggle source
# File lib/unicode_plot/plot.rb, line 10
def initialize(title: nil,
               xlabel: nil,
               ylabel: nil,
               border: DEFAULT_BORDER,
               margin: DEFAULT_MARGIN,
               padding: DEFAULT_PADDING,
               labels: true)
  @title = title
  @xlabel = xlabel
  @ylabel = ylabel
  @border = check_border(border)
  @margin = check_margin(margin)
  @padding = padding
  @labels_left = {}
  @colors_left = {}
  @labels_right = {}
  @colors_right = {}
  @decorations = {}
  @colors_deco = {}
  @show_labels = labels
  @auto_color = 0
end

Public Instance Methods

annotate!(loc, value, color: :normal) click to toggle source
# File lib/unicode_plot/plot.rb, line 66
def annotate!(loc, value, color: :normal)
  case loc
  when :l
    (0 ... n_rows).each do |row|
      if @labels_left.fetch(row, "") == ""
        @labels_left[row] = value
        @colors_left[row] = color
        break
      end
    end
  when :r
    (0 ... n_rows).each do |row|
      if @labels_right.fetch(row, "") == ""
        @labels_right[row] = value
        @colors_right[row] = color
        break
      end
    end
  when :t, :b, :tl, :tr, :bl, :br
    @decorations[loc] = value
    @colors_deco[loc] = color
  else
    raise ArgumentError,
      "unknown location to annotate (#{loc.inspect} for :t, :b, :l, :r, :tl, :tr, :bl, or :br)"
  end
end
annotate_row!(loc, row_index, value, color: :normal) click to toggle source
# File lib/unicode_plot/plot.rb, line 93
def annotate_row!(loc, row_index, value, color: :normal)
  case loc
  when :l
    @labels_left[row_index] = value
    @colors_left[row_index] = color
  when :r
    @labels_right[row_index] = value
    @colors_right[row_index] = color
  else
    raise ArgumentError, "unknown location `#{loc}`, try :l or :r instead"
  end
end
next_color() click to toggle source
# File lib/unicode_plot/plot.rb, line 119
def next_color
  COLOR_CYCLE[@auto_color]
ensure
  @auto_color = (@auto_color + 1) % COLOR_CYCLE.length
end
render(out=$stdout, newline: true, color: :auto) click to toggle source
# File lib/unicode_plot/plot.rb, line 106
def render(out=$stdout, newline: true, color: :auto)
  Renderer.render(IOContext.new(out, color: color), self, newline)
end
show_labels?() click to toggle source
# File lib/unicode_plot/plot.rb, line 62
def show_labels?
  @show_labels
end
title_given?() click to toggle source
# File lib/unicode_plot/plot.rb, line 46
def title_given?
  title && title != ""
end
to_s() click to toggle source
# File lib/unicode_plot/plot.rb, line 125
def to_s
  StringIO.open do |sio|
    render(sio, newline: false)
    sio.close
    sio.string
  end
end
xlabel_given?() click to toggle source
# File lib/unicode_plot/plot.rb, line 50
def xlabel_given?
  xlabel && xlabel != ""
end
ylabel_given?() click to toggle source
# File lib/unicode_plot/plot.rb, line 54
def ylabel_given?
  ylabel && ylabel != ""
end
ylabel_length() click to toggle source
# File lib/unicode_plot/plot.rb, line 58
def ylabel_length
  ylabel&.length || 0
end

Private Instance Methods

check_border(border) click to toggle source
# File lib/unicode_plot/plot.rb, line 146
        def check_border(border)
  return border if BORDER_MAP.key?(border)
  raise ArgumentError, "unknown border type: #{border}"
end
check_margin(margin) click to toggle source
# File lib/unicode_plot/plot.rb, line 133
        def check_margin(margin)
  if margin < 0
    raise ArgumentError, "margin must be >= 0"
  end
  margin
end
check_row_index(row_index) click to toggle source
# File lib/unicode_plot/plot.rb, line 140
        def check_row_index(row_index)
  unless 0 <= row_index && row_index < n_rows
    raise ArgumentError, "row_index out of bounds"
  end
end