An open API service indexing awesome lists of open source software.

https://github.com/usrme/gobarchar

The charting solution that might not suit you ๐Ÿ“Š
https://github.com/usrme/gobarchar

bar-chart chart go golang graph

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# gobarchar

![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/usrme/gobarchar)
[![test](https://github.com/usrme/gobarchar/actions/workflows/test.yml/badge.svg)](https://github.com/usrme/gobarchar/actions/workflows/test.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/usrme/gobarchar)](https://goreportcard.com/report/github.com/usrme/gobarchar)

For whatever reason, I was enamored with [Alex Chan's snippet of code](https://alexwlchan.net/2018/ascii-bar-charts/) 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](https://usrme.xyz/posts/glee-in-2023/#books-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](examples/demo.gif)

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](https://fly.io/) at https://gobarchar.usrme.xyz/:

```console
$ 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](#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:

```console
$ 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](https://jvns.ca/blog/2024/07/03/reasons-to-use-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`:

```bash
go install github.com/usrme/gobarchar/cmd/...@latest
```

- download a binary from the [releases](https://github.com/usrme/gobarchar/releases) page

- build it yourself (requires Go 1.17+):

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

## Removal

```bash
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](https://alexwlchan.net/2018/ascii-bar-charts/) 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](/LICENSE)