3

I am trying to create an XyDataSeries for every column of my Datatable programmatically to be used for charting (SciChart). I need to do this is because the amount of columns and names are unknown in advance.

For any given series the x value will always just be an incrementing integer from 0 and the y values are the column values.

I am importing excel data into the Datatable and then passing each column to a Dictionary as a List where the column header is the name of the List by the following;

dict = dt.Columns.Cast<DataColumn>().ToDictionary(c => c.ColumnName, c => dt.AsEnumerable().Select(r => r[c]).ToList());

Can i do something like the above where Dictionary<string, XyDataSeries<double, double>>?

Assuming i did know the amount of columns and their names, I could do something like this for every column/list;

Manually creating an XyDataSeries and iterating through a list to append the values. I then set the chartseries.DataSeries for the chart (defined in XAML) to the respective XyDataSeries.

var list = dict["ListName"];
XyDataSeries<double, double> xyseries;
xyseries = new XyDataSeries<double, double>() { SeriesName = "ListName" };
foreach (var i in list)
{
    double x = 1;
    var d = Convert.ToDouble(i);
    xyseries.Append(x++, d);
}
chartseries.DataSeries = xyseries;

I also have to declare the series in XAML - Can I get around this?

Having to do this over 600 times is far from ideal though and I am really desperate for an elegant solution for this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
User9123
  • 45
  • 6
  • you can iterate over Dictionary: `foreach(var keyValuePair in dict) { var list = keyValuePair.Value; ... }` – ASh Dec 04 '17 at 14:08
  • Hi, yeah i figured i could iterate over the dictionary, what really want to know is how can i create one `Dictionary>` so that I can iterate over that to return the XyDataSeries? Rather than having to set them all manually. – User9123 Dec 04 '17 at 14:20

1 Answers1

1

Can i do something like the above where Dictionary<string, XyDataSeries<double, double>>?

The ToDictionary method accepts a Func<TSource, TElement> as its second argument. So you could create and populate the XyDataSeries<double, double> in this one. Something like this:

dict = dt.Columns.Cast<DataColumn>()
    .ToDictionary(c => c.ColumnName, c =>
    {
        var dataSeries = new XyDataSeries<double, double>();
        double x = 1.0;
        dataSeries.Append(dt.AsEnumerable().Select(_ => x++), dt.AsEnumerable().Select(r => Convert.ToDouble(r[c])));
        return dataSeries;
    });
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thanks, I tried this and I get an exception basically saying it cant implicitly convert type List<> to Dictionary<>... The datatable is null until its data is imported through a click event, would a null value cause an issue here? – User9123 Dec 04 '17 at 22:26
  • Where are you converting a List to a Dictionary...!? – mm8 Dec 05 '17 at 08:00
  • Think i mis-understood the error; `Cannot implicitly convert type 'System.Collections.Generic.List>>' to 'System.Collections.Generic.Dictionary>'` – User9123 Dec 05 '17 at 11:14
  • Thanks. The only strange thing is that the x value seems to change depending on how much data i pull in rather than always starting at 1 for each series. Any thoughts on this? – User9123 Dec 09 '17 at 16:17