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.
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;
});