ComponentOne Chart for .NET (2.0) Search HelpCentral 

Binding the Chart Directly to the Data Source

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:

·      Visual Basic

' 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=… nwind.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"

·      C#

// DataBinding is only available with C1Chart

// version 1.0.20034.13244 and later.

//

// get chart data

string sql = "select * from products";

string conn = @"provider=… nwind.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";

·      Delphi

// DataBinding is only available with C1Chart

// version 1.0.20034.13244 and later.

 

 

var

  // global variables defined elsewhere

  c1chart: C1.Win.C1Chart.C1Chart;

  dt: DataTable;

 

  // local variables

  sql: String;

  conn: String;

  da: OleDbDataAdapter;

  dsc: ChartDataSeriesCollection;

  ds: ChartDataSeries;

 

begin

 

  sql := 'select * from products';

  conn := 'provider=… nwind.mdb;';

 

  da := OleDbDataAdapter.Create(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;

  dsc := c1chart.ChartGroups[0].ChartData.SeriesList;

  dsc.Clear;

 

  //  add unit price series;

  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';

end;

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.


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