Creating ASCII Charts in C# with asciichart-sharp

Blake Bradford Avatar

·

Article

Are you looking for a way to visually represent your data in a text-based format? Look no further than asciichart-sharp, a C# port of the popular asciichart library. In this article, we will explore the features of asciichart-sharp and learn how to use it to create beautiful ASCII art charts.

What is asciichart-sharp?

asciichart-sharp is a C# library that allows you to generate ASCII art charts in your console applications. It provides an easy-to-use API for plotting numerical data in a text-based format. Whether you are monitoring sensor readings, analyzing financial data, or simply adding a visual element to your reports, asciichart-sharp can help you create eye-catching charts with minimal effort.

Usage

To get started with asciichart-sharp, you’ll need to include the library in your C# project. You can do this by either downloading the source code and adding it to your project, or by using a package manager like NuGet to install the library.

Once you have the library set up, you can start creating ASCII art charts. The basic usage of asciichart-sharp involves creating an array of data points and passing it to the AsciiChart.Plot method. For example, consider the following code snippet:

var series = new double[100];
for (var i = 0; i < series.Length; i++)
{
series[i] = 15 * Math.Sin(i * ((Math.PI * 4) / series.Length));
}

Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine(AsciiChart.Plot(series));

This code will generate a sine wave chart with the specified data points. The Console.OutputEncoding = Encoding.UTF8 statement is necessary to ensure proper rendering of Unicode characters used in the chart.

Customization Options

asciichart-sharp provides various customization options to tailor the appearance of your charts. For example, you can adjust the axis label margins, chart height, fill character, and axis label format. All these options can be passed as parameters to the AsciiChart.Plot method. Here’s an example that demonstrates some of these options:

// same series as above
Console.WriteLine(AsciiChart.Plot(series, new Options
{
AxisLabelLeftMargin = 3,
AxisLabelRightMargin = 0,
Height = 4,
Fill = '·',
AxisLabelFormat = "0,000.000",
}));

By tweaking these options, you can create charts that best suit your needs and ensure they fit within your console window.

Auto Label Width

Another useful feature of asciichart-sharp is its ability to automatically size the chart to fit the largest visible label. This can be helpful when dealing with large data sets or when you want to maximize the chart’s visibility. Here’s an example that demonstrates this feature:

var series2 = new double[100];
for (var i = 0; i < series.Length; i++)
{
series2[i] = (200000 * Math.Cos(i * ((Math.PI * 8) / series.Length))) + 1000000;
}

Console.WriteLine(AsciiChart.Plot(series2, new Options{Height = 10}));

In this example, the chart is automatically adjusted to accommodate the larger data range, resulting in a chart with a higher level of detail.

Conclusion

asciichart-sharp is a powerful library that simplifies the process of creating ASCII art charts in C#. In this article, we explored its usage and customization options, enabling you to transform your numerical data into visually appealing charts. Whether you are a data analyst, software developer, or hobbyist, asciichart-sharp offers a fun and creative way to present your data.

Now it’s your turn to dive into the world of ASCII art charts with asciichart-sharp. Start experimenting with different data sets and customization options to create stunning visual representations. Happy charting!

References

Leave a Reply

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