Help with Building a BarChart using .NET

Help with Building a BarChart using .NET

Hello, I successfully built a two series bar chart using https://learn-powershell.net/2016/09/18/building-a-chart-using-powershell-and-chart-controls/, 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!

I got it:

$chart.ChartAreas.AxisX.Interval = 1

I would greatly appreciate it if someone could provide resources on using Powershell with new .NET objects . I stumbled upon an old Pluralsight course, “Everyday PowerShell for Developers,” by Jim Christopher. However, any other articles , courses, or books would be awesome. Not currently looking to become an expert with .NET, just would like enough knowledge on how to understand and navigate through the https://docs.microsoft.com/ (should I again need to create a custom System.Windows.Forms.DataVisualization.Charting.Chart object).

Combing through various blogs and forums on how to do simple things like changing the color or adjusting the labels is a real time-waster.

Thanks! :slight_smile:

Please stop submitting your topics multiple times. You are creating a mess that I have to clean up.

[quote quote=245667]Please stop submitting your topics multiple times. You are creating a mess that I have to clean up.

[/quote]
Thanks for fixing my post - I appreciate it.