Merging the chart objects with data

This second step shows how to merge the pre-defined chart objects with data from an XML data source. In the first step, a chart placeholder with a unique name has been added to the document. To merge the chart objects, the ChartCollection can be used to iterate through all charts in order to merge them with data.

Choose Load... from the File main menu and select the template created in step 1. Now, click Merge from the Charts main menu to start the merge process.

In the background, the XML data file is loaded into a DataSet. If the DataSet contains a DataTable with the name of the inserted ChartFrame, the new data is applied to the chart object:

[C#]
// create a new DataSet and load the XML data
DataSet ds = new DataSet();
ds.ReadXml("data.xml");

foreach (ChartFrame chartFrame in textControl1.Charts)
{
	// check whether the DataSet contains data
	// related to the chart name
	if (ds.Tables.Contains(chartFrame.Name) == false)
		continue;

	Chart chart = chartFrame.Chart as Chart;

	// set the x and y values to the first 2 columns of the DataTable
	chart.Series[0].XValueMember = ds.Tables[chartFrame.Name].Columns[0].ColumnName;
	chart.Series[0].YValueMembers = ds.Tables[chartFrame.Name].Columns[1].ColumnName;

	// set the data to the chart
	chart.DataSource = ds.Tables[chartFrame.Name];

	// data bind to the selected data source
	chart.DataBind(); 
}
[Visual Basic]
' create a new DataSet and load the XML data
Dim ds As New DataSet()
ds.ReadXml("data.xml")

For Each chartFrame As TXTextControl.ChartFrame In textControl1.Charts
	' check whether the DataSet contains data
	' related to the chart name
	If ds.Tables.Contains(chartFrame.Name) = False Then
		Continue For
	End If

	Dim chart As DataVisualization.Charting.Chart = TryCast(chartFrame.Chart, DataVisualization.Charting.Chart)

	' set the x and y values to the first 2 columns of the DataTable
	chart.Series(0).XValueMember = ds.Tables(chartFrame.Name).Columns(0).ColumnName
	chart.Series(0).YValueMembers = ds.Tables(chartFrame.Name).Columns(1).ColumnName

	' set the data to the chart
	chart.DataSource = ds.Tables(chartFrame.Name)

	' data bind to the selected data source
	chart.DataBind()
Next