Skip to content
ConsoleMonster

Draw a bar chart from numbers

Draw one bar per value with New-ConsoleChart or BarChart, scaled to a maximum and coloured where it crosses a warning or critical threshold.

A bar chart puts one bar per value beside its label, with the value on the right. Set a maximum to draw each bar as a share of it, and thresholds to colour the bars that cross them.

Install-Module ConsoleMonster -Scope CurrentUser
PowerShellbar-chart.ps1
# Draw a bar per value, coloured where it crosses a warning or critical threshold.
Import-Module -Name ConsoleMonster

# Data: invented disk use per server, in percent of capacity.
$disks = @(
    [pscustomobject] @{ Server = 'fs01'; Used = 62 }
    [pscustomobject] @{ Server = 'fs02'; Used = 81 }
    [pscustomobject] @{ Server = 'sql01'; Used = 93 }
    [pscustomobject] @{ Server = 'web01'; Used = 40 }
    [pscustomobject] @{ Server = 'web02'; Used = 57 }
)

# Layout: Property picks the value, Label the name; Maximum makes each bar a share of 100.
$chartOptions = @{
    Property = 'Used'
    Label = 'Server'
    Unit = '%'
    Maximum = 100
    Warning = 80
    Critical = 90
}
$chart = $disks | New-ConsoleChart @chartOptions

# Behaviour: print once.
$chart | Show-Console
Full program
# Draw a bar per value, coloured where it crosses a warning or critical threshold.
Import-Module -Name ConsoleMonster

# Data: invented disk use per server, in percent of capacity.
$disks = @(
    [pscustomobject] @{ Server = 'fs01'; Used = 62 }
    [pscustomobject] @{ Server = 'fs02'; Used = 81 }
    [pscustomobject] @{ Server = 'sql01'; Used = 93 }
    [pscustomobject] @{ Server = 'web01'; Used = 40 }
    [pscustomobject] @{ Server = 'web02'; Used = 57 }
)

# Layout: Property picks the value, Label the name; Maximum makes each bar a share of 100.
$chartOptions = @{
    Property = 'Used'
    Label = 'Server'
    Unit = '%'
    Maximum = 100
    Warning = 80
    Critical = 90
}
$chart = $disks | New-ConsoleChart @chartOptions

# Behaviour: print once.
$chart | Show-Console

Run: ./bar-chart.ps1

Terminal
dotnet new console -n DiskChart
cd DiskChart
dotnet add package ConsoleMonster
C#Program.cs
// Draw a bar per value, coloured where it crosses a warning or critical threshold.
using ConsoleMonster;
using ConsoleMonster.Widgets;

// Data: invented disk use per server, in percent of capacity.
var disks = new Disk[]
{
    new("fs01", 62),
    new("fs02", 81),
    new("sql01", 93),
    new("web01", 40),
    new("web02", 57)
};

// Layout: one bar per server; Maximum makes each bar a share of 100.
BarChart chart = new BarChart
{
    Unit = "%",
    Maximum = 100,
    Warning = 80,
    Critical = 90
};
foreach (var disk in disks)
    chart.Add(disk.Server, disk.Used);

// Behaviour: print once.
Terminal.Write(chart);

record Disk(string Server, int Used);
Full program
// Draw a bar per value, coloured where it crosses a warning or critical threshold.
using ConsoleMonster;
using ConsoleMonster.Widgets;

// Data: invented disk use per server, in percent of capacity.
var disks = new Disk[]
{
    new("fs01", 62),
    new("fs02", 81),
    new("sql01", 93),
    new("web01", 40),
    new("web02", 57)
};

// Layout: one bar per server; Maximum makes each bar a share of 100.
BarChart chart = new BarChart
{
    Unit = "%",
    Maximum = 100,
    Warning = 80,
    Critical = 90
};
foreach (var disk in disks)
    chart.Add(disk.Server, disk.Used);

// Behaviour: print once.
Terminal.Write(chart);

record Disk(string Server, int Used);

Run: dotnet run

Terminal
dotnet new console -n DiskChart
cd DiskChart
dotnet add package ConsoleMonster
C# fluentProgram.cs
// Draw a bar per value, coloured where it crosses a warning or critical threshold.
using ConsoleMonster;
using ConsoleMonster.Widgets;

// Data: invented disk use per server, in percent of capacity.
var disks = new Disk[]
{
    new("fs01", 62),
    new("fs02", 81),
    new("sql01", 93),
    new("web01", 40),
    new("web02", 57)
};

// Layout: one bar per server; Maximum makes each bar a share of 100.
BarChart chart = Ui.BarChart().Unit("%").Maximum(100).Warning(80).Critical(90);
foreach (var disk in disks)
    chart.Add(disk.Server, disk.Used);

// Behaviour: print once.
chart.Show();

record Disk(string Server, int Used);
Full program
// Draw a bar per value, coloured where it crosses a warning or critical threshold.
using ConsoleMonster;
using ConsoleMonster.Widgets;

// Data: invented disk use per server, in percent of capacity.
var disks = new Disk[]
{
    new("fs01", 62),
    new("fs02", 81),
    new("sql01", 93),
    new("web01", 40),
    new("web02", 57)
};

// Layout: one bar per server; Maximum makes each bar a share of 100.
BarChart chart = Ui.BarChart().Unit("%").Maximum(100).Warning(80).Critical(90);
foreach (var disk in disks)
    chart.Add(disk.Server, disk.Used);

// Behaviour: print once.
chart.Show();

record Disk(string Server, int Used);

Run: dotnet run

Show-Console 80×524-bitFull

Disk use for five invented servers at 80 columns.

  • -Property names the numeric property and -Label the one that names each bar. An ordered hashtable of label = value works too, with -InputObject.
  • Without -Maximum the longest bar fills the width. With it, 100 with -Unit '%' reads as a percentage of capacity.
  • -Warning and -Critical colour a bar amber or red and mark it once its value reaches them, so the problem stands out without a legend or colour.
  • In C#, set Maximum, Unit, Warning and Critical on a BarChart and call Add(label, value) per bar.

Next

Edit this page on GitHub