Binding the Chart to a DataTable from DataSet
Here is sample code that creates the chart from the data table.
In Code:
Private _dataSet As DataSet
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
' create connection and fill data set
Dim mdbFile As String = "c:\db\nwind.mdb"
Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile)
Dim conn As New OleDbConnection(connString)
Dim adapter As New OleDbDataAdapter("SELECT TOP 10 ProductName, UnitPrice FROM Products " & vbCr & vbLf & " ORDER BY UnitPrice;", conn)
_dataSet = New DataSet()
adapter.Fill(_dataSet, "Products")
' set data table rows as the source for chart data
c1Chart1.Data.ItemsSource = _dataSet.Tables("Products").Rows
End Sub
•C#
DataSet _dataSet;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// create connection and fill data set
string mdbFile = @"c:\db\nwind.mdb";
string connString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}",
mdbFile);
OleDbConnection conn = new OleDbConnection(connString);
OleDbDataAdapter adapter = new OleDbDataAdapter(
@"SELECT TOP 10 ProductName, UnitPrice FROM Products
ORDER BY UnitPrice;", conn);
_dataSet = new DataSet();
adapter.Fill(_dataSet, "Products");
// set data table rows as the source for chart data
c1Chart1.Data.ItemsSource = _dataSet.Tables["Products"].Rows;
}
In XAML:
<c1chart:C1Chart.Data>
<c1chart:ChartData ItemNameBinding="{Binding Path=[ProductName]}">
<c1chart:DataSeries ValueBinding="{Binding Path=[UnitPrice]}"/>
</c1chart:ChartData>
</c1chart:C1Chart.Data>