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.
- dataset
- dataset Column
- Data Table
- SqlCommand/OleDbCommand
- SqlDataAdapter/OleDbDataAdapter
- Array
Below are some examples of binding to different data sources at run time.
dataset
The Chart control's DataSource property can be set to a dataset at run time. The following code demonstrates setting up a dataset, setting the DataSource property to the dataset, creating a series, and setting the ValueMembersY property to the dataset 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:/Northwind.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 dataset 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:/Northwind.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 dataset 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);
dataset Column
In the Chart control, the ValueMembersX and ValueMembersY properties of a series can be set to a dataset column. The following code demonstrates creating a series, setting up a dataset, setting the DataSource property to the dataset, and setting the ValueMembersY and ValueMembersX properties to dataset 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:/Northwind.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 dataset 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:/Northwind.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 dataset 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;
Data Command
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:/Northwind.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:/Northwind.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();
Array
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);