asciichart 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/asciichart.rb +85 -0
- data/spec/asciichart_spec.rb +21 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 290ee1504ce51b5c46044b07f3381a85d080f3da
|
4
|
+
data.tar.gz: ad884cc6179d6fa239a1bf5d3eeb3a008bed20b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8f59921363ceb19f9872f0f52b3d1a31e68a222b25aab58c0f8d61488f85700ce7f9d2e40bf9fbbb95074ae3c6d6b5c7820a745f8da28f8a9f382114cc495eb
|
7
|
+
data.tar.gz: 6791f194512d4a1bcb25ee9982d306c810538837b12a0867e9e98e0df9c5c49573fa36872ac92f04a44063892bd6c55c29723560e0d4b863c79fb8a16a4699f7
|
data/lib/asciichart.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
class AsciiChart
|
4
|
+
attr_reader :maxx, :miny, :maxy, :series
|
5
|
+
|
6
|
+
def initialize(rows=20)
|
7
|
+
@rows = rows
|
8
|
+
@series = []
|
9
|
+
@maxx = 0
|
10
|
+
@miny = 0
|
11
|
+
@maxy = 0
|
12
|
+
@@colours = %w{red green blue}.map(&:to_sym)
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_series(series, name=nil)
|
16
|
+
@series << {
|
17
|
+
values: series,
|
18
|
+
colour: pick_colour(@series.length),
|
19
|
+
name: name ? name.capitalize : "Series #{@series.length}"
|
20
|
+
}
|
21
|
+
@miny = [series.min, @miny].min
|
22
|
+
@maxy = [series.max, @maxy].max
|
23
|
+
@maxx = [series.length, @maxx].max
|
24
|
+
end
|
25
|
+
|
26
|
+
def num_series
|
27
|
+
@series.length
|
28
|
+
end
|
29
|
+
|
30
|
+
def render
|
31
|
+
# we need one column for each series up the maximum
|
32
|
+
# length of any one series, plus one for padding
|
33
|
+
width = ((num_series + 2) * @maxx) + 2
|
34
|
+
step = (@maxy - @miny) / @rows.to_f
|
35
|
+
y_top = @maxy.to_f
|
36
|
+
|
37
|
+
bitmap = '-' * (width + 2)
|
38
|
+
bitmap += "\r\n|" + (' ' * width) + '|'
|
39
|
+
|
40
|
+
@rows.times do
|
41
|
+
bitmap += "\r\n| "
|
42
|
+
|
43
|
+
# loop on each distinct time/x value
|
44
|
+
@maxx.times do |x|
|
45
|
+
@series.each do |s|
|
46
|
+
bitmap += get_ascii_char(s, x, (y_top - step)..y_top)
|
47
|
+
end
|
48
|
+
bitmap += ' '
|
49
|
+
end
|
50
|
+
bitmap += '|'
|
51
|
+
y_top = (y_top - step).round(2)
|
52
|
+
end
|
53
|
+
bitmap += "\r\n" + ('-' * (width + 2))
|
54
|
+
|
55
|
+
# the legend
|
56
|
+
bitmap += "\r\n"
|
57
|
+
@series.each do |s|
|
58
|
+
bitmap += "\r\n" + s[:name].send(s[:colour])
|
59
|
+
end
|
60
|
+
|
61
|
+
puts bitmap
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_ascii_char(series, time, value_range)
|
65
|
+
# returns an ascii character to represent the value-range for the given series at the given time
|
66
|
+
# if the series does not have a high enough value at that time, we return ' '
|
67
|
+
value = series[:values][time]
|
68
|
+
if value and value >= value_range.max.to_f
|
69
|
+
c = '#'
|
70
|
+
elsif value and value > value_range.min.to_f
|
71
|
+
c = '.'
|
72
|
+
else
|
73
|
+
c = ' '
|
74
|
+
end
|
75
|
+
c.send(series[:colour])
|
76
|
+
end
|
77
|
+
|
78
|
+
def pick_colour(index)
|
79
|
+
@@colours[index % @@colours.length]
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
end
|
85
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative './asciichart'
|
2
|
+
require_relative './spec_helper'
|
3
|
+
|
4
|
+
describe 'graph' do
|
5
|
+
before(:each) do
|
6
|
+
@graph = AsciiChart.new 10, 20, 110, 220
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'can be created' do
|
10
|
+
expect(@graph.xstep).to eq(0.2)
|
11
|
+
expect(@graph.ystep).to eq(0.1)
|
12
|
+
expect(@graph.xrange).to eq(100)
|
13
|
+
expect(@graph.yrange).to eq(200)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'can have a series added' do
|
17
|
+
@graph.add_series([10, 20, 30, 40, 50])
|
18
|
+
expect(@graph.num_series).to eq(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: asciichart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
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
|
+
description: Renders data series as an ASCII-style bar chart
|
42
|
+
email: benito.m@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/asciichart.rb
|
48
|
+
- spec/asciichart_spec.rb
|
49
|
+
homepage: ''
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.1.10
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: asciichart
|
73
|
+
test_files:
|
74
|
+
- spec/asciichart_spec.rb
|