Search code examples
wpfvb.netxamlgraphoxyplot

OxyPlot not refreshing in VB.net


I was using Visual Studio to chart data when I ran into a problem with the logarithmic X-Axis. I have now turned to OxyPlot, however, I am running into huge issues with getting my data to populate. I switched from the windows forms to WPF, so this could be a XAML issue with binding (unsure). Essentially I took their very limited example in VB.net and can easily reproduce it. What I am struggling with is reproducing dynamic plotting with OxyPlot. Here is my code so far:

Sample: (http://docs.oxyplot.org/en/latest/getting-started/hello-wpf-vb.html)

Public Sub New()
        Model = New PlotModel()

        Model.Title = "Simple example"
        Model.Subtitle = "using OxyPlot in VB.NET"

        Dim series1 = New LineSeries()
        series1.Title = "Series 1"
        series1.MarkerType = MarkerType.Circle
        series1.Points.Add(New DataPoint(0, 0))
        series1.Points.Add(New DataPoint(10, 18))
        series1.Points.Add(New DataPoint(20, 12))
        series1.Points.Add(New DataPoint(30, 8))
        series1.Points.Add(New DataPoint(40, 15))

        Dim series2 = New LineSeries()
        series2.Title = "Series 2"
        series2.MarkerType = MarkerType.Square
        series2.Points.Add(New DataPoint(0, 4))
        series2.Points.Add(New DataPoint(10, 12))
        series2.Points.Add(New DataPoint(20, 16))
        series2.Points.Add(New DataPoint(30, 25))
        series2.Points.Add(New DataPoint(40, 5))

        Model.Series.Add(series1)
        Model.Series.Add(series2)
    End Sub

Then I attempt to add more data to simulate dynamically loading data. I also am aware that I must use invalidate() and that Update() is deprecated. I added a button to the form and give it another series to add to the chart upon click.

Button Click:

Private Sub test_Click(sender As Object, e As RoutedEventArgs) Handles test.Click


        Dim series3 = New LineSeries()
        series3.Title = "Series 3"
        series3.MarkerType = MarkerType.Square
        series3.Points.Add(New DataPoint(20, 20))
        series3.Points.Add(New DataPoint(21, 21))
        series3.Points.Add(New DataPoint(22, 22))
        series3.Points.Add(New DataPoint(23, 23))
        series3.Points.Add(New DataPoint(24, 24))
        Model.Series.Add(series3)
        Model.InvalidatePlot(True)
    End Sub

Here is also their lovely reference to updating their dataPlot: http://docs.oxyplot.org/en/latest/common-tasks/refresh-plot.html#examples


Solution

  • In your event handler, you need to add series3 to your Model before calling InvalidatePlot i.e.

    Model.Series.Add(series3);
    Model.InvalidatePlot(true);
    

    Update

    Also, you need to make sure your PlotModel notifies the view (XAML) of your changes. If I use the example from Oxyplot documentation as an example (the one you specified), I'd modify the code as such:

    Public Class MainViewModel Implements INotifyPropertyChanged
    
        Private mmodel As PlotModel
    
        Public Event PropertyChanged As PropertyChangedEventHandler _
            Implements INotifyPropertyChanged.PropertyChanged
    
        Public Sub New()
    
            Model = New PlotModel()
    
            Model.Title = "Simple example"
            Model.Subtitle = "using OxyPlot in VB.NET"
    
            Dim series1 = New LineSeries()
            series1.Title="Series 1"
            series1.MarkerType = MarkerType.Circle
            series1.Points.Add(New DataPoint(0, 0))
            series1.Points.Add(New DataPoint(10, 18))
            series1.Points.Add(New DataPoint(20, 12))
            series1.Points.Add(New DataPoint(30, 8))
            series1.Points.Add(New DataPoint(40, 15))
    
            Dim series2 = New LineSeries()
            series2.Title="Series 2"
            series2.MarkerType = MarkerType.Square
            series2.Points.Add(New DataPoint(0, 4))
            series2.Points.Add(New DataPoint(10, 12))
            series2.Points.Add(New DataPoint(20, 16))
            series2.Points.Add(New DataPoint(30, 25))
            series2.Points.Add(New DataPoint(40, 5))
    
            Model.Series.Add(series1)
            Model.Series.Add(series2)
    
        End Sub
    
        Property Model() As PlotModel
            Get
                Return mmodel
            End Get
            Set(value As PlotModel)
                mmodel = value
                NotifyPropertyChanged("Model")
            End Set
        End Property
    
        Private Sub test_Click(sender As Object, e As RoutedEventArgs) Handles test.Click
    
            Dim series3 = New LineSeries()
            series3.Title = "Series 3"
            series3.MarkerType = MarkerType.Square
            series3.Points.Add(New DataPoint(20, 20))
            series3.Points.Add(New DataPoint(21, 21))
            series3.Points.Add(New DataPoint(22, 22))
            series3.Points.Add(New DataPoint(23, 23))
            series3.Points.Add(New DataPoint(24, 24))
            Model.Series.Add(series3)
            Model.InvalidatePlot(True)
        End Sub
    
    End Class