Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creates a simple interactive bar chart using Microsoft's opensource Interactive Data Display project
## Example of how to use the opensource InteractveDataDisplay module from Microsoft to create a WPF chart in PowerShell
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Location to download the required libraries and reference them from
$Source = "C:\Users\tjones\OneDrive\PowerShell\POSH Projects\Interactive Data Display"
#region DownloadDependencies
$URLs = @(
'https://api.nuget.org/v3-flatcontainer/system.reactive.interfaces/3.1.1/system.reactive.interfaces.3.1.1.nupkg'
'https://api.nuget.org/v3-flatcontainer/microsoft.maps.mapcontrol.wpf/1.0.0.3/microsoft.maps.mapcontrol.wpf.1.0.0.3.nupkg'
'https://api.nuget.org/v3-flatcontainer/system.reactive.windows.threading/3.1.1/system.reactive.windows.threading.3.1.1.nupkg'
'https://api.nuget.org/v3-flatcontainer/system.reactive.platformservices/3.1.1/system.reactive.platformservices.3.1.1.nupkg'
'https://api.nuget.org/v3-flatcontainer/system.reactive.core/3.1.1/system.reactive.core.3.1.1.nupkg'
'https://api.nuget.org/v3-flatcontainer/interactivedatadisplay.wpf/1.0.0/interactivedatadisplay.wpf.1.0.0.nupkg'
'https://api.nuget.org/v3-flatcontainer/system.reactive/3.1.1/system.reactive.3.1.1.nupkg'
'https://api.nuget.org/v3-flatcontainer/system.reactive.linq/3.1.1/system.reactive.linq.3.1.1.nupkg'
)
Foreach ($URL in $URLs)
{
$SubfolderName = $($URL.Split('/') | Select -Last 1).trimend('.nupkg')
If (!(Test-Path $Source\$SubfolderName))
{
# Download file
$Output = "$Source\$($URL.Split('/') | Select -Last 1)"
Invoke-WebRequest -Uri $URL -OutFile $Output
# Create directory
$null = New-Item -Path $Source -Name $SubfolderName -ItemType Directory -Force
# Extract to directory
[System.IO.Compression.ZipFile]::ExtractToDirectory( $Output, "$Source\$SubfolderName" )
# Cleanup nupkgs
Remove-item -Path $Output -Force
}
}
#endregion
# Add the required libraries
Add-Type -Path "$Source\microsoft.maps.mapcontrol.wpf.1.0.0.3\lib\net40-Client\Microsoft.Maps.MapControl.WPF.dll"
Add-Type -Path "$Source\System.Reactive.Interfaces.3.1.1\lib\net45\System.Reactive.Interfaces.dll"
Add-Type -Path "$Source\System.Reactive.Core.3.1.1\lib\net45\System.Reactive.Core.dll"
Add-Type -Path "$Source\System.Reactive.Linq.3.1.1\lib\net45\System.Reactive.Linq.dll"
Add-Type -Path "$Source\System.Reactive.PlatformServices.3.1.1\lib\net45\System.Reactive.PlatformServices.dll"
Add-Type -Path "$Source\System.Reactive.Windows.Threading.3.1.1\lib\net45\System.Reactive.Windows.Threading.dll"
Add-Type -Path "$Source\InteractiveDataDisplay.WPF.1.0.0\lib\net452\InteractiveDataDisplay.WPF.dll"
# Define the UI in xaml
[XML]$Xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d3="clr-namespace:InteractiveDataDisplay.WPF;assembly=InteractiveDataDisplay.WPF"
Title = "InteractiveDataDisplay Bar Chart Example" SizeToContent="WidthAndHeight" MinHeight="450" MinWidth="450" WindowStartupLocation = "CenterScreen" ResizeMode="NoResize" >
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Label Content="X Axis" FontSize="16"/>
<TextBox Name="X1" Text="1" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5"/>
<TextBox Name="X2" Text="2" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
<TextBox Name="X3" Text="3" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
<TextBox Name="X4" Text="4" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
<TextBox Name="X5" Text="5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Label Content="Y Axis" FontSize="16"/>
<TextBox Name="Y1" Text="5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5"/>
<TextBox Name="Y2" Text="4" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
<TextBox Name="Y3" Text="3" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
<TextBox Name="Y4" Text="2" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
<TextBox Name="Y5" Text="1" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="30" FontSize="16" Margin="5" />
</StackPanel>
<Button Name="Plot_Button" Content="Plot" Height="50" Width="100" HorizontalAlignment="Center" FontSize="32" Foreground="green" Margin="5"/>
<d3:Chart Name="plotter" Height="430" Width="430">
<d3:Chart.Title>
<TextBlock HorizontalAlignment="Center" FontSize="18" Margin="0,5,0,5">Enter plot values</TextBlock>
</d3:Chart.Title>
<d3:BarGraph Name="barChart" Color="Blue" Height="430" Width="430" />
</d3:Chart>
</StackPanel>
</Grid>
</Window>
'@
# Load all the named objects into items in a hashtable
$Hash = [hashtable]::Synchronized(@{})
$Hash.Window = [Windows.Markup.XamlReader]::Load((New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml))
$XAML.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object -Process {
$Hash.$($_.Name) = $Hash.Window.FindName($_.Name)
}
# Handle the Button click event
$Hash.Plot_Button.Add_Click({
# Plot the chart
$Hash.barChart.Plot(@(
$Hash.X1.Text,$Hash.X2.Text,$Hash.X3.Text,$Hash.X4.Text,$Hash.X5.Text
),@(
$Hash.Y1.Text,$Hash.Y2.Text,$Hash.Y3.Text,$Hash.Y4.Text,$Hash.Y5.Text
))
})
# Display the window
$null = $Hash.window.Dispatcher.InvokeAsync{$Hash.window.ShowDialog()}.Wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.