Creating ASCII Line Charts in Elixir with asciichart
Are you looking for a simple and lightweight way to create line charts in Elixir? Look no further – the asciichart library is here to help. In this article, we will walk you through the process of installing the library, plotting data, customizing chart settings, and changing character sets. We will also provide information on versioning and licensing.
Installation
To get started with asciichart, you need to add it as a dependency in your mix.exs
file:
elixir
def deps do
[{:asciichart, "~> <version>"}]
end
Make sure to replace <version>
with the desired version of the library.
Plotting Data
Once you have the library installed, you can start plotting data. The plot
function takes an array of numbers and returns a chart object. Here’s an example:
elixir
{:ok, chart} = Asciichart.plot([1, 2, 3, 3, 2, 1])
chart |> IO.puts()
The output will be an ASCII representation of the line chart:
3.00 ┤ ╭─╮
2.00 ┤╭╯ ╰╮
1.00 ┼╯ ╰
Customizing Chart Settings
You can customize the appearance of the chart by passing various settings to the plot
function. Some of the available settings include:
-
offset
: Adjusts the left offset of the chart. -
height
: Adjusts the height of the chart. -
padding
: Sets the left padding for the labels. -
charset
: Customizes the character set used for the chart. -
precision
: Sets the number of fractional digits to display for floating-point values.
Here’s an example that demonstrates using some of these settings:
elixir
{:ok, chart} = Asciichart.plot([1, 2, 5, 5, 4, 3, 2, 100, 0], height: 3, offset: 10, padding: "__")
chart |> IO.puts()
The output will be a customized chart with a modified offset, height, and padding.
Changing Character Sets
If you want to change the character set used to render the chart, you can use a predefined one from the Asciichart.Charset
module or create your own. Here’s an example:
elixir
{:ok, chart} = Asciichart.plot([1, 2, 5, 5, 4, 3, 2, 0], height: 5, charset: Asciichart.Charset.single_char("*"))
chart |> IO.puts()
The output will be a chart with a different character set, represented by asterisks.
Versioning and License
The asciichart library follows the Semantic Versioning standard for versioning. Make sure to check the documentation or release notes for any breaking or backward-incompatible changes between versions.
This library is licensed under the Apache License, Version 2.0. The full text of the license can be found in the LICENSE file in the repository.
Conclusion
In this article, we have explored how to create ASCII line charts in Elixir using the asciichart library. We have covered installation, data plotting, chart customization, changing character sets, versioning, and licensing. With the knowledge gained from this article, you will be able to visualize data in a simple and visually appealing way.
If you have any questions or need further assistance, please feel free to ask. Happy charting!
Leave a Reply