Search code examples
c#wpflinegraphdynamic-data-display

C# dynamic data display - update LineGraph


In my XAML file, I create a ChartPlotter then I create in c# my LineGraphs and attatch them to my ChartPlotter. I tried to find a way to update these LineGraphs after their creation, but it always failed.

The only solution I found, is that I delete all LineGraphs , re-create them with new values and finally link them to my ChartPlotter.

How can I update LineGraph ?

for (int i = 0; i < lgs.Length; i++)
            if (lgs[i] != null)
                lgs[i].RemoveFromPlotter();

PS : lgs is my LineGraph array.


Solution

  • To update your LineGraphs, you have to use the ObservableDataSource object instead of the CompositeDataSource. With this object, you can use the method AppendAsync().

    public partial class MainWindow : Window
    {
        public ObservableDataSource<Point> source1 = null;
    
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }
    
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Create source         
            source1 = new ObservableDataSource<Point>();
            // Set identity mapping of point in collection to point on plot
            source1.SetXYMapping(p => p);
    
            // Add the graph. Colors are not specified and chosen random
            plotter.AddLineGraph(source1, 2, "Data row");
    
            // Force everyting to fit in view
            plotter.Viewport.FitToView();
    
            // Start computation process in second thread
            Thread simThread = new Thread(new ThreadStart(Simulation));
            simThread.IsBackground = true;
            simThread.Start();
        }
    
        private void Simulation()
        {
            int i = 0;
            while (true)
            {
                Point p1 = new Point(i * i, i);
                source1.AppendAsync(Dispatcher, p1);
    
                i++;
                Thread.Sleep(1000);
    
            }
        }
    }
    

    All you want is in the while of the method Simulation.

    source1.AppendAsync(Dispatcher, p1);