我正在尝试使用包含我需要的所有数据的数据表创建折线图,但似乎无法成功对其进行分组。
我需要按月/年对数据表进行分组,并对每个月/年内的所有记录进行计数。
我实际上试图提取数据并在excel中制作图形。所以应该看起来像这样。
忽略那些由excel自动生成的季度
代码如下:
var chartResults = (from myrow in chartInstruction.Chart.AsEnumerable()
group myrow by new
{
series = myrow.Field<string>(chartInstruction.Series),
xaxis = myrow.Field<DateTime>(chartInstruction.Xaxis).ToShortDateString()
}
into g
select new
{
Field = g.Key,
Total = g.Count()
}).ToArray();
string[] xAxis = chartResults.Select(x => x.Field.xaxis).Distinct().OrderBy(x => x).ToArray();
string[] series = chartResults.Select(x => x.Field.series).Distinct().OrderBy(x => x).ToArray();
var seriesInfo = new List<SeriesInfo>();
foreach (string s in series)
{
var tmp = new List<double>();
foreach (string xx in xAxis)
{
var found = chartResults.FirstOrDefault(x => x.Field.xaxis == xx && x.Field.series == s);
tmp.Add(found?.Total ?? 0);
}
seriesInfo.Add(new SeriesInfo { Name = s, Series = tmp.ToArray() });
}