Help with Building a BarChart using .NET
Hello, I successfully built a two series bar chart using Building a Chart Using PowerShell and Chart Controls | Learn Powershell | Achieve More, as a reference. However the axis X data labels will not show for the minor tick labels, only the labels are shown next to the major tick labels.
PS C:\$SummaryData
OUName Enabled Disabled Total
------ ------- -------- -----
OU1 400 150 550
OU2 374 3 377
OU3 125 2 127
OU4 98 0 98
OU5 83 0 83
OU6 48 0 48
OU7 35 7 42
OU8 38 1 39
OU9 1 29 30
OU10 25 0 25
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
$Chart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$SeriesEnabled = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series
$SeriesDisabled = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series
$ChartTypes = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]
$SeriesEnabled.ChartType = $ChartTypes::Bar
$SeriesDisabled.ChartType = $ChartTypes::Bar
$Chart.Series.Add($SeriesEnabled)
$Chart.Series.Add($SeriesDisabled)
$Chart.ChartAreas.Add($ChartArea)
# Creates System.Arrays
$SummaryX= @(foreach ($X in $SummaryData.OUName) {
$X.ToString()
})
$SummaryY1= @(foreach ($Y1 in $SummaryData.Enabled) {
$Y1.ToString()
})
$SummaryY2= @(foreach ($Y2 in $SummaryData.Disabled) {
$Y2.ToString()
})
#Rename Series and assignes X and Y Points
$Chart.Series['Series1'].Name = "Enabled"
$Chart.Series['Enabled'].Points.DataBindXY($SummaryX, $SummaryY1)
$Chart.Series['Series2'].Name = "Disabled"
$Chart.Series['Disabled'].Points.DataBindXY($SummaryX, $SummaryY2)
#Create data labels on chart
$Chart.Series["Enabled"].Label = "#VALY"
$Chart.Series["Disabled"].Label = "#VALY"
#Change colors
$Chart.Palette = [System.Windows.Forms.DataVisualization.Charting.ChartColorPalette]::None
$Chart.PaletteCustomColors = @( [System.Drawing.Color]::Pink, [System.Drawing.Color]::LightGreen )
#Chart's height
$Chart.Width = 600
$Chart.Height = 800
$Chart.BackColor = [System.Drawing.Color]::White
#Chart Legend
$Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend
$Legend.IsEquallySpacedItems = $True
$Legend.BorderColor = 'Black'
$Chart.Legends.Add($Legend)
$Chart.SaveImage(".\$(Get-Date -Format 'yyyyMMdd')_SummaryChartTest.png","png")
I think I have to add or re-set the tickmarkintervals (the default is 0):
TickMark = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.TickMark
$TickMark.Interval = 1
I’ll try to upload an image of my chart. Thanks for your help!