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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") $chartSavePath = 'C:\MonitorCPUusage' # Creating chart object # The System.Windows.Forms.DataVisualization.Charting namespace contains methods and properties for the Chart Windows forms control. $chartobject = New-object System.Windows.Forms.DataVisualization.Charting.Chart $chartobject.Width = 800 $chartobject.Height =800 $chartobject.BackColor = [System.Drawing.Color]::orange # Set Chart title [void]$chartobject.Titles.Add("dotnet-helpers chart-Memory Usage") $chartobject.Titles[0].Font = "Arial,13pt" $chartobject.Titles[0].Alignment = "topLeft" # 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" $chartareaobject.AxisX.Title = "dotnet-helpers chart - ProcessName" $chartareaobject.AxisY.Interval = 100 $chartareaobject.AxisX.Interval = 1 $chartobject.ChartAreas.Add($chartareaobject) # Creating legend for the chart $chartlegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend $legend.name = "Legend1" $chartobject.Legends.Add($legend) # Get the top 5 process using in our system $topCPUUtilization = Get-Process | sort PrivateMemorySize -Descending | Select-Object -First 5 # data series [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 = "#00bfff" $topCPUUtilization | ForEach-Object {$chartobject.Series["VirtualMemory"].Points.addxy( $_.Name , ($_.VirtualMemorySize / 1000000)) } # data series [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 = "#bf00ff" $topCPUUtilization | ForEach-Object {$chartobject.Series["PrivateMemory"].Points.addxy( $_.Name , ($_.PrivateMemorySize / 1000000)) } # save chart with the Time frame for identifying the usage at the specific time $chartobject.SaveImage("$chartSavePath\CPUusage_$(get-date -format `"yyyyMMdd_hhmmsstt`").png","png") |
[reflection.assembly]::loadwithpartialname(“System.Drawing”) | Out-Null
[reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null
#——————————————————————————— Form Objects Declaration
$form1 = New-Object System.Windows.Forms.Form
#——————————————————————————– Form properties
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 800
$System_Drawing_Size.Width = 800
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Font = New-Object System.Drawing.Font(“Consolas”,14.25,1,3,1)
$form1.FormBorderStyle = 6
$form1.Name = “form1”
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.StartPosition = 1
$form1.Text = “Add New Computers”
#
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms.DataVisualization”)
$chartSavePath = ‘C:\’
# Creating chart object
$chartobject = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$chartobject.Width = 800
$chartobject.Height =800
$chartobject.BackColor = [System.Drawing.Color]::orange
# Set Chart title
[void]$chartobject.Titles.Add(“dotnet-helpers chart-Memory Usage”)
$chartobject.Titles[0].Font = “Arial,13pt”
$chartobject.Titles[0].Alignment = “topLeft”
# 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)
# Creating legend for the chart
$chartlegend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$chartlegend.name = “Legend1”
$chartobject.Legends.Add($chartlegend)
# Getting the top 5 CPU utilzation process details
$topCPUUtilization = Get-Process | sort PrivateMemorySize -Descending | Select-Object -First 5
# 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)) }
$form1.Controls.Add($chartobject)
# save chart with the Time frame
#$chartobject.SaveImage(“$chartSavePath\CPUusage_$(get-date -format “yyyyMMdd_hhmmsstt“).png”,”png”)
$form1.ShowDialog()| Out-Null
At the bottom :
$form1.Controls.Add($chartobject)
$form1.ShowDialog()| Out-Null
To see it into into a form
At the top :
[reflection.assembly]::loadwithpartialname(“System.Drawing”) | Out-Null
[reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null
#——————————————————————————— Form Objects Declaration
$form1 = New-Object System.Windows.Forms.Form
#——————————————————————————– Form properties
$form1.BackColor = [System.Drawing.Color]::FromArgb(255,153,180,209)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 800
$System_Drawing_Size.Width = 800
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Font = New-Object System.Drawing.Font(“Consolas”,14.25,1,3,1)
$form1.FormBorderStyle = 6
$form1.Name = “form1”
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.StartPosition = 1
$form1.Text = “Add New Computers”
#
At the bottom :
$form1.ShowDialog()| Out-Null
incorrect code!!! if you execute as mentioned above; it’ll throw error for line number 27 & 28
Correct Cod:
$chartlegend.name = “Legend1”
$chartobject.Legends.Add($chartlegend)
If you trying want export data similar like osilloscope chat in excel, you can use “xlLine” type which will be similar to your expectation. If excel is your option to export then you can create chart in excel with powershell scrip.
I wrote a script to read the data stream from the serial port. I’d like to show the obtained data in a graph, similar to an oscilloscope.
The horizontal coordinate is time, or you can call it a counter.
Vertical coordinates are values and can be one or more.
Data is updated about once a second.
I’m trying to use “System.Windows.Forms.DataVisualization.Charting.Chart”. But I’m not sure if it is the correct choice.
Could any one show me a demo? Thanks.