Skip to content

Commit a00973a

Browse files
authored
Merge pull request #15 from red-data-tools/barplot_additions
Barplot additions
2 parents 6bfb316 + 888a073 commit a00973a

7 files changed

Lines changed: 116 additions & 0 deletions

File tree

lib/unicode_plot/barplot.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def print_row(out, row_index)
9797
raise ArgumentError, "invalid arguments"
9898
end
9999

100+
unless keys.length == heights.length
101+
raise ArgumentError, "The given vectors must be of the same length"
102+
end
103+
unless heights.min >= 0
104+
raise ArgumentError, "All values have to be positive. Negative bars are not supported."
105+
end
106+
100107
xlabel ||= ValueTransformer.transform_name(xscale)
101108
plot = Barplot.new(heights, width, color, symbol, xscale,
102109
border: border, xlabel: xlabel,
@@ -130,6 +137,7 @@ def print_row(out, row_index)
130137
if keys.empty?
131138
raise ArgumentError, "Can't append empty array to barplot"
132139
end
140+
133141
cur_idx = plot.n_rows
134142
plot.add_row!(heights)
135143
keys.each_with_index do |key, i|
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
 ┌ ┐
2+
a ┤ 1  
3+
b ┤ 1  
4+
c ┤ 1  
5+
d ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1000000  
6+
 └ ┘
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
 ┌ ┐
2+
5 ┤ 0  
3+
4 ┤ 0  
4+
3 ┤ 0  
5+
2 ┤ 0  
6+
1 ┤ 0  
7+
 └ ┘
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
 Relative sizes of cities
2+
 ┌ ┐
3+
Paris ┤■■■■■■ 2.244  
4+
New York ┤■■■■■■■■■■■■■■■■■■■■■■■ 8.406  
5+
Moskau ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11.92  
6+
Madrid ┤■■■■■■■■■ 3.165  
7+
 └ ┘
8+
 population [in mil]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
 Relative sizes of cities
2+
 ┌ ┐
3+
 ┤■■■■■■ 2.244  
4+
 ┤■■■■■■■■■■■■■■■■■■■■■■■ 8.406  
5+
 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 11.92  
6+
 ┤■■■■■■■■■ 3.165  
7+
 └ ┘
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
 Relative sizes of cities
2+
 ┌────────────────────────────────────────────────────────────┐
3+
Paris │========== 2.244 │
4+
New York │===================================== 8.406 │
5+
Moskau │===================================================== 11.92 │
6+
Madrid │============== 3.165 │
7+
 └────────────────────────────────────────────────────────────┘
8+
 population [in mil]

test/test-barplot.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ class BarplotTest < Test::Unit::TestCase
33
include Helper::WithTerm
44

55
sub_test_case("UnicodePlot.barplot") do
6+
test("errors") do
7+
assert_raise(ArgumentError) do
8+
UnicodePlot.barplot([:a], [-1, 2])
9+
end
10+
assert_raise(ArgumentError) do
11+
UnicodePlot.barplot([:a, :b], [-1, 2])
12+
end
13+
end
14+
615
test("colored") do
716
data = { bar: 23, foo: 37 }
817
plot = UnicodePlot.barplot(data: data)
@@ -63,12 +72,75 @@ class BarplotTest < Test::Unit::TestCase
6372
end
6473
end
6574

75+
sub_test_case("with parameters") do
76+
test("parameters1") do
77+
plot = UnicodePlot.barplot(
78+
["Paris", "New York", "Moskau", "Madrid"],
79+
[2.244, 8.406, 11.92, 3.165],
80+
title: "Relative sizes of cities",
81+
xlabel: "population [in mil]",
82+
color: :blue,
83+
margin: 7,
84+
padding: 3
85+
)
86+
_, output = with_term { plot.render($stdout) }
87+
assert_equal(fixture_path("barplot/parameters1.txt").read,
88+
output)
89+
end
90+
91+
test("parameters1_nolabels") do
92+
plot = UnicodePlot.barplot(
93+
["Paris", "New York", "Moskau", "Madrid"],
94+
[2.244, 8.406, 11.92, 3.165],
95+
title: "Relative sizes of cities",
96+
xlabel: "population [in mil]",
97+
color: :blue,
98+
margin: 7,
99+
padding: 3,
100+
labels: false
101+
)
102+
_, output = with_term { plot.render($stdout) }
103+
assert_equal(fixture_path("barplot/parameters1_nolabels.txt").read,
104+
output)
105+
end
106+
107+
test("parameters2") do
108+
plot = UnicodePlot.barplot(
109+
["Paris", "New York", "Moskau", "Madrid"],
110+
[2.244, 8.406, 11.92, 3.165],
111+
title: "Relative sizes of cities",
112+
xlabel: "population [in mil]",
113+
color: :yellow,
114+
border: :solid,
115+
symbol: "=",
116+
width: 60
117+
)
118+
_, output = with_term { plot.render($stdout) }
119+
assert_equal(fixture_path("barplot/parameters2.txt").read,
120+
output)
121+
end
122+
end
123+
66124
test("ranges") do
67125
plot = UnicodePlot.barplot(2..6, 11..15)
68126
_, output = with_term { plot.render($stdout) }
69127
assert_equal(fixture_path("barplot/ranges.txt").read,
70128
output)
71129
end
130+
131+
test("all zeros") do
132+
plot = UnicodePlot.barplot([5, 4, 3, 2, 1], [0, 0, 0, 0, 0])
133+
_, output = with_term { plot.render($stdout) }
134+
assert_equal(fixture_path("barplot/edgecase_zeros.txt").read,
135+
output)
136+
end
137+
138+
test("one large") do
139+
plot = UnicodePlot.barplot([:a, :b, :c, :d], [1, 1, 1, 1000000])
140+
_, output = with_term { plot.render($stdout) }
141+
assert_equal(fixture_path("barplot/edgecase_onelarge.txt").read,
142+
output)
143+
end
72144
end
73145

74146
sub_test_case("UnicodePlot.barplot!") do

0 commit comments

Comments
 (0)