關於圖表數據的來源:
1、通過XValueMember YValueMembers 設置
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand comm = new OleDbCommand();
string sqlstr = "select * from OriginalData order by ID";
OleDbDataAdapter myda ;
DataSet myds= new System.Data.DataSet();
try
{
conn.Open();
myda = new OleDbDataAdapter(sqlstr, conn);
//myds = new System.Data.DataSet();
myda.Fill(myds, "OriginalData");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
chart1.DataSource = myds.Tables["OriginalData"];
chart1.Series[0].XValueMember = "PositionV";
chart1.Series[0].YValueMembers = "LoadV";
//chart1.Series[0].XValueType = ChartValueType.Date;
//chart1.Series[0].Label = "#VAL"; //顯示對應點的值
chart1.Series[0].ToolTip = "#VALX : #VAL"; //鼠標在曲線圖上時 顯示對應點的值
chart1.Series[0].ChartType = SeriesChartType.Line;
2、用新定義的DataTable類型的dt來綁定
DataTable dt = new System.Data.DataTable();
AccessHelper ach= new AccessHelper(fileName);
string sqlstr = "select LoadValue ,PositionValue from OriginalData order by ID";
dt = ach.GetDataTableFromDB(sqlstr);
chart1.DataSource = dt;
chart1.Series[0].XValueMember = dt.Columns[0].ColumnName;
chart1.Series[0].YValueMembers = dt.Columns[1].ColumnName;
chart1.Series[0].ChartType = SeriesChartType.Line;
ach.ClostConn();
3、用類似chart1.Series[0].Points.DataBindXY(iXAxis,fTargetOT);中的DataBindXY(X,Y)來綁定數據。。其中,X是一個數組,Y也是數組,在綁定之前需要對數組中的每個元素賦值
DataTable dt = new System.Data.DataTable();
AccessHelper ach = new AccessHelper(fileName);
string sqlstr = "select LoadV ,PositionV from OriginalData order by ID";
dt = ach.GetDataTableFromDB(sqlstr);
////chart1.DataSource = dt;
////chart1.Series[0].XValueMember = dt.Columns[0].ColumnName;
////chart1.Series[0].YValueMembers = dt.Columns[1].ColumnName;
////chart1.Series[0].ChartType = SeriesChartType.Line;
int count = dt.Rows.Count;
if (count == 0)
{
return;
}
double[] loadValueArray = new double[count];
double[] posArray= new double[count];
for (int i = 0; i < count; i++)
{
loadValueArray[i] = Convert.ToDouble(dt.Rows[i][0].ToString());
posArray[i] = Convert.ToDouble(dt.Rows[i][1].ToString());
}
chart1.Series[0].Points .DataBindXY(loadValueArray,posArray);
此種方式,適合,讀過來的數據需要進行處理的情況,如果不需要處理,而且列特別多,則不建議使用這種方式,用方式二比較方便。
4、來自單獨的數據,動態添加序列 通過定時器 實時調用
chart3.Series.Clear();
Series seriesDownTime =newSeries("DownTime");//此時就添加一個序列,chart3.Series[0]
seriesDownTime.ChartType =SeriesChartType.Column;
//比如要添加的內容放在了數組X和數組Y中,則添加第一個值可以如下添加:
seriesDownTime.Points.AddXY(X[n],Y[n]);
下一個值與之類似,直到把很少的幾個點添加完成即可。。。
此種方式適合要顯示在圖表上的只有少量的幾個點,如果數據眾多,建議按集合來添加。
