barcharts 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5393825325d9bc8d34a37947ee8ab0f23fa1a33e
4
+ data.tar.gz: 5ee63429bba7ad9fbd65e199f6ae263be43083e9
5
+ SHA512:
6
+ metadata.gz: b21fa054fcc843fa93662d54cf2c981aab09f845cfc9933c32009a442418b5cc6b06241a37eace67e5a74573bcb87ca1463fb8e882511930425a673b3101806e
7
+ data.tar.gz: 639270dc6175fc3e8211d075b7845458ea7e31f370636d857e729c5934c74888f94e2433916701497492f53c2c44d7a6521c2f0651560200d1695c6aa2f0f149
@@ -0,0 +1,4 @@
1
+
2
+ ### 0.0.1 / 2020-03-06
3
+
4
+ * Everything is new. First release
@@ -0,0 +1,9 @@
1
+ CHANGELOG.md
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/barcharts.rb
6
+ lib/barcharts/version.rb
7
+ script/test_readme.rb
8
+ test/helper.rb
9
+ test/test_barchart.rb
@@ -0,0 +1,189 @@
1
+ # barcharts gem - print barcharts in your terminal in ascii or with unicode block elements
2
+
3
+ * home :: [github.com/feedreader/pluto](https://github.com/feedreader/pluto)
4
+ * bugs :: [github.com/feedreader/pluto/issues](https://github.com/feedreader/pluto/issues)
5
+ * gem :: [rubygems.org/gems/barcharts](https://rubygems.org/gems/barcharts)
6
+ * rdoc :: [rubydoc.info/gems/barcharts](http://rubydoc.info/gems/barcharts)
7
+ * forum :: [groups.google.com/group/wwwmake](http://groups.google.com/group/wwwmake)
8
+
9
+
10
+ ## Usage
11
+
12
+ Use the `barchart` method to print barcharts.
13
+ Let's use the
14
+ "Q: Where do Rubyists come from / What are the top countries for blogs?"
15
+ example from the Planet Ruby page:
16
+
17
+ ``` ruby
18
+ require 'barchart'
19
+
20
+ data = [
21
+ ['Australia', 3],
22
+ ['Austria', 1],
23
+ ['Canada', 1],
24
+ ['Croatia', 1],
25
+ ['India', 1],
26
+ ['Poland', 2],
27
+ ['Spain', 1],
28
+ ['Switzerland', 1],
29
+ ['Ukraine', 1],
30
+ ['United States', 6],
31
+ ]
32
+
33
+ # -or-
34
+
35
+ data = {
36
+ 'Australia': 3,
37
+ 'Austria': 1,
38
+ 'Canada': 1,
39
+ 'Croatia': 1,
40
+ 'India': 1,
41
+ 'Poland': 2,
42
+ 'Spain': 1,
43
+ 'Switzerland': 1,
44
+ 'Ukraine': 1,
45
+ 'United States': 6,
46
+ }
47
+
48
+ puts barchart( data, title: 'Location', chars: '*' )
49
+ ```
50
+
51
+ Resulting in:
52
+
53
+ ```
54
+ Location (n=18)
55
+ ---------------------------------
56
+ Australia (16%) | ******** 3
57
+ Austria ( 5%) | ** 1
58
+ Canada ( 5%) | ** 1
59
+ Croatia ( 5%) | ** 1
60
+ India ( 5%) | ** 1
61
+ Poland (11%) | ***** 2
62
+ Spain ( 5%) | ** 1
63
+ Switzerland ( 5%) | ** 1
64
+ Ukraine ( 5%) | ** 1
65
+ United States (33%) | **************** 6
66
+ ```
67
+
68
+ or using Unicode block elements:
69
+
70
+ ``` ruby
71
+ puts barchart( data, title: 'Location', chars: '▏▎▍▋▊▉' )
72
+ ```
73
+
74
+ Resulting in:
75
+
76
+ ```
77
+ Location (n=18)
78
+ ---------------------------------
79
+ Australia (16%) | ▉▉▉▉▉▉▉▉▍ 3
80
+ Austria ( 5%) | ▉▉▊ 1
81
+ Canada ( 5%) | ▉▉▊ 1
82
+ Croatia ( 5%) | ▉▉▊ 1
83
+ India ( 5%) | ▉▉▊ 1
84
+ Poland (11%) | ▉▉▉▉▉▋ 2
85
+ Spain ( 5%) | ▉▉▊ 1
86
+ Switzerland ( 5%) | ▉▉▊ 1
87
+ Ukraine ( 5%) | ▉▉▊ 1
88
+ United States (33%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▊ 6
89
+ ```
90
+
91
+
92
+ Aside: What are Unicode block elements?
93
+
94
+ Unicode among other block elements defines:
95
+
96
+ - `▏` - U+258F - Left one eighth block
97
+ - `▎` - U+258E - Left one quarter block
98
+ - `▍` - U+258D - Left three eighths block
99
+ - `▋` - U+258B - Left five eighths block
100
+ - `▊` - U+258A - Left three quarters block
101
+ - `▉` - U+2589 - Left seven eighths block
102
+
103
+ See [Block Elements @ Wikipedia](https://en.wikipedia.org/wiki/Block_Elements) for more.
104
+
105
+
106
+
107
+ ### Barchart Options - `sort: true|false`, `n: <sample size>`
108
+
109
+ Pass in `sort: true` to reverse sort the dataset (highest values first).
110
+ Let's use the
111
+ "Q: What (web site) publishing tools are in use?" example:
112
+
113
+ ``` ruby
114
+ data = [
115
+ ['Webgen', 1],
116
+ ['Jekyll', 12],
117
+ ['WordPress', 3],
118
+ ['Hugo', 1],
119
+ ['Ghost', 2],
120
+ ['Transistor', 1],
121
+ ['Simplecast', 1],
122
+ ['?', 30],
123
+ ]
124
+
125
+ puts barchart( data, title: 'Generators', sort: true )
126
+ ```
127
+
128
+ Resulting in:
129
+
130
+ ```
131
+ Generators (n=51)
132
+ ---------------------------------
133
+ ? (58%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▍ 30
134
+ Jekyll (23%) | ▉▉▉▉▉▉▉▉▉▉▉▊ 12
135
+ WordPress ( 5%) | ▉▉▉ 3
136
+ Ghost ( 3%) | ▉▉ 2
137
+ Hugo ( 1%) | ▉ 1
138
+ Transistor ( 1%) | ▉ 1
139
+ Simplecast ( 1%) | ▉ 1
140
+ Webgen ( 1%) | ▉ 1
141
+ ```
142
+
143
+
144
+ Pass in `n: <sample size>` to use a "custom" sample size
145
+ AND to turn off the auto-calculation of the
146
+ usage percentage.
147
+ Let's use the
148
+ "Q: What are the top topics / words in headlines?" example:
149
+
150
+ ``` ruby
151
+ data = [
152
+ ['rails', 13],
153
+ ['ruby', 12],
154
+ ['conferences', 6],
155
+ ['dry', 5],
156
+ ['rom', 4],
157
+ ['ml', 2],
158
+ ]
159
+
160
+ puts barchart( data, title: 'Top Words in Headlines 2020', n: 47)
161
+ ```
162
+
163
+ resulting in:
164
+
165
+ ```
166
+ Top Words in Headlines 2020 (n=47)
167
+ ---------------------------------
168
+ rails | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▍ 13
169
+ ruby | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▎ 12
170
+ conferences | ▉▉▉▉▉▉▉▏ 6
171
+ dry | ▉▉▉▉▉▉ 5
172
+ rom | ▉▉▉▉▊ 4
173
+ ml | ▉▉▍ 2
174
+ ```
175
+
176
+
177
+ That's all for now.
178
+
179
+
180
+
181
+ ## License
182
+
183
+ The `barcharts` scripts are dedicated to the public domain.
184
+ Use it as you please with no restrictions whatsoever.
185
+
186
+ ## Questions? Comments?
187
+
188
+ Send them along to the [wwwmake Forum/Mailing List](http://groups.google.com/group/wwwmake).
189
+ Thanks!
@@ -0,0 +1,28 @@
1
+ require 'hoe'
2
+ require './lib/barcharts/version.rb'
3
+
4
+ Hoe.spec 'barcharts' do
5
+
6
+ self.version = Barcharts::VERSION
7
+
8
+ self.summary = "barcharts - print barcharts in your terminal in ascii or with unicode block elements"
9
+ self.description = summary
10
+
11
+ self.urls = ['https://github.com/feedreader/pluto']
12
+
13
+ self.author = 'Gerald Bauer'
14
+ self.email = 'wwwmake@googlegroups.com'
15
+
16
+ # switch extension to .markdown for gihub formatting
17
+ self.readme_file = 'README.md'
18
+ self.history_file = 'CHANGELOG.md'
19
+
20
+ self.extra_deps = []
21
+
22
+ self.licenses = ['Public Domain']
23
+
24
+ self.spec_extras = {
25
+ required_ruby_version: '>= 2.2.2'
26
+ }
27
+
28
+ end
@@ -0,0 +1,96 @@
1
+ require 'pp'
2
+ require 'date'
3
+ require 'time'
4
+ require 'json'
5
+
6
+
7
+ # our own code
8
+ require 'barcharts/version' # note: let version always go first
9
+
10
+
11
+
12
+ def bar( value, max_value, max_width: 50, chars: '▏▎▍▋▊▉' )
13
+ # ▏ - U+258F Left one eighth block
14
+ # ▎ - U+258E Left one quarter block
15
+ # ▍ - U+258D Left three eighths block
16
+ # ▋ - U+258B Left five eighths block
17
+ # ▊ - U+258A Left three quarters block
18
+ # ▉ - U+2589 Left seven eighths block
19
+ #
20
+ ## for unicode block elements see
21
+ ## https://en.wikipedia.org/wiki/Block_Elements
22
+
23
+ width = (value*max_width).to_f / max_value ## 50 = max width same as 100%
24
+
25
+ buf = chars[-1] * width.floor
26
+
27
+ if chars.size > 1 ## check fractional part?
28
+ frac = width - width.floor # e.g. 12.5405405405405406
29
+ # becomes 0.5405405405405406
30
+ buf += chars[ (frac*chars.size).floor ] if frac > 0.0
31
+ end
32
+
33
+ buf
34
+ end
35
+
36
+
37
+ def barchart( data, title:,
38
+ sort: nil,
39
+ n: nil,
40
+ max_width: nil,
41
+ chars: nil )
42
+
43
+ ## note: always convert hash to array
44
+ ## example:
45
+ ## { 'one': 1,
46
+ ## 'two': 2 }
47
+ ## becomes: [[ 'one', 1],
48
+ ## [ 'two', 2]]
49
+ data = data.to_a if data.is_a?( Hash )
50
+
51
+ ## convert values to alway be numbers
52
+ ## e.g. support hash as value e.g. { count: 2 } becomes just 2
53
+ data = data.map do |rec|
54
+ rec[1] = rec[1][:count] if rec[1].is_a?( Hash )
55
+ rec
56
+ end
57
+
58
+ if sort
59
+ data = data.sort_by { |rec| rec[1] }.reverse
60
+ end
61
+
62
+
63
+
64
+ sum = data.reduce( 0 ) { |sum,rec| sum+rec[1] }
65
+ max_label = data.reduce( 4 ) { |max,rec| rec[0].size > max ? rec[0].size : max } ## max label length
66
+
67
+ n = sum if n.nil? ## no n passed in? use (default to) sum
68
+
69
+ bar_opts = { }
70
+ bar_opts[:max_width] = max_width if max_width
71
+ bar_opts[:chars ] = chars if chars
72
+
73
+
74
+ buf = "#{title} (n=#{n})\n"
75
+ buf << "---------------------------------\n"
76
+ data.each do |rec|
77
+ label = rec[0]
78
+ value = rec[1]
79
+
80
+ percent = value*100 / sum
81
+
82
+ buf << " %-#{max_label}s " % label
83
+ buf << " (%2d%%)" % percent if n == sum ## only print percent if NOT passed in custom n
84
+ buf << " | "
85
+ buf << bar( value, sum, **bar_opts )
86
+ buf << " %d" % value if value > 0
87
+ buf << "\n"
88
+ end
89
+
90
+ buf
91
+ end # method barchart
92
+
93
+
94
+
95
+ # say hello
96
+ puts Barcharts.banner if $DEBUG || (defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG)
@@ -0,0 +1,22 @@
1
+
2
+ module Barcharts
3
+ MAJOR = 0 ## todo: namespace inside version or something - why? why not??
4
+ MINOR = 1
5
+ PATCH = 0
6
+ VERSION = [MAJOR,MINOR,PATCH].join('.')
7
+
8
+ def self.version
9
+ VERSION
10
+ end
11
+
12
+ def self.banner
13
+ "barcharts/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
14
+ end
15
+
16
+ def self.root
17
+ "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
18
+ end
19
+ end # module Barcharts
20
+
21
+
22
+
@@ -0,0 +1,51 @@
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib script/test_readme.rb
4
+
5
+
6
+ require 'barcharts'
7
+
8
+
9
+
10
+ data = [
11
+ ['Australia', 3],
12
+ ['Austria', 1],
13
+ ['Canada', 1],
14
+ ['Croatia', 1],
15
+ ['India', 1],
16
+ ['Poland', 2],
17
+ ['Spain', 1],
18
+ ['Switzerland', 1],
19
+ ['Ukraine', 1],
20
+ ['United States', 6],
21
+ ]
22
+
23
+ puts barchart( data, title: 'Location', chars: '*' )
24
+
25
+ puts barchart( data, title: 'Location', chars: '▏▎▍▋▊▉' )
26
+
27
+
28
+
29
+ data = [
30
+ ['Webgen', 1],
31
+ ['Jekyll', 12],
32
+ ['WordPress', 3],
33
+ ['Hugo', 1],
34
+ ['Ghost', 2],
35
+ ['Transistor', 1],
36
+ ['Simplecast', 1],
37
+ ['?', 30],
38
+ ]
39
+
40
+ puts barchart( data, title: 'Generators', sort: true )
41
+
42
+ data = [
43
+ ['rails', 13],
44
+ ['ruby', 12],
45
+ ['conferences', 6],
46
+ ['dry', 5],
47
+ ['rom', 4],
48
+ ['ml', 2],
49
+ ]
50
+
51
+ puts barchart( data, title: 'Top Words in Headlines 2020', n: 47)
@@ -0,0 +1,7 @@
1
+ ## minitest setup
2
+ require 'minitest/autorun'
3
+
4
+ ## our own code
5
+ require 'barcharts'
6
+
7
+
@@ -0,0 +1,107 @@
1
+ ###
2
+ # to run use
3
+ # ruby -I ./lib -I ./test test/test_barchart.rb
4
+
5
+
6
+ require 'helper'
7
+
8
+
9
+ class TestBarchart < MiniTest::Test
10
+
11
+ def test_array
12
+ data = [
13
+ ['two', 2],
14
+ ['one', 1],
15
+ ['three', 3],
16
+ ['five', 5],
17
+ ['four', 4],
18
+ ]
19
+
20
+
21
+ assert_equal <<TXT, barchart( data, title: 'Test' )
22
+ Test (n=15)
23
+ ---------------------------------
24
+ two (13%) | ▉▉▉▉▉▉▊ 2
25
+ one ( 6%) | ▉▉▉▍ 1
26
+ three (20%) | ▉▉▉▉▉▉▉▉▉▉ 3
27
+ five (33%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▊ 5
28
+ four (26%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▍ 4
29
+ TXT
30
+
31
+ assert_equal <<TXT, barchart( data, title: 'Test', sort: true )
32
+ Test (n=15)
33
+ ---------------------------------
34
+ five (33%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▊ 5
35
+ four (26%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▍ 4
36
+ three (20%) | ▉▉▉▉▉▉▉▉▉▉ 3
37
+ two (13%) | ▉▉▉▉▉▉▊ 2
38
+ one ( 6%) | ▉▉▉▍ 1
39
+ TXT
40
+ end
41
+
42
+
43
+ def test_hash
44
+ data = {
45
+ 'two': 2,
46
+ 'one': 1,
47
+ 'three three': 3,
48
+ 'five': 5,
49
+ 'four': 4,
50
+ }
51
+
52
+ assert_equal <<TXT, barchart( data, title: 'Test' )
53
+ Test (n=15)
54
+ ---------------------------------
55
+ two (13%) | ▉▉▉▉▉▉▊ 2
56
+ one ( 6%) | ▉▉▉▍ 1
57
+ three three (20%) | ▉▉▉▉▉▉▉▉▉▉ 3
58
+ five (33%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▊ 5
59
+ four (26%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▍ 4
60
+ TXT
61
+
62
+ assert_equal <<TXT, barchart( data, title: 'Test', sort: true )
63
+ Test (n=15)
64
+ ---------------------------------
65
+ five (33%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▊ 5
66
+ four (26%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▍ 4
67
+ three three (20%) | ▉▉▉▉▉▉▉▉▉▉ 3
68
+ two (13%) | ▉▉▉▉▉▉▊ 2
69
+ one ( 6%) | ▉▉▉▍ 1
70
+ TXT
71
+ end
72
+
73
+ def test_hash_count
74
+ data = {
75
+ 'two': { count: 22 },
76
+ 'one': { count: 1 },
77
+ 'three': { count: 3 },
78
+ 'five': { count: 55 },
79
+ 'four': { count: 44 },
80
+ }
81
+
82
+ puts barchart( data, title: 'Test' )
83
+ puts barchart( data, title: 'Test', sort: true )
84
+
85
+ assert_equal <<TXT, barchart( data, title: 'Test' )
86
+ Test (n=125)
87
+ ---------------------------------
88
+ two (17%) | ▉▉▉▉▉▉▉▉▊ 22
89
+ one ( 0%) | ▍ 1
90
+ three ( 2%) | ▉▎ 3
91
+ five (44%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 55
92
+ four (35%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▋ 44
93
+ TXT
94
+
95
+ assert_equal <<TXT, barchart( data, title: 'Test', sort: true )
96
+ Test (n=125)
97
+ ---------------------------------
98
+ five (44%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 55
99
+ four (35%) | ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▋ 44
100
+ two (17%) | ▉▉▉▉▉▉▉▉▊ 22
101
+ three ( 2%) | ▉▎ 3
102
+ one ( 0%) | ▍ 1
103
+ TXT
104
+ end
105
+
106
+ end # class TestBarchart
107
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barcharts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gerald Bauer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rdoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hoe
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.16'
41
+ description: barcharts - print barcharts in your terminal in ascii or with unicode
42
+ block elements
43
+ email: wwwmake@googlegroups.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - CHANGELOG.md
48
+ - Manifest.txt
49
+ - README.md
50
+ files:
51
+ - CHANGELOG.md
52
+ - Manifest.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/barcharts.rb
56
+ - lib/barcharts/version.rb
57
+ - script/test_readme.rb
58
+ - test/helper.rb
59
+ - test/test_barchart.rb
60
+ homepage: https://github.com/feedreader/pluto
61
+ licenses:
62
+ - Public Domain
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options:
66
+ - "--main"
67
+ - README.md
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.2.2
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.5.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: barcharts - print barcharts in your terminal in ascii or with unicode block
86
+ elements
87
+ test_files: []