Phil Scott 223642b797
Add blog to docs (#484)
* Adding social card infrastructure
* Upgrades doc project to .NET 6
* Adds Playwright
* Changes the console to a web project for Playwright
* Adds social card template
* Added blog content
* Parallelized social image processing
* Updating CI to use .NET 6 for docs build
2021-07-15 19:53:01 +02:00

1.7 KiB

Title: Bar Chart Order: 20 Description: "Use BarChart to render bar charts to the console." Highlights: - Custom colors - Labels - Use your own data with a converter.

Use BarChart to render bar charts to the console.

Usage

Basic usage

AnsiConsole.Render(new BarChart()
    .Width(60)
    .Label("[green bold underline]Number of fruits[/]")
    .CenterLabel()
    .AddItem("Apple", 12, Color.Yellow)
    .AddItem("Orange", 54, Color.Green)
    .AddItem("Banana", 33, Color.Red));

Add items with converter

// Create a list of fruits
var items = new List<(string Label, double Value)>
{
    ("Apple", 12),
    ("Orange", 54),
    ("Banana", 33),
};

// Render bar chart
AnsiConsole.Render(new BarChart()
    .Width(60)
    .Label("[green bold underline]Number of fruits[/]")
    .CenterLabel()
    .AddItems(items, (item) => new BarChartItem(
        item.Label, item.Value, Color.Yellow)));

Add items implementing IBarChartItem

public sealed class Fruit : IBarChartItem
{
    public string Label { get; set; }
    public double Value { get; set; }
    public Color? Color { get; set; }

    public Fruit(string label, double value, Color? color = null)
    {
        Label = label;
        Value = value;
        Color = color;
    }
}

// Create a list of fruits
var items = new List<Fruit>
{
    new Fruit("Apple", 12, Color.Yellow),
    new Fruit("Orange", 54, Color.Red),
    new Fruit("Banana", 33, Color.Green),
};

// Render bar chart
AnsiConsole.Render(new BarChart()
    .Width(60)
    .Label("[green bold underline]Number of fruits[/]")
    .CenterLabel()
    .AddItem(new Fruit("Mango", 3))
    .AddItems(items));