0

I want to be able to refresh the graph created by Oxyplot but am struggling to do so.

In theory the this.model.InvalidatePlot(true); should update it, but it does not want to.

MainPage.xaml.cs

using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

namespace WeatherStation
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.BuildModel2();
            this.BuildModel();
        }

        public void BuildModel()
        {
            this.model = new PlotModel();

            LineSeries lineserie = new LineSeries();
            lineserie.Points.Add(new DataPoint(0, 0));
            lineserie.Points.Add(new DataPoint(1, 70));
            lineserie.Points.Add(new DataPoint(2, 20));
            lineserie.Points.Add(new DataPoint(3, 20));

            this.model.Series.Add(lineserie);
            this.model.InvalidatePlot(true);
            DataContext = this;
        }

        public void BuildModel2()
        {
            this.model = new PlotModel();

            LineSeries lineserie = new LineSeries();
            lineserie.Points.Add(new DataPoint(0, 0));
            lineserie.Points.Add(new DataPoint(1, 10));
            lineserie.Points.Add(new DataPoint(2, 80));
            lineserie.Points.Add(new DataPoint(3, 20));

            this.model.Series.Add(lineserie);
            this.model.InvalidatePlot(true);
            DataContext = this;
        }
        public PlotModel model { get; set; }
    }
}

MainPage.xaml

<Page
    x:Class="WeatherStation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WeatherStation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:oxy="using:OxyPlot.Windows"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <oxy:PlotView Model="{Binding model, Mode=OneWay}"/>
    </Grid>
</Page>

The general idea behind this code is that graph one gets drawn and then the next one should overwrite it. If that happens, I know I can update it by firing similar functions in the future.

1 Answers1

1

I've fixed it. I was too dumb to see the solution.

First of all we need this

public LineSeries lineserie { get; set; }

We add that to both BuildModels:

    public void BuildModel()
    {
        this.model = new PlotModel();

        this.lineserie = new LineSeries();
        this.lineserie.Points.Add(new DataPoint(0, 0));
        this.lineserie.Points.Add(new DataPoint(1, 70));
        this.lineserie.Points.Add(new DataPoint(2, 20));
        this.lineserie.Points.Add(new DataPoint(3, 20));

        this.model.Series.Add(lineserie);
        this.DataContext = this;
    }

Then we remove the old lineseries and add a new one, and then we update :)

    public void BuildModel2()
    {
        this.model.Series.Remove(this.lineserie);

        this.lineserie = new LineSeries();
        this.lineserie.Points.Add(new DataPoint(0, 0));
        this.lineserie.Points.Add(new DataPoint(1, 10));
        this.lineserie.Points.Add(new DataPoint(2, 80));
        this.lineserie.Points.Add(new DataPoint(3, 20));
        this.model.Series.Add(lineserie);
        this.model.InvalidatePlot(true);
    }

And we are good to go. It's basically working with the same object all the time while updating said object, without creating a new one.