Convert PowerShell output into a pie chart

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?

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!

2+
avatar
Share
20 Comments
  1. Nagi M Mickael 1 year ago

    wonderful post , thank you 

    0

  2. Christian Lindenberg 12 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

    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+
    avatar
  4. Matthew 5 months ago

    Awesome function, Graham!  Thanks very much for writing this.  I implemented it with a quick modification to the section on displaying to the screen.   Your original code did not change the window size when the user specified a different chart width/height, but this modification does.

    1+
    avatar
  5. Brooke 4 months ago

    I have a table in variable $aa with two columns...

    Column A | Column B

    Client1 | 11

    Client2 | 20

    Client3 | 2

    (Both columns show up as type strings in get-member)

    I use the function like $aa | Out-PieChart -PieChartTitle "Top Clients and Quantity" -DisplayToScreen

    I get the error "Cannot Convert Data"

     

    Is that because it cannot convert Column2 into an integer?  How do I get around that?

    0

    • I dont think it can work with two string objects. Even the example posted by Graham few comments above gives the same error: Cannot convert Data. If you check the function code, there is a block which is determining a property that is numeric, by trying to Double it. 

       

      0

    • Sorry the block got above the text 🙂

      0

  6. Author

    I had to update the code a little while ago, discovered a few bugs.

    Find the updated code here, https://github.com/Graham-Beer/PSCharts/blob/master/Out-PieChart.ps1

    0

  7. Author

    The issue appears to be the first column needs to be called "Name".
    The part of the code processing this are lines 85 to 95:

    So just call your first column "name" and it works.

    Hope this helps.

    0

  8. Neha 4 months ago

    can you please tell me why isnt working

    $pass = 33
    $fail = 60

    @'
    Name, amount
    Client1, '$pass'
    Client2, '$fail'
    '@ | ConvertFrom-Csv |  select Name, amount | Out-PieChart  -PieChartTitle "Custom PS Object" -DisplayToScreen

     

    Error: Unable to automatically determine properties to graph.

    and i can not fix values, need in a variables

    1+
    avatar
    • $pass = 33
      $fail = 60

      @'
      Name, amount
      Client1, {0}
      Client2, {1}
      '@ -f $pass,$fail

      Its because the @@ is just a text field. It does not evaluate variables. You have to use text formatting to replace the variables with values.

      0

  9. Author

    Make sure you are using double quotes, ", to allow the variable to be expanded.

    i.e. 

    $pass = 40

    Write-Output '$pass'
    $pass

    Write-Output "$pass"

    40

    0

  10. Hi Graham,

    while this is correct for similar commands (where you dont even need to put any quotes), but it does not work if you define the string like

    @'

    '@

    There you need to use the formatting. However it works if the field is defined as

    @"

    "@

    L

    0

  11. Author

    Hi Leos

    The here-string does require double quotes to expand the variable. Try this:

    $pass = 40

    @'

    $pass

    '@

     

    @"

    $pass

    "@

     

    You'll see that the single quotes returns '$pass' while the double quotes returns "40".

    0

    • Yes I agree on that 🙂 If single quote is used (for example because you need to put something with " inside the string) it can be handled with the formatting system.

      0

  12. Author

    Or you can just do this:

    @"

    '$pass'

    "@

    '40'

    Which will work fine 🙂

    0

    • Right. I remember now why I needed the formatting. I needed to create a text field and output that to a ps1 file. The file required to have a variable $result and the value was from another variable earlier in the script:

      if I would put " quotes in the string, it would also try to enumerate $result variable which I needed to have as a text (to generate another script). 

      This is why I got it fixed in my head 🙂

      0

  13. Hey Graham beer! Any plans to make it work on PowerShell 7? Great function!

    0

  14. Does anyone know what properties to set to ensure a Pie or Doughnut chart starts plotting at 0 degrees/12 o'clock?

    0

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