tty-pie 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/CHANGELOG.md +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +279 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/basic.rb +12 -0
- data/examples/fill.rb +12 -0
- data/lib/tty-pie.rb +1 -0
- data/lib/tty/pie.rb +216 -0
- data/lib/tty/pie/data_item.rb +51 -0
- data/lib/tty/pie/version.rb +7 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/unit/add_spec.rb +29 -0
- data/spec/unit/color_spec.rb +85 -0
- data/spec/unit/draw_spec.rb +68 -0
- data/spec/unit/fill_spec.rb +27 -0
- data/spec/unit/legend_spec.rb +65 -0
- data/spec/unit/update_spec.rb +31 -0
- data/tasks/console.rake +11 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/spec.rake +29 -0
- data/tty-pie.gemspec +29 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 11c000927f796d72dc81d3c669f54a8c3bd4ef61c3da69abf0b9057ddc8cbe60
|
4
|
+
data.tar.gz: d5504fe31f97866a694aeb8920787a6f88b865a4d7feae193eb6457e869f0e98
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abb3e24bee240ea3262e3db8de2e88a1013591355c1385e155c32c01c34cc336b7ce873eb8a946905e2e09a086ce59bba36d053a33cc2015610347b468c283b6
|
7
|
+
data.tar.gz: 64891d4c43466f012af0594575f3197588cb40494cf35dacd2023bc5f59705601b9f6a898012b843cabc7a53dcbd0f170a086f057f727d947319fa0c0de60803
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Piotr Murach
|
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,279 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/blob/master/images/tty.png" alt="tty logo" /></a>
|
3
|
+
</div>
|
4
|
+
|
5
|
+
# TTY:Pie [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
6
|
+
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/tty-pie.svg)][gem]
|
8
|
+
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-pie.svg?branch=master)][travis]
|
9
|
+
[![Build status](https://ci.appveyor.com/api/projects/status/9n69xpw64sylqh2y?svg=true)][appveyor]
|
10
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/dfac05073e1549e9dbb6/maintainability)][codeclimate]
|
11
|
+
[![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-pie/badge.svg)][coverage]
|
12
|
+
[![Inline docs](http://inch-ci.org/github/piotrmurach/tty-pie.svg?branch=master)][inchpages]
|
13
|
+
|
14
|
+
[gitter]: https://gitter.im/piotrmurach/tty
|
15
|
+
[gem]: http://badge.fury.io/rb/tty-pie
|
16
|
+
[travis]: http://travis-ci.org/piotrmurach/tty-pie
|
17
|
+
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-pie
|
18
|
+
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-pie/maintainability
|
19
|
+
[coverage]: https://coveralls.io/github/piotrmurach/tty-pie
|
20
|
+
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-pie
|
21
|
+
|
22
|
+
> Draw pie charts in your terminal window
|
23
|
+
|
24
|
+
**TTY::Pie** provides pie chart drawing component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
25
|
+
|
26
|
+
![Pie chart drawing](https://github.com/piotrmurach/tty-pie/blob/master/assets/tty-pie_chart_drawing_crypto.png)
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'tty-pie'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install tty-pie
|
43
|
+
|
44
|
+
## Contents
|
45
|
+
|
46
|
+
* [1. Usage](#1-usage)
|
47
|
+
* [2. Interface](#2-interface)
|
48
|
+
* [2.1 data](#21-data)
|
49
|
+
* [2.2 add](#22-add)
|
50
|
+
* [2.3 update](#23-update)
|
51
|
+
* [2.4 draw](#24-draw)
|
52
|
+
* [2.5 position](#25-position)
|
53
|
+
* [2.6 radius](#26-radius)
|
54
|
+
* [2.7 legend](#27-legend)
|
55
|
+
|
56
|
+
## 1. Usage
|
57
|
+
|
58
|
+
To draw a pie chart you need to provide an array of data items:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
data = [
|
62
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' },
|
63
|
+
{ name: 'BCH', value: 3045, color: :bright_green, fill: 'x' },
|
64
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta, fill: '@' },
|
65
|
+
{ name: 'ETH', value: 2350, color: :bright_cyan, fill: '+' }
|
66
|
+
]
|
67
|
+
```
|
68
|
+
|
69
|
+
Then pass data to **TTY::Pie** instance with a given radius:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
pie_chart = TTY::Pie.new(data: data, radius: 5)
|
73
|
+
```
|
74
|
+
|
75
|
+
and print the pie chart in your terminal window:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
print pie_chart
|
79
|
+
# =>
|
80
|
+
# ++***
|
81
|
+
# ++++++******* * BTC 44.60%
|
82
|
+
# @@@+++++*********
|
83
|
+
# @@@@@@+++********** x BCH 22.72%
|
84
|
+
# @@@@@@@@@+***********
|
85
|
+
# @@@@@@@@@@x********** @ LTC 15.15%
|
86
|
+
# @@@@@@@@@xx**********
|
87
|
+
# @@@@@@xxxx********* + ETH 17.53%
|
88
|
+
# @@@xxxxxxx*******
|
89
|
+
# xxxxxxxx*****
|
90
|
+
# xxxx*
|
91
|
+
#
|
92
|
+
```
|
93
|
+
|
94
|
+
## 2. Interface
|
95
|
+
|
96
|
+
### 2.1 data
|
97
|
+
|
98
|
+
To draw a pie chart you need to provide data. A single data item is just a Ruby hash that can contain the following keys:
|
99
|
+
|
100
|
+
* `:name` - used for setting the entry name in legend
|
101
|
+
* `:value` - used for calculating actual pie slice size
|
102
|
+
* `:color` - used to color a pie slice corresponding with a value
|
103
|
+
* `:fill` - used as a character to fill in a pie slice
|
104
|
+
|
105
|
+
At the very minimum you need to provide a `:value` in order for a pie to calculate slice sizes. If you wish to have a legend then add the `:name` key as well.
|
106
|
+
|
107
|
+
For example, the following will result in four slices in a pie chart:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
data = [
|
111
|
+
{ name: 'BTC', value: 5977 },
|
112
|
+
{ name: 'BCH', value: 3045 },
|
113
|
+
{ name: 'LTC', value: 2030 },
|
114
|
+
{ name: 'ETH', value: 2350 }
|
115
|
+
]
|
116
|
+
```
|
117
|
+
|
118
|
+
However, the above data slices will be displayed without any color. Use `:color` out of [supported colors](https://github.com/piotrmurach/pastel#3-supported-colors):
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
data = [
|
122
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow },
|
123
|
+
{ name: 'BCH', value: 3045, color: :bright_green },
|
124
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta },
|
125
|
+
{ name: 'ETH', value: 2350, color: :bright_cyan }
|
126
|
+
]
|
127
|
+
```
|
128
|
+
|
129
|
+
To further make your chart readable consider making pie chart slices visible by channging the displayed characters using `:fill` key:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
data = [
|
133
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' },
|
134
|
+
{ name: 'BCH', value: 3045, color: :bright_green, fill: 'x' },
|
135
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta, fill: '@' },
|
136
|
+
{ name: 'ETH', value: 2350, color: :bright_cyan, fill: '+' }
|
137
|
+
]
|
138
|
+
```
|
139
|
+
|
140
|
+
There is no limit to the amount of data you can present, however there is a point of scale and legibility to be considered when printing in the terminals.
|
141
|
+
|
142
|
+
You can add data to pie chart during initialization using `:data` keyword:
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
pie_chart = TTY::Pie.new(data: data)
|
146
|
+
```
|
147
|
+
|
148
|
+
Alternatively, you can delay adding data later with `add` or `<<` methods:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
pie_chart = TTY::Pie.new
|
152
|
+
pie_chart << { name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' }
|
153
|
+
pie_chart << { name: 'BCH', value: 3045, color: :bright_green, fill: 'x' }
|
154
|
+
pie_chart << { name: 'LTC', value: 2030, color: :bright_magenta, fill: '@' }
|
155
|
+
pie_chart << { name: 'ETH', value: 2350, color: :bright_cyan, fill: '+' }
|
156
|
+
```
|
157
|
+
|
158
|
+
### 2.2 add
|
159
|
+
|
160
|
+
You can also set data for the pie chart using the `add` or `<<` method calls. Once a pie chart is initialized, you can add data items:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
pie_chart = TTY::Pie.new
|
164
|
+
pie_chart << { name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' }
|
165
|
+
pie_chart << { name: 'BCH', value: 3045, color: :bright_green, fill: 'x' }
|
166
|
+
...
|
167
|
+
```
|
168
|
+
|
169
|
+
### 2.3 update
|
170
|
+
|
171
|
+
To replace current data completely with the new use `update`:
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
data = [
|
175
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' },
|
176
|
+
{ name: 'BCH', value: 3045, color: :bright_green, fill: 'x' }
|
177
|
+
]
|
178
|
+
pie_chart = TTY::Pie.new(data: data)
|
179
|
+
|
180
|
+
new_data = [
|
181
|
+
{ name: 'BTC', value: 3400, color: :bright_yellow, fill: '*' },
|
182
|
+
{ name: 'BCH', value: 1200, color: :bright_green, fill: 'x' },
|
183
|
+
]
|
184
|
+
|
185
|
+
pie_chart.update(new_data)
|
186
|
+
```
|
187
|
+
|
188
|
+
### 2.4 draw
|
189
|
+
|
190
|
+
Once a pie chart has been initialized use the `draw` or `to_s` method to return a string representation of the chart.
|
191
|
+
|
192
|
+
To actually show it in a terminal, you need to print it:
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
print pie_chart.draw
|
196
|
+
# => this will render chart in terminal
|
197
|
+
```
|
198
|
+
|
199
|
+
You can skip calling any method and simply print:
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
print pie_chart
|
203
|
+
# => this will render chart in terminal
|
204
|
+
```
|
205
|
+
|
206
|
+
### 2.5 position
|
207
|
+
|
208
|
+
If you don't provide location for you pie chart it will be printed at the current cursor location. In order to absolutely position the chart use `:left` and `:top` keyword arguments. For example, if you wanted to position the pie chart at `50th`column and `10th` row:
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
TTY::Pie.new(data: data, left: 50, top: 10)
|
212
|
+
```
|
213
|
+
|
214
|
+
### 2.6 radius
|
215
|
+
|
216
|
+
By default, a pie chart is rendered with a radius of `10`, you can change this using the `:radius` keyword:
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
TTY::Pie.new(data: data, radius: 5)
|
220
|
+
```
|
221
|
+
|
222
|
+
### 2.7 legend
|
223
|
+
|
224
|
+
Provided the following data:
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
data = [
|
228
|
+
{ name: 'BTC', value: 5977, fill: '*' },
|
229
|
+
{ name: 'BCH', value: 3045, fill: '+' },
|
230
|
+
{ name: 'LTC', value: 2030, fill: 'x' }
|
231
|
+
]
|
232
|
+
```
|
233
|
+
|
234
|
+
You can control how the legend is displayed using the `:legend` keyword and hash as value with the following keys:
|
235
|
+
|
236
|
+
* `:left` - used to determine spacing between a chart and a legend, defaults to `4` columns
|
237
|
+
* `:line` - used to determine spacing between legend labels, defaults to `1` line
|
238
|
+
|
239
|
+
For example, to place a legend `10` columns away from the pie chart and separate each label by `2` lines do:
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
pie_chart = TTY::Pie.new(data: data, radius: 3, legend: {left: 10, line: 2})
|
243
|
+
```
|
244
|
+
|
245
|
+
And printing in a terminal will produce:
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
print pie_chart
|
249
|
+
# =>
|
250
|
+
# x** * BTC 54.08%
|
251
|
+
# xxxx*****
|
252
|
+
# ++++xx*******
|
253
|
+
# ++++++******* + BCH 27.55%
|
254
|
+
# ++++++*******
|
255
|
+
# ++++*****
|
256
|
+
# +** x LTC 18.37%
|
257
|
+
```
|
258
|
+
|
259
|
+
## Development
|
260
|
+
|
261
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
262
|
+
|
263
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
264
|
+
|
265
|
+
## Contributing
|
266
|
+
|
267
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-pie_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.
|
268
|
+
|
269
|
+
## License
|
270
|
+
|
271
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
272
|
+
|
273
|
+
## Code of Conduct
|
274
|
+
|
275
|
+
Everyone interacting in the TTY::Pie project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/tty-pie_chart/blob/master/CODE_OF_CONDUCT.md).
|
276
|
+
|
277
|
+
## Copyright
|
278
|
+
|
279
|
+
Copyright (c) 2018 Piotr Murach. See [LICENSE.txt](https://github.com/piotrmurach/tty-pie_chart/blob/master/LICENSE.txt) for further details.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tty/pie_chart"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/examples/basic.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative '../lib/tty-pie'
|
2
|
+
|
3
|
+
data = [
|
4
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow },
|
5
|
+
{ name: 'BCH', value: 3045, color: :bright_green },
|
6
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta },
|
7
|
+
{ name: 'ETH', value: 2350, color: :bright_cyan }
|
8
|
+
]
|
9
|
+
|
10
|
+
pie = TTY::Pie.new(data: data, radius: 10)
|
11
|
+
|
12
|
+
puts pie
|
data/examples/fill.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative '../lib/tty-pie'
|
2
|
+
|
3
|
+
data = [
|
4
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' },
|
5
|
+
{ name: 'BCH', value: 3045, color: :bright_green, fill: 'x' },
|
6
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta, fill: '@' },
|
7
|
+
{ name: 'ETH', value: 2350, color: :bright_cyan, fill: '+' }
|
8
|
+
]
|
9
|
+
|
10
|
+
pie = TTY::Pie.new(data: data, radius: 10)
|
11
|
+
|
12
|
+
puts pie.draw
|
data/lib/tty-pie.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'tty/pie'
|
data/lib/tty/pie.rb
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pastel'
|
4
|
+
require 'tty-cursor'
|
5
|
+
|
6
|
+
require_relative 'pie/data_item'
|
7
|
+
require_relative 'pie/version'
|
8
|
+
|
9
|
+
module TTY
|
10
|
+
class Pie
|
11
|
+
FULL_CIRCLE_DEGREES = 360
|
12
|
+
|
13
|
+
POINT_SYMBOL = '•'
|
14
|
+
|
15
|
+
LEGEND_LINE_SPACE = 1
|
16
|
+
|
17
|
+
LEGEND_LEFT_SPACE = 4
|
18
|
+
|
19
|
+
attr_reader :top, :left
|
20
|
+
|
21
|
+
attr_reader :center_x, :center_y
|
22
|
+
|
23
|
+
attr_reader :radius
|
24
|
+
|
25
|
+
attr_reader :aspect_ratio
|
26
|
+
|
27
|
+
attr_reader :cursor
|
28
|
+
|
29
|
+
attr_reader :fill
|
30
|
+
|
31
|
+
attr_reader :legend
|
32
|
+
|
33
|
+
# Create pie chart
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# data = [ { name: 'BTC', value: 5977, fill: '*' } ]
|
37
|
+
# pie_chart = TTY::Pie.new(data: data, radius: 2)
|
38
|
+
#
|
39
|
+
# @param [Array[Hash]] data
|
40
|
+
# the data to display in each slice
|
41
|
+
# @param [Integer] top
|
42
|
+
# @param [Integer] left
|
43
|
+
# @param [Integer] radius
|
44
|
+
# @param [Boolean] legend
|
45
|
+
# @param [String] fill
|
46
|
+
# @param [Float] aspect_ratio
|
47
|
+
#
|
48
|
+
# @api public
|
49
|
+
def initialize(data: [], top: nil, left: nil, radius: 10, legend: {}, fill: POINT_SYMBOL, aspect_ratio: 2)
|
50
|
+
@data = data.dup
|
51
|
+
@top = top
|
52
|
+
@left = left
|
53
|
+
@radius = radius
|
54
|
+
@legend = legend
|
55
|
+
@fill = fill
|
56
|
+
@aspect_ratio = aspect_ratio
|
57
|
+
@center_x = (left || 0) + radius * aspect_ratio
|
58
|
+
@center_y = (top || 0) + radius
|
59
|
+
|
60
|
+
@pastel = Pastel.new
|
61
|
+
@cursor = TTY::Cursor
|
62
|
+
end
|
63
|
+
|
64
|
+
# Total for the data items
|
65
|
+
#
|
66
|
+
# @return [Integer]
|
67
|
+
#
|
68
|
+
# @api private
|
69
|
+
def total
|
70
|
+
@data.inject(0) { |sum, item| sum += item[:value]; sum }
|
71
|
+
end
|
72
|
+
|
73
|
+
# Convert data into DataItems
|
74
|
+
#
|
75
|
+
# @return [Array[DataItem]]
|
76
|
+
#
|
77
|
+
# @api private
|
78
|
+
def data_items
|
79
|
+
total_value = total
|
80
|
+
@data.map do |item|
|
81
|
+
percent = (item[:value] * 100) / total_value.to_f
|
82
|
+
color_fill = item[:fill] || fill
|
83
|
+
DataItem.new(item[:name], item[:value], percent,
|
84
|
+
item.fetch(:color, false), color_fill)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Add a data item
|
89
|
+
#
|
90
|
+
# @param [Hash]
|
91
|
+
#
|
92
|
+
# @return [self]
|
93
|
+
#
|
94
|
+
# @api public
|
95
|
+
def add(item)
|
96
|
+
@data << item
|
97
|
+
self
|
98
|
+
end
|
99
|
+
alias << add
|
100
|
+
|
101
|
+
# Replace current data with new set
|
102
|
+
#
|
103
|
+
# @param [Array[Hash]]
|
104
|
+
#
|
105
|
+
# @api public
|
106
|
+
def update(data)
|
107
|
+
@data = data
|
108
|
+
end
|
109
|
+
|
110
|
+
# Draw a pie based on the provided data
|
111
|
+
#
|
112
|
+
# @return [String]
|
113
|
+
#
|
114
|
+
# @api public
|
115
|
+
def draw
|
116
|
+
items = data_items
|
117
|
+
angles = data_angles(items)
|
118
|
+
output = []
|
119
|
+
|
120
|
+
labels = items.map(&:to_label)
|
121
|
+
label_vert_space = legend_line
|
122
|
+
label_horiz_space = legend_left
|
123
|
+
label_offset = labels.size / 2
|
124
|
+
label_boundry = label_vert_space * label_offset
|
125
|
+
labels_range = (-label_boundry..label_boundry).step(label_vert_space)
|
126
|
+
|
127
|
+
(-radius..radius).each do |y|
|
128
|
+
width = (Math.sqrt(radius * radius - y * y) * aspect_ratio).round
|
129
|
+
width = width.zero? ? (radius / aspect_ratio).round : width
|
130
|
+
|
131
|
+
output << ' ' * (center_x - width) if top.nil?
|
132
|
+
(-width..width).each do |x|
|
133
|
+
angle = radian_to_degree(Math.atan2(x, y))
|
134
|
+
item = items[select_data_item(angle, angles)]
|
135
|
+
if !top.nil?
|
136
|
+
output << cursor.move_to(center_x + x, center_y + y)
|
137
|
+
end
|
138
|
+
if item.color
|
139
|
+
output << @pastel.decorate(item.fill, item.color)
|
140
|
+
else
|
141
|
+
output << item.fill
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
if legend
|
146
|
+
if !top.nil?
|
147
|
+
output << cursor.move_to(center_x + aspect_ratio * radius + label_horiz_space, center_y + y)
|
148
|
+
end
|
149
|
+
if labels_range.include?(y)
|
150
|
+
output << ' ' * ((center_x - width) + label_horiz_space) if top.nil?
|
151
|
+
output << labels[label_offset + y / label_vert_space]
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
output << "\n"
|
156
|
+
end
|
157
|
+
|
158
|
+
output.join
|
159
|
+
end
|
160
|
+
alias to_s draw
|
161
|
+
|
162
|
+
private
|
163
|
+
|
164
|
+
# All angles from the data to slice the pie
|
165
|
+
#
|
166
|
+
# @return [Array[Numeric]]
|
167
|
+
#
|
168
|
+
# @api private
|
169
|
+
def data_angles(items)
|
170
|
+
start_angle = 0
|
171
|
+
items.reduce([]) do |acc, item|
|
172
|
+
acc << start_angle + item.angle
|
173
|
+
start_angle += item.angle
|
174
|
+
acc
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# The space between a legend and a chart
|
179
|
+
#
|
180
|
+
# @return [Integer]
|
181
|
+
#
|
182
|
+
# @api private
|
183
|
+
def legend_left
|
184
|
+
legend ? legend.fetch(:left, LEGEND_LEFT_SPACE) : LEGEND_LEFT_SPACE
|
185
|
+
end
|
186
|
+
|
187
|
+
# The space between each legend item
|
188
|
+
#
|
189
|
+
# @return [Integer]
|
190
|
+
#
|
191
|
+
# @api private
|
192
|
+
def legend_line
|
193
|
+
(legend ? legend.fetch(:line, LEGEND_LINE_SPACE) : LEGEND_LINE_SPACE) + 1
|
194
|
+
end
|
195
|
+
|
196
|
+
# Select data item index based on angle
|
197
|
+
#
|
198
|
+
# @return [Integer]
|
199
|
+
#
|
200
|
+
# @api private
|
201
|
+
def select_data_item(angle, angles)
|
202
|
+
angles.index { |a| (FULL_CIRCLE_DEGREES / 2 - angle) < a }
|
203
|
+
end
|
204
|
+
|
205
|
+
# Convert radians to degrees
|
206
|
+
#
|
207
|
+
# @param [Float] radians
|
208
|
+
#
|
209
|
+
# @return [Float]
|
210
|
+
#
|
211
|
+
# @api private
|
212
|
+
def radian_to_degree(radians)
|
213
|
+
radians * 180 / Math::PI
|
214
|
+
end
|
215
|
+
end # Pie
|
216
|
+
end # TTY
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal
|
2
|
+
|
3
|
+
require 'pastel'
|
4
|
+
|
5
|
+
module TTY
|
6
|
+
class Pie
|
7
|
+
class DataItem
|
8
|
+
attr_accessor :name
|
9
|
+
|
10
|
+
attr_accessor :value
|
11
|
+
|
12
|
+
attr_accessor :percent
|
13
|
+
|
14
|
+
attr_writer :angle
|
15
|
+
|
16
|
+
attr_accessor :color
|
17
|
+
|
18
|
+
attr_accessor :fill
|
19
|
+
|
20
|
+
# Creat a DataItem
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
def initialize(name, value, percent, color, fill)
|
24
|
+
@name = name
|
25
|
+
@value = value
|
26
|
+
@color = color
|
27
|
+
@percent = percent
|
28
|
+
@fill = fill
|
29
|
+
@pastel = Pastel.new
|
30
|
+
end
|
31
|
+
|
32
|
+
# The item start angle
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
def angle
|
36
|
+
percent * FULL_CIRCLE_DEGREES / 100.to_f
|
37
|
+
end
|
38
|
+
|
39
|
+
# Convert a data item into a legend label
|
40
|
+
#
|
41
|
+
# @return [String]
|
42
|
+
#
|
43
|
+
# @api private
|
44
|
+
def to_label
|
45
|
+
percent_fmt = '%.2f' % percent
|
46
|
+
label = color ? @pastel.decorate(fill, color) : fill
|
47
|
+
"#{label} #{name} #{percent_fmt}%"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end # Pie
|
51
|
+
end # TTY
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
if ENV['COVERAGE'] || ENV['TRAVIS']
|
4
|
+
require 'simplecov'
|
5
|
+
require 'coveralls'
|
6
|
+
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
9
|
+
Coveralls::SimpleCov::Formatter
|
10
|
+
]
|
11
|
+
|
12
|
+
SimpleCov.start do
|
13
|
+
command_name 'spec'
|
14
|
+
add_filter 'spec'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require "bundler/setup"
|
19
|
+
require "tty/pie"
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
# Enable flags like --only-failures and --next-failure
|
23
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
24
|
+
|
25
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
26
|
+
config.disable_monkey_patching!
|
27
|
+
|
28
|
+
config.expect_with :rspec do |c|
|
29
|
+
c.syntax = :expect
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pie, '#add' do
|
4
|
+
it "adds additional item" do
|
5
|
+
data = [ { name: 'BTC', value: 5977, fill: '*' } ]
|
6
|
+
|
7
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
8
|
+
pie << { name: 'BCH', value: 3045, fill: '+' }
|
9
|
+
pie << { name: 'LTC', value: 2030, fill: 'x' }
|
10
|
+
|
11
|
+
output = pie.draw
|
12
|
+
|
13
|
+
expect(output).to eq([
|
14
|
+
" x** * BTC 54.08%\n",
|
15
|
+
" +xx****\n",
|
16
|
+
"++++***** + BCH 27.55%\n",
|
17
|
+
" +++****\n",
|
18
|
+
" +** x LTC 18.37%\n"
|
19
|
+
].join)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "adds item without modifying original data source" do
|
23
|
+
data = [ { name: 'BTC', value: 5977, fill: '*' } ]
|
24
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
25
|
+
pie << { name: 'BCH', value: 3045, fill: '+' }
|
26
|
+
|
27
|
+
expect(data).to match([a_hash_including(name: 'BTC')])
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pie, ':color option' do
|
4
|
+
it "draws a pie chart without colors" do
|
5
|
+
data = [
|
6
|
+
{ name: 'BTC', value: 5977, fill: '*' },
|
7
|
+
{ name: 'BCH', value: 3045, fill: '+' },
|
8
|
+
{ name: 'LTC', value: 2030, fill: 'x' }
|
9
|
+
]
|
10
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
11
|
+
|
12
|
+
output = pie.draw
|
13
|
+
|
14
|
+
expect(output).to eq([
|
15
|
+
" x** * BTC 54.08%\n",
|
16
|
+
" +xx****\n",
|
17
|
+
"++++***** + BCH 27.55%\n",
|
18
|
+
" +++****\n",
|
19
|
+
" +** x LTC 18.37%\n"
|
20
|
+
].join)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "draws a pie chart with colors" do
|
24
|
+
data = [
|
25
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow },
|
26
|
+
{ name: 'BCH', value: 3045, color: :bright_green },
|
27
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta }
|
28
|
+
]
|
29
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
30
|
+
|
31
|
+
output = pie.draw
|
32
|
+
|
33
|
+
expect(output).to eq([
|
34
|
+
" \e[95m•\e[0m\e[93m•\e[0m\e[93m•\e[0m",
|
35
|
+
" \e[93m•\e[0m BTC 54.08%\n",
|
36
|
+
" \e[92m•\e[0m\e[95m•\e[0m\e[95m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\n",
|
37
|
+
"\e[92m•\e[0m\e[92m•\e[0m\e[92m•\e[0m\e[92m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m",
|
38
|
+
" \e[92m•\e[0m BCH 27.55%\n",
|
39
|
+
" \e[92m•\e[0m\e[92m•\e[0m\e[92m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\e[93m•\e[0m\n",
|
40
|
+
" \e[92m•\e[0m\e[93m•\e[0m\e[93m•\e[0m",
|
41
|
+
" \e[95m•\e[0m LTC 18.37%\n"
|
42
|
+
].join)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "draw a pie chart with legend and cursor positioning" do
|
46
|
+
data = [
|
47
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow },
|
48
|
+
{ name: 'BCH', value: 3045, color: :bright_green },
|
49
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta }
|
50
|
+
]
|
51
|
+
pie = TTY::Pie.new(data: data, radius: 2, left: 50, top: 10)
|
52
|
+
|
53
|
+
output = pie.draw
|
54
|
+
|
55
|
+
expect(output).to eq([
|
56
|
+
"\e[11;54H\e[95m•\e[0m\e[11;55H\e[93m•\e[0m\e[11;56H\e[93m•\e[0m",
|
57
|
+
"\e[11;63H\e[93m•\e[0m BTC 54.08%\n",
|
58
|
+
"\e[12;52H\e[92m•\e[0m\e[12;53H\e[95m•\e[0m\e[12;54H\e[95m•\e[0m\e[12;55H\e[93m•\e[0m\e[12;56H\e[93m•\e[0m\e[12;57H\e[93m•\e[0m\e[12;58H\e[93m•\e[0m\e[12;63H\n",
|
59
|
+
"\e[13;51H\e[92m•\e[0m\e[13;52H\e[92m•\e[0m\e[13;53H\e[92m•\e[0m\e[13;54H\e[92m•\e[0m\e[13;55H\e[93m•\e[0m\e[13;56H\e[93m•\e[0m\e[13;57H\e[93m•\e[0m\e[13;58H\e[93m•\e[0m\e[13;59H\e[93m•\e[0m",
|
60
|
+
"\e[13;63H\e[92m•\e[0m BCH 27.55%\n",
|
61
|
+
"\e[14;52H\e[92m•\e[0m\e[14;53H\e[92m•\e[0m\e[14;54H\e[92m•\e[0m\e[14;55H\e[93m•\e[0m\e[14;56H\e[93m•\e[0m\e[14;57H\e[93m•\e[0m\e[14;58H\e[93m•\e[0m\e[14;63H\n",
|
62
|
+
"\e[15;54H\e[92m•\e[0m\e[15;55H\e[93m•\e[0m\e[15;56H\e[93m•\e[0m",
|
63
|
+
"\e[15;63H\e[95m•\e[0m LTC 18.37%\n"
|
64
|
+
].join)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "draw a pie chart without legend and with cursor positioning" do
|
68
|
+
data = [
|
69
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow },
|
70
|
+
{ name: 'BCH', value: 3045, color: :bright_green },
|
71
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta }
|
72
|
+
]
|
73
|
+
pie = TTY::Pie.new(data: data, radius: 2, left: 50, top: 10, legend: false)
|
74
|
+
|
75
|
+
output = pie.draw
|
76
|
+
|
77
|
+
expect(output).to eq([
|
78
|
+
"\e[11;54H\e[95m•\e[0m\e[11;55H\e[93m•\e[0m\e[11;56H\e[93m•\e[0m\n",
|
79
|
+
"\e[12;52H\e[92m•\e[0m\e[12;53H\e[95m•\e[0m\e[12;54H\e[95m•\e[0m\e[12;55H\e[93m•\e[0m\e[12;56H\e[93m•\e[0m\e[12;57H\e[93m•\e[0m\e[12;58H\e[93m•\e[0m\n",
|
80
|
+
"\e[13;51H\e[92m•\e[0m\e[13;52H\e[92m•\e[0m\e[13;53H\e[92m•\e[0m\e[13;54H\e[92m•\e[0m\e[13;55H\e[93m•\e[0m\e[13;56H\e[93m•\e[0m\e[13;57H\e[93m•\e[0m\e[13;58H\e[93m•\e[0m\e[13;59H\e[93m•\e[0m\n",
|
81
|
+
"\e[14;52H\e[92m•\e[0m\e[14;53H\e[92m•\e[0m\e[14;54H\e[92m•\e[0m\e[14;55H\e[93m•\e[0m\e[14;56H\e[93m•\e[0m\e[14;57H\e[93m•\e[0m\e[14;58H\e[93m•\e[0m\n",
|
82
|
+
"\e[15;54H\e[92m•\e[0m\e[15;55H\e[93m•\e[0m\e[15;56H\e[93m•\e[0m\n",
|
83
|
+
].join)
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pie, '#draw' do
|
4
|
+
let(:data) {
|
5
|
+
[
|
6
|
+
{ name: 'BTC', value: 5977 },
|
7
|
+
{ name: 'BCH', value: 3045 },
|
8
|
+
{ name: 'LTC', value: 2030 }
|
9
|
+
]
|
10
|
+
}
|
11
|
+
|
12
|
+
it "draws a pie chart with legend and without cursor positioning" do
|
13
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
14
|
+
|
15
|
+
output = pie.draw
|
16
|
+
|
17
|
+
expect(output).to eq([
|
18
|
+
" ••• • BTC 54.08%\n",
|
19
|
+
" •••••••\n",
|
20
|
+
"••••••••• • BCH 27.55%\n",
|
21
|
+
" •••••••\n",
|
22
|
+
" ••• • LTC 18.37%\n"
|
23
|
+
].join)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "draws a pie chart without legend and without cursor positioning" do
|
27
|
+
pie = TTY::Pie.new(data: data, radius: 2, legend: false)
|
28
|
+
|
29
|
+
output = pie.draw
|
30
|
+
|
31
|
+
expect(output).to eq([
|
32
|
+
" •••\n",
|
33
|
+
" •••••••\n",
|
34
|
+
"•••••••••\n",
|
35
|
+
" •••••••\n",
|
36
|
+
" •••\n"
|
37
|
+
].join)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "draw a pie chart with legend and cursor positioning" do
|
41
|
+
pie = TTY::Pie.new(data: data, radius: 2, left: 50, top: 10)
|
42
|
+
|
43
|
+
output = pie.draw
|
44
|
+
|
45
|
+
expect(output).to eq([
|
46
|
+
"\e[11;54H•\e[11;55H•\e[11;56H•\e[11;63H• BTC 54.08%\n",
|
47
|
+
"\e[12;52H•\e[12;53H•\e[12;54H•\e[12;55H•\e[12;56H•\e[12;57H•\e[12;58H•\e[12;63H\n",
|
48
|
+
"\e[13;51H•\e[13;52H•\e[13;53H•\e[13;54H•\e[13;55H•\e[13;56H•\e[13;57H•\e[13;58H•\e[13;59H•\e[13;63H• BCH 27.55%\n",
|
49
|
+
"\e[14;52H•\e[14;53H•\e[14;54H•\e[14;55H•\e[14;56H•\e[14;57H•\e[14;58H•\e[14;63H\n",
|
50
|
+
"\e[15;54H•\e[15;55H•\e[15;56H•",
|
51
|
+
"\e[15;63H• LTC 18.37%\n"
|
52
|
+
].join)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "draw a pie chart without legend and with cursor positioning" do
|
56
|
+
pie = TTY::Pie.new(data: data, radius: 2, left: 50, top: 10, legend: false)
|
57
|
+
|
58
|
+
output = pie.draw
|
59
|
+
|
60
|
+
expect(output).to eq([
|
61
|
+
"\e[11;54H\•\e[11;55H•\e[11;56H•\n",
|
62
|
+
"\e[12;52H\•\e[12;53H•\e[12;54H•\e[12;55H•\e[12;56H•\e[12;57H•\e[12;58H•\n",
|
63
|
+
"\e[13;51H\•\e[13;52H•\e[13;53H•\e[13;54H•\e[13;55H•\e[13;56H•\e[13;57H•\e[13;58H•\e[13;59H•\n",
|
64
|
+
"\e[14;52H\•\e[14;53H•\e[14;54H•\e[14;55H•\e[14;56H•\e[14;57H•\e[14;58H•\n",
|
65
|
+
"\e[15;54H\•\e[15;55H•\e[15;56H•\n",
|
66
|
+
].join)
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pie, ':fill option' do
|
4
|
+
let(:data) {
|
5
|
+
[
|
6
|
+
{ name: 'BTC', value: 5977, color: :bright_yellow, fill: '*' },
|
7
|
+
{ name: 'BCH', value: 3045, color: :bright_green, fill: '+' },
|
8
|
+
{ name: 'LTC', value: 2030, color: :bright_magenta, fill: 'x' }
|
9
|
+
]
|
10
|
+
}
|
11
|
+
|
12
|
+
it "draws a pie chart with custom fill per data item" do
|
13
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
14
|
+
|
15
|
+
output = pie.draw
|
16
|
+
|
17
|
+
expect(output).to eq([
|
18
|
+
" \e[95mx\e[0m\e[93m*\e[0m\e[93m*\e[0m",
|
19
|
+
" \e[93m*\e[0m BTC 54.08%\n",
|
20
|
+
" \e[92m+\e[0m\e[95mx\e[0m\e[95mx\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m\n\e[92m+\e[0m\e[92m+\e[0m\e[92m+\e[0m\e[92m+\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m",
|
21
|
+
" \e[92m+\e[0m BCH 27.55%\n",
|
22
|
+
" \e[92m+\e[0m\e[92m+\e[0m\e[92m+\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m\e[93m*\e[0m\n",
|
23
|
+
" \e[92m+\e[0m\e[93m*\e[0m\e[93m*\e[0m",
|
24
|
+
" \e[95mx\e[0m LTC 18.37%\n"
|
25
|
+
].join)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pie, ':legend option' do
|
4
|
+
it "draws legend at default location with 1 line separator" do
|
5
|
+
data = [
|
6
|
+
{ name: 'BTC', value: 5977, fill: '*' },
|
7
|
+
{ name: 'BCH', value: 3045, fill: '+' },
|
8
|
+
{ name: 'LTC', value: 2030, fill: 'x' }
|
9
|
+
]
|
10
|
+
|
11
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
12
|
+
|
13
|
+
output = pie.draw
|
14
|
+
|
15
|
+
expect(output).to eq([
|
16
|
+
" x** * BTC 54.08%\n",
|
17
|
+
" +xx****\n",
|
18
|
+
"++++***** + BCH 27.55%\n",
|
19
|
+
" +++****\n",
|
20
|
+
" +** x LTC 18.37%\n"
|
21
|
+
].join)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "draws legend next to chart without any line separator" do
|
25
|
+
data = [
|
26
|
+
{ name: 'BTC', value: 5977, fill: '*' },
|
27
|
+
{ name: 'BCH', value: 3045, fill: '+' },
|
28
|
+
{ name: 'LTC', value: 2030, fill: 'x' }
|
29
|
+
]
|
30
|
+
|
31
|
+
pie = TTY::Pie.new(data: data, radius: 2, legend: {left: 0, line: 0})
|
32
|
+
|
33
|
+
output = pie.draw
|
34
|
+
|
35
|
+
expect(output).to eq([
|
36
|
+
" x**\n",
|
37
|
+
" +xx**** * BTC 54.08%\n",
|
38
|
+
"++++*****+ BCH 27.55%\n",
|
39
|
+
" +++**** x LTC 18.37%\n",
|
40
|
+
" +**\n"
|
41
|
+
].join)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "draws legend at custom location with line separator" do
|
45
|
+
data = [
|
46
|
+
{ name: 'BTC', value: 5977, fill: '*' },
|
47
|
+
{ name: 'BCH', value: 3045, fill: '+' },
|
48
|
+
{ name: 'LTC', value: 2030, fill: 'x' }
|
49
|
+
]
|
50
|
+
|
51
|
+
pie = TTY::Pie.new(data: data, radius: 3, legend: {left: 10, line: 2})
|
52
|
+
|
53
|
+
output = pie.draw
|
54
|
+
|
55
|
+
expect(output).to eq([
|
56
|
+
" x** * BTC 54.08%\n",
|
57
|
+
" xxxx*****\n",
|
58
|
+
"++++xx*******\n",
|
59
|
+
"++++++******* + BCH 27.55%\n",
|
60
|
+
"++++++*******\n",
|
61
|
+
" ++++*****\n",
|
62
|
+
" +** x LTC 18.37%\n"
|
63
|
+
].join)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Pie, '#update' do
|
4
|
+
it "updates current data" do
|
5
|
+
data = [ { name: 'BTC', value: 5977, fill: '*' } ]
|
6
|
+
|
7
|
+
pie = TTY::Pie.new(data: data, radius: 2)
|
8
|
+
|
9
|
+
output = pie.draw
|
10
|
+
|
11
|
+
expect(output).to eq([
|
12
|
+
" ***\n",
|
13
|
+
" *******\n",
|
14
|
+
"********* * BTC 100.00%\n",
|
15
|
+
" *******\n",
|
16
|
+
" ***\n"
|
17
|
+
].join)
|
18
|
+
|
19
|
+
pie.update([{name: 'LTC', value: 2030, fill: 'x'}])
|
20
|
+
|
21
|
+
output = pie.draw
|
22
|
+
|
23
|
+
expect(output).to eq([
|
24
|
+
" xxx\n",
|
25
|
+
" xxxxxxx\n",
|
26
|
+
"xxxxxxxxx x LTC 100.00%\n",
|
27
|
+
" xxxxxxx\n",
|
28
|
+
" xxx\n"
|
29
|
+
].join)
|
30
|
+
end
|
31
|
+
end
|
data/tasks/console.rake
ADDED
data/tasks/coverage.rake
ADDED
data/tasks/spec.rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc 'Run all specs'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
8
|
+
task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
namespace :spec do
|
12
|
+
desc 'Run unit specs'
|
13
|
+
RSpec::Core::RakeTask.new(:unit) do |task|
|
14
|
+
task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Run integration specs'
|
18
|
+
RSpec::Core::RakeTask.new(:integration) do |task|
|
19
|
+
task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
rescue LoadError
|
24
|
+
%w[spec spec:unit spec:integration].each do |name|
|
25
|
+
task name do
|
26
|
+
$stderr.puts "In order to run #{name}, do `gem install rspec`"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/tty-pie.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "tty/pie/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "tty-pie"
|
7
|
+
spec.version = TTY::Pie::VERSION
|
8
|
+
spec.authors = ["Piotr Murach"]
|
9
|
+
spec.email = [""]
|
10
|
+
|
11
|
+
spec.summary = %q{Draw pie charts in your terminal window.}
|
12
|
+
spec.description = %q{Draw pie charts in your terminal window.}
|
13
|
+
spec.homepage = "https://piotrmurach.github.io/tty"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = Dir['{lib,spec,examples}/**/*.rb']
|
17
|
+
spec.files += Dir['{bin,tasks}/*', 'tty-pie.gemspec']
|
18
|
+
spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 2.0.0'
|
22
|
+
|
23
|
+
spec.add_dependency 'pastel', '~> 0.7.2'
|
24
|
+
spec.add_dependency 'tty-cursor', '~> 0.6.0'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tty-pie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piotr Murach
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pastel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.7.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.7.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tty-cursor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.6.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.6.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Draw pie charts in your terminal window.
|
84
|
+
email:
|
85
|
+
- ''
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- CHANGELOG.md
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- bin/console
|
95
|
+
- bin/setup
|
96
|
+
- examples/basic.rb
|
97
|
+
- examples/fill.rb
|
98
|
+
- lib/tty-pie.rb
|
99
|
+
- lib/tty/pie.rb
|
100
|
+
- lib/tty/pie/data_item.rb
|
101
|
+
- lib/tty/pie/version.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
- spec/unit/add_spec.rb
|
104
|
+
- spec/unit/color_spec.rb
|
105
|
+
- spec/unit/draw_spec.rb
|
106
|
+
- spec/unit/fill_spec.rb
|
107
|
+
- spec/unit/legend_spec.rb
|
108
|
+
- spec/unit/update_spec.rb
|
109
|
+
- tasks/console.rake
|
110
|
+
- tasks/coverage.rake
|
111
|
+
- tasks/spec.rake
|
112
|
+
- tty-pie.gemspec
|
113
|
+
homepage: https://piotrmurach.github.io/tty
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 2.0.0
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.7.3
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Draw pie charts in your terminal window.
|
137
|
+
test_files: []
|