加載數據到一個鏈表,並插入丟失的數據點:
using System;
using System.Collections.Generic;
namespace Whatever
{
public struct DataPoint
{
private DateTime time;
private int value;
public DataPoint(DateTime time, int value)
{
this.time = time;
this.value = value;
}
public DateTime Time
{
get { return this.time; }
}
public int Value
{
get { return this.value; }
}
public override string ToString()
{
return string.Format("{0:D2}/{1}: {2}", this.time.Month, this.time.Year, this.value);
}
}
public static class Program
{
public static void Main()
{
// List of the datapoints, e.g. loaded from a database
var dataPoints = new List<DataPoint>();
dataPoints.Add(new DataPoint(new DateTime(2010, 11, 1), 10));
dataPoints.Add(new DataPoint(new DateTime(2011, 2, 1), 20));
dataPoints.Add(new DataPoint(new DateTime(2011, 3, 1), 30));
dataPoints.Add(new DataPoint(new DateTime(2011, 6, 1), 40));
dataPoints.Add(new DataPoint(new DateTime(2011, 9, 1), 50));
dataPoints.Add(new DataPoint(new DateTime(2011, 12, 1), 60));
dataPoints.Add(new DataPoint(new DateTime(2012, 2, 1), 70));
// Endpoints of the measurement interval
var begin = new DateTime(2010, 9, 1);
var end = new DateTime(2012, 4, 1);
// Check each month and insert missing datapoints
var time = begin;
var i = 0;
while (time <= end)
{
if (i < dataPoints.Count)
{
if (time < dataPoints[i].Time)
{
var dataPoint = new DataPoint(time, 0);
dataPoints.Insert(i, dataPoint);
}
}
else
{
var dataPoint = new DataPoint(time, 0);
dataPoints.Add(dataPoint);
}
++i;
time = time.AddMonths(1);
}
// Print list
foreach (var dataPoint in dataPoints)
Console.WriteLine(dataPoint);
}
}
}
編輯: 如果您只需要描繪出這些數據,那麼就沒有必要插入丟失的數據點。我只是在現有的點之間進行插值,我的意思是將它們連接起來。如果現有點用實心圓表示,那麼缺失點可以用空圓圈表示,坐在這些連接線上(這樣的圖形控制可以寫入通過插值給出的繪圖點而不存儲它們)。
來源
2011-11-12 22:25:44
kol
從你在哪裏得到的數據?數據庫?網絡服務?其他一些數據源? – PiRX
數據來自存儲過程的數據庫。 – Darren