0

Im having bit of a problem. I am trying to achieve something like this: http://www.codeproject.com/Articles/168056/Windows-Charting-Application The first chart on the page. I have my code here

### Load assemblies
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

$Servers = Get-XAWorkerGroup -WorkerGroupName Agresso | select servernames -ExpandProperty servernames
$SessionTable = @()
foreach ($Server in $Servers) {
    $ActiveSessions = (Get-XASession -ServerName $Server | ?{$_.State -eq "Active"}).Count
    $DisconnectedSessions = (Get-XASession -ServerName $Server | ?{$_.State -eq "Disconnected"}).Count
    $ListeningSessions = (Get-XASession -ServerName $Server | ?{$_.State -eq "Listening"}).Count
    $LoopArray = "$Server,$ActiveSessions,$DisconnectedSessions,$ListeningSessions"
    $SessionTable += $LoopArray
}

### CREATE CHART
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 1600
$Chart.Height = 400
$Chart.Left = 10 
$Chart.Top = 10

# create a chartarea to draw on and add to chart 
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea 
$Chart.ChartAreas.Add($ChartArea) 
foreach ($server in $SessionTable) {
    $Splitted = $Server.Split(",")
    $Name = $Splitted[0]
    $Active = $Splitted[1]
    $Disconnected = $Splitted[2]
    $Listening = $Splitted[3]
    [void]$Chart.Series.Add($Name)
    $dp1 = new-object System.Windows.Forms.DataVisualization.Charting.DataPoint(0,$Active)
    $dp2 = New-Object System.Windows.Forms.DataVisualization.Charting.DataPoint(0,$Disconnected)
    $dp3 = New-Object System.Windows.Forms.DataVisualization.Charting.DataPoint(0,$Listening)
    $dp1.Color = "Blue"
    $dp2.Color = "Red"
    $dp3.Color = "Orange"
    $dp1.AxisLabel = $Name + " Active"
    $dp2.AxisLabel = $Name + " Disconnected"
    $dp3.AxisLabel = $Name + " Listening"
    $Chart.Series[$Name].Points.Add($dp1)
    $Chart.Series[$Name].Points.Add($dp2)
    $Chart.Series[$Name].Points.Add($dp3)
    $ChartArea.AxisX.LabelStyle.Angle = "-90"
    $ChartArea.AxisX.LabelStyle.Interval = "1"
}
$title = new-object System.Windows.Forms.DataVisualization.Charting.Title 
$Chart.Titles.Add( $title ) 
$Chart.Titles[0].Text = "Testing"
$Chart.Titles[0].Font = New-Object System.Drawing.Font("arial",20,[System.Drawing.FontStyle]::Bold)
### Show on form
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor 
                [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left 
$Form = New-Object Windows.Forms.Form
$Form.Text = "Testing form"
$Form.Width = 1400
$Form.Height = 820
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

But my output looks like this enter image description here

I want the output to be like the one i linked at the top, so in my case: Servername with 3 combined values for different sessions. Server1 with values 6, 7, 0 for example Hope I am making myself clear on what i want to achieve! :D

user2782999
  • 385
  • 1
  • 7
  • 23

1 Answers1

0

I think, you would need to use 'AlignDataPointsByAxisLabel' to get this to work. Here is how your code would look.

### Load assemblies
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

$SessionTable = @()
$SessionTable = Get-Content C:\Users\hemanth.damecharla\Desktop\serverdetails.txt

### CREATE CHART
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 1024
$Chart.Height = 768
$Chart.Left = 10 
$Chart.Top = 10

# create a chartarea to draw on and add to chart 
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea 
$Chart.ChartAreas.Add($ChartArea) 
$seriesnames = @() #need to get all the series name to use with AlignDataPointsByAxisLabel
foreach ($server in $SessionTable) {
    $Splitted = $Server.Split(",")
    $Name = $Splitted[0]
    $Active = [double]($Splitted[1])
    $Disconnected = [double]($Splitted[2])
    $Listening = [double]($Splitted[3])
    [void]$Chart.Series.Add($Name)
    $dp1 = new-object System.Windows.Forms.DataVisualization.Charting.DataPoint(0,$Active)
    $dp2 = New-Object System.Windows.Forms.DataVisualization.Charting.DataPoint(0,$Disconnected)
    $dp3 = New-Object System.Windows.Forms.DataVisualization.Charting.DataPoint(0,$Listening)
    $dp1.Color = "Blue"
    $dp2.Color = "Red"
    $dp3.Color = "Orange"
    $dp1.AxisLabel = $Name + " Active"
    $dp2.AxisLabel = $Name + " Disconnected"
    $dp3.AxisLabel = $Name + " Listening"
    $Chart.Series[$Name].Points.Add($dp1)
    $Chart.Series[$Name].Points.Add($dp2)
    $Chart.Series[$Name].Points.Add($dp3)
    $ChartArea.AxisX.LabelStyle.Angle = "-90"
    $ChartArea.AxisX.LabelStyle.Interval = "1"

    $seriesnames += $Name #collect each series name
}
$seriesnames = $seriesnames -join ','

$title = new-object System.Windows.Forms.DataVisualization.Charting.Title 
$Chart.Titles.Add( $title ) 
$Chart.Titles[0].Text = "Testing"
$Chart.Titles[0].Font = New-Object System.Drawing.Font("arial",20,[System.Drawing.FontStyle]::Bold)

### Show on form
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor 
                [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left 
$Chart.AlignDataPointsByAxisLabel($seriesnames) #call the method
$Form = New-Object Windows.Forms.Form
$Form.Text = "Testing form"
$Form.Width = 1400
$Form.Height = 820
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
sqlchow
  • 156
  • 5
  • When I use your code my result turns out like this: https://gofile.me/2q0zW/MofWSb9p The only thing i change is my Servers input, which I use like before :) – user2782999 Apr 15 '14 at 05:57
  • I know this is a little late. But, yes that picture you showed is how it turns out. I tried several different things but, couldn't figure out a way to get to what you wanted. Also, regarding the code project sample you linked to in your post and graph you were referring to from that post. The graph is for four different series's and not the datapoints from a single series. I think that could be because of the way the series are constructed. – sqlchow May 03 '14 at 06:49