|
1 | 1 | require 'date' |
2 | 2 |
|
3 | 3 | module UnicodePlot |
4 | | - class GridCanvas < Plot |
5 | | - MIN_WIDTH = 5 |
6 | | - DEFAULT_WIDTH = 40 |
7 | | - MIN_HEIGHT = 2 |
8 | | - DEFAULT_HEIGHT = 15 |
9 | | - |
10 | | - def initialize(x, y, canvas, |
11 | | - width: DEFAULT_WIDTH, |
12 | | - height: DEFAULT_HEIGHT, |
13 | | - xlim: [0, 0], |
14 | | - ylim: [0, 0], |
15 | | - grid: true, |
16 | | - **kw) |
17 | | - unless xlim.length == 2 && ylim.length == 2 |
18 | | - raise ArgumentError, "xlim and ylim must be 2-length arrays" |
19 | | - end |
20 | | - width = [width, MIN_WIDTH].max |
21 | | - height = [height, MIN_HEIGHT].max |
22 | | - min_x, max_x = Utils.extend_limits(x, xlim) |
23 | | - min_y, max_y = Utils.extend_limits(y, ylim) |
24 | | - origin_x = min_x |
25 | | - origin_y = min_y |
26 | | - plot_width = max_x - origin_x |
27 | | - plot_height = max_y - origin_y |
28 | | - @canvas = Canvas.create(canvas, width, height, |
29 | | - origin_x: origin_x, |
30 | | - origin_y: origin_y, |
31 | | - plot_width: plot_width, |
32 | | - plot_height: plot_height) |
33 | | - super(**kw) |
34 | | - |
35 | | - min_x_str = (Utils.roundable?(min_x) ? min_x.round : min_x).to_s |
36 | | - max_x_str = (Utils.roundable?(max_x) ? max_x.round : max_x).to_s |
37 | | - min_y_str = (Utils.roundable?(min_y) ? min_y.round : min_y).to_s |
38 | | - max_y_str = (Utils.roundable?(max_y) ? max_y.round : max_y).to_s |
39 | | - |
40 | | - annotate_row!(:l, 0, max_y_str, color: :light_black) |
41 | | - annotate_row!(:l, height-1, min_y_str, color: :light_black) |
42 | | - annotate!(:bl, min_x_str, color: :light_black) |
43 | | - annotate!(:br, max_x_str, color: :light_black) |
44 | | - |
45 | | - if grid |
46 | | - if min_y < 0 && 0 < max_y |
47 | | - step = plot_width.fdiv(width * @canvas.x_pixel_per_char - 1) |
48 | | - min_x.step(max_x, by: step) do |i| |
49 | | - @canvas.point!(i, 0, :normal) |
50 | | - end |
51 | | - end |
52 | | - if min_x < 0 && 0 < max_x |
53 | | - step = plot_height.fdiv(height * @canvas.y_pixel_per_char - 1) |
54 | | - min_y.step(max_y, by: step) do |i| |
55 | | - @canvas.point!(0, i, :normal) |
56 | | - end |
57 | | - end |
58 | | - end |
59 | | - end |
60 | | - |
61 | | - def origin_x |
62 | | - @canvas.origin_x |
63 | | - end |
64 | | - |
65 | | - def origin_y |
66 | | - @canvas.origin_y |
67 | | - end |
68 | | - |
69 | | - def plot_width |
70 | | - @canvas.plot_width |
71 | | - end |
72 | | - |
73 | | - def plot_height |
74 | | - @canvas.plot_height |
75 | | - end |
76 | | - |
77 | | - def n_rows |
78 | | - @canvas.height |
79 | | - end |
80 | | - |
81 | | - def n_columns |
82 | | - @canvas.width |
83 | | - end |
84 | | - |
85 | | - def points!(x, y, color) |
86 | | - @canvas.points!(x, y, color) |
87 | | - end |
88 | | - |
89 | | - def lines!(x, y, color) |
90 | | - @canvas.lines!(x, y, color) |
91 | | - end |
92 | | - |
93 | | - def print_row(out, row_index) |
94 | | - @canvas.print_row(out, row_index) |
95 | | - end |
96 | | - end |
97 | | - |
98 | | - class Lineplot < GridCanvas |
99 | | - def initialize(x, y, canvas, |
100 | | - **kw) |
101 | | - if x.length != y.length |
102 | | - raise ArgumentError, "x and y must be the same length" |
103 | | - end |
104 | | - unless x.length > 0 |
105 | | - raise ArgumentError, "x and y must not be empty" |
106 | | - end |
107 | | - super(x, y, canvas, **kw) |
108 | | - end |
| 4 | + class Lineplot < GridPlot |
109 | 5 | end |
110 | 6 |
|
111 | 7 | module_function def lineplot(*args, |
|
0 commit comments