Share via


使用图表控件将数据绑定到序列

本主题介绍各种数据绑定技术。有关将数据绑定到序列的教程,请参见教程:将图表与数据库进行数据绑定

数据绑定方法

方法

优点

缺点

Chart.DataBindTable

  • X 和 Y 值的简单绑定。

  • 基于数据源中的列数自动创建序列。

  • 只需传递一次数据。

  • 每个序列没有多个 Y 值。

  • 所有序列具有相同 X 值,或未设置 X 值。

  • 没有用于扩展图表属性(如工具提示)的绑定。

Chart.DataSource

Chart.DataBind

  • 针对所有序列循环访问数据源一次。

  • 支持多个 Y 值。

  • 没有用于扩展图表属性(如工具提示)的绑定。

Points.DataBind(X)Y

  • 支持多个数据源,包括 X 值和 Y 值的单独数据源。

  • 支持多个 Y 值。

  • 比上面列出的两个方法更加灵活。

  • 没有用于扩展图表属性(如工具提示)的绑定。

  • 针对每个序列循环访问数据一次。

Points.DataBind

同上,另外还有:

  • 支持用于扩展图表属性(如工具提示)的绑定。

  • 针对每个序列循环访问数据一次。

  • 不支持一个序列的 X 和 Y 值具有不同的数据源。

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

其他资源

数据绑定和操作