Page 1 of 1

Chart not updating on VCL form after modify the DataSource dynamically

Posted: Tue Apr 25, 2023 5:22 pm
by 16490696
Hi,
I would like to ask for help from people who use TeeChart (pro version, but I think have not different in standard verion)...
I use the following code to modify the Series values for the Chart (linked to database table field), but after running this code the Chart does not update on VCL form...
(Delphi 11.3 + Teechart Pro 2022.34 on windows 11 x64)

What am I missing or doing incorrectly?

Code: Select all

    DataSource := DM.DataSourceTest;
    DataSet := DataSource.DataSet;
    FieldName := Series1.YValues.ValueSource;
    DataSet.First;
    while not DataSet.Eof do begin
      FieldValue := DataSet.FieldByName(FieldName).AsInteger;
      if FieldValue > 100 then begin
        FieldValue := FieldValue - random(10);
        DataSet.Edit;
        DataSet.FieldByName(FieldName).AsInteger := FieldValue;
        DataSet.Post;
      end;
      DataSet.Next;
    end;
    Series1.CheckDatasource;
    dbChartTest.Invalidate;
    dbChartTest.Refresh;
    Application.ProcessMessages;
Thank you!

Re: Chart not updating on VCL form after modify the DataSource dynamically

Posted: Tue May 09, 2023 12:16 pm
by yeray
Hello,

This is a simple example of a TDBChart with a TLineSeries linked to a TFDMemTable updating values with a TTimer.
It seems to work fine for me here just calling CheckDataSource everytime the table is modified.

Code: Select all

var FDMemTable: TFDMemTable;
    Chart: TDBChart;
    Series: TLineSeries;
    Timer: TTimer;

procedure TForm1.FormCreate(Sender: TObject);
var val: Double;
    i: Integer;
begin
  FDMemTable:=TFDMemTable.Create(Self);
  FDMemTable.FieldDefs.Add('Price', ftInteger);
  FDMemTable.CreateDataSet;
  FDMemTable.Open;

  val:=1000+Random(500);
  for i:=0 to 10 do
  begin
    FDMemTable.AppendRecord([val]);
    val:=val+Round(Random(10)-4.5);
  end;

  Chart:=TDBChart.Create(Self);
  Chart.Parent:=Self;
  Chart.Align:=alClient;
  Chart.View3D:=False;
  Chart.Legend.Hide;

  Series:=TLineSeries(Chart.AddSeries(TLineSeries));
  Series.DataSource:=FDMemTable;
  Series.YValues.ValueSource:='Price';

  Timer:=TTimer.Create(Self);
  Timer.Interval:=1000;
  Timer.OnTimer:=TimerTick;
  Timer.Enabled:=True;
end;

procedure TForm1.TimerTick(Sender: TObject);
var val: Double;
begin
  val:=Series.YValues.Last+Round(Random(10)-4.5);
  FDMemTable.AppendRecord([val]);

  Series.CheckDataSource;
end;