• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 6 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Draw pie charts in your terminal window
tty logo

TTY:Pie Gitter

Gem Version Actions CI Build status Maintainability Coverage Status

Draw pie charts in your terminal window

TTY::Pie provides pie chart drawing component for TTY toolkit.

Pie chart drawing

Installation

Add this line to your application's Gemfile:

gem "tty-pie"

And then execute:

$ bundle

Or install it yourself as:

$ gem install tty-pie

Contents

1. Usage

To render a pie chart you need to provide an array of data items:

data = [
  { name: "BTC", value: 5977, color: :bright_yellow, fill: "*" },
  { name: "BCH", value: 3045, color: :bright_green, fill: "x" },
  { name: "LTC", value: 2030, color: :bright_magenta, fill: "@" },
  { name: "ETH", value: 2350, color: :bright_cyan, fill: "+" }
]

Then pass data to TTY::Pie instance with a given radius:

pie_chart = TTY::Pie.new(data: data, radius: 5)

and print the pie chart in your terminal window:

print pie_chart
# =>
#         ++***
#     ++++++*******        * BTC 44.60%
#   @@@+++++*********
#  @@@@@@+++**********     x BCH 22.72%
# @@@@@@@@@+***********
# @@@@@@@@@@x**********    @ LTC 15.15%
# @@@@@@@@@xx**********
#  @@@@@@xxxx*********     + ETH 17.53%
#   @@@xxxxxxx*******
#     xxxxxxxx*****
#         xxxx*
# 

2. Interface

2.1 data

To render a pie chart you need to provide data. A single data item is just a Ruby hash that can contain the following keys:

  • :name - used for setting the entry name in legend
  • :value - used for calculating actual pie slice size
  • :color - used to color a pie slice corresponding with a value
  • :fill - used as a character to fill in a pie slice

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.

For example, the following will result in four slices in a pie chart:

data = [
  { name: "BTC", value: 5977 },
  { name: "BCH", value: 3045 },
  { name: "LTC", value: 2030 },
  { name: "ETH", value: 2350 }
]

However, the above data slices will be displayed without any color. Use :color out of supported colors:

data = [
  { name: "BTC", value: 5977, color: :bright_yellow },
  { name: "BCH", value: 3045, color: :bright_green },
  { name: "LTC", value: 2030, color: :bright_magenta },
  { name: "ETH", value: 2350, color: :bright_cyan }
]

To further make your chart readable consider making pie chart slices visible by channging the displayed characters using :fill key:

data = [
  { name: "BTC", value: 5977, color: :bright_yellow, fill: "*" },
  { name: "BCH", value: 3045, color: :bright_green, fill: "x" },
  { name: "LTC", value: 2030, color: :bright_magenta, fill: "@" },
  { name: "ETH", value: 2350, color: :bright_cyan, fill: "+" }
]

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.

You can add data to pie chart during initialization using :data keyword:

pie_chart = TTY::Pie.new(data: data)

Alternatively, you can delay adding data later with add or << methods:

pie_chart = TTY::Pie.new
pie_chart << { name: "BTC", value: 5977, color: :bright_yellow, fill: "*" }
pie_chart << { name: "BCH", value: 3045, color: :bright_green, fill: "x" }
pie_chart << { name: "LTC", value: 2030, color: :bright_magenta, fill: "@" }
pie_chart << { name: "ETH", value: 2350, color: :bright_cyan, fill: "+" }

2.2 add

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:

pie_chart = TTY::Pie.new
pie_chart << { name: "BTC", value: 5977, color: :bright_yellow, fill: "*" }
pie_chart << { name: "BCH", value: 3045, color: :bright_green, fill: "x" }
...

2.3 update

To replace current data completely with the new use update:

data = [
  { name: "BTC", value: 5977, color: :bright_yellow, fill: "*" },
  { name: "BCH", value: 3045, color: :bright_green, fill: "x" }
]
pie_chart = TTY::Pie.new(data: data)

new_data = [
  { name: "BTC", value: 3400, color: :bright_yellow, fill: "*" },
  { name: "BCH", value: 1200, color: :bright_green, fill: "x" },
]

pie_chart.update(new_data)

2.4 render

Once a pie chart has been initialized use the render or to_s method to return a string representation of the chart.

To actually show it in a terminal, you need to print it:

print pie_chart.render
# => this will render chart in terminal

You can skip calling any method and simply print:

print pie_chart
# => this will render chart in terminal

2.5 position

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 50thcolumn and 10th row:

TTY::Pie.new(data: data, left: 50, top: 10)

2.6 radius

By default, a pie chart is rendered with a radius of 10, you can change this using the :radius keyword:

TTY::Pie.new(data: data, radius: 5)

2.7 legend

Provided the following data:

data = [
  { name: "BTC", value: 5977, fill: "*" },
  { name: "BCH", value: 3045, fill: "+" },
  { name: "LTC", value: 2030, fill: "x" }
]

You can control how the legend is displayed using the :legend keyword and hash as value with the following keys:

  • :left - used to determine spacing between a chart and a legend, defaults to 4 columns
  • :line - used to determine spacing between legend labels, defaults to 1 line
  • :format - used to format a display label using template named strings
  • :precision - used to determine currency display decimal places, defaults to 2
  • :delimiter - used to set thousands delimiter in currency format

For example, to place a legend 10 columns away from the pie chart and separate each label by 2 lines do:

pie_chart = TTY::Pie.new(data: data, radius: 3, legend: {left: 10, line: 2})

And printing in a terminal will produce:

print pie_chart
# =>
#      x**               * BTC 54.08%
#   xxxx*****
# ++++xx*******
# ++++++*******          + BCH 27.55%
# ++++++*******
#   ++++*****
#      +**               x LTC 18.37%

2.7.1 format

The :format uses Ruby's format sequences and named strings placeholders:

  • <label> - the icon matching pie chart display
  • <name> - the label name provided in data
  • <value> - the label value provided in data, by default not displayed
  • <currency> - the label value formatted as currency
  • <percent> - the percent automatically calculated from data

By default the label is formatted according to the following pattern with named strings:

"%<label>s %<name>s %<percent>.2f%%"

Given data items:

data = [
  { name: "BTC", value: 5977.12345, fill: "*" },
  { name: "BCH", value: 3045.2, fill: "+" },
  { name: "LTC", value: 2030.444, fill: "x" }
]

The legend will show:

# =>
#      x**       * BTC 54.08%
#   xxxx*****
# ++++xx*******
# ++++++*******  + BCH 27.55%
# ++++++*******
#   ++++*****
#      +**       x LTC 18.37%

To display value together with percent, use <value> named string in the format:

legend: {
  format: "%<label>s %<name>s %<value>d (%<percent>.2f%%)"
}

The legend will show:

# =>
#      x**       * BTC 5977 (54.08%)
#   xxxx*****
# ++++xx*******
# ++++++*******  + BCH 3045 (27.55%)
# ++++++*******
#   ++++*****
#      +**       x LTC 2030 (18.37%)

To display value as currency use <currency> name string in the format:

legend: {
  format: "%<label>s %<name>s $%<currency>s (%<percent>.0f%%)"
}

The legend will show:

# =>
#      x**       * BTC $5,977 (54%)
#   xxxx*****
# ++++xx*******
# ++++++*******  + BCH $3,045 (28%)
# ++++++*******
#   ++++*****
#      +**       x LTC $2,030 (18%)

The currency can be further customised using :precision and :delimiter keys:

legend: {
  format: "%<label>s %<name>s $%<currency>s (%<percent>.0f%%)",
  precision: 3,
  delimiter: "*"
}

The legend will show:

# =>
#      x**       * BTC $5*977.123 (54%)
#   xxxx*****
# ++++xx*******
# ++++++*******  + BCH $3*045.200 (28%)
# ++++++*******
#   ++++*****
#      +**       x LTC $2*030.444 (18%)

2.8 enable_color

You can control when to apply colouring to a pie chart output with :enable_color option.

Valid values are true, false or nil. By default :enable_color is set to nil which performs automatic terminal colour support detection. Setting :enable_color to false will disable coloured output. To force the output to be always coloured set :enable_color to true like so:

 TTY::Pie.new(data: data, enable_color: true)

Development

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.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-pie. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the TTY::Pie project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Copyright

Copyright (c) 2018 Piotr Murach. See LICENSE.txt for further details.

More Repositories

1

tty

Toolkit for developing sleek command line apps.
Ruby
2,505
star
2

tty-prompt

A beautiful and powerful interactive command line prompt
Ruby
1,467
star
3

github

Ruby interface to GitHub API
Ruby
1,151
star
4

finite_machine

A minimal finite state machine with a straightforward syntax.
Ruby
807
star
5

pastel

Terminal output styling with intuitive and clean API.
Ruby
638
star
6

rspec-benchmark

Performance testing matchers for RSpec
Ruby
602
star
7

tty-spinner

A terminal spinner for tasks that have non-deterministic time frame.
Ruby
428
star
8

tty-progressbar

Display a single or multiple progress bars in the terminal.
Ruby
422
star
9

loaf

Manages and displays breadcrumb trails in Rails app - lean & mean.
Ruby
407
star
10

tty-command

Execute shell commands with pretty output logging and capture stdout, stderr and exit status.
Ruby
400
star
11

tty-markdown

Convert a markdown document or text into a terminal friendly output.
Ruby
307
star
12

tty-logger

A readable, structured and beautiful logging for the terminal
Ruby
294
star
13

github_cli

GitHub on your command line. Use your terminal, not the browser.
Ruby
266
star
14

tty-table

A flexible and intuitive table generator
Ruby
190
star
15

tty-box

Draw various frames and boxes in your terminal window
Ruby
183
star
16

awesome-ruby-cli-apps

A curated list of awesome command-line applications in Ruby.
Ruby
169
star
17

rack-policy

Rack middleware for the EU ePrivacy Directive compliance in Ruby Web Apps
Ruby
147
star
18

necromancer

Conversion from one object type to another with a bit of black magic.
Ruby
135
star
19

strings

A set of useful functions for transforming strings.
Ruby
129
star
20

coinpare

Compare cryptocurrency trading data across multiple exchanges and blockchains in the comfort of your terminal
Ruby
113
star
21

tty-exit

Terminal exit codes.
Ruby
99
star
22

strings-case

Convert strings between different cases.
Ruby
97
star
23

tty-reader

A set of methods for processing keyboard input in character, line and multiline modes.
Ruby
89
star
24

tty-screen

Terminal screen detection - cross platform, major ruby interpreters
Ruby
86
star
25

tty-option

A declarative command-line parser
Ruby
85
star
26

merkle_tree

A merkle tree is a data structure used for efficiently summarizing sets of data, often one-time signatures.
Ruby
82
star
27

verse

[DEPRECATED] Text transformations
Ruby
71
star
28

tty-cursor

Terminal cursor movement and manipulation of cursor properties such as visibility
Ruby
70
star
29

tty-file

File manipulation utility methods
Ruby
67
star
30

supervision

Write distributed systems that are resilient and self-heal.
Ruby
65
star
31

tty-config

A highly customisable application configuration interface for building terminal tools.
Ruby
63
star
32

tty-font

Terminal fonts
Ruby
60
star
33

benchmark-trend

Measure performance trends of Ruby code
Ruby
60
star
34

lex

Lex is an implementation of lex tool in Ruby.
Ruby
56
star
35

tty-tree

Print directory or structured data in a tree like format
Ruby
56
star
36

strings-truncation

Truncate strings with fullwidth characters and ANSI codes.
Ruby
50
star
37

slideck

Present Markdown-powered slide decks in the terminal.
Ruby
44
star
38

tty-pager

Terminal output paging - cross-platform, major ruby interpreters
Ruby
40
star
39

tty-color

Terminal color capabilities detection
Ruby
35
star
40

tty-link

Hyperlinks in your terminal
Ruby
32
star
41

strings-inflection

Convert between singular and plural forms of English nouns
Ruby
31
star
42

tty-platform

Operating system detection
Ruby
29
star
43

tty-sparkline

Sparkline charts for terminal applications.
Ruby
29
star
44

tty-editor

Opens a file or text in the user's preferred editor
Ruby
28
star
45

communist

Library for mocking CLI calls to external APIs
Ruby
25
star
46

splay_tree

A self-balancing binary tree optimised for fast access to frequently used nodes.
Ruby
24
star
47

equatable

Allows ruby objects to implement equality comparison and inspection methods.
Ruby
24
star
48

minehunter

Terminal mine hunting game.
Ruby
23
star
49

rotation.js

Responsive and mobile enabled jQuery plugin to help create rotating content.
JavaScript
22
star
50

strings-ansi

Handle ANSI escape codes in strings
Ruby
20
star
51

benchmark-malloc

Trace memory allocations and collect stats
Ruby
20
star
52

strings-numeral

Express numbers as string numerals
Ruby
20
star
53

tty-which

Cross-platform implementation of Unix `which` command
Ruby
19
star
54

benchmark-perf

Benchmark execution time and iterations per second
Ruby
13
star
55

tty-runner

A command routing tree for terminal applications
Ruby
12
star
56

queen

English language linter to hold your files in high esteem.
Ruby
8
star
57

impact

Ruby backend for Impact.js framework
Ruby
8
star
58

pastel-cli

CLI tool for intuitive terminal output styling
Ruby
7
star
59

dotfiles

Configuration files for Unix tools
Vim Script
7
star
60

tty-markdown-cli

CLI tool for displaying nicely formatted Markdown documents in the terminal
Ruby
6
star
61

static_deploy

Automate deployment of static websites
Ruby
6
star
62

tenpin

Terminal tenpin bowling game
Ruby
4
star
63

tty.github.io

TTY toolkit website.
SCSS
3
star
64

tytus

Helps you manage page titles in your Rails app.
Ruby
3
star
65

peter-murach.github.com

Personal webpage
JavaScript
2
star
66

wc.rb

A Ruby clone of Unix wc utility.
Ruby
2
star
67

exportable

Rails plugin to ease exporting tasks.
Ruby
1
star
68

capistrano-git-stages

Multistage capistrano git tags
Ruby
1
star
69

leek

Cucumber steps and RSpec expectations for command line apps
Ruby
1
star
70

tabster

Ruby
1
star
71

unicorn.github.io

Website for the github_api and github_cli ruby gems.
CSS
1
star
72

tty-color-cli

CLI tool for terminal color capabilities detection
Ruby
1
star
73

finite_machine.github.io

Website for finite_machine Ruby gem
SCSS
1
star
74

strings-wrapping

Wrap strings with fullwidth characters and ANSI codes
Ruby
1
star