unicode_plot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ __END__
2
+ class BoxplotTest < Test::Unit::TestCase
3
+ include Helper::Fixture
4
+ include Helper::WithTerm
5
+
6
+ sub_test_case("UnicodePlot.boxplot") do
7
+ sub_test_case("print to tty") do
8
+ test("without name") do
9
+ plot = UnicodePlot.boxplot([1, 2, 3, 4, 5])
10
+ _, output = with_term { plot.render($stdout) }
11
+ assert_equal(fixture_path("boxplot/default_name.txt").read,
12
+ output)
13
+ end
14
+
15
+ test("with name") do
16
+ plot = UnicodePlot.boxplot("series1", [1, 2, 3, 4, 5])
17
+ _, output = with_term { plot.render($stdout) }
18
+ assert_equal(fixture_path("boxplot/default_name.txt").read,
19
+ output)
20
+ end
21
+ end
22
+
23
+ sub_test_case("with parameters") do
24
+ def setup
25
+ @plot = UnicodePlot.boxplot("series1", [1, 2, 3, 4, 5],
26
+ title: "Test", xlim: [-1, 8],
27
+ color: :blue, width: 50,
28
+ border: :solid, xlabel: "foo")
29
+ end
30
+
31
+ test("print to tty") do
32
+ _, output = with_term { plot.render($stdout) }
33
+ assert_equal(fixture_path("boxplot/default_parameters.txt").read,
34
+ output)
35
+ end
36
+
37
+ test("print to non-tty IO") do
38
+ output = StringIO.open do |sio|
39
+ @plot.render(sio)
40
+ sio.close
41
+ sio.string
42
+ end
43
+ assert_equal(fixture_path("boxplot/default_parameters_nocolor.txt").read,
44
+ output)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,124 @@
1
+ module CanvasTestCases
2
+ include Helper::Fixture
3
+ include Helper::WithTerm
4
+
5
+ CANVAS_CLASSES = {
6
+ ascii: UnicodePlot::AsciiCanvas,
7
+ braille: UnicodePlot::BrailleCanvas
8
+ }.freeze
9
+
10
+ def self.included(mod)
11
+ mod.module_eval do
12
+ def setup
13
+ # seed!(1337)
14
+ # x1, y1 = rand(20), rand(20)
15
+ # x2, y2 = rand(50), rand(50)
16
+ @x1 = [0.226582, 0.504629, 0.933372, 0.522172, 0.505208,
17
+ 0.0997825, 0.0443222, 0.722906, 0.812814, 0.245457,
18
+ 0.11202, 0.000341996, 0.380001, 0.505277, 0.841177,
19
+ 0.326561, 0.810857, 0.850456, 0.478053, 0.179066]
20
+ @y1 = [0.44701, 0.219519, 0.677372, 0.746407, 0.735727,
21
+ 0.574789, 0.538086, 0.848053, 0.110351, 0.796793,
22
+ 0.987618, 0.801862, 0.365172, 0.469959, 0.306373,
23
+ 0.704691, 0.540434, 0.405842, 0.805117, 0.014829]
24
+ @x2 = [0.486366, 0.911547, 0.900818, 0.641951, 0.546221,
25
+ 0.036135, 0.931522, 0.196704, 0.710775, 0.969291,
26
+ 0.32546, 0.632833, 0.815576, 0.85278, 0.577286,
27
+ 0.887004, 0.231596, 0.288337, 0.881386, 0.0952668,
28
+ 0.609881, 0.393795, 0.84808, 0.453653, 0.746048,
29
+ 0.924725, 0.100012, 0.754283, 0.769802, 0.997368,
30
+ 0.0791693, 0.234334, 0.361207, 0.1037, 0.713739,
31
+ 0.510725, 0.649145, 0.233949, 0.812092, 0.914384,
32
+ 0.106925, 0.570467, 0.594956, 0.118498, 0.699827,
33
+ 0.380363, 0.843282, 0.28761, 0.541469, 0.568466]
34
+ @y2 = [0.417777, 0.774845, 0.00230619, 0.907031, 0.971138,
35
+ 0.0524795, 0.957415, 0.328894, 0.530493, 0.193359,
36
+ 0.768422, 0.783238, 0.607772, 0.0261113, 0.0849032,
37
+ 0.461164, 0.613067, 0.785021, 0.988875, 0.131524,
38
+ 0.0657328, 0.466453, 0.560878, 0.925428, 0.238691,
39
+ 0.692385, 0.203687, 0.441146, 0.229352, 0.332706,
40
+ 0.113543, 0.537354, 0.965718, 0.437026, 0.960983,
41
+ 0.372294, 0.0226533, 0.593514, 0.657878, 0.450696,
42
+ 0.436169, 0.445539, 0.0534673, 0.0882236, 0.361795,
43
+ 0.182991, 0.156862, 0.734805, 0.166076, 0.1172]
44
+
45
+ canvas_class = CANVAS_CLASSES[self.class::CANVAS_NAME]
46
+ @canvas = canvas_class.new(40, 10,
47
+ origin_x: 0,
48
+ origin_y: 0,
49
+ plot_width: 1,
50
+ plot_height: 1)
51
+ end
52
+
53
+ test("empty") do
54
+ if self.class::CANVAS_NAME == :braille
55
+ _, output = with_term { @canvas.show($stdout) }
56
+ assert_equal(fixture_path("canvas/empty_braille_show.txt").read,
57
+ output)
58
+ else
59
+ _, output = with_term { @canvas.show($stdout) }
60
+ assert_equal(fixture_path("canvas/empty_show.txt").read,
61
+ output)
62
+ end
63
+ end
64
+
65
+ sub_test_case("with drawing") do
66
+ def setup
67
+ super
68
+ @canvas.line!(0, 0, 1, 1, :blue)
69
+ @canvas.points!(@x1, @y1, :white)
70
+ @canvas.pixel!(2, 4, :cyan)
71
+ @canvas.points!(@x2, @y2, :red)
72
+ @canvas.line!(0, 1, 0.5, 0, :green)
73
+ @canvas.point!(0.05, 0.3, :cyan)
74
+ @canvas.lines!([1, 2], [2, 1])
75
+ @canvas.line!(0, 0, 9, 9999, :yellow)
76
+ @canvas.line!(0, 0, 1, 1, :blue)
77
+ @canvas.line!(0.1, 0.7, 0.9, 0.6, :red)
78
+ end
79
+
80
+ test("print_row") do
81
+ _, output = with_term { @canvas.print_row($stdout, 2) }
82
+ assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_printrow.txt").read,
83
+ output)
84
+ end
85
+
86
+ test("print") do
87
+ _, output = with_term { @canvas.print($stdout) }
88
+ assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_print.txt").read,
89
+ output)
90
+ end
91
+
92
+ test("print_nocolor") do
93
+ _, output = with_term(false) { @canvas.print($stdout) }
94
+ assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_print_nocolor.txt").read,
95
+ output)
96
+ end
97
+
98
+ test("sow") do
99
+ _, output = with_term { @canvas.show($stdout) }
100
+ assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_show.txt").read,
101
+ output)
102
+ end
103
+
104
+ test("show_nocolor") do
105
+ _, output = with_term(false) { @canvas.show($stdout) }
106
+ assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_show_nocolor.txt").read,
107
+ output)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ class BrailleCanvasTest < Test::Unit::TestCase
115
+ CANVAS_NAME = :braille
116
+
117
+ include CanvasTestCases
118
+ end
119
+
120
+ class AsciiCanvasTest < Test::Unit::TestCase
121
+ CANVAS_NAME = :ascii
122
+
123
+ include CanvasTestCases
124
+ end
@@ -0,0 +1,132 @@
1
+ require 'date'
2
+
3
+ class LineplotTest < Test::Unit::TestCase
4
+ include Helper::Fixture
5
+ include Helper::WithTerm
6
+
7
+ sub_test_case("UnicodePlot.lineplot") do
8
+ def setup
9
+ @x = [-1, 1, 3, 3, -1]
10
+ @y = [2, 0, -5, 2, -5]
11
+ end
12
+
13
+ test("ArgumentError") do
14
+ assert_raise(ArgumentError) { UnicodePlot.lineplot() }
15
+ assert_raise(ArgumentError) { UnicodePlot.lineplot(Math.method(:sin), @x, @y) }
16
+ assert_raise(ArgumentError) { UnicodePlot.lineplot([], 0, 3) }
17
+ assert_raise(ArgumentError) { UnicodePlot.lineplot([], @x) }
18
+ assert_raise(ArgumentError) { UnicodePlot.lineplot([]) }
19
+ assert_raise(ArgumentError) { UnicodePlot.lineplot([1, 2], [1, 2, 3]) }
20
+ assert_raise(ArgumentError) { UnicodePlot.lineplot([1, 2, 3], [1, 2]) }
21
+ assert_raise(ArgumentError) { UnicodePlot.lineplot([1, 2, 3], 1..2) }
22
+ assert_raise(ArgumentError) { UnicodePlot.lineplot(1..3, [1, 2]) }
23
+ assert_raise(ArgumentError) { UnicodePlot.lineplot(1..3, 1..2) }
24
+ end
25
+
26
+ sub_test_case("with numeric array") do
27
+ test("default") do
28
+ plot = UnicodePlot.lineplot(@x, @y)
29
+ _, output = with_term { plot.render($stdout) }
30
+ assert_equal(fixture_path("lineplot/default.txt").read,
31
+ output)
32
+
33
+ plot = UnicodePlot.lineplot(@x.map(&:to_f), @y)
34
+ _, output = with_term { plot.render($stdout) }
35
+ assert_equal(fixture_path("lineplot/default.txt").read,
36
+ output)
37
+
38
+ plot = UnicodePlot.lineplot(@x, @y.map(&:to_f))
39
+ _, output = with_term { plot.render($stdout) }
40
+ assert_equal(fixture_path("lineplot/default.txt").read,
41
+ output)
42
+ end
43
+
44
+ test("y only") do
45
+ plot = UnicodePlot.lineplot(@y)
46
+ _, output = with_term { plot.render($stdout) }
47
+ assert_equal(fixture_path("lineplot/y_only.txt").read,
48
+ output)
49
+ end
50
+
51
+ test("range") do
52
+ plot = UnicodePlot.lineplot(6..10)
53
+ _, output = with_term { plot.render($stdout) }
54
+ assert_equal(fixture_path("lineplot/range1.txt").read,
55
+ output)
56
+
57
+ plot = UnicodePlot.lineplot(11..15, 6..10)
58
+ _, output = with_term { plot.render($stdout) }
59
+ assert_equal(fixture_path("lineplot/range2.txt").read,
60
+ output)
61
+ end
62
+ end
63
+
64
+ test("axis scaling and offsets") do
65
+ plot = UnicodePlot.lineplot(
66
+ @x.map {|x| x * 1e+3 + 15 },
67
+ @y.map {|y| y * 1e-3 - 15 }
68
+ )
69
+ _, output = with_term { plot.render($stdout) }
70
+ assert_equal(fixture_path("lineplot/scale1.txt").read,
71
+ output)
72
+
73
+ plot = UnicodePlot.lineplot(
74
+ @x.map {|x| x * 1e-3 + 15 },
75
+ @y.map {|y| y * 1e+3 - 15 }
76
+ )
77
+ _, output = with_term { plot.render($stdout) }
78
+ assert_equal(fixture_path("lineplot/scale2.txt").read,
79
+ output)
80
+
81
+ tx = [-1.0, 2, 3, 700000]
82
+ ty = [1.0, 2, 9, 4000000]
83
+ plot = UnicodePlot.lineplot(tx, ty)
84
+ _, output = with_term { plot.render($stdout) }
85
+ assert_equal(fixture_path("lineplot/scale3.txt").read,
86
+ output)
87
+
88
+ plot = UnicodePlot.lineplot(tx, ty, width: 5, height: 5)
89
+ _, output = with_term { plot.render($stdout) }
90
+ assert_equal(fixture_path("lineplot/scale3_small.txt").read,
91
+ output)
92
+ end
93
+
94
+ test("dates") do
95
+ d = [*Date.new(1999, 12, 31) .. Date.new(2000, 1, 30)]
96
+ v = 0.step(3*Math::PI, by: 3*Math::PI / 30)
97
+
98
+ y1 = v.map(&Math.method(:sin))
99
+ plot = UnicodePlot.lineplot(d, y1, name: "sin", height: 5, xlabel: "date")
100
+ _, output = with_term { plot.render($stdout) }
101
+ assert_equal(fixture_path("lineplot/dates1.txt").read,
102
+ output)
103
+
104
+ y2 = v.map(&Math.method(:cos))
105
+ assert_same(plot,
106
+ UnicodePlot.lineplot!(plot, d, y2, name: "cos"))
107
+
108
+ _, output = with_term { plot.render($stdout) }
109
+ assert_equal(fixture_path("lineplot/dates2.txt").read,
110
+ output)
111
+ end
112
+
113
+ test("line with intercept and slope") do
114
+ plot = UnicodePlot.lineplot(@y)
115
+ assert_same(plot,
116
+ UnicodePlot.lineplot!(plot, -3, 1))
117
+ _, output = with_term { plot.render($stdout) }
118
+ assert_equal(fixture_path("lineplot/slope1.txt").read,
119
+ output)
120
+
121
+ assert_same(plot,
122
+ UnicodePlot.lineplot!(plot, -4, 0.5, color: :cyan, name: "foo"))
123
+ _, output = with_term { plot.render($stdout) }
124
+ assert_equal(fixture_path("lineplot/slope2.txt").read,
125
+ output)
126
+ end
127
+
128
+ # TODO: functions
129
+
130
+ # TODO: stairs
131
+ end
132
+ end
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "unicode_plot/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "unicode_plot"
7
+ version_components = [
8
+ UnicodePlot::Version::MAJOR.to_s,
9
+ UnicodePlot::Version::MINOR.to_s,
10
+ UnicodePlot::Version::MICRO.to_s,
11
+ UnicodePlot::Version::TAG
12
+ ]
13
+ spec.version = version_components.compact.join(".")
14
+ spec.authors = ["mrkn"]
15
+ spec.email = ["mrkn@mrkn.jp"]
16
+
17
+ spec.summary = %q{Plot your data by Unicode characters}
18
+ spec.description = %q{Plot your data by Unicode characters}
19
+ spec.homepage = "https://github.com/mrkn/unicode_plot.rb"
20
+ spec.license = "MIT"
21
+
22
+ spec.files = ["README.md", "Rakefile", "Gemfile", "#{spec.name}.gemspec"]
23
+ spec.files << "LICENSE.txt"
24
+ spec.files.concat Dir.glob("lib/**/*.rb")
25
+
26
+ spec.test_files.concat Dir.glob("test/**/*.rb")
27
+
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", ">= 1.17"
33
+ spec.add_development_dependency "rake"
34
+ spec.add_development_dependency "test-unit"
35
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicode_plot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mrkn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Plot your data by Unicode characters
56
+ email:
57
+ - mrkn@mrkn.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - lib/unicode_plot.rb
67
+ - lib/unicode_plot/ascii_canvas.rb
68
+ - lib/unicode_plot/barplot.rb
69
+ - lib/unicode_plot/boxplot.rb
70
+ - lib/unicode_plot/braille_canvas.rb
71
+ - lib/unicode_plot/canvas.rb
72
+ - lib/unicode_plot/lineplot.rb
73
+ - lib/unicode_plot/plot.rb
74
+ - lib/unicode_plot/renderer.rb
75
+ - lib/unicode_plot/styled_printer.rb
76
+ - lib/unicode_plot/value_transformer.rb
77
+ - lib/unicode_plot/version.rb
78
+ - test/helper.rb
79
+ - test/helper/fixture.rb
80
+ - test/helper/with_term.rb
81
+ - test/run-test.rb
82
+ - test/test-barplot.rb
83
+ - test/test-boxplot.rb
84
+ - test/test-canvas.rb
85
+ - test/test-lineplot.rb
86
+ - unicode_plot.gemspec
87
+ homepage: https://github.com/mrkn/unicode_plot.rb
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.0.3
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Plot your data by Unicode characters
110
+ test_files:
111
+ - test/run-test.rb
112
+ - test/test-barplot.rb
113
+ - test/helper.rb
114
+ - test/test-lineplot.rb
115
+ - test/test-canvas.rb
116
+ - test/helper/with_term.rb
117
+ - test/helper/fixture.rb
118
+ - test/test-boxplot.rb