Skip to main content

A simple text-only chart generator

Project description

textchart

Dead-simple tools for generating pure-text bargraphs and scatterplots

Usage

>>> from textchart import textchart
>>> data1 = {"bees": 5, "fish": 30, "highway": 6}
>>> textchart.print_graph(data1)
┌──────────────────────────────────────────────────────┐
│    bees: ■■■■■■ 5                                    │
│    fish: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 30 │
│ highway: ■■■■■■■■ 6                                  │
└──────────────────────────────────────────────────────┘
>>> data2 = [(1,3), (4,6), (4,6), (10, 5)]
>>> textchart.print_graph(data2)
┌──────────────────────────────────────────────────────────────────────┐
│   6.3┨                                             ┌───────────────┐ │
│      ┃              *                              │ "x": 1 point  │ │
│      ┃                                             │ "*": 2 points │ │
│      ┃                                             └───────────────┘ │
│   4.6┨                                    x                          │
│      ┃                                                               │
│      ┃                                                               │
│      ┃                                                               │
│   2.9┨   x                                                           │
│      ┃                                                               │
│      ┃                                                               │
│      ┃                                                               │
│   1.3┨                                                               │
│      ┃                                                               │
│      ┃                                                               │
│      ┃                                                               │
│   0.0╄━━━━━━┯━━━━━━━━┯━━━━━━━━┯━━━━━━━━┯━━━━━━━┯                     │
│      0.0    1.6      4.1      6.5      9.0     11.2                  │
│                                                                      │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

Utility Objects

FORMATTERS

  • FORMATTER.num: round to the nearest .1
  • FORMATTER.round: round to the nearest integer

SCALE FUNCTIONS

  • SCALE_FN.linear: linear scale axis
  • SCALE_FN.log: log scale axis

SORTING FUNCTIONS

  • SORTER.default: keeps order from iterating through label_value_pairs
  • SORTER.identity: default python sorting
  • SORTER.alphabetical: sort alphabetically
  • SORTER.lookup_list(l): creates a sort function that preserves the order in list l

API

Functions

add_border

(
string,
max_width=None,
fit=False,
box_chars='│┐└┘┌─',
bold=False
)

Parameters:

  • string: the string
  • max_width: if set, text will wrap if it exceeds a given width.
    • defaults to 80
  • fit: if True, the border will be fit tightly to the text. If False, the border will have width max_width.
    • defaults to False
  • box_chars: the text symbols for horizontal, top-right, bottom-left, bottom-right, top-left, and vertical border parts.
    • defaults to │┐└┘┌─
  • bold: if set, replaces the default box chars │┐└┘┌─, with bold glyfs ┃┓┗┛┏━
    • defaults to False

example:

>> add_border("this is a very long test string", max_width=10)

    ┌───────────┐
    │ this is a │
    │ very long │
    │ test      │
    │ string    │
    └───────────┘

bar_graph

(
label_value_pairs,
filler_char='■',
sorter=SORTERS.identity,
max_width=40,
horizontal=True,
size_labels=True,
border=False,
title=''
)

Parameters:

  • label_value_pairs: a list of pairs or a dict mapping labels to values
  • filler_char: the character to use as a filler
    • default ■
  • sorters: a function that sorts the labels along the axis.
    • default SORTERS.identity
  • horizontal: sets the orientation of the graph. Currently only horizontal bars are available
    • default True
  • size_labels: if true, includes the value as text at the top of each bar
    • default True

example:

    >> bar_graph({1:17, "2":3, "3 & OTHER": 1, 5: 16})

                1: ■■■■■■■■■■■■■■■■■■■■■■ 17
                2: ■■■■ 3
                3: ■■■■■■■■■■■■■ 10
        3 & OTHER: ■ 1
                5: ■■■■■■■■■■■■■■■■■■■■■ 16

scatterplot

(
xy,
border=False
glyphs='.x*',
height=15,
show_key=True,
title=None,
unit_block=' ',
width=40,
x_formatter=FORMATTER.num,
x_label='',
x_range=None,
x_scale_fn=SCALE_FN.linear,
x_ticks=5,
y_formmatter=FORMATTER.num,
y_label='',
y_range=None,
y_scale_fn=SCALE_FN.linear,
y_ticks=5,
)

Parameters:

  • xy: a set of xy pairs
  • x_range: an optional pair setting the maximum and minimum values for the x axis
  • y_range: an optional pair setting the maximum and minimum values for the y axis
  • height: an optional value setting the height of the y axis
  • width: an optional value setting the width of the x axis
  • x_scale_fn: an optional scale function, which takes (min_, max_, steps, current_step) as inputs and returns the scalar value of the begginning of that step. SCALE_FN.log is available.
    • Defaults to SCALE_FN.linear
  • y_scale_fn: an optional scale function, which takes (min_, max_, steps, current_step) as inputs and returns the scalar value of the begginning of that step. SCALE_FN.log is available.
    • Defaults to SCALE_FN.linear
  • x_label: the label for the x axis
  • y_label: the label for the y axis
  • glyphs: a set of symbols to use to represent overlapping points.
    • defaults to ".x*"
    • NOTE: on some monitors, ・•● may improve clarity, but can also cause rendering errors due to variable width characters.
  • unit_block: sets the "background" for the chart. Used as the unit for combuting width.
  • x_ticks: the number of "ticks" along the x axis
  • y_ticks: the number of "ticks" along the y axis
  • show_key: self explanatory.

Example:

    >> # generating some random data
    >> data = [
      (random.normalvariate(50, 5)*random.randint(1,3), random.normalvariate(3, 1))
      for _ in range(400)
      ]
    >> # Plot command:
    >> scatterplot(
          data,
          title='test title',
          x_label='number of X values',
          y_label='number of\nunits of\nY value',
          border=True)

        ┌─────────────────────────────────────────────────────────────────────────────────────┐
        │                                 test title                                          │
        │                                                                                     │
        │            6.7┨                                              ┌────────────────────┐ │
        │               ┃                                              │ ".": 1 - 3 points  │ │
        │               ┃         .       .                            │ "x": 4 - 6 points  │ │
        │               ┃          .        .      . .                 │ "*": 7 - 10 points │ │
        │            4.7┨         .x        . .       ... .            └────────────────────┘ │
        │               ┃        .x.. .    .x. .. ... .. ..                                   │
        │ number of     ┃        .xx.     ....xx... ..........                                │
        │ units of      ┃        .**.  . ....xx......x..x...                                  │
        │  Y value   2.8┨        .**x  .. x.**x......xxx..   .                                │
        │               ┃         **x    . ..*..   x.. x xx...                                │
        │               ┃        ...x     ..*...    ....... . .                               │
        │               ┃        ....     .  . . ..  ....... ..                               │
        │            0.8┨         .         .  .    .    .                                    │
        │               ┃        . .       .  .                                               │
        │               ┃         .        ..          .                                      │
        │               ┃                                                                     │
        │           -0.7╄━━━━━━┯━━━━━━━━┯━━━━━━━━┯━━━━━━━━┯━━━━━━━┯                           │
        │               0.0    30.0     75.1     120.1    165.1   205.2                       │
        │                                                                                     │
        │                      number of X values                                             │
        └─────────────────────────────────────────────────────────────────────────────────────┘

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textchart-0.0.3.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textchart-0.0.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file textchart-0.0.3.tar.gz.

File metadata

  • Download URL: textchart-0.0.3.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for textchart-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a7f2943b425362aab8c12b86ba5a6f73fb4dd32cca31526699bd5ae1a7b99335
MD5 f7f880f7b0f3b9f3beff45e1a0538521
BLAKE2b-256 ea358512e353594c8794cf3ad34d89be6a63043db1ffa2289880e757afd9b888

See more details on using hashes here.

File details

Details for the file textchart-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: textchart-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for textchart-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 26475cf5b99da976d726181157ff8a82a91ec3b4eee1ae47be5edfa7b8472792
MD5 40683c83e681fbbe4b0228d51cc1676e
BLAKE2b-256 3d616dc84c3542f65b8f53076a29a594c41ada602e12f2293cc4d0a0f9dcd883

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page