I'm developing a WPF app with C#, .NET Framework 4.7. and Oxyplot 1.0.
I'm trying to update the graphic at runtime, but it doesn't do anything.
I have tried to use ObsevableCollection
and InvalidateFlag
but without success.
This is the XAML:
<oxy:Plot Title="{Binding Title}" InvalidateFlag="{Binding InvalidateFlag}">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding BestFitness}"/>
<oxy:LineSeries ItemsSource="{Binding WorstFitness}"/>
<oxy:LineSeries ItemsSource="{Binding AverageFitness}"/>
</oxy:Plot.Series>
</oxy:Plot>
And this is the view model:
public class MainViewModel : ObservableObject
{
private int count;
private int _invalidateFlag;
public string Title { get; set; }
public int InvalidateFlag
{
get { return _invalidateFlag; }
set
{
_invalidateFlag = value;
RaisePropertyChangedEvent("InvalidateFlag");
}
}
public ObservableCollection<DataPoint> BestFitness { get; set; }
public ObservableCollection<DataPoint> WorstFitness { get; set; }
public ObservableCollection<DataPoint> AverageFitness { get; set; }
public ICommand StartCommand
{
get { return new DelegateCommand(Start); }
}
public ICommand RefereshCommand
{
get { return new DelegateCommand(Refresh); }
}
public MainViewModel()
{
this.Title = "Example 2";
this.BestFitness = new ObservableCollection<DataPoint>
{
new DataPoint(0, 4),
new DataPoint(10, 13),
new DataPoint(20, 15),
new DataPoint(30, 16),
new DataPoint(40, 12),
new DataPoint(50, 12)
};
}
private void Start()
{
Random rnd = new Random((int)DateTime.Now.Ticks);
Program program = new Program(rnd);
program.Algorithm.EvolutionEnded += Algorithm_EvolutionEnded;
count = 0;
this.BestFitness = new ObservableCollection<DataPoint>();
this.WorstFitness = new ObservableCollection<DataPoint>();
this.AverageFitness = new ObservableCollection<DataPoint>();
Task.Run(() => program.Run(null));
}
private void Refresh()
{
this.BestFitness.Clear();
}
private void Algorithm_EvolutionEnded(object sender, EventArgs e)
{
EvolutionEndedEventArgs args = (EvolutionEndedEventArgs)e;
BestFitness.Add(new DataPoint(count, args.BestFitness));
WorstFitness.Add(new DataPoint(count, args.WorstFitness));
AverageFitness.Add(new DataPoint(count, args.AverageFitness));
InvalidateFlag++;
}
}
Do I need to do anything else?