Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.

Overview

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 ┤   ╰╯    ╰╯╰╯

Command line interface

This package also brings a small utility for command line usage. Assuming $GOPATH/bin is in your $PATH, simply go get it then install CLI.

CLI Installation

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

or download binaries from the releases page.

Feed it data points via stdin:

$ seq 1 72 | asciigraph -h 10 -c "plot data from stdin"
  72.00 ┼
  65.55 ┤                                                                  ╭────
  59.09 ┤                                                           ╭──────╯
  52.64 ┤                                                    ╭──────╯
  46.18 ┤                                             ╭──────╯
  39.73 ┤                                      ╭──────╯
  33.27 ┤                              ╭───────╯
  26.82 ┤                       ╭──────╯
  20.36 ┤                ╭──────╯
  13.91 ┤         ╭──────╯
   7.45 ┤  ╭──────╯
   1.00 ┼──╯
           plot data from stdin

Realtime graph for data points 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:

Issues
  • Ansi colors

    Ansi colors

    Colors! This branch also includes my changes in #34 since colors are particularly useful for differentiating between different series in the same plot. AnsiRainbow

    opened by NathanBaulch 10
  • Remove append()

    Remove append()

    This added a bit of performance

    BenchmarkPlotOld 2058 557990 ns/op 213007 B/op 1200 allocs/op BenchmarkPlotNew 3272 367223 ns/op 105121 B/op 532 allocs/op

    opened by sinkevichmm 3
  • Panic when no positive values provided

    Panic when no positive values provided

    When no positive floats are provided to Plot(), a panic is observed:

    Examples:

    package main
    
    import "github.com/guptarohit/asciigraph"
    
    func main() {
    	println(asciigraph.Plot([]float64{0}))
    }
    
    package main
    
    import "github.com/guptarohit/asciigraph"
    
    func main() {
    	println(asciigraph.Plot([]float64{-1}))
    }
    

    Result:

    PS C:\Users\Lee\test> go run .\main.go
    panic: runtime error: index out of range
    
    goroutine 1 [running]:
    github.com/guptarohit/asciigraph.Plot(0xc00007df78, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0)
            C:/Users/Lee/go/pkg/mod/github.com/guptarohit/[email protected]/asciigraph.go:92 +0x13aa
    main.main()
            C:/Users/Lee/test/main.go:6 +0x67
    exit status 2
    

    Line 92 is as follows:

    plot[rows-y0][config.Offset-1] = "┼" // first value
    
    opened by 0x4c6565 3
  • fixed the BADPREC issue when both the maximum and minimum values in a series are 0

    fixed the BADPREC issue when both the maximum and minimum values in a series are 0

    Currently, if each value of a series is 0, here are the test code, this PR can fix the issue and avoid if the series's value is always 0.

    package main
    
    import (
        "fmt"
        "github.com/guptarohit/asciigraph"
    )
    
    func main() {
        data := []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        graph := asciigraph.Plot(data)
    
        fmt.Println(graph)
    }
    

    Output:

    %!(BADPREC)            0.000000 ┼──────────
    
    		                                  
    		                                    
    	                                    
    opened by JeffreySE 3
  • Prevent panics when data is flat

    Prevent panics when data is flat

    This avoids division by 0 that occurs when the incoming data is all one value. Instead of panicing, Plot should now plot flat data as expected, in a flat line.

    opened by xordspar0 3
  • Axis cross (┼) is in the wrong place at some scales

    Axis cross (┼) is in the wrong place at some scales

    For some inputs and at certain scales, the axis cross doesn't line up with the plotted data: screen shot 2019-02-02 at 2 37 29 pm

    I can reproduce this with the following inputs:

    echo '3084
    3056
    3047
    3043
    3041
    3043
    3043
    3036
    3033
    3042
    3042
    3040
    3032
    3039
    3018
    2925
    2926
    2925
    2923
    2922
    2924
    2920
    2910
    2906
    2907
    2908
    2908
    2908
    2908
    2917
    2914
    2911
    2914
    2909
    2913
    2910
    2914
    2915
    2894
    2915
    2913
    2915
    2907
    2908
    2916
    2911
    2910
    2912
    2903
    2914
    2911
    2905
    2903
    2901
    2910
    2899
    2901
    2901
    2902
    2909
    2904
    2908
    2893
    2901
    2903
    2903
    2908
    2910
    2900
    2898
    2903
    2895' | asciigraph -h 24
    
    opened by xordspar0 2
  • Plot multiple series together

    Plot multiple series together

    Display multiple series in the same plot, just like the original ported project supports.

     20 ┤                               ╭───────╭╮───────╮
     19 ┤                        ╭──╭───╭───────╭╮───────╮───╮──╮
     18 ┤                    ╭─╭──╭─╭───╭───────╭╮───────╮───╮─╮──╮─╮
     17 ┤                 ╭─╭─╭─╭─╭──╭──────────╯╰──────────╮──╮─╮─╮─╮─╮
     16 ┤              ╭─╭─╭╭─╭─╭────╯                      ╰────╮─╮─╮╮─╮─╮
     15 ┤            ╭╭─╭─╭╭─╭──╯                                ╰──╮─╮╮─╮─╮╮
     14 ┤          ╭╭─╭╭─╭╭──╯                                      ╰──╮╮─╮╮─╮╮
     13 ┤        ╭─╭╭╭─╭╭─╯                                            ╰─╮╮─╮╮╮─╮
     12 ┤       ╭╭╭─╭╭╭─╯                                                ╰─╮╮╮─╮╮╮
     11 ┤     ╭─╭╭╭╭╭─╯                                                    ╰─╮╮╮╮╮─╮
     10 ┤    ╭╭─╭╭╭╭╯                                                        ╰╮╮╮╮─╮╮
      9 ┤   ╭╭╯╭╭╭╭╯                                                          ╰╮╮╮╮╰╮╮
      8 ┤  ╭╭╯╭╭╭╭╯                                                            ╰╮╮╮╮╰╮╮
      7 ┤  ││╭╭╭╭╯                                                              ╰╮╮╮╮││
      6 ┤ ╭╭╭╭╭╭╯                                                                ╰╮╮╮╮╮╮
      5 ┤ ││││││                                                                  ││││││
      4 ┤╭╭╭╭╭╭╯                                                                  ╰╮╮╮╮╮╮
      3 ┤││││││                                                                    ││││││
      2 ┤││││││                                                                    ││││││
      1 ┤││││││                                                                    ││││││
      0 ┼╶╶╶╶╶╯                                                                    ╰╴╴╴╴╴
    
    opened by NathanBaulch 1
  • Remove append

    Remove append

    This added a bit of performance

    BenchmarkPlotOld 2058 557990 ns/op 213007 B/op 1200 allocs/op BenchmarkPlotNew 3272 367223 ns/op 105121 B/op 532 allocs/op

    opened by sinkevichmm 1
  • NaN numbers

    NaN numbers

    It would be great it the library can accept NaN float64 values. It can simply create a discontinuity in the the graph. Currently I have to do a validation to prevent the package from panicking.

    opened by pjebs 1
  • Can I plot bar graphs?

    Can I plot bar graphs?

    I would like to know if I could use this library for plotting bar graphs. I could not find any examples that show bar plot using this library.

    Thanks.

    opened by n-is 1
  • Panics if data is flat

    Panics if data is flat

    If each data point in the input is the same, asciigraph panics. This can be demonstrated simply using the command line interface:

    > echo 1 1 | asciigraph
    panic: runtime error: index out of range
    
    goroutine 1 [running]:
    github.com/guptarohit/asciigraph.Plot(0xc420057c18, 0x2, 0x40, 0xc420057eb8, 0x4, 0x4, 0x17, 0x115f6c0)
            /home/me/go/src/github.com/guptarohit/asciigraph/asciigraph.go:92 +0x1525
    main.main()
            /home/me/go/src/github.com/guptarohit/asciigraph/cmd/asciigraph/main.go:56 +0x5e0
    

    I've also written a test that demonstrates it: https://github.com/xordspar0/asciigraph/commit/f02e7a51539d4db8dff95971007e1b208e30ff41

    opened by xordspar0 1
  • Confused about the Offset option

    Confused about the Offset option

    I'm a bit confused about the meaning of the Offset option.

    for i := 0; i < 10; i++ {
    	println("offset", i, ":", Plot([]float64{0}, Offset(i)))
    }
    
    offset 0 :  0.00 ┼
    offset 1 : ┼
    offset 2 :  0.00┼
    offset 3 :  0.00 ┼
    offset 4 :  0.00  ┼
    offset 5 :  0.00   ┼
    offset 6 :   0.00   ┼
    offset 7 :    0.00   ┼
    offset 8 :     0.00   ┼
    offset 9 :      0.00   ┼
    

    This is pretty odd behavior that raises a few questions. Is the purpose of this option to add space before or after the y-axis label? It seems to do a bit of both in an inconsistent way.

    opened by NathanBaulch 2
  • Print X-Axis values

    Print X-Axis values

    Hi there, pretty awesome lib!

    How complex would it be to add an option to print the x-axis in addition to the y-axis? I want to use your library in my fan2go project to print fan curve data to console, and it would be very helpful for users to see the x-axis values (0-255).

    Thx!

    opened by markusressel 1
  • Code reformat

    Code reformat

    Following PR fixes issue mentioned in #18. I implemented Graph struct with it's methods and implemented already written Plot() function to use Graph struct

    opened by bnMikheili 2
  • Reformat Code

    Reformat Code

    It would be nice if instead of having only one function that just generates a string, we had a struct named 'struct graph' for example, that had this method, configurations and other utilities inside. Subsequently, it would be easier to change implementations of other methods without changing the interface.

    Besides that, we can add other methods, e.g. one could be to plot several graphs together or show statistics. However, (from my standpoint) it is essential to rewrite code so that it supports structure which will implement methods. In addition, it would encourage other users to write code for this project and making contributions much easier.

    DISCLAIMER: I don't mean to delete old code, because other projects might be using it, so I will just extend it to support classes.

    opened by CSenshi 4
  • Add Min and Max options

    Add Min and Max options

    This PR adds a couple of options to set the min and max values of the Y axis. I needed them for a personal project and figured it might be useful to other people.

    opened by carlosms 13
Releases(v0.5.5)
Owner
Rohit Gupta
Rohit Gupta
cli is a simple, fast, and fun package for building command line apps in Go.

cli cli is a simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable comm

凹语言 1 Jan 8, 2022
This repository contains utility functions that do not make sense in other packages.

Installation go get github.com/IQ-tech/go-utils Executing code before process exits AtInterruption receives a function that will be called once befor

IQ Tech 0 Dec 9, 2021
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Tom Wright 3.2k May 17, 2022
OTF font with vertical bars for one-line ASCII spectrum analyzers, graphs, etc

graph-bars-font OTF font with vertical bars for one-line ASCII spectrum analyzers, graphs, etc. I didn't find anything similar on the net so I decided

Andrew Dunai 16 Apr 16, 2022
Start Go command line apps with ease

Start Start Go command line apps with ease Executive Summary The start package for Go provides two basic features for command line applications: Read

Christoph Berger 15 Oct 29, 2021
A simple library to build golang command line (cli / cmd)apps

A simple library to build golang command line (cli / cmd)apps

seastart dev team 0 Jan 11, 2022
Command-line program to download videos from YouTube.com and other video sites

youtube-dl - download videos from youtube.com or other video platforms INSTALLATION DESCRIPTION OPTIONS CONFIGURATION OUTPUT TEMPLATE FORMAT SELECTION

youtube-dl 110k May 16, 2022
Bofin - A command line tool that can be used by to make Weblink development more productive

Bofin A command line tool that can be used by to make Weblink development more p

Gavin Bannerman 0 Jan 13, 2022
ASCII table in golang

ASCII Table Writer Generate ASCII table on the fly ... Installation is simple as go get github.com/olekukonko/tablewriter Features Automatic Padding

Oleku Konko 3.4k May 12, 2022
Generate ANSI-/Ascii-art version images/Gifs in your terminal.

ANSI-Art NOTE: This toy project is not yet finished. ANSI-version Logo Block ANSI-version Logo ASCII-version Logo Support Platform You are kindly remi

Weiran Huang 41 Apr 10, 2022
Browser based Ascii-art generator with simple web design

Browser based Ascii-art generator with simple web design

null 0 Nov 12, 2021
Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.

Sensible and fast command-line flag parsing with excellent support for subcommands and positional values. Flags can be at any position. Flaggy has no

Eric Greer 792 May 11, 2022
Fonts is a package that provides helpers to access font details and easily retrive font bytes with ZERO dependencies

Fonts Fonts is a package that provides helpers to access font details and easily retrieve font bytes. This package has ZERO 3rd-party dependencies. Fo

Go Swiss 10 Mar 3, 2022
Clones dependencies from .resolved file of Swift Package Manager.

SPM-dep-cloner Clones dependencies from .resolved file of Swift Package Manager. Useful for setup of new project with dependencies in another repos. H

Artyom Andreev 0 Nov 29, 2021
Package command provide simple API to create modern command-line interface

Package command Package command provide simple API to create modern command-line interface, mainly for lightweight usage, inspired by cobra Usage pack

chenen 0 Jan 16, 2022
An open-source GitLab command line tool bringing GitLab's cool features to your command line

GLab is an open source GitLab CLI tool bringing GitLab to your terminal next to where you are already working with git and your code without switching

Clement Sam 1.9k May 14, 2022
A command line tool that builds and (re)starts your web application everytime you save a Go or template fileA command line tool that builds and (re)starts your web application everytime you save a Go or template file

# Fresh Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file. If the web framework yo

null 0 Nov 22, 2021
A command line tool to prompt for a value to be included in another command line.

readval is a command line tool which is designed for one specific purpose—to prompt for a value to be included in another command line. readval prints

Venky 0 Dec 22, 2021
An os/exec like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustments

dockerexec An "os/exec" like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustmen

Segev Finer 1 Feb 1, 2022