views:

724

answers:

3

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.

A: 

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
A: 

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
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
+2  A: 

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
Thanks, this worked.I got rid of the DataSource, did this instead and it worked:Bar.DataSource := ADataSet
Robo
Great! I'm glad that helped.
Argalatyr