Recent

Author Topic: TAChart Plot while execution not terminated  (Read 433 times)

Jones

  • Newbie
  • Posts: 4
TAChart Plot while execution not terminated
« on: March 30, 2022, 10:22:10 am »
Hi,
I have a TAChart that displays the evolution in time of the pressure of different gas filled tanks that are filled or emptied.
The calculation can take up to 5 minutes, so instead of only showing the graph at the end of the execution of the program, I want to plot 'live'. Basically, I want to plot every point in time, at the moment it is calculated. In this way, the graph is being plotted gradually along the X-axis. Is this possible with TAChart and how should I do this?
Thanks!

wp

  • Hero Member
  • *****
  • Posts: 10266
Re: TAChart Plot while execution not terminated
« Reply #1 on: March 30, 2022, 11:50:08 am »
Simply AddXY the new data point to the series when it is available, and the chart will grow automatically.

Here is a simple example requiring a chart, a lineseries and a button (and add a boolean variable FRunning to the form):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   SECONDS_PER_DAY = 24 * 60 * 60;
  4. var
  5.   t, t0, p: Double;
  6. begin
  7.   if FRunning then begin
  8.     FRunning := false;
  9.     Button1.Caption := 'Start';
  10.   end else
  11.   begin
  12.     Button1.Caption := 'Stop';
  13.     FRunning := true;
  14.     Chart1LineSeries1.Clear;
  15.     t0 := Now;
  16.     while FRunning do begin
  17.       t := (Now - t0) * SECONDS_PER_DAY;  // Elapsed time in seconds
  18.       Sleep(2000);                        // Simulates a lengthy calculation
  19.       p := 1000 * exp(-t/60);
  20.       Chart1LineSeries1.AddXY(t, p);
  21.       Application.ProcessMessages;
  22.     end;
  23.   end;
  24. end;
« Last Edit: March 31, 2022, 05:56:36 pm by wp »

Jones

  • Newbie
  • Posts: 4
Re: TAChart Plot while execution not terminated
« Reply #2 on: March 31, 2022, 05:52:12 pm »
Thanks!
it was actually just Application.ProcessMessages; that was missing.
Do you think this will increase execution time?

wp

  • Hero Member
  • *****
  • Posts: 10266
Re: TAChart Plot while execution not terminated
« Reply #3 on: March 31, 2022, 06:10:47 pm »
It depends on how often you call it and how long each loop cycle takes. In my sample code, which simulated some lengthy calculation of 2 seconds by a Sleep command you certainly will not notice any delay. But when each calculation takes only a fraction of a millisecond you will notice it severely. The delay will not be due to the Application.processmessages itself, but due to repainting of the chart with every cycle.

But I should tell you also that if you have a long calculation time per cycle you generally have a very poor usability of your application which mostly is not responsive. Rather than calling Application.ProcessMessages, it would be much better to put the calculation into a separate thread (look for TThread).

 

TinyPortal © 2005-2018