I am experimenting with a robotic arm and fuzzy logic. My goal is to plot the membership function (simple triangle and trapezoid functions) created by the user. The oxy Plot 'object' is located on an expander, which is located on MainWindow. Plot selection will most likely be from a drop-down or text-box.
Unfortunately, I cannot figure out how to update a plot with new information. All of the examples appear to be dynamic, based on time. But I need it to be based on user demand! The closest examples I can find are the Refresh and Task demo files, I think.
Up to now I have been able to produce an original, blank plot with a title and axes using the following syntax:
public partial class MainWindow : Window
{
private PlotModel plotModel;
public PlotModel PlotModel
{
get
{
return this.plotModel;
}
set
{
this.plotModel = value;
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.PlotModel = CreatePlot("mag1");
}
private PlotModel CreatePlot(string title)
{
var pm = new PlotModel(title);
return pm;
}
And the XAML
<Window x:Class="FuzzyGripper.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="clr-namespace:OxyPlot.Wpf;assembly=OxyPlot.Wpf"
Title="MainWindow" Height="800" Width="1000" Closing="Window_Closing" Loaded="Window_Loaded">
...... A ton of stuff ...... then......
<oxy:Plot x:Name="plot1" Model="{Binding PlotModel}"/>
The problem comes when I want to update the plot with something as simple as a new title. From what I can tell the OxyPlot can only be updated when the object is located in the MainWindow constructor. For example the following snippet is called after the original plot is created (outside of MainWindow(){}):
this.plotModel = CreatePlot("Mag2");
plot1.RefreshPlot(true);
It doesn't work. The original plot remains, with title "Mag1". I have a feeling I missing something related to context, specifically -- DataContext -- but I am not extremely comfortable with the language, so I am stuck! With that being said, I am not completely sold on Oxyplot. If there is a better approach I am willing to give it a try. Thanks for reading.
The idea behind using the
PlotModel
in OxyPlot is to use MVVM, so you shouldn't really use the Codebehind to do what you're trying to do. You should check first the concept of MVVM which is quite important for WPF and then look at different MVVM Frameworks to help you with it, like Caliburn Micro or MVVM LightThe issue with your code is that when you're doing
this.plotModel = CreatePlot("Mag2")
, you're creating a new PlotModel object, WPF then loses the binding that he had with the old objects. You should notify him (implementingINotifyPropertyChange
) that the PlotModel property has changed.But anyway, to change the title you shouldn't re-create a whole PlotModel, you should simply do something like
this.PlotModel.Title = "Mag2"
.Also, the PlotModel itself has a refresh method, it's better to call this one instead of calling the Plot's refresh.
Good luck.