Hi All,
I'd like to create a line chart with Powershell + Windows charting. The process would be this: I query the system date as "yy.mmmm.dd" and the system time as "hh:mm", separatedly (you'll see why). Furthermore, I query the stage of
one specific port and the number of connections on this port. I export all this four datas into a .csv file with -append for later examination. Then, I open this .csv file and import only the 5 latest "time" and "connection number". I'd
like to create the chart so, that the Title holds the actual chart title, plus the date, plus the stage of the port (because I haven't found any athor place where I could show them, like header, footer, remark, anything). Now, the Y-axis shows the query of
connection numbers and the X-axis shows the timepoints when the queries have been made (say, in 5 minutes intervals). Below is my code so far. my problem is, that I'm getting an empty chart, showing only the title field. What am I doing wrong?
Thanks in advance.
-------------------------------------------------------------------
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
Try{
$Date = Get-Date -f "yyyy.MMMM.dd."
$Time = Get-Date -f "HH:mm"
$connection = New-Object Net.Sockets.TcpClient
$connection.Connect("ipaddress","portnumber")
if($connection.Connected)
{
"Port is connected"
}
else
{
"Port is disconnected"
}
$connection_num = Get-NetTCPConnection | Where-Object { $_.LocalAddress -eq "ipaddress" -and $_.LocalPort -eq "portnumber"} | Select-Object LocalAddress, LocalPort | Measure-Object | % {$_.Count}
$report = New-Object psobject
$report | Add-Member -MemberType NoteProperty -Name Date -Value $Date
$report | Add-Member -MemberType NoteProperty -Name Time -Value $Time
$report | Add-Member -MemberType NoteProperty -Name Connection -Value $connection.Connected
$report | Add-Member -MemberType NoteProperty -Name Connection_num -Value $connection_num
$report | Export-Csv -Append -Path Path\linechart.csv -NoTypeInformation
}
Catch{
Throw $_}
Try{
if($connection.Connected)
{
$msg = "Port connected"
}
else
{
$msg = "Port disconnected"
}
$chart1 = [System.Windows.Forms.DataVisualization.Charting.Chart]::New()
$chart1.Size = '1700,900'
$chart1.Titles.Add([System.Windows.Forms.DataVisualization.Charting.Title]::New())
$chart1.Titles[0].Font = "Arial, 24pt"
$chart1.Titles[0].Text = "Monitoring " + "--- " + $Date + " " + " " + $msg
$chart1.ChartAreas.Add([System.Windows.Forms.DataVisualization.Charting.ChartArea]::New("Default"))
$chartarea.Name = "ChartArea1"
$chartarea.AxisX.Interval = 10
$chartarea.AxisY.Interval = 5
$datasource = @{
"Conn" = ((Import-Csv Path\report.csv) | Select Time, connection_num) | Select -Last 5
}
[void]$chart1.Series.Add("Data")
$chart1.Series["Data"].ChartType = "Line"
foreach ($datapoint in $datasource) {
$x = [int]$datapoint.Connection_num
$y = [string]$datapoint.Time
$chart1.Series["Data"].Points.addxy($x,$y)
}
$imageFile = "Path\linechart.png"
$chart1.SaveImage($imageFile,"PNG")
}
Catch{
Throw $_}