由于我对图表控件知之甚少,我尝试了一些东西。首先,我将数据与 Y 轴上的 Id=0 绑定以获取日期(可能会做得更好)然后我循环数据并每行制作一个系列。每个系列我都做了一个随机的颜色,但有时颜色太白,所以不显示。
public class VersionData
{
public int Id { get; set; }
public double VersionNo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
string dateFormat = "yyyy MMM dd";
List<VersionData> version = new List<VersionData>();
version.Add(new VersionData() { Id=0, VersionNo = 1.1, StartDate = new DateTime(2013, 1, 1), EndDate = new DateTime(2013, 1, 31) });
version.Add(new VersionData() { Id=0, VersionNo = 1.2, StartDate = new DateTime(2013, 2, 1), EndDate = new DateTime(2013, 12, 31) });
version.Add(new VersionData() {Id=0, VersionNo = 2.1, StartDate = new DateTime(2013, 1, 10), EndDate = new DateTime(2013, 1, 25) });
version.Add(new VersionData() {Id=0, VersionNo = 2.2, StartDate = new DateTime(2013, 1, 26), EndDate = new DateTime(2013, 2, 16) });
version.Add(new VersionData() { Id=0, VersionNo = 3.1, StartDate = new DateTime(2013, 3, 16), EndDate = new DateTime(2013, 4, 30) });
Chart1.Series[0].YValueMembers = "Id";
Chart1.DataSource = version;
Random randomGen = new Random();
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
for (int i = 0; i < version.Count; i++)
{
Series s = new Series("s" + i.ToString());
s.ChartType = SeriesChartType.Line;
s.Color = Color.FromKnownColor(names[randomGen.Next(names.Length)]);
s.BorderWidth = 4;
Chart1.Series.Add(s);
DataPoint p = new DataPoint();
p.SetValueXY(version[i].StartDate, version[i].VersionNo);
s.Points.Add(p);
DataPoint p2 = new DataPoint();
p2.SetValueXY(version[i].EndDate, version[i].VersionNo);
s.Points.Add(p2);
}
Chart1.Series[0].XValueMember = "StartDate";
Chart1.ChartAreas[0].AxisX.Interval = 1;
Chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months;
Chart1.ChartAreas[0].AxisX.LabelStyle.Format = dateFormat;
Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
Chart1.ChartAreas[0].AxisY.Interval = 0.5;
Chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0.0";
}