public Form1()
{
InitializeComponent();
var serieItem = this.chart1.Series[0];
serieItem.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
//X轴坐标 值类型为 时间类型
serieItem.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
serieItem.IsValueShownAsLabel = true;
serieItem.IsXValueIndexed = true;
DataTable table = GetDataSource();
DataPoint pointItem = null;
for (int i = 0; i < table.Rows.Count; i++)
{
pointItem = new DataPoint();
pointItem.SetValueXY(table.Rows[i]["TimeValue"], table.Rows[i]["Value"]);
serieItem.Points.Add(pointItem);
}
}
DataTable GetDataSource()
{
DataTable table = new DataTable();
table.Columns.Add("Value", typeof(double));
table.Columns.Add("TimeValue", typeof(DateTime));
DataRow row = table.NewRow();
row["Value"] = 45;
row["TimeValue"] = DateTime.Parse("2010-08-08 9:30");
table.Rows.Add(row);
row = table.NewRow();
row["Value"] = 50;
row["TimeValue"] = DateTime.Parse("2010-08-08 10:00");
table.Rows.Add(row);
row = table.NewRow();
row["Value"] = 55;
row["TimeValue"] = DateTime.Parse("2010-08-08 10:30");
table.Rows.Add(row);
row = table.NewRow();
row["Value"] = 45;
row["TimeValue"] = DateTime.Parse("2010-08-08 11:00");
table.Rows.Add(row);
row = table.NewRow();
row["Value"] = 52;
row["TimeValue"] = DateTime.Parse("2010-08-08 11:30");
table.Rows.Add(row);
row = table.NewRow();
row["Value"] = 43;
row["TimeValue"] = DateTime.Parse("2010-08-08 12:00");
table.Rows.Add(row);
row = table.NewRow();
row["Value"] = 60;
row["TimeValue"] = DateTime.Parse("2010-08-08 12:30");
table.Rows.Add(row);
return table;
}
提示:
serieItem.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
这句就是设置 X 轴 值的类型。