David's Blog

Visualize Data with Asciigraph in Golang

By David Li on 2023-04-26T15:32:49.017Z

Visualize Data with Asciigraph in Golang

Asciigraph is a lightweight and versatile library for creating ASCII line graph visualizations in Golang. This library is perfect for creating simple visualizations in the terminal or plain text environments. In this article, we will explore how to create and customize line graphs using Asciigraph in Golang.

Prerequisites

To follow along, you should have a basic understanding of Golang and be familiar with the process of installing packages using go get. If you need a refresher, check out the official Golang website.

Installing Asciigraph

To get started, install the Asciigraph library using the following command:

go get -u github.com/guptarohit/asciigraph

Once the package is installed, you can import it into your project like this:

import "github.com/guptarohit/asciigraph"

Creating a Basic Line Graph

Creating a line graph with Asciigraph is simple. First, you need a slice of float64 values that represent the data points to be plotted. Then, you can call the asciigraph.Plot() function to generate the graph.

Here’s a basic example:

package main

import (
	"fmt"

	"github.com/guptarohit/asciigraph"
)

func main() {
	data := []float64{3, 4, 9, 6, 2, 4, 5, 8, 5}

	graph := asciigraph.Plot(data)

	fmt.Println(graph)
}

This code snippet will output the following graph:

  9.00 ┤  ╭─╮    
  8.00 ┤  �? �?    
  7.00 ┤  �? �?    
  6.00 ┤ ╭╯ �?    
  5.00 ┤ �?  ╰╮   
  4.00 ┤╭╯   ╰╮  
  3.00 ┼╯      ╰ 

Customizing the Graph

Asciigraph provides several options for customizing the appearance of your graph. You can adjust the height, width, caption, and more by passing an asciigraph.Config struct to the asciigraph.Plot() function.

Here’s an example that demonstrates some of the available options:

package main

import (
	"fmt"

	"github.com/guptarohit/asciigraph"
)

func main() {
	data := []float64{3, 4, 9, 6, 2, 4, 5, 8, 5}

	config := asciigraph.Config{
		Height:  10,
		Width:   50,
		Caption: "Sample Graph",
	}

	graph := asciigraph.Plot(data, config)

	fmt.Println(graph)
}

This will output a graph with a custom height, width, and caption:

Sample Graph
  9.00 ┤       ╭─╮
  8.00 ┤       �? �?
  7.00 ┤       �? �?
  6.00 ┤     ╭─╯ �?
  5.00 ┤     �?   ╰─╮
  4.00 ┤   ╭─╯     ╰─╮
  3.00 ┼─╭─╯         ╰

Conclusion

Asciigraph offers a simple and effective way to create ASCII line graphs in Golang. With just a few lines of code, you can visualize data in the terminal or other plain text environments. This library is particularly useful for developers who need to quickly analyze data without the overhead of more complex visualization tools.

To learn more about Asciigraph and explore additional customization options, visit the official Asciigraph GitHub repository.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-12-19.