2D Chart for WinForms | ComponentOne
Data Binding / Functional Program Using Data Binding
In This Topic
    Functional Program Using Data Binding
    In This Topic

    The following code, when added to a new project with the C1Chart control added to the form, demonstrates a fully functional program using data binding. Please note that it is also necessary to create and handle the Form_Load event. The database used in this sample is the NorthWind database supplied and used by Microsoft, as well as by ComponentOne Studio Enterprise. If the file path does not match your installation, it will be necessary to change the database before execution.

    The following sample manipulates the database and C1Chart entirely through code. However, using the Microsoft .NET IDE and its property sheet, the entire setup can be managed at design time. Please note that the use of two DataAdapters with TableMapping allows a single DataSet to be used as the DataSource, with two series charted from the same table.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub BindMultipleSeriesViewsAndChartSetup(ByVal chart As C1.Win.C1Chart.C1Chart)
    
            ' following objects are in namespace System.Data.OleDb
            Dim connect As OleDbConnection = New OleDbConnection()
            Dim adapt1 As OleDbDataAdapter = New OleDbDataAdapter()
            Dim adapt2 As OleDbDataAdapter = New OleDbDataAdapter()
            Dim select1 As OleDbCommand = New OleDbCommand()
            Dim select2 As OleDbCommand = New OleDbCommand()
    
            ' set up the connect to the ComponentOne sample database.
            connect.ConnectionString = _
              "Provider=Microsoft.Jet.OLEDB.4.0;" + _
              "User ID=Admin;" + _
              "Data Source=C:\Program Files\ComponentOne Studio.Net\common\C1NWIND.MDB;" + _ 
                  "Jet OLEDB:Engine Type=5;"
    
            ' select Save-A-Lot entries in [Sales Totals by Amount]
            select1.CommandText = _
              "SELECT SaleAmount, ShippedDate, CompanyName " + _
              "FROM [Sales Totals by Amount] " + _
              "WHERE (CompanyName = 'Save-a-lot Markets') " + _
              "ORDER BY ShippedDate"
            select1.Connection = connect
    
            ' select Quick-Stop entries [Sales Totals by Amount]
            select2.CommandText = _
              "SELECT SaleAmount, ShippedDate, CompanyName " + _
              "FROM [Sales Totals by Amount] " + _
              "WHERE (CompanyName = 'QUICK-Stop') " + _
              "ORDER BY ShippedDate"
            select2.Connection = connect
    
            ' using System.Data.Common namespace for the Mapping objects.
            ' TableMapping is used to allow multiple views of the same table
            ' to appear in the same DataSet.
    
            ' set up the adapter, adapt1.
            adapt1.SelectCommand = select1
            Dim ColumnMaps_SaveALot As DataColumnMapping() = _
            { _
              New DataColumnMapping("SaleAmount", "SaleAmount"), _
              New DataColumnMapping("CompanyName", "CompanyName"), _
              New DataColumnMapping("ShippedDate", "ShippedDate") _
            }
            adapt1.TableMappings.Add(New DataTableMapping("Table", "SaveALot", _
             ColumnMaps_SaveALot))
    
            ' set up the adapter, adapt2.
            adapt2.SelectCommand = select2
    
            Dim ColumnMaps_QuickStop As DataColumnMapping() = _
            { _
              New DataColumnMapping("SaleAmount", "SaleAmount"), _
              New DataColumnMapping("CompanyName", "CompanyName"), _
              New DataColumnMapping("ShippedDate", "ShippedDate") _
            }
    
            adapt2.TableMappings.Add(New DataTableMapping("Table", "QuickStop", _
             ColumnMaps_QuickStop))
    
            ' Create the dataset and fill it from all adapters.
            Dim ds As DataSet = New DataSet()
            adapt1.Fill(ds)
            adapt2.Fill(ds)
    
            ' set up the chart, assigning the DataSource, DataFields and properties.
            chart.Dock = DockStyle.Fill
            chart.DataSource = ds
            Dim sc As ChartDataSeriesCollection = chart.ChartGroups(0).ChartData.SeriesList
            sc.RemoveAll()
    
            ' Add the Save-A-Lot series.
            Dim s As ChartDataSeries = sc.AddNewSeries()
            s.Label = "Save-A-Lot"
            s.X.DataField = "SaveALot.ShippedDate"
            s.Y.DataField = "SaveALot.SaleAmount"
    
            ' Add the Quick-Stop series.
            s = sc.AddNewSeries()
            s.Label = "Quick-Stop"
            s.X.DataField = "QuickStop.ShippedDate"
            s.Y.DataField = "QuickStop.SaleAmount"
    
            ' Set up the Axes and Legend.
            chart.ChartArea.AxisX.AnnoFormat = FormatEnum.DateShort
            chart.ChartArea.AxisY.AnnoFormat = FormatEnum.NumericCurrency
            chart.ChartArea.AxisY.Min = 0
    
            ' Change to a bar chart.
            chart.ChartGroups[0].ChartType = Chart2DTypeEnum.Bar
            chart.ChartGroups[0].ShowOutline = false
    
            ' Position, Orient and Show the Legend
            chart.Legend.Compass = CompassEnum.North
            chart.Legend.Orientation = LegendOrientationEnum.Horizontal
            chart.Legend.Visible = true
    End Sub
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       C1Chart1.Location = New Point(0)
       C1Chart1.Size = Me.ClientSize
       BindMultipleSeriesViewsAndChartSetup(C1Chart1)
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void BindMultipleSeriesViewsAndChartSetup(C1.Win.C1Chart.C1Chart chart)
    {
            // following objects are in namespace System.Data.OleDb
            OleDbConnection connect = new OleDbConnection();
            OleDbDataAdapter adapt1 = new OleDbDataAdapter();
            OleDbDataAdapter adapt2 = new OleDbDataAdapter();
            OleDbCommand select1 = new OleDbCommand();
            OleDbCommand select2 = new OleDbCommand();
    
            // set up the connect to the ComponentOne sample database
            connect.ConnectionString =
                "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "User ID=Admin;" + 
                "Data Source=C:\\Program Files\\ComponentOne Studio.Net\\common\\C1NWIND.MDB;" + "Jet OLEDB:Engine Type=5;";
    
            // select Save-A-Lot entries in [Sales Totals by Amount]
            select1.CommandText =
                "SELECT SaleAmount, ShippedDate, CompanyName " +
                "FROM [Sales Totals by Amount] " +
                "WHERE (CompanyName = \'Save-a-lot Markets\') " + 
                "ORDER BY ShippedDate";
            select1.Connection = connect;
    
            // select Quick-Stop entries [Sales Totals by Amount]
            select2.CommandText =
                "SELECT SaleAmount, ShippedDate, CompanyName " + 
                "FROM [Sales Totals by Amount] " + 
                "WHERE (CompanyName = \'QUICK-Stop\') " + 
                "ORDER BY ShippedDate";
            select2.Connection = connect;
    
            // using System.Data.Common namespace for the Mapping objects.
            // TableMapping is used to allow multiple views of the same table
            //   to appear in the same DataSet.
    
            // set up the adapter, adapt1.
            adapt1.SelectCommand = select1;
    
            DataColumnMapping [] ColumnMaps_SaveALot =
            {
                new DataColumnMapping("SaleAmount", "SaleAmount"),
                new DataColumnMapping("CompanyName", "CompanyName"),
                new DataColumnMapping("ShippedDate", "ShippedDate")
            };
    
            adapt1.TableMappings.Add(new DataTableMapping("Table", "SaveALot", ColumnMaps_SaveALot));
    
            // set up the adapter, adapt2.
            adapt2.SelectCommand = select2;
            DataColumnMapping [] ColumnMaps_QuickStop =
            {
                new DataColumnMapping("SaleAmount", "SaleAmount"),
                new DataColumnMapping("CompanyName", "CompanyName"),
                new DataColumnMapping("ShippedDate", "ShippedDate")
            };
    
            adapt2.TableMappings.Add(new DataTableMapping("Table", "QuickStop", ColumnMaps_QuickStop));
    
            // Create the dataset and fill it from all adapters.
            DataSet ds = new DataSet();
            adapt1.Fill(ds);
            adapt2.Fill(ds);
    
            // set up the chart, assigning the DataSource, DataFields and properties.
            chart.Dock = DockStyle.Fill;
            chart.DataSource = ds;
            ChartDataSeriesCollection sc = chart.ChartGroups[0].ChartData.SeriesList;
            sc.RemoveAll();
    
            // Add the Save-A-Lot series.
            ChartDataSeries s = sc.AddNewSeries();
            s.Label = "Save-A-Lot";
            s.X.DataField = "SaveALot.ShippedDate";
            s.Y.DataField = "SaveALot.SaleAmount";
    
            // Add the Quick-Stop series.
            s = sc.AddNewSeries();
            s.Label = "Quick-Stop";
            s.X.DataField = "QuickStop.ShippedDate";
            s.Y.DataField = "QuickStop.SaleAmount";
    
            // Set up the Axes and Legend.
            chart.ChartArea.AxisX.AnnoFormat = FormatEnum.DateShort;
            chart.ChartArea.AxisY.AnnoFormat = FormatEnum.NumericCurrency;
            chart.ChartArea.AxisY.Min = 0;
    
            // Change to a bar chart.
            chart.ChartGroups[0].ChartType = Chart2DTypeEnum.Bar;
            chart.ChartGroups[0].ShowOutline = false;
    
            // Position, Orient and Show the Legend
            chart.Legend.Compass = CompassEnum.North;
            chart.Legend.Orientation = LegendOrientationEnum.Horizontal;
            chart.Legend.Visible = true;  chart.Legend.Visible = true;
    }
    
    private void Form1_Load(object sender, System.EventArgs e)
    {
            c1Chart1.Location = new Point(0);
            c1Chart1.Size = this.ClientSize;
            BindMultipleSeriesViewsAndChartSetup(c1Chart1);
    }
    
    See Also