Search code examples
databasedelphiteechart

Delphi TeeChart only showing one record from dataset


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.


Solution

  • 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.