DEV Community

luthfisauqi17
luthfisauqi17

Posted on

Visualize Data in 5 Lines of Code with Golang and Asciigraph

Data visualization doesn't always have to happen in a complex web dashboard or a heavy GUI application. Sometimes, when you are building CLI tools or monitoring system metrics, you want a quick and lightweight way to see trends directly in your terminal.

In this post, we will explore a very interesting Go library called Asciigraph. This library allows you to create beautiful ASCII line graphs with minimal effort.

Why Asciigraph?

Asciigraph is perfect for developers who want to:

  • Visualize data without leaving the terminal.
  • Add graphical monitoring to CLI applications.
  • Keep their tools lightweight and dependency-free from heavy plotting libraries.

Prerequisites

Before we dive in, make sure you have Go installed on your machine. We will start by setting up a new project directory.

mkdir golang-asciigraph
cd golang-asciigraph
go mod init golang-graph
Enter fullscreen mode Exit fullscreen mode

Installation

To get started, you need to install the library by running the following command:

go get github.com/guptarohit/asciigraph
Enter fullscreen mode Exit fullscreen mode

Creating Your First Graph

Let’s write a simple program to plot a basic set of data. Create a file named main.go and add the following code:

package main

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

func main() {
    // Define your data series
    data := []float64{3, 4, 9, 6, 2, 4, 5, 8, 5, 10, 2, 7, 2, 5, 6}

    // Generate the graph
    graph := asciigraph.Plot(data)

    // Print the output to the terminal
    fmt.Println(graph)
}
Enter fullscreen mode Exit fullscreen mode

Running the Code

Execute the program using the command:

go run main.go
Enter fullscreen mode Exit fullscreen mode

You will see a clean line graph rendered entirely in ASCII characters, following the peaks and valleys of your data points.

Use Case Ideas

The possibilities with this library are endless. Here are a few ideas on how you could implement it:

  1. Network Monitoring: Visualize ping response times in real-time.
  2. System Metrics: Monitor CPU or RAM usage directly from a terminal dashboard.
  3. Crypto/Stock Tracker: Build a small CLI tool to watch price fluctuations.

Conclusion

This is just the beginning. Asciigraph also supports advanced configurations like setting the graph height, width, and adding captions. In the next part of this series, I will show you more advanced techniques to make your terminal graphs even more dynamic.

If you have any interesting ideas on how to use this library, feel free to share them!


Special thanks to Rohit Gupta for the incredible asciigraph library. You can find the source code here: https://github.com/guptarohit/asciigraph

Top comments (0)