cmd_plot 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +26 -0
- data/LICENSE +340 -0
- data/README.md +179 -0
- data/lib/cmd_plot/bar_chart.rb +43 -0
- data/lib/cmd_plot/braille.rb +56 -0
- data/lib/cmd_plot/color_map.rb +61 -0
- data/lib/cmd_plot/data.rb +38 -0
- data/lib/cmd_plot/legacy.rb +41 -0
- data/lib/cmd_plot/line_plot.rb +156 -0
- data/lib/cmd_plot/tables.rb +88 -0
- data/lib/cmd_plot/version.rb +5 -0
- data/lib/cmd_plot.rb +53 -5
- metadata +26 -18
- data/lib/cmd_plot/bar.rb +0 -30
- data/lib/cmd_plot/basic_data.rb +0 -33
- data/lib/cmd_plot/pcolor.rb +0 -73
- data/lib/cmd_plot/plot.rb +0 -116
data/lib/cmd_plot/plot.rb
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
class CmdPlot::Plot
|
|
2
|
-
def initialize(width = 140, height = 40, symbol = '°')
|
|
3
|
-
@width = width
|
|
4
|
-
@height = height
|
|
5
|
-
@symbol = symbol
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def plot(x, y = nil)
|
|
9
|
-
# If only one parameter is given treat it as the y-axis data and fill up x automatically
|
|
10
|
-
if y.nil?
|
|
11
|
-
y = x
|
|
12
|
-
x = BasicData::linspace(0.0, y.length, y.length)
|
|
13
|
-
end
|
|
14
|
-
# Scale x axis to the desired console resolution
|
|
15
|
-
x_max = x.compact.max
|
|
16
|
-
x_min = x.compact.min
|
|
17
|
-
x = x.map { |v| v.nil? ? nil : ((v - x_min) / (x_max - x_min).to_f * (@width - 1)).round }
|
|
18
|
-
# Scale y axis to the desired console resolution
|
|
19
|
-
y_max = y.compact.max
|
|
20
|
-
y_min = y.compact.min
|
|
21
|
-
y = y.map { |v| v.nil? ? nil : ((v - y_min) / (y_max - y_min).to_f * (@height - 1)).round }
|
|
22
|
-
|
|
23
|
-
# Full plot-picture starts out empty
|
|
24
|
-
picture = Array.new(@height) { Array.new(@width) {' '} }
|
|
25
|
-
# Run through all the points of the curve and fill in the plotting symbol
|
|
26
|
-
for point in x.zip(y)
|
|
27
|
-
next if point[1].nil? || point[0].nil?
|
|
28
|
-
picture[@height - 1 - point[1]][point[0]] = @symbol
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# X-axis
|
|
32
|
-
x_ticks = BasicData::linspace(x_min, x_max, [2, @width / 15].max).map { |v|
|
|
33
|
-
tick_to_display(v)
|
|
34
|
-
}
|
|
35
|
-
# Y-axis
|
|
36
|
-
y_ticks = BasicData::linspace(y_max, y_min, [2, @height / 10].max).map { |v|
|
|
37
|
-
tick_to_display(v)
|
|
38
|
-
}
|
|
39
|
-
y_tick_width = y_ticks.map { |e| e.length }.max
|
|
40
|
-
y_tick_spacing = (@height - y_ticks.length) / (y_ticks.length - 1)
|
|
41
|
-
missing = @height - (y_tick_spacing * (y_ticks.length - 1) + y_ticks.length)
|
|
42
|
-
missing_dist = missing == 0 ? @height : y_ticks.length / missing.to_f
|
|
43
|
-
|
|
44
|
-
# Print the y-axis and plot picture
|
|
45
|
-
row = 0
|
|
46
|
-
next_tick = 0
|
|
47
|
-
tick_offset = 0
|
|
48
|
-
for line in picture
|
|
49
|
-
if row == (tick_offset + next_tick * (y_tick_spacing + 1))
|
|
50
|
-
print ' ' * (y_tick_width - y_ticks[next_tick].length)
|
|
51
|
-
print y_ticks[next_tick] + ' ├ '
|
|
52
|
-
next_tick += 1
|
|
53
|
-
tick_offset += 1 if ((tick_offset + 1) * missing_dist) < (next_tick + 2)
|
|
54
|
-
else
|
|
55
|
-
print ' ' * y_tick_width + ' │ '
|
|
56
|
-
end
|
|
57
|
-
puts line.join
|
|
58
|
-
row += 1
|
|
59
|
-
end
|
|
60
|
-
# Print x-axis
|
|
61
|
-
x_tick_spacing = (@width - x_ticks.length) / (x_ticks.length - 1)
|
|
62
|
-
missing = @width - (x_tick_spacing * (x_ticks.length - 1) + x_ticks.length)
|
|
63
|
-
missing_dist = missing == 0 ? @width : x_ticks.length / missing.to_f
|
|
64
|
-
print ' ' * y_tick_width + ' └─'
|
|
65
|
-
next_tick = 0
|
|
66
|
-
added_space = 0
|
|
67
|
-
for tick in x_ticks
|
|
68
|
-
if tick == x_ticks[-1]
|
|
69
|
-
puts '┴'
|
|
70
|
-
else
|
|
71
|
-
print '┴'
|
|
72
|
-
print '─' * x_tick_spacing
|
|
73
|
-
next_tick += 1
|
|
74
|
-
if ((added_space + 1) * missing_dist) < (next_tick + 2)
|
|
75
|
-
added_space += 1
|
|
76
|
-
print '─'
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
# Print x labels
|
|
81
|
-
total_label_length = x_ticks.map(&:length).inject(:+)
|
|
82
|
-
x_tick_label_spacing = (@width - total_label_length) / (x_ticks.length - 1)
|
|
83
|
-
missing = @width - (x_tick_label_spacing * (x_ticks.length - 1) + total_label_length)
|
|
84
|
-
missing_dist = missing == 0 ? @width : x_ticks.length / missing.to_f
|
|
85
|
-
print ' ' * y_tick_width + ' '
|
|
86
|
-
next_tick = 0
|
|
87
|
-
added_space = 0
|
|
88
|
-
for tick in x_ticks
|
|
89
|
-
if tick == x_ticks[-1]
|
|
90
|
-
puts tick
|
|
91
|
-
else
|
|
92
|
-
print tick
|
|
93
|
-
print ' ' * x_tick_label_spacing
|
|
94
|
-
next_tick += 1
|
|
95
|
-
if ((added_space + 1) * missing_dist) < (next_tick + 2)
|
|
96
|
-
added_space += 1
|
|
97
|
-
print ' '
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
return nil
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def tick_to_display(v)
|
|
105
|
-
s = v.to_s
|
|
106
|
-
dot = s.index('.')
|
|
107
|
-
dot = s.length - 1 if dot.nil?
|
|
108
|
-
if dot == 1 && s[0] == '0'
|
|
109
|
-
for dot in 1..(s.length - 1)
|
|
110
|
-
next if s[dot + 1] == '0' || s[dot + 1] == '.'
|
|
111
|
-
break
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
s[0..(dot+2)]
|
|
115
|
-
end
|
|
116
|
-
end
|