Class: UnicodePlot::Plot
- Inherits:
-
Object
- Object
- UnicodePlot::Plot
- Includes:
- StyledPrinter
- Defined in:
- src/lib/unicode_plot/plot.rb
Constant Summary collapse
- DEFAULT_WIDTH =
40
- DEFAULT_BORDER =
:solid
- DEFAULT_MARGIN =
3
- DEFAULT_PADDING =
1
- COLOR_CYCLE =
[ :green, :blue, :red, :magenta, :yellow, :cyan ].freeze
Constants included from StyledPrinter
StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS
Instance Attribute Summary collapse
-
#border ⇒ Object
readonly
Returns the value of attribute border.
-
#colors_deco ⇒ Object
readonly
Returns the value of attribute colors_deco.
-
#colors_left ⇒ Object
readonly
Returns the value of attribute colors_left.
-
#colors_right ⇒ Object
readonly
Returns the value of attribute colors_right.
-
#decorations ⇒ Object
readonly
Returns the value of attribute decorations.
-
#labels_left ⇒ Object
readonly
Returns the value of attribute labels_left.
-
#labels_right ⇒ Object
readonly
Returns the value of attribute labels_right.
-
#margin ⇒ Object
readonly
Returns the value of attribute margin.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#xlabel ⇒ Object
readonly
Returns the value of attribute xlabel.
-
#ylabel ⇒ Object
readonly
Returns the value of attribute ylabel.
Instance Method Summary collapse
-
#annotate!(loc, value, color: :normal) ⇒ Object
-
#annotate_row!(loc, row_index, value, color: :normal) ⇒ Object
-
#initialize(title: nil, xlabel: nil, ylabel: nil, border: DEFAULT_BORDER, margin: DEFAULT_MARGIN, padding: DEFAULT_PADDING, labels: true) ⇒ Plot
constructor
A new instance of Plot.
-
#next_color ⇒ Object
-
#render(out = $stdout, newline: true, color: :auto) ⇒ Object
-
#show_labels? ⇒ Boolean
-
#title_given? ⇒ Boolean
-
#to_s ⇒ Object
-
#xlabel_given? ⇒ Boolean
-
#ylabel_given? ⇒ Boolean
-
#ylabel_length ⇒ Object
Methods included from StyledPrinter
Constructor Details
#initialize(title: nil, xlabel: nil, ylabel: nil, border: DEFAULT_BORDER, margin: DEFAULT_MARGIN, padding: DEFAULT_PADDING, labels: true) ⇒ Plot
Returns a new instance of Plot.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'src/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 |
Instance Attribute Details
#border ⇒ Object (readonly)
Returns the value of attribute border.
36 37 38 |
# File 'src/lib/unicode_plot/plot.rb', line 36 def border @border end |
#colors_deco ⇒ Object (readonly)
Returns the value of attribute colors_deco.
44 45 46 |
# File 'src/lib/unicode_plot/plot.rb', line 44 def colors_deco @colors_deco end |
#colors_left ⇒ Object (readonly)
Returns the value of attribute colors_left.
40 41 42 |
# File 'src/lib/unicode_plot/plot.rb', line 40 def colors_left @colors_left end |
#colors_right ⇒ Object (readonly)
Returns the value of attribute colors_right.
42 43 44 |
# File 'src/lib/unicode_plot/plot.rb', line 42 def colors_right @colors_right end |
#decorations ⇒ Object (readonly)
Returns the value of attribute decorations.
43 44 45 |
# File 'src/lib/unicode_plot/plot.rb', line 43 def decorations @decorations end |
#labels_left ⇒ Object (readonly)
Returns the value of attribute labels_left.
39 40 41 |
# File 'src/lib/unicode_plot/plot.rb', line 39 def labels_left @labels_left end |
#labels_right ⇒ Object (readonly)
Returns the value of attribute labels_right.
41 42 43 |
# File 'src/lib/unicode_plot/plot.rb', line 41 def labels_right @labels_right end |
#margin ⇒ Object (readonly)
Returns the value of attribute margin.
37 38 39 |
# File 'src/lib/unicode_plot/plot.rb', line 37 def margin @margin end |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
38 39 40 |
# File 'src/lib/unicode_plot/plot.rb', line 38 def padding @padding end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
33 34 35 |
# File 'src/lib/unicode_plot/plot.rb', line 33 def title @title end |
#xlabel ⇒ Object (readonly)
Returns the value of attribute xlabel.
34 35 36 |
# File 'src/lib/unicode_plot/plot.rb', line 34 def xlabel @xlabel end |
#ylabel ⇒ Object (readonly)
Returns the value of attribute ylabel.
35 36 37 |
# File 'src/lib/unicode_plot/plot.rb', line 35 def ylabel @ylabel end |
Instance Method Details
#annotate!(loc, value, color: :normal) ⇒ Object
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 |
# File 'src/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) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'src/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 ⇒ Object
119 120 121 122 123 |
# File 'src/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) ⇒ Object
106 107 108 |
# File 'src/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? ⇒ Boolean
62 63 64 |
# File 'src/lib/unicode_plot/plot.rb', line 62 def show_labels? @show_labels end |
#title_given? ⇒ Boolean
46 47 48 |
# File 'src/lib/unicode_plot/plot.rb', line 46 def title_given? title && title != "" end |
#to_s ⇒ Object
125 126 127 128 129 130 131 |
# File 'src/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? ⇒ Boolean
50 51 52 |
# File 'src/lib/unicode_plot/plot.rb', line 50 def xlabel_given? xlabel && xlabel != "" end |
#ylabel_given? ⇒ Boolean
54 55 56 |
# File 'src/lib/unicode_plot/plot.rb', line 54 def ylabel_given? ylabel && ylabel != "" end |
#ylabel_length ⇒ Object
58 59 60 |
# File 'src/lib/unicode_plot/plot.rb', line 58 def ylabel_length ylabel&.length || 0 end |