0

模型

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

模型到图表系列方法

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 获取数据。

有没有办法在上述方法中计算这个系列的平均值?有小费吗?

我希望我能解释...

谢谢...

4

1 回答 1

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;
}
于 2013-05-09T11:50:11.597 回答