ComponentOne Chart for .NET (2.0) Search HelpCentral 

Functional Program Using Data Binding

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 for .NET. 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.

·      Visual Basic

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\NWIND.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

·      C#

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\\NWIND.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);

}

·      Delphi

procedure TWinForm.BindMultipleSeriesViewsAndChartSetup(chart: C1.Win.C1Chart.C1Chart);

var

  connect: OleDbConnection;

  adapt1: OleDbDataAdapter;

  adapt2: OleDbDataAdapter;

  select1: OleDbCommand;

  select2: OleDbCommand;

 

  ColumnMaps_SaveALot: array of DataColumnMapping;

  ColumnMaps_QuickStop: array of DataColumnMapping;

  ds: DataSet;

 

  sc: ChartDataSeriesCollection;

  s: ChartDataSeries;

 

begin

  // following objects are in namespace System.Data.OleDb

  connect := OleDbConnection.Create;

  adapt1 := OleDbDataAdapter.Create;

  adapt2 := OleDbDataAdapter.Create;

  select1 := OleDbCommand.Create;

  select2 := OleDbCommand.Create;

 

  // 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\\NWIND.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;

  SetLength(ColumnMaps_SaveALot, 3);

  ColumnMaps_SaveALot[0] := DataColumnMapping.Create('SaleAmount', 'SaleAmount');

  ColumnMaps_SaveALot[1] := DataColumnMapping.Create('CompanyName', 'CompanyName');

  ColumnMaps_SaveALot[2] := DataColumnMapping.Create('ShippedDate', 'ShippedDate');

  adapt1.TableMappings.Add(DataTableMapping.Create('Table', 'SaveALot',

    ColumnMaps_SaveALot));

 

  // set up the adapter, adapt2.

  adapt2.SelectCommand := select2;

  SetLength(ColumnMaps_QuickStop, 3);

  ColumnMaps_QuickStop[0] := DataColumnMapping.Create('SaleAmount', 'SaleAmount');

  ColumnMaps_QuickStop[1] := DataColumnMapping.Create('CompanyName', 'CompanyName');

  ColumnMaps_QuickStop[2] := DataColumnMapping.Create('ShippedDate', 'ShippedDate');

  adapt2.TableMappings.Add(DataTableMapping.Create('Table', 'QuickStop',

    ColumnMaps_QuickStop));

 

  // Create the dataset and fill it from all adapters

  ds := DataSet.Create;

  adapt1.Fill(ds);

  adapt2.Fill(ds);

 

  // set up the chart, assigning the DataSource, DataFields and properties.

  chart.Dock := DockStyle.Fill;

  chart.DataSource := ds;

  sc := chart.ChartGroups[0].ChartData.SeriesList;

  sc.RemoveAll();

 

  // Add the Save-A-Lot series.

  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

  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;

 

procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);

begin

  Self.C1Chart1.Location := Point.Create(0);

  Self.C1Chart1.Size := Self.ClientSize;

 

  BindMultipleSeriesViewsAndChartSetup(C1Chart1);

end;

 


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.