Skip to content

The charting solution that might not suit you πŸ“Š

License

Notifications You must be signed in to change notification settings

usrme/gobarchar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

53 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

gobarchar

GitHub go.mod Go version test Go Report Card

For whatever reason, I was enamored with Alex Chan's snippet of code where a key-value pair list is turned into a passable bar chart for quick demonstration purposes. When writing a blog post mentioning the number of books I've read I wanted to quickly visualize the values, but didn't want to spend too much time on it. The solution above worked wonders! But I wanted the same thing without having to actually open a terminal (heresy, I know), thus this thing was born: the charting solution that might not suit you πŸ“Š

GoBarChar - animated GIF demo

Try the same link as in the demo above, https://gobarchar.usrme.xyz/?2012=8&2013=6&2014=8&2015=14, or just load the site without any parameters to get random data.

Features

  • Send data and get chart back
  • Perform calculation for average to the nearest integer
  • Perform calculation for sum of all values
  • Sort ascending or descending, or don't sort at all
    • Add the sort query parameter and pass either asc or desc as the value
    • The default is to keep the rows ordered as the query parameters are
  • Replace %20 HTML entity with a space
    • Add the spaces query parameter and pass yes as the value
      • Any other value will be disregarded
    • The default is to not do any replacing
  • Display a custom title for the chart to make sharing charts better
    • Add the title query parameter and any value you want
      • For curl requests the value needs to be HTML encoded
        • Something like the following could be used to automate this: curl "http://localhost:8080/?February=5&March=20&title=$(echo 'Title with spaces in it' | python3 -c 'import urllib.parse,sys; print(urllib.parse.quote(sys.stdin.read().strip()))')"
        • Alternatively, after rendering the chart with the title in the browser, just copy the generated link from the page

Usage

On the Fly

(Excuse the pun). Instead of installing the thing, you can also access the same functionality using the instance hosted on Fly at https://gobarchar.usrme.xyz/:

$ curl https://gobarchar.usrme.xyz/
April        7 β–ˆβ–Š
February    88 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–
June        16 β–ˆβ–ˆβ–ˆβ–ˆ
March       99 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
October     19 β–ˆβ–ˆβ–ˆβ–ˆβ–Š
September   98 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹
Avg.        55 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰
Total      327 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ

Loading the page without any query parameters randomly chooses some data and will do so upon every reload; a link will be presented at the bottom to link to the data. The site should always be available for usage, but if it's not then do open up an issue and I'll see what I can do.

Local

After installation, execute gobarchar, which by default starts a web server listening on port 8080, though you can specify a different port through the PORT environment variable. Once the web server is running, you can use something like curl to perform requests:

$ gobarchar &
[1] 32357
2024/07/26 12:23:13 listening on: 8080
$ curl localhost:8080
2024/07/26 12:23:16 completed in: 87.958Β΅s
August        32 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
February      50 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
July           7 β–ˆβ–ˆβ–ˆβ–Œ
March          6 β–ˆβ–ˆβ–ˆ
November      44 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
September      8 β–ˆβ–ˆβ–ˆβ–ˆ
Avg.       24.50 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž
Total        147 β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ

If no query parameters are provided then random data is generated. The "completed in" log line isn't a part of the chart output, but rather the result of using job control and having the standard output of a background process being interleaved with the standard output of a foreground process.

Installation

  • using go install:
go install github.com/usrme/gobarchar/cmd/...@latest
  • download a binary from the releases page

  • build it yourself (requires Go 1.17+):

git clone https://github.com/usrme/gobarchar.git
cd gobarchar
go build -o gobarchar cmd/gobarchar/main.go

Removal

rm -f "${GOPATH}/bin/gobarchar"
rm -rf "${GOPATH}/pkg/mod/github.com/usrme/gobarchar*"

Acknowledgments

Heavily inspired by the 'Drawing ASCII bar charts' blog post by Alex Chan. If there was any prior art that pretty much does the same thing (present an ASCII chart based on query parameters or request payload), then I honestly wasn't aware of it and just created this for fun.

License

MIT