roxxsteady
roxxsteady

Reputation: 11

OxyPlot v2.1.0 DataBinding

My existing c# WPF project with OxyPlot.Wpf v2.0 NuGet Package runs fine and uses Data binding. OxyPlot.Wpf 2.1.2 is out but I have problems that the data binding no longer processes updates for use as Live-Chart.

To test, I wrote 2 test programs to clarify that. In both versions INotifyPropertyChanged is implemented and I use the following properties for my data:

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

...


private ObservableCollection<DataPoint> valueList1;
public ObservableCollection<DataPoint> ValueList1
{
    get => valueList1;
    set
    {
        valueList1 = value;
        OnPropertyChanged();
    }
}

private ObservableCollection<DataPoint> valueList2;
public ObservableCollection<DataPoint> ValueList2
{
    get => valueList2;
    set
    {
        valueList2 = value;
        OnPropertyChanged();
    }
}

In the constructor, I add a few test data that can also be seen directly in the OxyPlot Chart when it starts in both OxyPlot versions. So the data binding should be fine with that.

I add further data to the ObservableCollection Propertys by clicking a button. In Version 2.0 the WPF-Chart updates and shows the new Data. In Version 2.1 nothing happens, data binding doesn't seem to trigger any updates...

As part of the update to OxyPlot.Wpf v2.1, I have to move my XAML code to the code behind:

XAML v2.0

<oxy:Plot Title="OxyTest" >
    <oxy:Plot.Axes>
        <oxy:LinearAxis MaximumPadding="0.1" IsZoomEnabled="True" MajorStep="100" />
    </oxy:Plot.Axes>
    <oxy:Plot.Series>
        <oxy:AreaSeries Color="Red" Title="DataLine1" ItemsSource="{Binding Path=ValueList1}" />
        <oxy:LineSeries Color="Blue" Title="DataLine2" ItemsSource="{Binding Path=ValueList2}" />
    </oxy:Plot.Series>
</oxy:Plot>

Code Behind v2.1

MyModel = new PlotModel
{
    Title = "OxyTest",
};

MyModel.Axes.Add(new LinearAxis
{
    MaximumPadding = 0.1,
    IsZoomEnabled = true,
    MajorStep = 100
});

MyModel.Series.Add(new AreaSeries
{
    Color = OxyColor.FromRgb(255, 0, 0),
    Title = "DataLine1",
    ItemsSource = ValueList1
});

MyModel.Series.Add(new LineSeries
{
    Color = OxyColor.FromRgb(0, 0, 255),
    Title = "DataLine2",
    ItemsSource = ValueList2
});

EDIT:

The InvalidatePlot() method must be called for the display to be updated. The method has existed for a long time but in version 2.0 I didn't have to trigger it manually!?

Thanks to this Article How to refresh oxyplot plot when data changes

The Solution from heltonbiker looks good so i added this in my project:

ValueList1.CollectionChanged += (a, b) => MyModel.InvalidatePlot(true);
ValueList2.CollectionChanged += (a, b) => MyModel.InvalidatePlot(true);

Maybe this will help someone else.

The offical OxyPlot documentation is labeld as under construction.

EDIT2:

I found another Solution for my DataBinding Problem. Just add OxyPlot.Contrib.Wpf to the Project and change the XMLNS from "http://oxyplot.org/wpf" to "http://oxyplot.org/wpf/contrib" and use the Exact same XAML from v2.0. No need to trigger the CollectionChanged Event. Databinding works fine.

Upvotes: 1

Views: 441

Answers (0)

Related Questions