將資料繫結至數列 (Chart 控制項)
本主題描述各種資料繫結技巧。如需將資料繫結至數列的教學課程,請參閱教學課程:將圖表資料繫結至資料庫。
資料繫結方法
方法 |
優點 |
缺點 |
---|---|---|
Chart.DataBindTable |
|
|
Chart.DataSource 和 Chart.DataBind |
|
|
Points.DataBind(X)Y |
|
|
Points.DataBind |
同上,加上:
|
|
Chart.DataBindCrossTable |
|
|
資料來源
下列是用於繫結的可能資料來源:
DataView。
資料讀取器 (SQL、OleDB)。
DataSet (僅限 DataSource 資料繫結)。
陣列。
清單。
所有 IEnumerable 物件。
注意
使用非表格式資料來源 (例如清單或陣列) 時,無論使用的資料繫結方法類型為何,只能繫結 Y 值。這是因為資料行無法指定給 X 值和其他圖表屬性,例如 Tooltip。
範例
下列程式碼示範如何將資料行圖表繫結至 Access 資料庫資料表。資料表 "SALESCOUNTS" 有包含銷售人名的 "SalesRep" 資料行、"Prod A" 資料行、"Prod B" 資料行、"Prod C" 資料行、"Prod D" 資料行、"Prod E" 資料行和 "Other" 資料行。DataBindTable 方法會自動建立六個 Series 物件:每個產品資料行各一個,"Other" 資料行一個。每筆記錄中每個數列自動擁有一個資料點。此方法也會將六個數列的 X 值繫結至 "SalesRep" 資料行,並將其用於資料點的軸標籤。
Imports System.Data.OleDb
Imports System.Data
Imports System.Web.UI.DataVisualization.Charting
...
' Resolve the address to the Access database. We assume database is
' in Bin folder.
Dim fileNameString As String = "chartdata.mdb"
' Initialize a connection string.
Dim myConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString
' Define the database query.
Dim mySelectQuery As String = "SELECT * FROM SALESCOUNTS;"
' Create a database connection object using the connection string.
Dim myConnection As OleDbConnection = New OleDbConnection(myConnectionString)
' Create a database command on the connection using query.
Dim myCommand As OleDbCommand = New OleDbCommand(mySelectQuery,myConnection)
' Open the connection.
myCommand.Connection.Open()
' Create a database reader.
Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
' Specify the Name column to be used for point's X values.
chart1.DataBindTable(myReader,"SalesRep")
' Close the connection.
myConnection.Close()
' This is a loop to set all created charts appearance with custom attribute.
Dim series As Series
For Each series In chart1.Series
series.CustomAttributes = "DrawingStyle=LightToDark"
Next
using System.Data.OleDb;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
...
// Resolve the address to the Access database. We assume database is
// in Bin folder.
string fileNameString = "chartdata.mdb";
// Initialize a connection string.
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// Define the database query.
string mySelectQuery="SELECT * FROM SALESCOUNTS;";
// Create a database connection object using the connection string.
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
// Create a database command on the connection using query.
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
// Open the connection.
myCommand.Connection.Open();
// Create a database reader.
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// Specify the Name column to be used for point's X values.
chart1.DataBindTable(myReader,"SalesRep");
// Close the connection.
myConnection.Close();
// This is a loop to set all created charts appearance with custom attribute.
foreach (Series series in chart1.Series)
{
series.CustomAttributes = "DrawingStyle=LightToDark";
}
請參閱
參考
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting