Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell 3D WinForms 3D Charts

I'm having some trouble making my charts appear in 3D, here's my code

[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
$WeekTable = @{
    "Week1" = 50
    "Week2" = 50
}
$WeekChart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart
$WeekChart.Width = 1200
$WeekChart.Height = 768
$WeekChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea3DStyle
$WeekChartArea.Enable3D = $true
$WeekChart.ChartAreas.Add($WeekChartArea)
$WeekChart.Series.Add("Data")
$WeekChart.Series["Data"].Points.DataBindXY($WeekTable.Keys, $WeekTable.Values)
#$WeekChart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
# Display chart on form
$WeekChart.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 = "Escape Windows XP statistics"
$Form.Width = 1024
$Form.Height = 820
$Form.controls.add($WeekChart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

The chart shows up fine on my form but it is not displayed in 3D. The Enable3D property is true as it should be?? if i check when the script has finished

like image 666
user2782999 Avatar asked Sep 18 '25 16:09

user2782999


1 Answers

The problem you're seeing is that the ChartArea3DStyle is not a ChartArea, because it does not inherit from the ChartArea class. However, you are using it like it is a ChartArea when you call $WeekChart.ChartAreas.Add($WeekChartArea). I don't know why that isn't throwing an exception, but it sure seems to me like it should.

Instead, you need to simply create a ChartArea, then change its Area3DStyle property to the value of your ChartArea3DStyle instance. Don't treat the ChartArea3DStyle object like a ChartArea, because it isn't one.

$Area3DStyle = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea3DStyle;
$Area3DStyle.Enable3D = $true;
$ChartArea = $WeekChart.ChartAreas.Add('ChartArea');
$ChartArea.Area3DStyle = $WeekChartArea;

3D Windows Forms Chart Screenshot

The final script would look like this:

[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
$WeekTable = @{
    "Week1" = 50
    "Week2" = 50
}
$WeekChart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart
$WeekChart.Width = 1200
$WeekChart.Height = 768
$Area3DStyle = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea3DStyle;
$Area3DStyle.Enable3D = $true;
$ChartArea = $WeekChart.ChartAreas.Add('ChartArea');
$ChartArea.Area3DStyle = $Area3DStyle;

$ChartSeries = $WeekChart.Series.Add("Data")
$WeekChart.Series["Data"].Points.DataBindXY($WeekTable.Keys, $WeekTable.Values)
#$WeekChart.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
# Display chart on form
$WeekChart.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 = "Escape Windows XP statistics"
$Form.Width = 1024
$Form.Height = 820
$Form.controls.add($WeekChart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!