XamarinAndroidTutorial8

From Steema Software Reference Wiki
Jump to: navigation, search

Net android.png

Contents page
Previous | Next

Tutorial 8 - Database Access


Introduction


Connecting a TeeChart control to a database can be done at at run-time with few lines of code.
Any Series can be connected to tables or queries. Every Series can be connected to the same or different table or query using the same or different database.
This design gives you total flexibility on how do you connect Charts to databases, as you aren't restricted to only one database or to only one table or query.

Connecting to a database at run-time


DataSet
What was described above can be easily reproduced programmatically.

Every Series has the "DataSource" property, which determines the origin of database values (the Table or Query), and the "YValues.DataMember" and "LabelMember" properties to specify which fields we want to plot.

Example

[C#] 
private void LoadChart()
{
  Steema.TeeChart.Styles.Candle candle = new Steema.TeeChart.Styles.Candle(tChart1.Chart);

  candle.DataSource = GetData();
  candle.OpenValues.DataMember = "Open";
  candle.HighValues.DataMember = "High";
  candle.LowValues.DataMember = "Low";
  candle.CloseValues.DataMember = "Close";
  candle.DateValues.DataMember = "Date";
  candle.DateValues.DateTime = true;
  candle.LabelMember = "Candle Chart";
}

private DataSet GetData()
{
  DataSet TeeDataSet = new DataSet();
  DataTable TeeDataTable = new DataTable();

  DataColumn date = new DataColumn("Date", typeof(DateTime));
  DataColumn open = new DataColumn("Open", typeof(double));
  DataColumn high = new DataColumn("High", typeof(double));
  DataColumn low = new DataColumn("Low", typeof(double));
  DataColumn close = new DataColumn("Close", typeof(double));

  TeeDataTable.Columns.Add(date);
  TeeDataTable.Columns.Add(open);
  TeeDataTable.Columns.Add(high);
  TeeDataTable.Columns.Add(low);
  TeeDataTable.Columns.Add(close);

  DataRow newRow;
  Random rnd = new Random();

  for (int i = 0; i < 25; i )
  {
     newRow = TeeDataTable.NewRow();
     newRow[date] = DateTime.Now.AddDays(i);
     newRow[open] = rnd.Next();
     newRow[high] = rnd.Next();
     newRow[low] = rnd.Next();
     newRow[close] = rnd.Next();

     TeeDataTable.Rows.Add(newRow);
  }

  TeeDataSet.Tables.Add(TeeDataTable);

  return TeeDataSet;
}


In case the database values are changed and you want to "refresh" the connection and retrieve the values again, you should do the following:

TChart1.Series(0).CheckDataSource


The "CheckDataSource" method will force the Series to retrieve all records again.
All Series have also the "XValues.ValueSource" property. This property is used for XY charting only (when you have an X coordinate for each point).


PREVIOUS

NEXT


© 1998-2019 Steema Software SL. All rights reserved.