Class: UnicodePlot::GridPlot
- Defined in:
- src/lib/unicode_plot/grid_plot.rb
Direct Known Subclasses
Constant Summary collapse
- MIN_WIDTH =
5
- MIN_HEIGHT =
2
- DEFAULT_HEIGHT =
15
Constants inherited from Plot
Plot::COLOR_CYCLE, Plot::DEFAULT_BORDER, Plot::DEFAULT_MARGIN, Plot::DEFAULT_PADDING, Plot::DEFAULT_WIDTH
Constants included from StyledPrinter
StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS
Instance Attribute Summary
Attributes inherited from Plot
#border, #colors_deco, #colors_left, #colors_right, #decorations, #labels_left, #labels_right, #margin, #padding, #title, #xlabel, #ylabel
Instance Method Summary collapse
-
#initialize(x, y, canvas, width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT, xlim: [0, 0], ylim: [0, 0], grid: true, **kw) ⇒ GridPlot
constructor
A new instance of GridPlot.
-
#lines!(x, y, color) ⇒ Object
-
#n_columns ⇒ Object
-
#n_rows ⇒ Object
-
#origin_x ⇒ Object
-
#origin_y ⇒ Object
-
#plot_height ⇒ Object
-
#plot_width ⇒ Object
-
#points!(x, y, color) ⇒ Object
-
#print_row(out, row_index) ⇒ Object
Methods inherited from Plot
#annotate!, #annotate_row!, #next_color, #render, #show_labels?, #title_given?, #to_s, #xlabel_given?, #ylabel_given?, #ylabel_length
Methods included from StyledPrinter
Constructor Details
#initialize(x, y, canvas, width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT, xlim: [0, 0], ylim: [0, 0], grid: true, **kw) ⇒ GridPlot
Returns a new instance of GridPlot.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 7 def initialize(x, y, canvas, width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT, xlim: [0, 0], ylim: [0, 0], grid: true, **kw) if x.length != y.length raise ArgumentError, "x and y must be the same length" end unless x.length > 0 raise ArgumentError, "x and y must not be empty" end unless xlim.length == 2 && ylim.length == 2 raise ArgumentError, "xlim and ylim must be 2-length arrays" end width = [width, MIN_WIDTH].max height = [height, MIN_HEIGHT].max min_x, max_x = Utils.extend_limits(x, xlim) min_y, max_y = Utils.extend_limits(y, ylim) origin_x = min_x origin_y = min_y plot_width = max_x - origin_x plot_height = max_y - origin_y @canvas = Canvas.create(canvas, width, height, origin_x: origin_x, origin_y: origin_y, plot_width: plot_width, plot_height: plot_height) super(**kw) min_x_str = (Utils.roundable?(min_x) ? min_x.round : min_x).to_s max_x_str = (Utils.roundable?(max_x) ? max_x.round : max_x).to_s min_y_str = (Utils.roundable?(min_y) ? min_y.round : min_y).to_s max_y_str = (Utils.roundable?(max_y) ? max_y.round : max_y).to_s annotate_row!(:l, 0, max_y_str, color: :light_black) annotate_row!(:l, height-1, min_y_str, color: :light_black) annotate!(:bl, min_x_str, color: :light_black) annotate!(:br, max_x_str, color: :light_black) if grid if min_y < 0 && 0 < max_y step = plot_width.fdiv(width * @canvas.x_pixel_per_char - 1) min_x.step(max_x, by: step) do |i| @canvas.point!(i, 0, :normal) end end if min_x < 0 && 0 < max_x step = plot_height.fdiv(height * @canvas.y_pixel_per_char - 1) min_y.step(max_y, by: step) do |i| @canvas.point!(0, i, :normal) end end end end |
Instance Method Details
#lines!(x, y, color) ⇒ Object
92 93 94 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 92 def lines!(x, y, color) @canvas.lines!(x, y, color) end |
#n_columns ⇒ Object
84 85 86 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 84 def n_columns @canvas.width end |
#n_rows ⇒ Object
80 81 82 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 80 def n_rows @canvas.height end |
#origin_x ⇒ Object
64 65 66 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 64 def origin_x @canvas.origin_x end |
#origin_y ⇒ Object
68 69 70 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 68 def origin_y @canvas.origin_y end |
#plot_height ⇒ Object
76 77 78 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 76 def plot_height @canvas.plot_height end |
#plot_width ⇒ Object
72 73 74 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 72 def plot_width @canvas.plot_width end |
#points!(x, y, color) ⇒ Object
88 89 90 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 88 def points!(x, y, color) @canvas.points!(x, y, color) end |
#print_row(out, row_index) ⇒ Object
96 97 98 |
# File 'src/lib/unicode_plot/grid_plot.rb', line 96 def print_row(out, row_index) @canvas.print_row(out, row_index) end |