1

Using Delphi Steema TeeChart component, if I link a BarSeries to a dataset using the user interface, it shows up fine, but if I do it using code (which I need to), it's only showing one bar, even when I have several records in the database. What am I doing wrong?

Code:

var
   i:Integer;
   Bar:TBarSeries;
begin
   ADataSet.Close;
   ADataSet.LoadFromDataSet(mtbl);
   ADataSet.Active := true;
   ADataSet.First;
   ASource.DataSet := ADataSet;

   Bar := TBarSeries.Create(AChart);
   Bar.Assign(Series2);
   Bar.ParentChart := AChart;
   Bar.DataSource := ASource;
   Bar.XLabelsSource := 'Date';
   Bar.YValues.ValueSource := 'Load';

   for i := 0 to AChart.SeriesCount - 1 do
   begin
      AChart.Series[i].CheckDataSource;
   end;

ADataSet is a DevExpress MemData (TdxMemData). When I run the program, the X axis is only showing one bar, the first record in the dataset, even though I have 4 records in the dataset.

Argalatyr
  • 4,639
  • 3
  • 36
  • 62
Robo
  • 4,588
  • 7
  • 40
  • 48
  • The name of the component is "tchart", so you might want to edit your title and text. There are a couple of other StackOverflow questions tagged "tchart", so this will help in searching for similar. – Argalatyr Feb 10 '09 at 03:53
  • I understand, I used TeeChart to emphasis this is Steema's component, not Delphi's default one. – Robo Feb 10 '09 at 04:40

2 Answers2

3

This code works for me (using an Access database with fields ID and Height, I dropped a TDBChart, TADODataSet, and a TButton on a form):

procedure TForm1.Button1Click(Sender: TObject);  
var   
    Bar : TBarSeries;  
begin  
    ADODataSet1.Close;  
    ADODataSet1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;...';  
    Bar := TBarSeries.Create(DBChart1);  
    DBChart1.AddSeries(Bar);  
    Bar.ParentChart := DBChart1;  
    Bar.DataSource := ADODataSet1;  
    Bar.XLabelsSource := 'ID';  
    Bar.YValues.ValueSource := 'Height';  
    ADODataSet1.Active := true;  
end;

Note that the Datasource should be a TTable, TQuery, or TDataSet (not a TDataSource - go figure!).

Hope this helps.

Argalatyr
  • 4,639
  • 3
  • 36
  • 62
  • Thanks, this worked. I got rid of the DataSource, did this instead and it worked: Bar.DataSource := ADataSet – Robo Feb 10 '09 at 21:53
0

TChart refreshes the query each time you set

ADataSet.Active := true;

so, move this command to the end of your block (e.g. after you've set up the series properties).

Argalatyr
  • 4,639
  • 3
  • 36
  • 62
  • Thanks, but this didn't help. I moved LoadFromDataSet and Active := true to below the part where TBarSeries gets setup, and it still showed only one record. Since it showed one record, it must know the dataset is active and has records inside, not sure why it's not displaying all of them. – Robo Feb 10 '09 at 04:44
  • Have you tried temporarily switching to a generic delphi dataset (say just a tadodataset pointed to an Excel spreadsheet, something simple like that) to separate the vendors (dbx and steema) for this issue?\ – Argalatyr Feb 10 '09 at 05:29