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

Using BackgroundWorker to return multiple chart series

2 Answers 175 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
M
Top achievements
Rank 1
M asked on 26 Nov 2012, 06:12 PM
EDIT: I solved the issue, I just had to create a new background worker for each series.

Hey peeps,

I'm just getting started with Telerik's controls and have successfully managed to plot a multi series chart. Now due to the time it takes to populate the chartdata, I wanted to do this with a background worker. I successfully managed to have a backgroundworker plot a single chartseries, but I don't know how to have it plot multiple. Lets look at the code:


public BackgroundWorker worker = new BackgroundWorker();
 public Window1()
 {
     InitializeComponent();
     InitializeBackgroundWorker();
 }
 
 // Set up the BackgroundWorker object by 
 // attaching event handlers. 
 private void InitializeBackgroundWorker()
 {
     worker.DoWork +=new DoWorkEventHandler(worker_DoWork);
     worker.RunWorkerCompleted +=new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
 }
 
 //Define a New Chart Data Class for each graph
 public class ChartDataClass1
 {
     public double X1 { get; set; }
     public double Y1 { get; set; }
 
     public ChartDataClass1()
     {
 
     }
 }
 
 public class ChartDataClass2
 {
 
     public double X2 { get; set; }
     public double Y2 { get; set; }
 
     public ChartDataClass2()
     {
 
     }
 }
 
 private void btn1_Click(object sender, RoutedEventArgs e)
 {
     worker.RunWorkerAsync();
 }
 
 void worker_DoWork(object sender, DoWorkEventArgs e)
 {
     // On the worker thread...cannot make UI calls from here.
     e.Result = MTLB(worker,e);
 
 
private void worker_RunWorkerCompleted(
     object sender, RunWorkerCompletedEventArgs e)
 {
      
     // First, handle the case where an exception was thrown.
     if (e.Error != null)
     {
         MessageBox.Show(e.Error.Message);
     }
 
     else
     {
         // Finally, handle the case where the operation 
         // succeeded.
         xline.Series[0].ItemsSource = (System.Collections.IEnumerable)e.Result;
     }
 
 }
 
 public System.Collections.Generic.List<ChartDataClass1> MTLB(BackgroundWorker worker, DoWorkEventArgs e)
 {
     int st = 1;
     int sp = 20;
  
     //MATLAB inputs
     MWNumericArray start = 1;
     MWNumericArray stop = 20;
 
     //MATLAB fn
     Reactor.Class1 rxtr_lib = new Reactor.Class1();
     MWArray[] result = rxtr_lib.rxtr(1, start, stop);
     System.Array NH3 = new double[2 * (sp - st + 1)];
     NH3 = ((MWNumericArray)result[0]).ToVector(MWArrayComponent.Real);
 
 
     List<ChartDataClass1> chartData1 = new List<ChartDataClass1>();
     List<ChartDataClass2> chartData2 = new List<ChartDataClass2>();
     for (int i = 0; i < (sp - st); i++)
     {
         ChartDataClass1 cdc = new ChartDataClass1();
         ChartDataClass2 cdc2 = new ChartDataClass2();
         cdc.X1 = i;
         cdc.Y1 = Convert.ToDouble(NH3.GetValue(2 * i));
         cdc2.X2 = i;
         cdc2.Y2 = Convert.ToDouble(NH3.GetValue(2 * i + 1));
 
         chartData1.Add(cdc);
         chartData2.Add(cdc2);
     }
      
     return chartData1;
     
}

The code above populates chartData1 and chartData2, but only allows me to return a single type(chartdataclass1 OR chartdataclass2). My question is, what must I do so that I can return chartData1 AND chartData2 to e.Result? Also, assuming I can return both, how do I then set up e.Result such that I can define Series[0] and Series[1] using e.Result?

2 Answers, 1 is accepted

Sort by
0
Maryam
Top achievements
Rank 1
answered on 02 Jan 2015, 07:56 PM
i have written a wcf service:

        public List<string> GetECGValue()
        {
            var query2 = (from r in obj.ECGs where (r.Reading_ID >= 1 && r.Reading_ID <= 10) select r.Value).ToList();
            return query2;
        }

        DataClasses1DataContext obj = new DataClasses1DataContext();
    }
and this is the chart where i need to bind this service:

<chart:LineSeries x:Name="C" Stroke="Orange" StrokeThickness="2" ItemsSource="{Binding}">

                        <!--<chart:LineSeries.DataPoints>
                            <chartEngine:CategoricalDataPoint Value="20"/>
                            <chartEngine:CategoricalDataPoint Value="40"/>
                            <chartEngine:CategoricalDataPoint Value="35"/>
                            <chartEngine:CategoricalDataPoint Value="40"/>
                            <chartEngine:CategoricalDataPoint Value="30"/>
                            <chartEngine:CategoricalDataPoint Value="50"/>
                        </chart:LineSeries.DataPoints>-->
                    </chart:LineSeries> 
ServiceReference1.Service1Client ser = new ServiceReference1.Service1Client();
        public GraphECG()
        {
            InitializeComponent();
            ser.GetECGValueCompleted += new EventHandler<GetECGValueCompletedEventArgs>(ser_GetECGValueCompleted);
            ser.GetECGValueAsync();
        }
        void ser_GetECGValueCompleted(object sender, GetECGValueCompletedEventArgs e)
        {
            this.chart.Series[0].ItemsSource = e.Result;
        }
when i run the project, it show "no data to plot"..



0
Martin Ivanov
Telerik team
answered on 05 Jan 2015, 07:28 AM
Hello Maryam,

In order to show the chart's data points when the series is data bound you should set the series' binding properties. The ValueBinding and CategoryBinding properties for the categorical series and the XValueBinding and YValueBinding for the continuous series. They are used to bind the properties from your view model to the DataPoint's properties.

<chart:LineSeries ItemsSource="{Binding}" ValueBinding="MyYValue" CategoryBinding="MyXValue">

You can take a look at the Create Data-Bound Chart help article which demonstrates this.

I hope this helps.

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ChartView
Asked by
M
Top achievements
Rank 1
Answers by
Maryam
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or