As system admins, we are used to digging through heaps of searching, selecting, sorting, filtering, until we got what we were looking for. My point is, while you might appreciate nicely presented, If we are providing the technical details in paragraph format will usually get bored and things they don’t understand, this what we will do most of the time. Most of the management peoples like simple and colorful representations like charts. Basically, it will be easily understandable without reading long texts.

Microsoft Chart Controls (MCCs) is a great tool to show up reports and you can create a wide range of different chart types, with a custom design. You can display your chart in a GUI (e.g. Windows Forms) or save it as a graphics file to include it in an HTML report or email. A standard chart created with MCCs basically consists of three elements that are Chart object, a ChartArea object, and one or more data Series.

From MSDN, Two important properties of the Chart class are the Series and ChartAreas properties, both of which are collection properties. The Series collection property stores Series objects, which are used to store data that is to be displayed, along with attributes of that data. The ChartAreas collection property stores ChartArea objects, which are primarily used to draw one or more charts using one set of axes.

How to create a simple Memory Usage Chart and save it in your local drive: In this post, we will create a chart that shows your pc’s top 5 processes by memory usage and save it as a *.png-file.

STEP #1

Let you loading the necessary assembly and determine the path whether the PNG need to save.

[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms.DataVisualization”)
$chartSavePath = ‘C:\MonitorCPUusage’

STEP #2

In this example we won’t display this one in a GUI so you only need to set a few properties like black color and size. Here we going to create instance of the .NET-object we’ve just created and named “$chartobject” and set the Width, Height, back color. The System.Windows.Forms.DataVisualization.Charting namespace contains methods and properties for the Chart Windows forms control.

# Creating chart object
$chartobject = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$chartobject.Width = 800
$chartobject.Height =800
$chartobject.BackColor = [System.Drawing.Color]::orange

STEP #3

Next, you need to add a title to the chart. A Chart object can have multiple titles (e.g. a title and a subtitle, or a title for each ChartArea attached to this Chart object), so we create it by adding it with title collection.

# Set Chart title
[void]$chartobject.Titles.Add(“dotnet-helpers chart-Memory Usage”)
$chartobject.Titles[0].Font = “Arial,13pt”
$chartobject.Titles[0].Alignment = “topLeft”

STEP #4

As mentioned before, a Chart object can have multiple ChartArea objects. Per default, they will automatically share the available space of the Chart object (determined by the Chart object’s height and width properties). But you can also customize the location and size of each ChartArea by setting the “$ChartArea.Position.Auto” – property to $false and set size and position yourself

# create a chartarea to draw on and add to chart
$chartareaobject = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$chartareaobject.Name = “ChartArea1”
$chartareaobject.AxisY.Title = “dotnet-helpers chart – Memory(MB)”
$chartareaobject.AxisX.Title = “dotnet-helpers chart – Process Name”
$chartareaobject.AxisY.Interval = 100
$chartareaobject.AxisX.Interval = 1
$chartobject.ChartAreas.Add($chartareaobject)

STEP #5

Typically, if you want to add a legend along with a chart,I will avoid having anything on the actual chart itself and leave the description for each piece to be in the legend. Because we’ll have two data Series in the same ChartArea and it’s probably a good idea to be represent in the legend way.

# Creating legend for the chart
$chartlegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = “Legend1”
$chartobject.Legends.Add($legend)

STEP #6

Here we getting the using process details by get-process cmdlet, sort by private memory size and take the top five entries of the list.

# Getting the top 5 CPU utilzation process details
$topCPUUtilization = Get-Process | sort PrivateMemorySize -Descending | Select-Object -First 5

STEP #7

Finally, we need to set the data series to chart object. We create it by setting a name for the Series and adding it directly to the chart’s Series collection. For this example, i had set the chart type to column. When choosing the type, there is one important thing to consider: You know already that a ChartArea can have multiple Series.

# Set Series to CharObject
[void]$chartobject.Series.Add(“VirtualMemory”)
$chartobject.Series[“VirtualMemory”].ChartType = “Column”
$chartobject.Series[“VirtualMemory”].BorderWidth = 3
$chartobject.Series[“VirtualMemory”].IsVisibleInLegend = $true
$chartobject.Series[“VirtualMemory”].chartarea = “ChartArea1”
$chartobject.Series[“VirtualMemory”].Legend = “Legend1”
$chartobject.Series[“VirtualMemory”].color = “#62B5CC”
$topCPUUtilization | ForEach-Object {$chartobject.Series[“VirtualMemory”].Points.addxy( $_.Name , ($_.VirtualMemorySize / 1000000)) }

# Set Series to CharObject
[void]$chartobject.Series.Add(“PrivateMemory”)
$chartobject.Series[“PrivateMemory”].ChartType = “Column”
$chartobject.Series[“PrivateMemory”].IsVisibleInLegend = $true
$chartobject.Series[“PrivateMemory”].BorderWidth = 3
$chartobject.Series[“PrivateMemory”].chartarea = “ChartArea1”
$chartobject.Series[“PrivateMemory”].Legend = “Legend1”
$chartobject.Series[“PrivateMemory”].color = “#E3B64C”
$topCPUUtilization | ForEach-Object {$chartobject.Series[“PrivateMemory”].Points.addxy( $_.Name , ($_.PrivateMemorySize / 1000000)) }

STEP #8

At last, you want to save chart as a image file using the .saveImage() method of the Chart object.

# save chart with the Time frame
$chartobject.SaveImage(“$chartSavePath\CPUusage_$(get-date -format "yyyyMMdd_hhmmsstt“).png”,”png”)

Full Code

 

OUTPUT:

Creating Chart Reports using Powershell Chart controls