ScottPlot.NET
GitHub Repo stars

WPF Quickstart

Step 1: Install the ScottPlot.WPF NuGet package

Step 2: Add this to the root element of your XAML file:

xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"

Step 3: Add a WpfPlot to your layout and give it a unique name

<ScottPlot:WpfPlot x:Name="WpfPlot1" />

Step 4: Plot some data in your start-up sequence

double[] dataX = { 1, 2, 3, 4, 5 };
double[] dataY = { 1, 4, 9, 16, 25 };
WpfPlot1.Plot.Add.Scatter(dataX, dataY);
WpfPlot1.Refresh();

.NET Framework Apps

Projects targeting .NET Framework should setup their plot inside the Loaded event instead of directly in the constructor.

public MainWindow()
{
    InitializeComponent();

    Loaded += (s, e) =>
    {
        double[] dataX = { 1, 2, 3, 4, 5 };
        double[] dataY = { 1, 4, 9, 16, 25 };
        WpfPlot1.Plot.Add.Scatter(dataX, dataY);
        WpfPlot1.Refresh();
    };
}

Plot using MVVM

WPF applications may be created using MVVM (Model–view–viewmodel) pattern to improve separation between the GUI layer and business logic.

  • All code accessing the WpfPlot object can be kept in the view model - the class set as the DataContext

  • Adapt the concrete implementation to the framework (e.g., use attributes to define the PlotControl)

MVVM Example

View: Add a ContentControl instead of WpfPlot in the layout

    <ContentControl Content="{Binding PlotControl, Mode=OneTime}"/>

View Model: Add a PlotControl property

    WpfPlot PlotControl { get; } = new WpfPlot();

Code-behind: Setup the plot in the view model’s constructor

void Plot()
{
    double[] dataX = { 1, 2, 3, 4, 5 };
    double[] dataY = { 1, 4, 9, 16, 25 };
    PlotControl.Plot.Add.Scatter(dataX, dataY);
    PlotControl.Refresh();
}

Updating the Plot:

  • Access the control by using the PlotControl property in the view model

  • Plot updates would most likely be done as a reaction to an event or modification of an other property.

  • When to update the plot depends on your use-case. Note that rendering large amounts of data frequently may negatively impact performance.