ascii_chart 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +9 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +98 -0
- data/Rakefile +41 -0
- data/ascii_chart.gemspec +28 -0
- data/example.png +0 -0
- data/lib/ascii_chart.rb +53 -0
- data/lib/ascii_chart/version.rb +3 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9f52bed3abbf751d780b2483c53812d87d9a85e6f04fd06eb4383cc1963bf81b
|
4
|
+
data.tar.gz: 84744af2bac421d20c956bd669c5db05fc88e81254178124c2273a0041ef36da
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cba6c5484aed3e4e336c1690e348da1c622fdaec59f7fa85020e84bd90f2fc83903e20624d75bc6e561f944a89a09cc919bbdec11b278a5111bb8bf0f1713589
|
7
|
+
data.tar.gz: 1a84111bdee0f910ab5857cf04d140fc007c76d1895bc2dad93bbac823313584b621b89b3400c292a701d7e396fd7256f57dfdf215017b815e251e469642ce5e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 zhustec
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# AsciiChart
|
2
|
+
|
3
|
+
[![Travis](https://img.shields.io/travis/zhustec/ascii_chart/master.svg?style=flat-square)](https://travis-ci.com/zhustec/ascii_chart)
|
4
|
+
[![Coveralls github](https://img.shields.io/coveralls/github/zhustec/ascii_chart/master.svg?style=flat-square)](https://coveralls.io/github/zhustec/ascii_chart)
|
5
|
+
[![Gem](https://img.shields.io/gem/v/ascii_chart.svg?style=flat-square)](https://rubygems.org/gems/ascii_chart)
|
6
|
+
[![license](https://img.shields.io/github/license/zhustec/ascii_chart.svg?style=flat-square)](https://github.com/zhustec/ascii_chart/blob/master/LICENSE)
|
7
|
+
|
8
|
+
Nice-looking lightweight console ASCII line charts ╭┈╯. Port of kroitor/asciichart.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'ascii_chart'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
``` ruby
|
27
|
+
$ gem install ascii_chart
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'ascii_chart'
|
34
|
+
|
35
|
+
puts AsciiChart.plot(50.times.map { '%.2f' % (Random.rand * 10) })
|
36
|
+
```
|
37
|
+
|
38
|
+
Some sample cases are provided, to see them, execute:
|
39
|
+
|
40
|
+
```bash
|
41
|
+
$ rake -T sample
|
42
|
+
rake sample:1 # Output the first case
|
43
|
+
rake sample:2 # Output the second case
|
44
|
+
rake sample:3 # Output the third case
|
45
|
+
rake sample:random # Out put a random case
|
46
|
+
```
|
47
|
+
|
48
|
+
e.g. For an random case, execute:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
$ rake sample:random
|
52
|
+
```
|
53
|
+
|
54
|
+
```text
|
55
|
+
5.00 ┼---╮ ╭------╮ ╭--
|
56
|
+
4.00 ┤ ╰-╮ ╭-╯ ╰-╮ ╭-╯
|
57
|
+
3.00 ┤ ╰-╮ ╭-╯ ╰-╮ ╭-╯
|
58
|
+
2.00 ┤ ╰-╮ ╭-╯ ╰-╮ ╭-╯
|
59
|
+
1.00 ┤ ╰╮ ╭╯ ╰╮ ╭╯
|
60
|
+
0.00 ┼ ╰╮ ╭╯ ╰╮ ╭╯
|
61
|
+
-1.00 ┤ ╰-╮ ╭-╯ ╰-╮ ╭-╯
|
62
|
+
-2.00 ┤ ╰╮ ╭╯ ╰╮ ╭╯
|
63
|
+
-3.00 ┤ ╰-╮ ╭-╯ ╰-╮ ╭-╯
|
64
|
+
-4.00 ┤ ╰--╮ ╭--╯ ╰--╮ ╭--╯
|
65
|
+
-5.00 ┤ ╰-----╯ ╰-----╯
|
66
|
+
```
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
```text
|
71
|
+
9.00 ┤ ╭╮ ╭╮ ╭╮ ╭╮ ╭╮
|
72
|
+
8.00 ┤ ╭╮ || || ╭-╮ ╭╮ || ╭╮ || || ╭╮
|
73
|
+
7.00 ┤ || || || ╭╮╭╮| | ||╭╮ ╭╯| || ╭╮ || || ||
|
74
|
+
6.00 ┤ || || ||╭╮ ||||| | |||| | | || ||╭╮ |╰-╮ || ╭╮ ||
|
75
|
+
5.00 ┤╭╮|| || |||| ||||| | ╭╯||| | | || ╭╯||| | | |╰╮ || ╭╮||
|
76
|
+
4.00 ┤|||| || |||╰╮||||| | | ||| | | || | ||| | | | | || ||||
|
77
|
+
3.00 ┤|||| || ||| |||||| | | ||| | | || | ||╰╮| | | | || ╭╮ ||||
|
78
|
+
2.00 ┤|||| || ╭╮||| |||||| | | ||| ╭╯ | || | ╰╯ || | | ╰╮ || || ||||
|
79
|
+
1.00 ┼╯||╰-╮ || ╭╮ ||||| |||||| |╭╯ ||╰╮| |╭╯| | || | | | || || ||||
|
80
|
+
0.00 ┼ ╰╯ | ||╭╮ || ╭-╮ |||╰╯ ||╰╯|| || || ╰╯ ╰╯ | | || | | | || || ╭╮ |||| ╭
|
81
|
+
-1.00 ┤ | |||| || | | ||| || || || || ╰╮| || | | | || || |╰╮ |||| |
|
82
|
+
-2.00 ┤ | |||| || | | ||| ╰╯ || || || || || | | | ||╭╯| | | |╰╯| |
|
83
|
+
-3.00 ┤ |╭╮|||| || | | ||| || || || ╰╯ || | | | |╰╯ ╰-╮| | | | |
|
84
|
+
-4.00 ┤ |||||||╭╮ || | | ||| || || || ╰╯ | | | | || | | | |
|
85
|
+
-5.00 ┤ ╰╯|||||||╭-╯|╭╯ | ╭╯|| || || ╰╯ | | | | || ╰-╯ | |
|
86
|
+
-6.00 ┤ ||||╰╯╰╯ || | | || ╰╯ || ╰-╯ | | || | |
|
87
|
+
-7.00 ┤ |||| || |╭-╮ | ╰╯ ╰╯ | | || | |
|
88
|
+
-8.00 ┤ ╰╯╰╯ ╰╯ ╰╯ | | |╭╯ || | |
|
89
|
+
-9.00 ┤ ╰-╯ ╰╯ ╰╯ ╰-╯
|
90
|
+
```
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zhustec/ascii_chart. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
95
|
+
|
96
|
+
## License
|
97
|
+
|
98
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
task default: :test
|
11
|
+
|
12
|
+
namespace :sample do
|
13
|
+
require 'ascii_chart'
|
14
|
+
|
15
|
+
desc 'Out put a random case'
|
16
|
+
task :random do
|
17
|
+
sample = Random.rand(3)
|
18
|
+
Rake::Task["sample:#{sample + 1}"].invoke
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Output the first case'
|
22
|
+
task :'1' do
|
23
|
+
puts AsciiChart.plot((0...90).map { |i|
|
24
|
+
Math.cos(i * ((Math::PI * 4) / 90)).round(2) * 5
|
25
|
+
})
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'Output the second case'
|
29
|
+
task :'2' do
|
30
|
+
puts AsciiChart.plot((0...90).map { |i|
|
31
|
+
Math.cos(i * ((Math::PI * 8) / 90)).round(2) * 10
|
32
|
+
})
|
33
|
+
end
|
34
|
+
|
35
|
+
desc 'Output the third case'
|
36
|
+
task :'3' do
|
37
|
+
puts AsciiChart.plot(90.times.map {
|
38
|
+
Random.rand(10) * (-1) ** Random.rand(2)
|
39
|
+
})
|
40
|
+
end
|
41
|
+
end
|
data/ascii_chart.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'ascii_chart/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ascii_chart'
|
8
|
+
spec.version = AsciiChart::VERSION
|
9
|
+
spec.authors = ['zhustec']
|
10
|
+
spec.email = ['zhustec@foxmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Nice-looking lightweight console ASCII line charts ┈╯'
|
13
|
+
spec.description = 'Nice-looking lightweight console ASCII line charts ┈╯'
|
14
|
+
spec.homepage = 'https://github.com/zhustec/ascii_chart'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.bindir = 'bin'
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
end
|
data/example.png
ADDED
Binary file
|
data/lib/ascii_chart.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'ascii_chart/version'
|
2
|
+
|
3
|
+
module AsciiChart
|
4
|
+
class << self
|
5
|
+
DEFAULTS = {
|
6
|
+
offset: 3,
|
7
|
+
format: '%8.2f ',
|
8
|
+
height: nil
|
9
|
+
}
|
10
|
+
|
11
|
+
def plot(series, options = {})
|
12
|
+
max, min = series.max, series.min
|
13
|
+
interval = (max - min).abs
|
14
|
+
|
15
|
+
options = DEFAULTS.merge(height: interval).merge(options)
|
16
|
+
offset = options[:offset]
|
17
|
+
radio = options[:height].to_f / interval
|
18
|
+
|
19
|
+
intmax, intmin = (max * radio).floor, (min * radio).ceil
|
20
|
+
rows = (intmax - intmin).abs
|
21
|
+
width = series.length + offset
|
22
|
+
|
23
|
+
result = (0..rows).map { [' '] * width }
|
24
|
+
|
25
|
+
(intmin..intmax).each do |y|
|
26
|
+
label = options[:format] % (max - (((y - intmin) * interval).to_f / rows))
|
27
|
+
result[y - intmin][[offset - label.length, 0].max] = label
|
28
|
+
result[y - intmin][offset - 1] = y == 0 ? '┼' : '┤'
|
29
|
+
end
|
30
|
+
|
31
|
+
highest = (series.first * radio - intmin).to_i
|
32
|
+
result[rows - highest][offset - 1] = '┼'
|
33
|
+
|
34
|
+
(0...series.length - 1).each do |x|
|
35
|
+
_curr = ((series[x + 0] * radio).round - intmin).to_i
|
36
|
+
_next = ((series[x + 1] * radio).round - intmin).to_i
|
37
|
+
|
38
|
+
if _curr == _next
|
39
|
+
result[rows - _curr][x + offset] = '-'
|
40
|
+
else
|
41
|
+
result[rows - _curr][x + offset] = _curr > _next ? '╮' : '╯'
|
42
|
+
result[rows - _next][x + offset] = _curr > _next ? '╰' : '╭'
|
43
|
+
|
44
|
+
([_curr, _next].min + 1...[_curr, _next].max).each do |y|
|
45
|
+
result[rows - y][x + offset] = '|'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
result.map(&:join).join("\n")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ascii_chart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- zhustec
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-02 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Nice-looking lightweight console ASCII line charts ┈╯
|
56
|
+
email:
|
57
|
+
- zhustec@foxmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- ascii_chart.gemspec
|
69
|
+
- example.png
|
70
|
+
- lib/ascii_chart.rb
|
71
|
+
- lib/ascii_chart/version.rb
|
72
|
+
homepage: https://github.com/zhustec/ascii_chart
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.7.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Nice-looking lightweight console ASCII line charts ┈╯
|
96
|
+
test_files: []
|