Creating Pretty Line Graphs Using ASCII Characters

Blake Bradford Avatar

·

Visualizing Data with Rasciigraph: Creating Pretty Line Graphs Using ASCII Characters

Are you looking for a simple and elegant way to visualize your data? Look no further than Rasciigraph, a tiny Rust library that lets you create beautiful line graphs using ASCII characters. In this article, we will explore the usage, examples, and acknowledgements of Rasciigraph, providing you with a comprehensive understanding of this powerful visualization tool.

Usage

To start using Rasciigraph, simply add it as a dependency in your Cargo.toml file:

toml
[dependencies]
rasciigraph = "0.2"

Next, add the following line of code to the top of your source code:

rust
extern crate rasciigraph;

If you prefer to use the Rust 2018 edition, you can use the following import statement instead:

rust
use rasciigraph::{plot, Config};

Examples

Rasciigraph provides a variety of customization options to suit your needs. Let’s take a look at a simple example that plots a single series of data points:

“`rust
extern crate rasciigraph;

use rasciigraph::{plot, Config};

fn main() {
println!(
“{}”,
plot(
vec![
0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0, -0.5, 9.0, -3.0, 0.0, 0.0, 1.0, 2.0, 1.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0, -0.5, 8.0, -3.0, 0.0, 0.0, 1.0,
2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0, -0.5, 10.0, -3.0,
0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0, 0.0, 0.0
],
Config::default()
.with_offset(10)
.with_height(10)
.with_caption(“I’m a doctor, not an engineer.”.to_string())
)
);
}
“`

Running this code will produce the following output:

10.00 ┤ ╭╮
8.70 ┤ ╭╮ ││
7.40 ┼ ││ ╭╮ ││
6.10 ┤ ││ ││ ││
4.80 ┤ ││ ││ ││
3.50 ┤ ││ ││ ││
2.20 ┤ ││ ╭╮ ││ ╭╮ ││ ╭╮
0.90 ┤ ╭╮ ││ ╭╯╰╮ ╭╮ ││ ╭╯╰╮ ╭╮ ││ ╭╯╰╮
-0.40 ┼───╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───
-1.70 ┤ ││ ││ ││
-3.00 ┤ ╰╯ ╰╯ ╰╯
I'm a doctor, not an engineer.

Rasciigraph also supports plotting multiple series of data points. Here’s an example that demonstrates how to plot multiple series using Rasciigraph:

rust
fn main() {
let res = rasciigraph::plot_many(
vec![
vec![0.0f64, 1.0, 0.0],
vec![2.0, 3.0, 4.0, 3.0, 2.0],
vec![4.0, 5.0, 6.0, 7.0, 6.0, 5.0, 4.0],
],
rasciigraph::Config::default().with_width(21),
);
print!("{}", res);
}

Running this code will produce the following output:

7.00 ┤ ╭──╮
6.00 ┤ ╭───╯ ╰───╮
5.00 ┤ ╭──╯ ╰──╮
4.00 ┼─╯ ╭───╮ ╰─
3.00 ┤ ╭──╯ ╰──╮
2.00 ┼─╯ ╰──
1.00 ┤ ╭───╮
0.00 ┼─╯ ╰─

Acknowledgement

Rasciigraph is a Rust port of the popular asciigraph library, originally written by @guptarohit.

Istasciigraph itself is a port of the asciichart library, created by @kroitor.

In conclusion, Rasciigraph is a powerful and easy-to-use library for creating visually appealing line graphs using ASCII characters. Whether you’re a data scientist, developer, or enthusiast, Rasciigraph provides an intuitive way to visualize your data. With its customizable features and straightforward usage, Rasciigraph is a valuable addition to any data visualization toolkit. Now go ahead and give it a try!

If you have any questions or need further assistance, please feel free to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *