계열에 데이터 바인딩(차트 컨트롤)
이 항목에서는 다양한 데이터 바인딩 기술에 대해 설명합니다. 계열에 데이터를 바인딩하는 방법에 대한 자습서는 자습서: 데이터베이스에 차트 데이터 바인딩을 참조하십시오.
데이터 바인딩 메서드
메서드 |
이점 |
단점 |
---|---|---|
Chart.DataBindTable |
|
|
Chart.DataSource 및 Chart.DataBind |
|
|
Points.DataBind(X)Y |
|
|
Points.DataBind |
위와 동일
|
|
Chart.DataBindCrossTable |
|
|
데이터 소스
바인딩하는 데 사용될 수 있는 데이터 소스는 다음과 같습니다.
DataView
데이터 판독기(SQL, OleDB)
데이터 집합(DataSource 데이터 바인딩에만 해당)
배열
목록
모든 IEnumerable 개체
참고
목록, 배열 등과 같이 표 형식이 아닌 데이터 소스를 사용할 경우 사용되는 데이터 바인딩 메서드의 유형에 관계없이 Y 값만 바인딩할 수 있습니다. X 값과 다른 차트 속성(예: Tooltip)에 대해 열을 지정할 수 없기 때문입니다.
예제
다음 코드에서는 세로 막대형 차트를 Access 데이터베이스 테이블에 바인딩하는 방법을 보여 줍니다. "SALESCOUNTS" 테이블에는 영업 사원의 이름이 있는 "SalesRep" 열과 "Prod A" 열, "Prod B" 열, "Prod C" 열, "Prod D" 열, "Prod E" 열 및 "Other" 열이 있습니다. DataBindTable 메서드는 각 제품 열과 "Other" 열에 대해 하나씩 6개의 Series 개체를 자동으로 만듭니다. 레코드별로 각 계열에 하나의 데이터 요소가 자동으로 존재합니다. 또한 메서드는 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