This is a migrated thread and some comments may be shown as answers.

Multi-Threading Scatter Chart

1 Answer 32 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 01 Feb 2012, 03:08 PM
Hi, Are there any examples in the documentation which show how threading with the Chart works?

Specifically, my issue is that I have a long loop which adds "Coordinate" objects to an ObservableCollection which is set as the ItemsSource of some ScatterSeries. Assume that this loop takes minutes to complete, as I add a Thread.Sleep(1000) for each iteration. Importantly, I would like the chart to display each "Coordinate" object as and when it is added to the ObservableCollection, without seizing up the whole chart.

I have followed some examples of creating a Dispatcher, however this appears to have no effect. Example code of what I am trying to achieve:

private ObservableCollection<Coordinate> coordinates = new ObservableCollection<Coordinate>();
private Queue<Coordinate> coordinatesQueue = new Queue<Coordinate>();
 
private void FillQueue()
{
    .
    .
    .
}
 
private void UpdateChart()
{
    SomeScatterSeriesinChart.ItemsSource = coordinates;
    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        (ThreadStart) delegate()
                {
                    while (coordinatesQueue.Count != 0)
                    {
                        coordinates.Add(coordinatesQueue.Dequeue());
                        Thread.Sleep(1000);
                    }
                }
    );
}


Thanks for any help in advanced!

1 Answer, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 06 Feb 2012, 05:46 PM
Hello Alan,

You need to perform the data update in a BackgroundWorker thread to achieve the desired mutli-threading effect. Note, however, that if you are starting the project now, we would suggest you to consider using our new next-generation charting solution RadChartView instead as it was implemented with such scenarios in mind and works seamlessly out-of-the-box (RadChart would also work with a BackgroundWorker update but you will need to wrap the actual "update" call inside a Dispatcher.BeginInvoke(...) statement due to the specifics of the control implementation, while with RadChartView you do not need any UI hooks in your ViewModel to achieve the same effect).

We have attached a sample application with RadChartView to get you started.


Greetings,
Giuseppe
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Chart
Asked by
Alan
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Share this question
or