Home

Charting data from an Observable

Is it possible to Chart in realtime using an observable?

I have an observable that i can dump in realtime but would be really nice to visualize in a chart.
StockPrice has a timestamp and price (double) but .Chart() does not seem to work with an Observable.

Anybody attempting anything like this or know if its possible?

cheers

Comments

  • There's no built-in functionality to chart observables in real-time. You could write a custom extension method in My Extensions to do this using the techniques described here:

    https://www.linqpad.net/CustomVisualizers.aspx

  • I have done it with System.Windows.Forms.DataVisualization.Charting.Chart and it works just fine to update the series inplace. You must retain the one instance of Chart that you've dumped though.

  • Try this code:

        var chart = new Chart();
        var ca = new ChartArea();
        chart.ChartAreas.Add(ca);
        chart.Dump();
    
        Observable
            .Interval(TimeSpan.FromSeconds(0.1))
            .Scan(new List<long>(), (a, x) => { a.Add(x); return a; })
            .ObserveOn(chart)
            .Subscribe(xs =>
            {
                chart.Series.Clear();
                var series = new Series() { ChartType = SeriesChartType.Line };
                xs.ForEach(x => series.Points.AddXY(x, x % 4));
                chart.Series.Add(series);
            });
    
  • I don't know much about Reactive Observables, or the DataVisualization charting objects for that matter, but I landed on this thread looking for an answer of how to display LinqPad Chart dumps in real-time (in my case, for some SQL data, updating every second). I eventually found an answer on my own so I'll describe my solution here, even if it's not totally relevant to OP's question, in case anyone else needs help in this more generic scenario.

    What I discovered is that if you simply .Dump() from LinqPad's built-in .Chart function, then the output doesn't update until the code finishes... thus, the code below doesn't work very well, because it doesn't show any charts at all until the end of the loop, and then you see each chart individually (1 data point, 2 data points, 3 data points, etc):

    for(int i=0; i<10; i++) {
        // get data (from sql, or wherever)...  not shown.
        list.Add(data);           // 'list' is just a List or other object that supports .Chart
        list.Chart(...).Dump();   // try to dump the .Chart inside the loop - doesn't work very well
        Thread.Sleep(1000);       // sleep 1 second, then restart loop, adding more data each time
    }
    

    The solution I found was to create a DumpContainer, but more importantly, Dump the output of ToBitmap instead of the Chart. This allows the single instance of the DumpContainer to update in real-time (using a DumpContainer without using ToBitmap produces the same problem as in the code above). This code works much better:

    var dc = new DumpContainer().Dump();
    for(int i=0; i<10; i++) {
        list.Add(...);
        dc.Content = list.Chart(...).ToBitmap();
        Thread.Sleep(1000);
    }
    
Sign In or Register to comment.