2D Chart for WinForms | ComponentOne
Data Binding / Binding the Chart Directly to the Data Source
In This Topic
    Binding the Chart Directly to the Data Source
    In This Topic

    In most cases, data needs to be summarized before it can be charted, so there is a layer between the data source and the actual chart. In some cases, however, the data you want to plot may already be available in a DataView or similar data source object. In these cases, you can bind the chart directly to the data source object.

    To bind a C1Chart control to a data source, you should start by setting the control's DataSource property to a data source object such as a DataView or DataTable. Then bind individual series to columns on the data source object using the DataSeries.X.DataField and DataSeries.Y.DataField properties.

    You can do all this using the Chart Wizard, but if you prefer to do it in code here is a sample that demonstrates the entire process:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' DataBinding is only available with C1Chart
    ' version 1.0.20034.13244 and later. 
    ' get chart data
    Dim sql As String = "select * from products"
    Dim conn As String = "provider=… c1nwind.mdb" 
    Dim da As New OleDbDataAdapter(sql, conn)
    da.Fill(dt) 
    
    ' bind chart to table (each series will bind to a field on the table).          
    c1chart.DataSource = dt 
    
    ' clear data series collection.
    Dim dsc As ChartDataSeriesCollection = c1chart.ChartGroups(0).ChartData.SeriesList
    dsc.Clear() 
    
    ' add unit price series.
    Dim ds As ChartDataSeries = dsc.AddNewSeries() 
    
    'ds.AutoEnumerate = true ' (in case you don't want to set the X values).
    ds.X.DataField = "ProductName"
    ds.Y.DataField = "UnitPrice" 
    
    ' add units in stock series.
    ds = dsc.AddNewSeries()
    ds.X.DataField = "ProductName"
    ds.Y.DataField = "UnitsInStock" 
    
    ' apply filters, sorting, etc.
    dt.DefaultView.RowFilter = "CategoryID = 4"                       
    

    To write code in C#

    C#
    Copy Code
    // DataBinding is only available with C1Chart.
    // version 1.0.20034.13244 and later.  
    // get chart data
    string sql = "select * from products";
    string conn = @"provider=… c1nwind.mdb;";
    OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
    da.Fill(dt);
    
    // bind chart to table (each series will bind to a field on the table).   
    c1chart.DataSource = dt;
    // clear data series collection.
    ChartDataSeriesCollection dsc = c1chart.ChartGroups[0].ChartData.SeriesList;
    dsc.Clear();
    
    // add unit price series.
    ChartDataSeries ds = dsc.AddNewSeries();
    
    //ds.AutoEnumerate = true; // (in case you don't want to set the X values).
    ds.X.DataField = "ProductName";
    ds.Y.DataField = "UnitPrice";
    
    // add units in stock series.
    ds = dsc.AddNewSeries();
    ds.X.DataField = "ProductName";
    ds.Y.DataField = "UnitsInStock";
    
    // apply filters, sorting, etc.
    dt.DefaultView.RowFilter = "CategoryID = 4";
    

    The code retrieves the NWind Products table, then creates two data series, each bound to a column on the data source.

    The most interesting part of the bound table is that it maintains a dynamic connection to the data source. Any changes applied to the data source, including value changes, filters, sorting, new or deleted records, and so on, are automatically reflected on the chart.

    See Also