The idea to display information as a pie chart came about when I needed to write a summary email about a task I had done with PowerShell. I had the results captured in a CSV file and was about to hit send, when I thought: how quick and easy would it be to interpret this information as a visual chart?

Graham Beer

Graham is an experienced system engineer with a passion for PowerShell and automation. You can follow his blog or reach him on Twitter at @GKBeer.

Opening a CSV file, going to the bottom to look at the totals of successful and failed or enabled and disabled settings might only take a minute or so. But how much easier is it to view this information as a pie chart?

So I had the idea, how could I do this in PowerShell? Working with PowerShell 5.1, I have the full capabilities of .NET, so you can be sure someone has done this already, be it in C#, VB.NET, or even PowerShell. Initially I wanted to create a solution in Windows Presentation Foundation (WPF), but this wasn't easy. However, I found a way using the Winforms assembly System.Windows.Forms.DataVisualization.

This class exposes all of the properties, methods and events of the Chart Windows control.

This is the first line in the MSDN documentation.

Constructing the pie chart ^

To start building the chart, I needed to use the namespace System.Windows.Forms.DataVisualization.Charting. The MSDN documentation provides this information. To start working with the assembly, we need to add it to the session with the Add-Type cmdlet:

The simplest way to know which properties you can work with is to use Get-Member. Initialize a new instance of the Chart class and pipe it to Get-Member using the -MemberType parameter to look at the properties:

By going through the properties and cross-referencing them against the MSDN documentation, you can build up what you need to create the framework of the pie chart. Here are the steps to build the pie chart:

  1. Set up the initial framework.
  2. Format the chart, i.e., font and alignment.
  3. Create the chart area.
  4. Define the chart area.
  5. Style the chart.
  6. Build the function.

The function's Begin block will contain the pie chart's construction. The Process block serves to collate and format the data for the pie chart to use. Within the Process block, there are two If statements: one for collecting the numeric values passed to the function and one for the other labels.

To correctly discern whether the property is numeric, the function parses each property through the double type keyword and if so, captures the property name to a ValueProperty. For the labels, it parses each property and checks whether it has a name property. If so, it sets a LabelProperty variable.

It populates the two array lists by using the InputObject, which is dot notation with the ValueProperty and LabelProperty variables. The function has an option to set these values manually, or you can let the function work it out automatically.

The End block of the function populates the data-bind points of X and Y to the pie chart we constructed in the Begin block. Two parameter switches are variable, either to save the pie chart or display it to the screen.

Using the function to create pie charts ^

Now that we have our Out-PieChart function, how do we use it? The function relies on correctly formatting the data passed. This means passing properties with values and labels meant to work together.

Time for an example. I'm collecting the top five processes running:

Top five windows processes running

Top five windows processes running

We could also use the function's 3D switch to save the status of services on the machine to a file:

 

Service status

Service status

Summary ^

The function can take all sorts of information to display it as a pie chart, and even data you have collated in CSV files. Taking data and quickly displaying it as a pie chart aids readability by generating a clear visual. I've added the function below, and the code is up on my GitHub for you to use.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

1+
Share
4 Comments
  1. Nagi M Mickael 9 months ago

    wonderful post , thank you 

    0

  2. Christian Lindenberg 6 months ago

    Hi Graham,

    that really Looks awesome! This is exactly what I was searching for. But i don't understand your function. 🙁

    What do I have to do in order to Output a custom object to a pie Chart? 

    $obj = New-Object PSObject
    Add-Member -InputObject $obj -MemberType NoteProperty -Name "Property1" -Value "Value1"
    Add-Member -InputObject $obj -MemberType NoteProperty -Name "Property2" -Value "Value2"

    $obj | Out-PieChart -PieChartTitle "Custom PS Object" -DisplayToScreen

    --> I get this error every time: "Unable to automatically determine properties to graph"

    Thank you very much!

    Christian

    0

  3. Author
    Graham Beer 5 months ago

    Hi Christian

    The problem you have is what you are trying to convert,

    Property1 Property2
    --------- ---------
    Value1 Value2

    There are no values.

    Take a look at something like this for an example:

    @'
    Value1, 200
    Value2, 300
    '@ | ConvertFrom-Csv -Header Property1, Property2 |
    Out-PieChart -LabelProperty Property1 -ValueProperty Property2 -PieChartTitle "Custom PS Object" -DisplayToScreen

    Note how I can provide my own label with "LabelProperty" and the "ValueProperty". So the "Property1" has two labels of Value1 and Value2, with corresponding values of 200 and 300.
    Have a play with this and give me a shout if I can help further.

    Thanks

    Graham

    1+

    Users who have LIKED this comment:

    • avatar

Leave a reply

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

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account