Forum > TAChart

TAChart Plot while execution not terminated

(1/1)

Jones:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);const  SECONDS_PER_DAY = 24 * 60 * 60;var  t, t0, p: Double;begin  if FRunning then begin    FRunning := false;    Button1.Caption := 'Start';  end else  begin    Button1.Caption := 'Stop';    FRunning := true;    Chart1LineSeries1.Clear;    t0 := Now;    while FRunning do begin      t := (Now - t0) * SECONDS_PER_DAY;  // Elapsed time in seconds      Sleep(2000);                        // Simulates a lengthy calculation      p := 1000 * exp(-t/60);      Chart1LineSeries1.AddXY(t, p);      Application.ProcessMessages;    end;  end;end;

Jones:
Thanks!
it was actually just Application.ProcessMessages; that was missing.
Do you think this will increase execution time?

wp:
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).

Navigation

[0] Message Index

Go to full version