asciigraph

package module
v0.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2022 License: BSD-3-Clause Imports: 8 Imported by: 248

README

asciigraph

Build status Go Report Card Coverage Status GoDoc License Mentioned in Awesome Go

Go package to make lightweight ASCII line graphs ╭┈╯.

image

Installation

go get github.com/guptarohit/asciigraph

Usage

Basic graph
package main

import (
    "fmt"
    "github.com/guptarohit/asciigraph"
)

func main() {
    data := []float64{3, 4, 9, 6, 2, 4, 5, 8, 5, 10, 2, 7, 2, 5, 6}
    graph := asciigraph.Plot(data)

    fmt.Println(graph)
}

Running this example would render the following graph:

  10.00 ┤        ╭╮
   9.00 ┤ ╭╮     ││
   8.00 ┤ ││   ╭╮││
   7.00 ┤ ││   ││││╭╮
   6.00 ┤ │╰╮  ││││││ ╭
   5.00 ┤ │ │ ╭╯╰╯│││╭╯
   4.00 ┤╭╯ │╭╯   ││││
   3.00 ┼╯  ││    ││││
   2.00 ┤   ╰╯    ╰╯╰╯
Multiple Series
package main

import (
    "fmt"
    "github.com/guptarohit/asciigraph"
)

func main() {
	data := [][]float64{{0, 1, 2, 3, 3, 3, 2, 0}, {5, 4, 2, 1, 4, 6, 6}}
	graph := asciigraph.PlotMany(data)

	fmt.Println(graph)
}

Running this example would render the following graph:

 6.00 ┤    ╭─
 5.00 ┼╮   │
 4.00 ┤╰╮ ╭╯
 3.00 ┤ │╭│─╮
 2.00 ┤ ╰╮│ ╰╮
 1.00 ┤╭╯╰╯  │
 0.00 ┼╯     ╰

Command line interface

This package also brings a small utility for command line usage.

> asciigraph --help
Usage of asciigraph:
  asciigraph [options]
Options:
  -b buffer
    	data points buffer when realtime graph enabled, default equal to `width`
  -c caption
    	caption for the graph
  -f fps
    	set fps to control how frequently graph to be rendered when realtime graph enabled (default 24)
  -h height
    	height in text rows, 0 for auto-scaling
  -o offset
    	offset in columns, for the label (default 3)
  -p precision
    	precision of data point labels along the y-axis (default 2)
  -r realtime
    	enables realtime graph for data stream
  -w width
    	width in columns, 0 for auto-scaling
asciigraph expects data points from stdin. Invalid values are logged to stderr.
CLI Installation

Assuming $GOPATH/bin is in your $PATH, simply go get it then install CLI with following command:

go install github.com/guptarohit/asciigraph/cmd/asciigraph

or pull Docker image:

docker pull ghcr.io/guptarohit/asciigraph:latest

or download binaries from the releases page.

CLI Usage

Feed it data points via stdin:

seq 1 72 | asciigraph -h 10 -c "plot data from stdin"

or use Docker image:

seq 1 72 | docker run -i --rm ghcr.io/guptarohit/asciigraph -h 10 -c "plot data from stdin"

Output:

 72.00 ┤                                                                  ╭────
 64.90 ┤                                                           ╭──────╯
 57.80 ┤                                                    ╭──────╯
 50.70 ┤                                             ╭──────╯
 43.60 ┤                                      ╭──────╯
 36.50 ┤                              ╭───────╯
 29.40 ┤                       ╭──────╯
 22.30 ┤                ╭──────╯
 15.20 ┤         ╭──────╯
  8.10 ┤  ╭──────╯
  1.00 ┼──╯
                                  plot data from stdin

Example of real-time graph for data points stream via stdin:

ping -i.2 google.com | grep -oP '(?<=time=).*(?=ms)' --line-buffered | asciigraph -r -h 10 -w 40 -c "realtime plot data (google ping in ms) from stdin"

asciinema

Acknowledgement

This package started as golang port of asciichart.

Contributing

Feel free to make a pull request! :octocat:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Clear func()

clear terminal screen

Functions

func Plot

func Plot(series []float64, options ...Option) string

Plot returns ascii graph for a series.

func PlotMany added in v0.5.4

func PlotMany(data [][]float64, options ...Option) string

PlotMany returns ascii graph for multiple series.

Types

type Option added in v0.3.0

type Option interface {
	// contains filtered or unexported methods
}

Option represents a configuration setting.

func Caption added in v0.3.0

func Caption(caption string) Option

Caption sets the graphs caption.

func Height added in v0.3.0

func Height(h int) Option

Height sets the graphs height.

func Offset added in v0.3.0

func Offset(o int) Option

Offset sets the graphs offset.

func Precision added in v0.5.2

func Precision(p uint) Option

Precision sets the graphs precision.

func Width added in v0.3.0

func Width(w int) Option

Width sets the graphs width. By default, the width of the graph is determined by the number of data points. If the value given is a positive number, the data points are interpolated on the x axis. Values <= 0 reset the width to the default value.

Directories

Path Synopsis
cmd
asciigraph command
examples
rainbow command
sine command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL