Last active
October 14, 2017 19:36
-
-
Save midnightfreddie/0a89f457b5d724616fa1757a072342e3 to your computer and use it in GitHub Desktop.
Very hackish reply to https://www.reddit.com/r/PowerShell/comments/76c0xq/ms_charting_in_powershell_help_with_empty_data/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$test = 5, 4, 5, $null, 4, 8, 3, 5, 4 | |
$chartdatasource = $test | ForEach-Object { | |
Write-Output @{ Value = $PSItem } | |
} | |
# load the appropriate assemblies | |
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | |
[void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms.DataVisualization”) | |
# create chart object | |
$Chart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart | |
$Chart1.Width = 500 | |
$Chart1.Height = 400 | |
$Chart1.Left = 40 | |
$Chart1.Top = 30 | |
# create a chartarea to draw on and add to chart | |
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea | |
$Chart1.ChartAreas.Add($ChartArea) | |
[void]$Chart1.Series.Add(“Value”) | |
$x = 0 | |
foreach ($datapoint in $chartdatasource) { | |
$x = [double]$x | |
$y = $datapoint.Value | |
#write-host "Y is "$y | |
if ($y -ne $null) { $chart1.Series["Value"].Points.addxy($x,$y) } | |
$x++ | |
} | |
$Chart1.Series["Value"].ChartType = "Line" | |
$chart1.Series["Value"].IsVisibleInLegend = $true | |
$chart1.Series["Value"].BorderWidth = 3 | |
$chart1.Series["Value"].chartarea = "ChartArea1" | |
#$chart1.Series["Value"].Legend = "Legend1" | |
$chart1.Series["Value"].color = "green" | |
$chart1.Series["Value"].EmptyPointStyle.color = "red" | |
# display the chart on a 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 = “PowerShell Chart” | |
$Form.Width = 600 | |
$Form.Height = 600 | |
$Form.controls.add($Chart1) | |
$Form.Add_Shown({$Form.Activate()}) | |
$Form.ShowDialog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lines 26-47 are from the original post's question, except I modified line 30 so it only adds the point if
$y
isn't null. The rest is just adapted from another script example to set up and display the chart, plus the test data generation.The question was how to connect the dots when there are missing data points, and while the question isn't clear I think this may be the answer: just don't add the data point if it's invalid in any way.
The image where x=4 isn't plotted is from the code as shown, and the image with x=4 and y=0 is plotted is without the
if
statement around the line 30 statement