Glossary Item Box
The Chart control makes it easy to set the data source for a chart control, series, or data points collection at run time.
Below is a list of objects that can be used as data sources.
Below are some examples of binding to different data sources at run time.
The Chart control's DataSource property can be set to a data set at run time. The following code demonstrates setting up a data set, setting the DataSource property to the data set, creating a series, and setting the ValueMembersY property to the data set expression at run time.
' Visual Basic ' create the series Dim s As New DataDynamics.ActiveReports.Chart.Series Dim m_cnnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NWIND.MDB; _ Persist Security Info=False" Dim m_cnn As New System.Data.OleDb.OleDbConnection(m_cnnString) Dim oDBAdapter As System.Data.OleDb.OleDbDataAdapter ' create the data set Dim oDS As DataSet oDBAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT ShipCountry, SUM(Freight) AS _ Expr1 FROM Orders GROUP BY ShipCountry", m_cnnString) oDS = New DataSet oDBAdapter.Fill(oDS, "Expr1") ' set the DataSource and ValueMembersY properties Me.ChartControl1.DataSource = oDS s.ValueMembersY = "Expr1" Me.ChartControl1.Series.Add(s)
// C# // create the series DataDynamics.ActiveReports.Chart.Series s = new DataDynamics.ActiveReports.Chart.Series(); string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NWIND.MDB;Persist Security Info=False"; System.Data.OleDb.OleDbConnection m_cnn = new System.Data.OleDb.OleDbConnection(m_cnnString); System.Data.OleDb.OleDbDataAdapter oDBAdapter; // create the data set System.Data.DataSet oDS; oDBAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT ShipCountry, SUM(Freight) AS Expr1 FROM Orders GROUP BY ShipCountry", m_cnnString); oDS = new System.Data.DataSet(); oDBAdapter.Fill(oDS, "Expr1"); // set the DataSource and ValueMembersY properties this.ChartControl1.DataSource = oDS; s.ValueMembersY = "Expr1"; this.ChartControl1.Series.Add(s);
In the Chart control, the ValueMembersX and ValueMembersY properties of a series can be set to a data set column. The following code demonstrates creating a series, setting up a data set, setting the DataSource property to the data set, and setting the ValueMembersY and ValueMembersX properties to data set columns at run time.
' Visual Basic ' create the series Dim s As New DataDynamics.ActiveReports.Chart.Series Dim m_cnnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NWIND.MDB; _ Persist Security Info=False" Dim m_cnn As New System.Data.OleDb.OleDbConnection(m_cnnString) Dim oDBAdapter As System.Data.OleDb.OleDbDataAdapter ' create the data set Dim oDS As DataSet oDBAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * from Orders WHERE OrderDate _ < #08/17/1994#", m_cnnString) oDS = New DataSet oDBAdapter.Fill(oDS, "Orders") ' set the DataSource, ValueMembersY, and ValueMembersX properties Me.ChartControl1.DataSource = oDS Me.ChartControl1.Series.Add(s) Me.ChartControl1.Series(0).ValueMembersY = oDS.Tables("Orders").Columns(7).ColumnName Me.ChartControl1.Series(0).ValueMemberX = oDS.Tables("Orders").Columns(8).ColumnName
// C# // create the series DataDynamics.ActiveReports.Chart.Series s = new DataDynamics.ActiveReports.Chart.Series(); string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NWIND.MDB;Persist Security Info=False"; System.Data.OleDb.OleDbConnection m_cnn = new System.Data.OleDb.OleDbConnection(m_cnnString); System.Data.OleDb.OleDbDataAdapter oDBAdapter; // create the data set System.Data.DataSet oDS; oDBAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT * from Orders WHERE OrderDate < #08/17/1994#", m_cnnString); oDS = new System.Data.DataSet(); oDBAdapter.Fill(oDS, "Orders"); // set the DataSource, ValueMembersY, and ValueMembersX properties this.ChartControl1.DataSource = oDS; this.ChartControl1.Series.Add(s); this.ChartControl1.Series[0].ValueMembersY = oDS.Tables["Orders"].Columns[7].ColumnName; this.ChartControl1.Series[0].ValueMemberX = oDS.Tables["Orders"].Columns[8].ColumnName;
A chart's data source can be set to a SqlCommand or OleDbCommand. The following code demonstrates creating a series, creating an OleDbCommand, setting the DataSource property to the data command, and setting the ValueMembersY property for the series at run time.
' Visual Basic ' create the series Dim s As New DataDynamics.ActiveReports.Chart.Series Dim m_cnnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NWIND.MDB; _ Persist Security Info=False" Dim m_cnn As New System.Data.Oledb.OleDbConnection(m_cnnString) Dim query As String = "SELECT ShipCountry, SUM(Freight) AS Expr1 FROM Orders GROUP BY _ ShipCountry" ' create the OleDbCommand and open the connection Dim command As New System.Data.Oledb.OleDbCommand(query, m_cnn) command.Connection.Open() ' set the DataSource and ValueMembersY properties Me.ChartControl1.DataSource = command Me.ChartControl1.Series.Add(s) Me.ChartControl1.Series(0).ValueMembersY = "Expr1" ' close the connection m_cnn.Close() // C# // create the series DataDynamics.ActiveReports.Chart.Series s = new DataDynamics.ActiveReports.Chart.Series(); string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/NWIND.MDB;Persist Security Info=False"; System.Data.Oledb.OleDbConnection m_cnn = new System.Data.Oledb.OleDbConnection(m_cnnString); string query = "SELECT ShipCountry, SUM(Freight) AS Expr1 FROM Orders GROUP BY ShipCountry"; // create the OleDbCommand and opent the connection System.Data.Oledb.OleDbCommand command = new System.Data.Oledb.OleDbCommand(query, m_cnn); command.Connection.Open(); // set the DataSource and ValueMembersY properties this.ChartControl1.DataSource = command; this.ChartControl1.Series.Add(s); this.ChartControl1.Series[0].ValueMembersY = "Expr1"; // close the connection m_cnn.Close();
The Chart control allows the data source for the data points collection to be set to an array. The following code demonstrates creating a series, creating an array, and using the DataBindY method to set the data source for the data points collection at run time.
' Visual Basic ' create the series Dim s As New DataDynamics.ActiveReports.Chart.Series ' create the array Dim a As Double() = {1, 4, 2, 6, 3, 3, 4, 7} ' set the data source for the data points collection Me.ChartControl1.Series.Add(s) Me.ChartControl1.Series(0).Points.DataBindY(a)
// C# // create the series DataDynamics.ActiveReports.Chart.Series s = new DataDynamics.ActiveReports.Chart.Series(); // create the array double [] a = {1,4,2,6,3,3,4,7}; // set the data source for the data points collection this.ChartControl1.Series.Add(s); this.ChartControl1.Series[0].Points.DataBindY(a);
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.