|
| 1 | +# coding: utf-8 |
| 2 | +module UnicodePlot |
| 3 | + |
| 4 | + |
| 5 | +# ==Description |
| 6 | +# |
| 7 | +# Draws a staircase plot on a new canvas. |
| 8 | +# |
| 9 | +# The first vector `x` should contain the horizontal |
| 10 | +# positions for all the points. The second vector `y` should then |
| 11 | +# contain the corresponding vertical positions respectively. This |
| 12 | +# means that the two vectors must be of the same length and |
| 13 | +# ordering. |
| 14 | +# |
| 15 | +# ==Usage |
| 16 | +# |
| 17 | +# UnicodePlot.stairs(x, y, style: :post, name: "", title: "", xlabel: "", ylabel: "", labels: true, border: :solid, margin: 3, padding: 1, color: :auto, width: 40, height: 15, xlim: [0, 0], ylim: [0, 0], canvas: :braille, grid: true) |
| 18 | +# |
| 19 | +# @param x [Array<Numeric>] The horizontal position for each point. |
| 20 | +# @param y [Array<Numeric>] The vertical position for each point. |
| 21 | +# @param style [Symbol] Specifies where the transition of the stair takes place. Can be either `:pre` or `:post`. |
| 22 | +# @param name [String] Annotation of the current drawing to be displayed on the right. |
| 23 | +# @param height [Integer] Number of character rows that should be used for plotting. |
| 24 | +# @param xlim [Array<Numeric>] Plotting range for the x axis. `[0, 0]` stands for automatic. |
| 25 | +# @param ylim [Array<Numeric>] Plotting range for the y axis. `[0, 0]` stands for automatic. |
| 26 | +# @param canvas [Symbol] The type of canvas that should be used for drawing. |
| 27 | +# @param grid [Boolean] If `true`, draws grid-lines at the origin. |
| 28 | +# |
| 29 | +# @return [Plot] A plot object |
| 30 | +# |
| 31 | +# @example |
| 32 | +# UnicodePlot.stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], style: :post, title: "My Staircase Plot") |
| 33 | +# |
| 34 | +# |
| 35 | +# ┌────────────────────────────────────────┐ |
| 36 | +# 7 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 37 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 38 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 39 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 40 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 41 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 42 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸│ |
| 43 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⡄⠀⠀⠀⠀⢸│ |
| 44 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢸│ |
| 45 | +# │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢸│ |
| 46 | +# │⠀⠀⠀⠀⠀⢸⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢸│ |
| 47 | +# │⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⢸│ |
| 48 | +# │⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠧⠤⠤⠤⠤⠼│ |
| 49 | +# │⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ |
| 50 | +# 1 │⣀⣀⣀⣀⣀⣸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ |
| 51 | +# └────────────────────────────────────────┘ |
| 52 | +# 1 8 |
| 53 | +# |
| 54 | +# @see Plot |
| 55 | +# @see scatterplot |
| 56 | +# @see lineplot |
| 57 | +# |
| 58 | + module_function def stairs(xvec, yvec, style: :post, **kw) |
| 59 | + x_vex, y_vex = compute_stair_lines(xvec, yvec, style: style) |
| 60 | + lineplot(x_vex, y_vex, **kw) |
| 61 | + end |
| 62 | + |
| 63 | + # Similar to stairs, but takes an existing plot object as a first argument. |
| 64 | + module_function def stairs!(plot, xvec, yvec, style: :post, **kw) |
| 65 | + x_vex, y_vex = compute_stair_lines(xvec, yvec, style: style) |
| 66 | + lineplot!(plot, x_vex, y_vex, **kw) |
| 67 | + end |
| 68 | + |
| 69 | + module_function def compute_stair_lines(x, y, style: :post) |
| 70 | + x_vex = Array.new(x.length * 2 - 1, 0) |
| 71 | + y_vex = Array.new(x.length * 2 - 1, 0) |
| 72 | + x_vex[0] = x[0] |
| 73 | + y_vex[0] = y[0] |
| 74 | + o = 0 |
| 75 | + if style == :post |
| 76 | + (1 ... x.length).each do |i| |
| 77 | + x_vex[i + o] = x[i] |
| 78 | + x_vex[i + o + 1] = x[i] |
| 79 | + y_vex[i + o] = y[i-1] |
| 80 | + y_vex[i + o + 1] = y[i] |
| 81 | + o += 1 |
| 82 | + end |
| 83 | + elsif style == :pre |
| 84 | + (1 ... x.length).each do |i| |
| 85 | + x_vex[i + o] = x[i-1] |
| 86 | + x_vex[i + o + 1] = x[i] |
| 87 | + y_vex[i + o] = y[i] |
| 88 | + y_vex[i + o + 1] = y[i] |
| 89 | + o += 1 |
| 90 | + end |
| 91 | + end |
| 92 | + return [x_vex, y_vex] |
| 93 | + end |
| 94 | + |
| 95 | +end |
0 commit comments