模型
public class MeterReadingsBccChartData
{
public string BranchName { get; set; }
public string TypeName { get; set; }
public string Unit { get; set; }
public List<Tuple<DateTime, double>> DateAndValue { get; set; }
public MeterReadingsBccChartData(List<Tuple<DateTime, double>> dateAndValue, string typeName, string unit, string BranchName)
{
this.DateAndValue = dateAndValue;
this.TypeName = typeName;
this.Unit = unit;
this.BranchName = BranchName;
}
}
model-to-chartSeries方法
public static List<Series> GetBccChartData(IEnumerable<MeterReadingsBccChartData> meterReadings)
{
List<Series> series = new List<Series>();
foreach (var item in meterReadings)
{
int lenght = item.DateAndValue.Count();
object[,] data = new object[lenght, 2];
for (int i = 0; i < lenght; i++)
{
data[i, 0] = item.DateAndValue[i].Item1;
data[i, 1] = item.DateAndValue[i].Item2;
}
Series localSeries = new Series { Name = item.BranchName + " " + item.TypeName + " " + item.Unit, Data = new Data(data), Type = ChartTypes.Column };
series.Add(localSeries);
}
return series;
}
以上代码说明
我从db读取数据。数据类型为MeterReadingsBccChartData
。然后我将其转换为Series
数据类型,并将此系列存储为列表。我想在此列表中添加一个平均系列。但可能没有相同日期的数据。例如,我的数据如下:
Series 1 : { { date : 01.01.2013, value : 5 }, { date : 02.01.2013, value : 10 } }
Series 2 : { { date : 01.01.2013, value : 11 }, { date : 03.01.2013, value : 3 } }
我想计算这些系列的平均值我想要这样的事情:
Series AVG : { { date : 01.01.2013, value : 8 }, { date : 02.01.2013, value : 10 } { date : 03.01.2013, value : 3 } }
我可以通过第二次db获取来完成此操作。我得到日期之间的平均值。所以我从db获取数据两次。
有没有办法在上面的方法中计算这个系列的平均值?有小费吗?
我希望我能解释一下......
...谢谢
答案 0 :(得分:0)
可能我无法解释我的问题。我找到了解决方案。我不知道这是最好的,但我从db中获取数据一次。
public static List<Series> GetBccChartData(IEnumerable<MeterReadingsBccChartData> meterReadings)
{
List<Tuple<DateTime, double>> allData = new List<Tuple<DateTime, double>>();
List<Series> series = new List<Series>();
foreach (var item in meterReadings)
{
int lenght = item.DateAndValue.Count();
object[,] data = new object[lenght, 2];
for (int i = 0; i < lenght; i++)
{
allData.Add(item.DateAndValue[i]);
data[i, 0] = item.DateAndValue[i].Item1;
data[i, 1] = item.DateAndValue[i].Item2;
}
Series localSeries = new Series { Name = item.BranchName + " " + item.TypeName + " " + item.Unit, Data = new Data(data), Type = ChartTypes.Column };
series.Add(localSeries);
}
// ortalama serisi hesaplanıyor.
List<Tuple<DateTime, double>> averageData = allData.GroupBy(x => x.Item1).Select(x => Tuple.Create<DateTime, double>(x.Key, x.Average(item => item.Item2))).ToList();
int avgDataLenght = averageData.Count();
object[,] dataAvg = new object[avgDataLenght, 2];
for (int i = 0; i < avgDataLenght; i++)
{
dataAvg[i, 0] = averageData[i].Item1;
dataAvg[i, 1] = averageData[i].Item2;
}
Series avgSeries = new Series { Name = "Genel Toplam", Data = new Data(dataAvg), Type = ChartTypes.Line };
series.Add(avgSeries);
return series;
}