unicode_plot 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b33753adec2204f206dff50057dbf735be77c5d1596d8386b103058e76355ba2
4
- data.tar.gz: 3209b35c716a2e8293735a6f774185f12c6627996707fb28c59fec3ac02debd7
3
+ metadata.gz: d3752009f07860fb96eba974ed4322ab975bd5547832e7747c95544918ad96c6
4
+ data.tar.gz: 1e86d05df7dd39473144ed8b26ec00b32980f6d61a684f978e7a6c914d42b71f
5
5
  SHA512:
6
- metadata.gz: dc5c517d8bdbce3d622767d03e4775365d51ac8a1e882e96d4aaba16c05345c66f0431be9f1eb79ad5cbcbfdfb92e395f06dba5f8f18c8a98b9f93491c25235d
7
- data.tar.gz: b5aa69592cf09b140c1dff55edeafbc8204929fe99125dd67d31218286157f4beaa93051da151e5efddcefd24054ec08d458b1aeeee0ac0acabe26813b348b48
6
+ metadata.gz: 68c546819e475ddb1a5e24114b8a8fae8bb0809e723870abfe8cb3583ce838fedb7601b051085f38898ac039e58addee618651f446ed9a22f9d627f64775e2cc
7
+ data.tar.gz: 86da79ec98b4d5c461bb0c3c27632fb3fda90e457066dcc5efa987d402f1e93c7a55e1330551eef270c1bb71f68a9ba3c9c24b578c7f6f8ad617ac7167d65433
data/README.md CHANGED
@@ -10,7 +10,7 @@ $ gem install unicode_plot
10
10
 
11
11
  ## Usage
12
12
 
13
- ```
13
+ ```ruby
14
14
  require 'unicode_plot'
15
15
 
16
16
  x = 0.step(3*Math::PI, by: 3*Math::PI / 30)
@@ -28,12 +28,59 @@ You can get the results below by running the above script:
28
28
 
29
29
  ## Supported charts
30
30
 
31
- - barplot
32
- - boxplot
33
- - densityplot
34
- - histogram
35
- - lineplot
36
- - scatterplot
31
+ ### barplot
32
+
33
+ ```ruby
34
+ plot = UnicodePlot.barplot(data: {'foo': 20, 'bar': 50}, title: "Bar")
35
+ plot.render($stdout)
36
+ ```
37
+
38
+ <img src="img/barplot.png" width="50%" />
39
+
40
+ ### boxplot
41
+
42
+ ```ruby
43
+ plot = UnicodePlot.boxplot(data: {foo: [1, 3, 5], bar: [3, 5, 7]}, title: "Box")
44
+ plot.render($stdout)
45
+ ```
46
+
47
+ <img src="img/boxplot.png" width="50%" />
48
+
49
+ ### densityplot
50
+
51
+ ```ruby
52
+ x = Array.new(500) { 20*rand - 10 } + Array.new(500) { 6*rand - 3 }
53
+ y = Array.new(1000) { 30*rand - 10 }
54
+ plot = UnicodePlot.densityplot(x, y, title: "Density")
55
+ plot.render($stdout)
56
+ ```
57
+
58
+ <img src="img/densityplot.png" width="50%" />
59
+
60
+ ### histogram
61
+
62
+ ```ruby
63
+ x = Array.new(100) { rand(10) } + Array.new(100) { rand(30) + 10 }
64
+ plot = UnicodePlot.histogram(x, title: "Histogram")
65
+ plot.render($stdout)
66
+ ```
67
+
68
+ <img src="img/histogram.png" width="50%" />
69
+
70
+ ### lineplot
71
+
72
+ See [Usage](#usage) section above.
73
+
74
+ ### scatterplot
75
+
76
+ ```ruby
77
+ x = Array.new(50) { rand(20) - 10 }
78
+ y = x.map {|xx| xx*rand(30) - 10 }
79
+ plot = UnicodePlot.scatterplot(x, y, title: "Scatter")
80
+ plot.render($stdout)
81
+ ```
82
+
83
+ <img src="img/scatterplot.png" width="50%" />
37
84
 
38
85
  ## Acknowledgement
39
86
 
@@ -1,3 +1,5 @@
1
+ require 'stringio'
2
+
1
3
  require 'unicode_plot/version'
2
4
 
3
5
  require 'unicode_plot/utils'
@@ -3,7 +3,6 @@ module UnicodePlot
3
3
  include ValueTransformer
4
4
 
5
5
  MIN_WIDTH = 10
6
- DEFAULT_WIDTH = 40
7
6
  DEFAULT_COLOR = :green
8
7
  DEFAULT_SYMBOL = "■"
9
8
 
@@ -68,16 +67,10 @@ module UnicodePlot
68
67
  end
69
68
  [max, i]
70
69
  end
71
-
72
- private def check_row_index(row_index)
73
- unless 0 <= row_index && row_index < n_rows
74
- raise ArgumentError, "row_index is out of range"
75
- end
76
- end
77
70
  end
78
71
 
79
72
  module_function def barplot(*args,
80
- width: Barplot::DEFAULT_WIDTH,
73
+ width: Plot::DEFAULT_WIDTH,
81
74
  color: Barplot::DEFAULT_COLOR,
82
75
  symbol: Barplot::DEFAULT_SYMBOL,
83
76
  border: :barplot,
@@ -4,7 +4,6 @@ module UnicodePlot
4
4
  class Boxplot < Plot
5
5
  MIN_WIDTH = 10
6
6
  DEFAULT_COLOR = :green
7
- DEFAULT_WIDTH = 40
8
7
 
9
8
  def initialize(data, width, color, min_x, max_x, **kw)
10
9
  if min_x == max_x
@@ -20,7 +19,8 @@ module UnicodePlot
20
19
  super(**kw)
21
20
  end
22
21
 
23
- attr_reader :min_x, :max_x
22
+ attr_reader :min_x
23
+ attr_reader :max_x
24
24
 
25
25
  def n_data
26
26
  @data.length
@@ -89,19 +89,13 @@ module UnicodePlot
89
89
  val.round(half: :even).clamp(1, @width).to_i
90
90
  end
91
91
  end
92
-
93
- private def check_row_index(row_index)
94
- unless 0 <= row_index && row_index < n_rows
95
- raise ArgumentError, "row_index out of bounds"
96
- end
97
- end
98
92
  end
99
93
 
100
94
  module_function def boxplot(*args,
101
95
  data: nil,
102
96
  border: :corners,
103
97
  color: Boxplot::DEFAULT_COLOR,
104
- width: Boxplot::DEFAULT_WIDTH,
98
+ width: Plot::DEFAULT_WIDTH,
105
99
  xlim: [0, 0],
106
100
  **kw)
107
101
  case args.length
@@ -1,7 +1,6 @@
1
1
  module UnicodePlot
2
2
  class GridPlot < Plot
3
3
  MIN_WIDTH = 5
4
- DEFAULT_WIDTH = 40
5
4
  MIN_HEIGHT = 2
6
5
  DEFAULT_HEIGHT = 15
7
6
 
@@ -0,0 +1,51 @@
1
+ module UnicodePlot
2
+ class GridLayout
3
+ DEFAULT_WIDTH = 80
4
+
5
+ def initialize(n_rows, n_columns, width: Layout::DEFAULT_WIDTH)
6
+ @n_rows = n_rows
7
+ @n_columns = n_columns
8
+ @width = width
9
+ end
10
+
11
+ def [](i, j)
12
+ @plots[i * n_cols + j]
13
+ end
14
+
15
+ def []=(i, j, plot)
16
+ @plots[i * n_cols + j] = plot
17
+ end
18
+
19
+ def <<(plot)
20
+ @plots << plot
21
+ end
22
+
23
+ def render(out)
24
+ buffers = []
25
+ (0 ... n_rows).each do |i|
26
+ (0 ... n_columns).each do |j|
27
+ StringIO.open do |sio|
28
+ def sio.tty?; true; end
29
+ render_cell(sio, i, j)
30
+ sio.close
31
+ buffers << sio.string
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def render_cell(out, i, j)
38
+ plot = self[i, j]
39
+ return unless plot
40
+ plot.width = cell_width
41
+ end
42
+ end
43
+
44
+ module_function def grid_layout(n_rows, n_cols, *plots, **kw)
45
+ grid = GridLayout.new(n_rows, n_cols, **kw)
46
+ plots.each do |plot|
47
+ grid << plot
48
+ end
49
+ grid
50
+ end
51
+ end
@@ -2,6 +2,7 @@ module UnicodePlot
2
2
  class Plot
3
3
  include StyledPrinter
4
4
 
5
+ DEFAULT_WIDTH = 40
5
6
  DEFAULT_BORDER = :solid
6
7
  DEFAULT_MARGIN = 3
7
8
  DEFAULT_PADDING = 1
@@ -29,18 +30,18 @@ module UnicodePlot
29
30
  @auto_color = 0
30
31
  end
31
32
 
32
- attr_reader :title,
33
- :xlabel,
34
- :ylabel,
35
- :border,
36
- :margin,
37
- :padding,
38
- :labels_left,
39
- :colors_left,
40
- :labels_right,
41
- :colors_right,
42
- :decorations,
43
- :colors_deco
33
+ attr_reader :title
34
+ attr_reader :xlabel
35
+ attr_reader :ylabel
36
+ attr_reader :border
37
+ attr_reader :margin
38
+ attr_reader :padding
39
+ attr_reader :labels_left
40
+ attr_reader :colors_left
41
+ attr_reader :labels_right
42
+ attr_reader :colors_right
43
+ attr_reader :decorations
44
+ attr_reader :colors_deco
44
45
 
45
46
  def title_given?
46
47
  title && title != ""
@@ -102,8 +103,8 @@ module UnicodePlot
102
103
  end
103
104
  end
104
105
 
105
- def render(out)
106
- Renderer.render(out, self)
106
+ def render(out=$stdout, newline: true)
107
+ Renderer.render(out, self, newline)
107
108
  end
108
109
 
109
110
  COLOR_CYCLE = [
@@ -123,7 +124,7 @@ module UnicodePlot
123
124
 
124
125
  def to_s
125
126
  StringIO.open do |sio|
126
- render(sio)
127
+ render(sio, newline: false)
127
128
  sio.close
128
129
  sio.string
129
130
  end
@@ -135,5 +136,11 @@ module UnicodePlot
135
136
  end
136
137
  margin
137
138
  end
139
+
140
+ private def check_row_index(row_index)
141
+ unless 0 <= row_index && row_index < n_rows
142
+ raise ArgumentError, "row_index out of bounds"
143
+ end
144
+ end
138
145
  end
139
146
  end
@@ -59,8 +59,8 @@ module UnicodePlot
59
59
  class Renderer
60
60
  include BorderPrinter
61
61
 
62
- def self.render(out, plot)
63
- new(plot).render(out)
62
+ def self.render(out, plot, newline)
63
+ new(plot).render(out, newline)
64
64
  end
65
65
 
66
66
  def initialize(plot)
@@ -71,13 +71,14 @@ module UnicodePlot
71
71
  attr_reader :plot
72
72
  attr_reader :out
73
73
 
74
- def render(out)
74
+ def render(out, newline)
75
75
  @out = out
76
76
  init_render
77
77
 
78
78
  render_top
79
79
  render_rows
80
80
  render_bottom
81
+ out.puts if newline
81
82
  end
82
83
 
83
84
  private
@@ -1,5 +1,5 @@
1
1
  module UnicodePlot
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
 
4
4
  module Version
5
5
  numbers, TAG = VERSION.split("-", 2)
@@ -15,12 +15,12 @@ class BarplotTest < Test::Unit::TestCase
15
15
  test("colored") do
16
16
  data = { bar: 23, foo: 37 }
17
17
  plot = UnicodePlot.barplot(data: data)
18
- _, output = with_term { plot.render($stdout) }
18
+ _, output = with_term { plot.render($stdout, newline: false) }
19
19
  assert_equal(fixture_path("barplot/default.txt").read,
20
20
  output)
21
21
 
22
22
  plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
23
- _, output = with_term { plot.render($stdout) }
23
+ _, output = with_term { plot.render($stdout, newline: false) }
24
24
  assert_equal(fixture_path("barplot/default.txt").read,
25
25
  output)
26
26
  end
@@ -40,7 +40,7 @@ class BarplotTest < Test::Unit::TestCase
40
40
  test("mixed") do
41
41
  data = { bar: 23.0, 2.1 => 10, foo: 37.0 }
42
42
  plot = UnicodePlot.barplot(data: data)
43
- _, output = with_term { plot.render($stdout) }
43
+ _, output = with_term { plot.render($stdout, newline: false) }
44
44
  assert_equal(fixture_path("barplot/default_mixed.txt").read,
45
45
  output)
46
46
  end
@@ -53,7 +53,7 @@ class BarplotTest < Test::Unit::TestCase
53
53
  title: "Logscale Plot",
54
54
  xscale: :log10
55
55
  )
56
- _, output = with_term { plot.render($stdout) }
56
+ _, output = with_term { plot.render($stdout, newline: false) }
57
57
  assert_equal(fixture_path("barplot/log10.txt").read,
58
58
  output)
59
59
  end
@@ -66,7 +66,7 @@ class BarplotTest < Test::Unit::TestCase
66
66
  xlabel: "custom label",
67
67
  xscale: :log10
68
68
  )
69
- _, output = with_term { plot.render($stdout) }
69
+ _, output = with_term { plot.render($stdout, newline: false) }
70
70
  assert_equal(fixture_path("barplot/log10_label.txt").read,
71
71
  output)
72
72
  end
@@ -83,7 +83,7 @@ class BarplotTest < Test::Unit::TestCase
83
83
  margin: 7,
84
84
  padding: 3
85
85
  )
86
- _, output = with_term { plot.render($stdout) }
86
+ _, output = with_term { plot.render($stdout, newline: false) }
87
87
  assert_equal(fixture_path("barplot/parameters1.txt").read,
88
88
  output)
89
89
  end
@@ -99,7 +99,7 @@ class BarplotTest < Test::Unit::TestCase
99
99
  padding: 3,
100
100
  labels: false
101
101
  )
102
- _, output = with_term { plot.render($stdout) }
102
+ _, output = with_term { plot.render($stdout, newline: false) }
103
103
  assert_equal(fixture_path("barplot/parameters1_nolabels.txt").read,
104
104
  output)
105
105
  end
@@ -115,7 +115,7 @@ class BarplotTest < Test::Unit::TestCase
115
115
  symbol: "=",
116
116
  width: 60
117
117
  )
118
- _, output = with_term { plot.render($stdout) }
118
+ _, output = with_term { plot.render($stdout, newline: false) }
119
119
  assert_equal(fixture_path("barplot/parameters2.txt").read,
120
120
  output)
121
121
  end
@@ -123,21 +123,21 @@ class BarplotTest < Test::Unit::TestCase
123
123
 
124
124
  test("ranges") do
125
125
  plot = UnicodePlot.barplot(2..6, 11..15)
126
- _, output = with_term { plot.render($stdout) }
126
+ _, output = with_term { plot.render($stdout, newline: false) }
127
127
  assert_equal(fixture_path("barplot/ranges.txt").read,
128
128
  output)
129
129
  end
130
130
 
131
131
  test("all zeros") do
132
132
  plot = UnicodePlot.barplot([5, 4, 3, 2, 1], [0, 0, 0, 0, 0])
133
- _, output = with_term { plot.render($stdout) }
133
+ _, output = with_term { plot.render($stdout, newline: false) }
134
134
  assert_equal(fixture_path("barplot/edgecase_zeros.txt").read,
135
135
  output)
136
136
  end
137
137
 
138
138
  test("one large") do
139
139
  plot = UnicodePlot.barplot([:a, :b, :c, :d], [1, 1, 1, 1000000])
140
- _, output = with_term { plot.render($stdout) }
140
+ _, output = with_term { plot.render($stdout, newline: false) }
141
141
  assert_equal(fixture_path("barplot/edgecase_onelarge.txt").read,
142
142
  output)
143
143
  end
@@ -159,21 +159,21 @@ class BarplotTest < Test::Unit::TestCase
159
159
  plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
160
160
  assert_same(plot,
161
161
  UnicodePlot.barplot!(plot, ["zoom"], [90]))
162
- _, output = with_term { plot.render($stdout) }
162
+ _, output = with_term { plot.render($stdout, newline: false) }
163
163
  assert_equal(fixture_path("barplot/default2.txt").read,
164
164
  output)
165
165
 
166
166
  plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
167
167
  assert_same(plot,
168
168
  UnicodePlot.barplot!(plot, "zoom", 90))
169
- _, output = with_term { plot.render($stdout) }
169
+ _, output = with_term { plot.render($stdout, newline: false) }
170
170
  assert_equal(fixture_path("barplot/default2.txt").read,
171
171
  output)
172
172
 
173
173
  plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
174
174
  assert_same(plot,
175
175
  UnicodePlot.barplot!(plot, data: { zoom: 90 }))
176
- _, output = with_term { plot.render($stdout) }
176
+ _, output = with_term { plot.render($stdout, newline: false) }
177
177
  assert_equal(fixture_path("barplot/default2.txt").read,
178
178
  output)
179
179
  end
@@ -182,7 +182,7 @@ class BarplotTest < Test::Unit::TestCase
182
182
  plot = UnicodePlot.barplot(2..6, 11..15)
183
183
  assert_same(plot,
184
184
  UnicodePlot.barplot!(plot, 9..10, 20..21))
185
- _, output = with_term { plot.render($stdout) }
185
+ _, output = with_term { plot.render($stdout, newline: false) }
186
186
  assert_equal(fixture_path("barplot/ranges2.txt").read,
187
187
  output)
188
188
  end
@@ -6,14 +6,14 @@ class BoxplotTest < Test::Unit::TestCase
6
6
  sub_test_case("print to tty") do
7
7
  test("without name") do
8
8
  plot = UnicodePlot.boxplot([1, 2, 3, 4, 5])
9
- _, output = with_term { plot.render($stdout) }
9
+ _, output = with_term { plot.render($stdout, newline: false) }
10
10
  assert_equal(fixture_path("boxplot/default.txt").read,
11
11
  output)
12
12
  end
13
13
 
14
14
  test("with name") do
15
15
  plot = UnicodePlot.boxplot("series1", [1, 2, 3, 4, 5])
16
- _, output = with_term { plot.render($stdout) }
16
+ _, output = with_term { plot.render($stdout, newline: false) }
17
17
  assert_equal(fixture_path("boxplot/default_name.txt").read,
18
18
  output)
19
19
  end
@@ -28,14 +28,14 @@ class BoxplotTest < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  test("print to tty") do
31
- _, output = with_term { @plot.render($stdout) }
31
+ _, output = with_term { @plot.render($stdout, newline: false) }
32
32
  assert_equal(fixture_path("boxplot/default_parameters.txt").read,
33
33
  output)
34
34
  end
35
35
 
36
36
  test("print to non-tty IO") do
37
37
  output = StringIO.open do |sio|
38
- @plot.render(sio)
38
+ @plot.render(sio, newline: false)
39
39
  sio.close
40
40
  sio.string
41
41
  end
@@ -49,7 +49,7 @@ class BoxplotTest < Test::Unit::TestCase
49
49
  test("with scaling") do
50
50
  i, max_x = data
51
51
  plot = UnicodePlot.boxplot([1, 2, 3, 4, 5], xlim: [0, max_x])
52
- _, output = with_term { plot.render($stdout) }
52
+ _, output = with_term { plot.render($stdout, newline: false) }
53
53
  assert_equal(fixture_path("boxplot/scale#{i}.txt").read,
54
54
  output)
55
55
  end
@@ -63,19 +63,19 @@ class BoxplotTest < Test::Unit::TestCase
63
63
  title: "Multi-series",
64
64
  xlabel: "foo",
65
65
  color: :yellow)
66
- _, output = with_term { plot.render($stdout) }
66
+ _, output = with_term { plot.render($stdout, newline: false) }
67
67
  assert_equal(fixture_path("boxplot/multi1.txt").read,
68
68
  output)
69
69
 
70
70
  assert_same(plot,
71
71
  UnicodePlot.boxplot!(plot, "one more", [-1, 2, 3, 4, 11]))
72
- _, output = with_term { plot.render($stdout) }
72
+ _, output = with_term { plot.render($stdout, newline: false) }
73
73
  assert_equal(fixture_path("boxplot/multi2.txt").read,
74
74
  output)
75
75
 
76
76
  assert_same(plot,
77
77
  UnicodePlot.boxplot!(plot, [4, 2, 2.5, 4, 14], name: "last one"))
78
- _, output = with_term { plot.render($stdout) }
78
+ _, output = with_term { plot.render($stdout, newline: false) }
79
79
  assert_equal(fixture_path("boxplot/multi3.txt").read,
80
80
  output)
81
81
  end
@@ -16,7 +16,7 @@ class DensityplotTest < Test::Unit::TestCase
16
16
  dy2 = @dy.map {|y| y + 2 }
17
17
  assert_same(plot,
18
18
  UnicodePlot.densityplot!(plot, dx2, dy2))
19
- _, output = with_term { plot.render($stdout) }
19
+ _, output = with_term { plot.render($stdout, newline: false) }
20
20
  expected = fixture_path("scatterplot/densityplot.txt").read
21
21
  assert_equal(output, expected)
22
22
  end
@@ -31,7 +31,7 @@ class DensityplotTest < Test::Unit::TestCase
31
31
  dy2 = @dy.map {|y| y + 2 }
32
32
  assert_same(plot,
33
33
  UnicodePlot.densityplot!(plot, dx2, dy2, name: "bar"))
34
- _, output = with_term { plot.render($stdout) }
34
+ _, output = with_term { plot.render($stdout, newline: false) }
35
35
  expected = fixture_path("scatterplot/densityplot_parameters.txt").read
36
36
  assert_equal(output, expected)
37
37
  end
@@ -10,8 +10,9 @@ class HistogramTest < Test::Unit::TestCase
10
10
  test("default") do
11
11
  plot = UnicodePlot.histogram(@x)
12
12
  _, output = with_term { plot.render($stdout) }
13
+ assert_equal("\n", output[-1])
13
14
  assert_equal(fixture_path("histogram/default.txt").read,
14
- output)
15
+ output.chomp)
15
16
  end
16
17
 
17
18
  test("nocolor") do
@@ -21,13 +22,14 @@ class HistogramTest < Test::Unit::TestCase
21
22
  sio.close
22
23
  sio.string
23
24
  end
25
+ assert_equal("\n", output[-1])
24
26
  assert_equal(fixture_path("histogram/default_nocolor.txt").read,
25
- output)
27
+ output.chomp)
26
28
  end
27
29
 
28
30
  test("losed: :left") do
29
31
  plot = UnicodePlot.histogram(@x, closed: :left)
30
- _, output = with_term { plot.render($stdout) }
32
+ _, output = with_term { plot.render($stdout, newline: false) }
31
33
  assert_equal(fixture_path("histogram/default.txt").read,
32
34
  output)
33
35
  end
@@ -35,7 +37,7 @@ class HistogramTest < Test::Unit::TestCase
35
37
  test("x 100") do
36
38
  x100 = @x.map {|a| a * 100 }
37
39
  plot = UnicodePlot.histogram(x100)
38
- _, output = with_term { plot.render($stdout) }
40
+ _, output = with_term { plot.render($stdout, newline: false) }
39
41
  assert_equal(fixture_path("histogram/default_1e2.txt").read,
40
42
  output)
41
43
  end
@@ -43,28 +45,28 @@ class HistogramTest < Test::Unit::TestCase
43
45
  test("x0.01") do
44
46
  x100 = @x.map {|a| a * 0.01 }
45
47
  plot = UnicodePlot.histogram(x100)
46
- _, output = with_term { plot.render($stdout) }
48
+ _, output = with_term { plot.render($stdout, newline: false) }
47
49
  assert_equal(fixture_path("histogram/default_1e-2.txt").read,
48
50
  output)
49
51
  end
50
52
 
51
53
  test("xscale: :log10") do
52
54
  plot = UnicodePlot.histogram(@x, xscale: :log10)
53
- _, output = with_term { plot.render($stdout) }
55
+ _, output = with_term { plot.render($stdout, newline: false) }
54
56
  assert_equal(fixture_path("histogram/log10.txt").read,
55
57
  output)
56
58
  end
57
59
 
58
60
  test("xscale: :log10 with custom label") do
59
61
  plot = UnicodePlot.histogram(@x, xscale: :log10, xlabel: "custom label")
60
- _, output = with_term { plot.render($stdout) }
62
+ _, output = with_term { plot.render($stdout, newline: false) }
61
63
  assert_equal(fixture_path("histogram/log10_label.txt").read,
62
64
  output)
63
65
  end
64
66
 
65
67
  test("nbins: 5, closed: :right") do
66
68
  plot = UnicodePlot.histogram(@x, nbins: 5, closed: :right)
67
- _, output = with_term { plot.render($stdout) }
69
+ _, output = with_term { plot.render($stdout, newline: false) }
68
70
  assert_equal(fixture_path("histogram/hist_params.txt").read,
69
71
  output)
70
72
  end
@@ -76,7 +78,7 @@ class HistogramTest < Test::Unit::TestCase
76
78
  color: :blue,
77
79
  margin: 7,
78
80
  padding: 3)
79
- _, output = with_term { plot.render($stdout) }
81
+ _, output = with_term { plot.render($stdout, newline: false) }
80
82
  assert_equal(fixture_path("histogram/parameters1.txt").read,
81
83
  output)
82
84
  end
@@ -89,7 +91,7 @@ class HistogramTest < Test::Unit::TestCase
89
91
  margin: 7,
90
92
  padding: 3,
91
93
  labels: false)
92
- _, output = with_term { plot.render($stdout) }
94
+ _, output = with_term { plot.render($stdout, newline: false) }
93
95
  assert_equal(fixture_path("histogram/parameters1_nolabels.txt").read,
94
96
  output)
95
97
  end
@@ -102,9 +104,15 @@ class HistogramTest < Test::Unit::TestCase
102
104
  border: :solid,
103
105
  symbol: "=",
104
106
  width: 50)
105
- _, output = with_term { plot.render($stdout) }
107
+ _, output = with_term { plot.render($stdout, newline: false) }
106
108
  assert_equal(fixture_path("histogram/parameters2.txt").read,
107
109
  output)
108
110
  end
111
+
112
+ test("issue #24") do
113
+ assert_nothing_raised do
114
+ UnicodePlot.histogram([1, 2])
115
+ end
116
+ end
109
117
  end
110
118
  end
@@ -26,36 +26,36 @@ class LineplotTest < Test::Unit::TestCase
26
26
  sub_test_case("with numeric array") do
27
27
  test("default") do
28
28
  plot = UnicodePlot.lineplot(@x, @y)
29
- _, output = with_term { plot.render($stdout) }
29
+ _, output = with_term { plot.render($stdout, newline: false) }
30
30
  assert_equal(fixture_path("lineplot/default.txt").read,
31
31
  output)
32
32
 
33
33
  plot = UnicodePlot.lineplot(@x.map(&:to_f), @y)
34
- _, output = with_term { plot.render($stdout) }
34
+ _, output = with_term { plot.render($stdout, newline: false) }
35
35
  assert_equal(fixture_path("lineplot/default.txt").read,
36
36
  output)
37
37
 
38
38
  plot = UnicodePlot.lineplot(@x, @y.map(&:to_f))
39
- _, output = with_term { plot.render($stdout) }
39
+ _, output = with_term { plot.render($stdout, newline: false) }
40
40
  assert_equal(fixture_path("lineplot/default.txt").read,
41
41
  output)
42
42
  end
43
43
 
44
44
  test("y only") do
45
45
  plot = UnicodePlot.lineplot(@y)
46
- _, output = with_term { plot.render($stdout) }
46
+ _, output = with_term { plot.render($stdout, newline: false) }
47
47
  assert_equal(fixture_path("lineplot/y_only.txt").read,
48
48
  output)
49
49
  end
50
50
 
51
51
  test("range") do
52
52
  plot = UnicodePlot.lineplot(6..10)
53
- _, output = with_term { plot.render($stdout) }
53
+ _, output = with_term { plot.render($stdout, newline: false) }
54
54
  assert_equal(fixture_path("lineplot/range1.txt").read,
55
55
  output)
56
56
 
57
57
  plot = UnicodePlot.lineplot(11..15, 6..10)
58
- _, output = with_term { plot.render($stdout) }
58
+ _, output = with_term { plot.render($stdout, newline: false) }
59
59
  assert_equal(fixture_path("lineplot/range2.txt").read,
60
60
  output)
61
61
  end
@@ -66,7 +66,7 @@ class LineplotTest < Test::Unit::TestCase
66
66
  @x.map {|x| x * 1e+3 + 15 },
67
67
  @y.map {|y| y * 1e-3 - 15 }
68
68
  )
69
- _, output = with_term { plot.render($stdout) }
69
+ _, output = with_term { plot.render($stdout, newline: false) }
70
70
  assert_equal(fixture_path("lineplot/scale1.txt").read,
71
71
  output)
72
72
 
@@ -74,19 +74,19 @@ class LineplotTest < Test::Unit::TestCase
74
74
  @x.map {|x| x * 1e-3 + 15 },
75
75
  @y.map {|y| y * 1e+3 - 15 }
76
76
  )
77
- _, output = with_term { plot.render($stdout) }
77
+ _, output = with_term { plot.render($stdout, newline: false) }
78
78
  assert_equal(fixture_path("lineplot/scale2.txt").read,
79
79
  output)
80
80
 
81
81
  tx = [-1.0, 2, 3, 700000]
82
82
  ty = [1.0, 2, 9, 4000000]
83
83
  plot = UnicodePlot.lineplot(tx, ty)
84
- _, output = with_term { plot.render($stdout) }
84
+ _, output = with_term { plot.render($stdout, newline: false) }
85
85
  assert_equal(fixture_path("lineplot/scale3.txt").read,
86
86
  output)
87
87
 
88
88
  plot = UnicodePlot.lineplot(tx, ty, width: 5, height: 5)
89
- _, output = with_term { plot.render($stdout) }
89
+ _, output = with_term { plot.render($stdout, newline: false) }
90
90
  assert_equal(fixture_path("lineplot/scale3_small.txt").read,
91
91
  output)
92
92
  end
@@ -97,7 +97,7 @@ class LineplotTest < Test::Unit::TestCase
97
97
 
98
98
  y1 = v.map(&Math.method(:sin))
99
99
  plot = UnicodePlot.lineplot(d, y1, name: "sin", height: 5, xlabel: "date")
100
- _, output = with_term { plot.render($stdout) }
100
+ _, output = with_term { plot.render($stdout, newline: false) }
101
101
  assert_equal(fixture_path("lineplot/dates1.txt").read,
102
102
  output)
103
103
 
@@ -105,7 +105,7 @@ class LineplotTest < Test::Unit::TestCase
105
105
  assert_same(plot,
106
106
  UnicodePlot.lineplot!(plot, d, y2, name: "cos"))
107
107
 
108
- _, output = with_term { plot.render($stdout) }
108
+ _, output = with_term { plot.render($stdout, newline: false) }
109
109
  assert_equal(fixture_path("lineplot/dates2.txt").read,
110
110
  output)
111
111
  end
@@ -114,34 +114,34 @@ class LineplotTest < Test::Unit::TestCase
114
114
  plot = UnicodePlot.lineplot(@y)
115
115
  assert_same(plot,
116
116
  UnicodePlot.lineplot!(plot, -3, 1))
117
- _, output = with_term { plot.render($stdout) }
117
+ _, output = with_term { plot.render($stdout, newline: false) }
118
118
  assert_equal(fixture_path("lineplot/slope1.txt").read,
119
119
  output)
120
120
 
121
121
  assert_same(plot,
122
122
  UnicodePlot.lineplot!(plot, -4, 0.5, color: :cyan, name: "foo"))
123
- _, output = with_term { plot.render($stdout) }
123
+ _, output = with_term { plot.render($stdout, newline: false) }
124
124
  assert_equal(fixture_path("lineplot/slope2.txt").read,
125
125
  output)
126
126
  end
127
127
 
128
128
  test("limits") do
129
129
  plot = UnicodePlot.lineplot(@x, @y, xlim: [-1.5, 3.5], ylim: [-5.5, 2.5])
130
- _, output = with_term { plot.render($stdout) }
130
+ _, output = with_term { plot.render($stdout, newline: false) }
131
131
  assert_equal(fixture_path("lineplot/limits.txt").read,
132
132
  output)
133
133
  end
134
134
 
135
135
  test("nogrid") do
136
136
  plot = UnicodePlot.lineplot(@x, @y, grid: false)
137
- _, output = with_term { plot.render($stdout) }
137
+ _, output = with_term { plot.render($stdout, newline: false) }
138
138
  assert_equal(fixture_path("lineplot/nogrid.txt").read,
139
139
  output)
140
140
  end
141
141
 
142
142
  test("color: :blue") do
143
143
  plot = UnicodePlot.lineplot(@x, @y, color: :blue, name: "points1")
144
- _, output = with_term { plot.render($stdout) }
144
+ _, output = with_term { plot.render($stdout, newline: false) }
145
145
  assert_equal(fixture_path("lineplot/blue.txt").read,
146
146
  output)
147
147
  end
@@ -152,7 +152,7 @@ class LineplotTest < Test::Unit::TestCase
152
152
  title: "Scatter",
153
153
  xlabel: "x",
154
154
  ylabel: "y")
155
- _, output = with_term { plot.render($stdout) }
155
+ _, output = with_term { plot.render($stdout, newline: false) }
156
156
  assert_equal(fixture_path("lineplot/parameters1.txt").read,
157
157
  output)
158
158
 
@@ -160,7 +160,7 @@ class LineplotTest < Test::Unit::TestCase
160
160
  UnicodePlot.lineplot!(plot,
161
161
  [0.5, 1, 1.5],
162
162
  name: "points2"))
163
- _, output = with_term { plot.render($stdout) }
163
+ _, output = with_term { plot.render($stdout, newline: false) }
164
164
  assert_equal(fixture_path("lineplot/parameters2.txt").read,
165
165
  output)
166
166
 
@@ -169,11 +169,11 @@ class LineplotTest < Test::Unit::TestCase
169
169
  [-0.5, 0.5, 1.5],
170
170
  [0.5, 1, 1.5],
171
171
  name: "points3"))
172
- _, output = with_term { plot.render($stdout) }
172
+ _, output = with_term { plot.render($stdout, newline: false) }
173
173
  assert_equal(fixture_path("lineplot/parameters3.txt").read,
174
174
  output)
175
175
  output = StringIO.open do |sio|
176
- plot.render(sio)
176
+ plot.render(sio, newline: false)
177
177
  sio.close
178
178
  sio.string
179
179
  end
@@ -187,7 +187,7 @@ class LineplotTest < Test::Unit::TestCase
187
187
  canvas: :dot,
188
188
  width: 10,
189
189
  height: 5)
190
- _, output = with_term { plot.render($stdout) }
190
+ _, output = with_term { plot.render($stdout, newline: false) }
191
191
  assert_equal(fixture_path("lineplot/canvassize.txt").read,
192
192
  output)
193
193
  end
@@ -0,0 +1,20 @@
1
+ require 'stringio'
2
+
3
+ class TestPlot < Test::Unit::TestCase
4
+ sub_test_case("#render") do
5
+ test("render to $stdout when no arguments") do
6
+ sio = StringIO.new
7
+ UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio)
8
+
9
+ begin
10
+ save_stdout, $stdout = $stdout, StringIO.new
11
+ UnicodePlot.barplot(data: {a: 23, b: 37}).render
12
+ assert do
13
+ sio.string == $stdout.string
14
+ end
15
+ ensure
16
+ $stdout = save_stdout
17
+ end
18
+ end
19
+ end
20
+ end
File without changes
@@ -24,28 +24,28 @@ class ScatterplotTest < Test::Unit::TestCase
24
24
 
25
25
  test("default") do
26
26
  plot = UnicodePlot.scatterplot(@x, @y)
27
- _, output = with_term { plot.render($stdout) }
27
+ _, output = with_term { plot.render($stdout, newline: false) }
28
28
  assert_equal(fixture_path("scatterplot/default.txt").read,
29
29
  output)
30
30
  end
31
31
 
32
32
  test("y only") do
33
33
  plot = UnicodePlot.scatterplot(@y)
34
- _, output = with_term { plot.render($stdout) }
34
+ _, output = with_term { plot.render($stdout, newline: false) }
35
35
  assert_equal(fixture_path("scatterplot/y_only.txt").read,
36
36
  output)
37
37
  end
38
38
 
39
39
  test("one range") do
40
40
  plot = UnicodePlot.scatterplot(6..10)
41
- _, output = with_term { plot.render($stdout) }
41
+ _, output = with_term { plot.render($stdout, newline: false) }
42
42
  assert_equal(fixture_path("scatterplot/range1.txt").read,
43
43
  output)
44
44
  end
45
45
 
46
46
  test("two ranges") do
47
47
  plot = UnicodePlot.scatterplot(11..15, 6..10)
48
- _, output = with_term { plot.render($stdout) }
48
+ _, output = with_term { plot.render($stdout, newline: false) }
49
49
  assert_equal(fixture_path("scatterplot/range2.txt").read,
50
50
  output)
51
51
  end
@@ -54,7 +54,7 @@ class ScatterplotTest < Test::Unit::TestCase
54
54
  x = @x.map {|a| a * 1e3 + 15 }
55
55
  y = @y.map {|a| a * 1e-3 - 15 }
56
56
  plot = UnicodePlot.scatterplot(x, y)
57
- _, output = with_term { plot.render($stdout) }
57
+ _, output = with_term { plot.render($stdout, newline: false) }
58
58
  assert_equal(fixture_path("scatterplot/scale1.txt").read,
59
59
  output)
60
60
  end
@@ -63,7 +63,7 @@ class ScatterplotTest < Test::Unit::TestCase
63
63
  x = @x.map {|a| a * 1e-3 + 15 }
64
64
  y = @y.map {|a| a * 1e3 - 15 }
65
65
  plot = UnicodePlot.scatterplot(x, y)
66
- _, output = with_term { plot.render($stdout) }
66
+ _, output = with_term { plot.render($stdout, newline: false) }
67
67
  assert_equal(fixture_path("scatterplot/scale2.txt").read,
68
68
  output)
69
69
  end
@@ -72,28 +72,28 @@ class ScatterplotTest < Test::Unit::TestCase
72
72
  miny = -1.2796649117521434e218
73
73
  maxy = -miny
74
74
  plot = UnicodePlot.scatterplot([1], [miny], xlim: [1, 1], ylim: [miny, maxy])
75
- _, output = with_term { plot.render($stdout) }
75
+ _, output = with_term { plot.render($stdout, newline: false) }
76
76
  expected = fixture_path("scatterplot/scale3.txt").read
77
77
  assert_equal(expected, output)
78
78
  end
79
79
 
80
80
  test("limits") do
81
81
  plot = UnicodePlot.scatterplot(@x, @y, xlim: [-1.5, 3.5], ylim: [-5.5, 2.5])
82
- _, output = with_term { plot.render($stdout) }
82
+ _, output = with_term { plot.render($stdout, newline: false) }
83
83
  assert_equal(fixture_path("scatterplot/limits.txt").read,
84
84
  output)
85
85
  end
86
86
 
87
87
  test("nogrid") do
88
88
  plot = UnicodePlot.scatterplot(@x, @y, grid: false)
89
- _, output = with_term { plot.render($stdout) }
89
+ _, output = with_term { plot.render($stdout, newline: false) }
90
90
  assert_equal(fixture_path("scatterplot/nogrid.txt").read,
91
91
  output)
92
92
  end
93
93
 
94
94
  test("blue") do
95
95
  plot = UnicodePlot.scatterplot(@x, @y, color: :blue, name: "points1")
96
- _, output = with_term { plot.render($stdout) }
96
+ _, output = with_term { plot.render($stdout, newline: false) }
97
97
  assert_equal(fixture_path("scatterplot/blue.txt").read,
98
98
  output)
99
99
  end
@@ -104,7 +104,7 @@ class ScatterplotTest < Test::Unit::TestCase
104
104
  title: "Scatter",
105
105
  xlabel: "x",
106
106
  ylabel: "y")
107
- _, output = with_term { plot.render($stdout) }
107
+ _, output = with_term { plot.render($stdout, newline: false) }
108
108
  expected = fixture_path("scatterplot/parameters1.txt").read
109
109
  assert_equal(expected, output)
110
110
 
@@ -112,7 +112,7 @@ class ScatterplotTest < Test::Unit::TestCase
112
112
  UnicodePlot.scatterplot!(plot,
113
113
  [0.5, 1, 1.5],
114
114
  name: "points2"))
115
- _, output = with_term { plot.render($stdout) }
115
+ _, output = with_term { plot.render($stdout, newline: false) }
116
116
  assert_equal(fixture_path("scatterplot/parameters2.txt").read,
117
117
  output)
118
118
 
@@ -121,11 +121,11 @@ class ScatterplotTest < Test::Unit::TestCase
121
121
  [-0.5, 0.5, 1.5],
122
122
  [0.5, 1, 1.5],
123
123
  name: "points3"))
124
- _, output = with_term { plot.render($stdout) }
124
+ _, output = with_term { plot.render($stdout, newline: false) }
125
125
  assert_equal(fixture_path("scatterplot/parameters3.txt").read,
126
126
  output)
127
127
  output = StringIO.open do |sio|
128
- plot.render(sio)
128
+ plot.render(sio, newline: false)
129
129
  sio.close
130
130
  sio.string
131
131
  end
@@ -139,7 +139,7 @@ class ScatterplotTest < Test::Unit::TestCase
139
139
  canvas: :dot,
140
140
  width: 10,
141
141
  height: 5)
142
- _, output = with_term { plot.render($stdout) }
142
+ _, output = with_term { plot.render($stdout, newline: false) }
143
143
  expected = fixture_path("scatterplot/canvassize.txt").read
144
144
  assert_equal(expected, output)
145
145
  end
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
31
 
32
- spec.add_runtime_dependency "enumerable-statistics", ">= 2.0.0.pre"
32
+ spec.add_runtime_dependency "enumerable-statistics", ">= 2.0.1"
33
33
 
34
34
  spec.add_development_dependency "bundler", ">= 1.17"
35
35
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unicode_plot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - mrkn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-18 00:00:00.000000000 Z
11
+ date: 2020-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: enumerable-statistics
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.0.0.pre
19
+ version: 2.0.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.0.0.pre
26
+ version: 2.0.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -88,6 +88,7 @@ files:
88
88
  - lib/unicode_plot/dot_canvas.rb
89
89
  - lib/unicode_plot/grid_plot.rb
90
90
  - lib/unicode_plot/histogram.rb
91
+ - lib/unicode_plot/layout.rb
91
92
  - lib/unicode_plot/lineplot.rb
92
93
  - lib/unicode_plot/lookup_canvas.rb
93
94
  - lib/unicode_plot/plot.rb
@@ -107,6 +108,8 @@ files:
107
108
  - test/test-densityplot.rb
108
109
  - test/test-histogram.rb
109
110
  - test/test-lineplot.rb
111
+ - test/test-plot.rb
112
+ - test/test-result.rb
110
113
  - test/test-scatterplot.rb
111
114
  - unicode_plot.gemspec
112
115
  homepage: https://github.com/red-data-tools/unicode_plot.rb
@@ -128,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
131
  - !ruby/object:Gem::Version
129
132
  version: '0'
130
133
  requirements: []
131
- rubygems_version: 3.0.3
134
+ rubygems_version: 3.1.2
132
135
  signing_key:
133
136
  specification_version: 4
134
137
  summary: Plot your data by Unicode characters
@@ -139,7 +142,9 @@ test_files:
139
142
  - test/helper.rb
140
143
  - test/test-lineplot.rb
141
144
  - test/test-densityplot.rb
145
+ - test/test-plot.rb
142
146
  - test/test-canvas.rb
147
+ - test/test-result.rb
143
148
  - test/test-histogram.rb
144
149
  - test/helper/with_term.rb
145
150
  - test/helper/fixture.rb