系列へのデータのバインド (グラフ コントロール)
ここでは、さまざまなデータ バインディング技術について説明します。系列にデータをバインドする方法のチュートリアルについては、「チュートリアル: グラフからデータベースへのデータ バインディング」を参照してください。
データ バインドのメソッド
メソッド |
利点 |
欠点 |
---|---|---|
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 メソッドによって 6 個の Series オブジェクトが自動的に作成されます。製品の各列に 1 個、"Other" 列に 1 個が作成されます。1 つのレコードにつき、各系列内の 1 つのデータ ポイントが自動的に作成されます。また、このメソッドで、6 個の系列の 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