Recent

Author Topic: Tchart with log scale and needed real time on X-axis  (Read 10550 times)

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Tchart with log scale and needed real time on X-axis
« on: December 19, 2023, 06:33:16 pm »
Dear all,

In my log graph I need on the X-axis real time in hh:mm:ss can someone help with my example....

Thanks in advance!
Best,

Johan


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, TAGraph, TASeries, TATransformations, TASources,
  9.   TAChartExtentLink, TAChartCombos, TANavigation, TATools, Forms, Controls,
  10.   Graphics, Dialogs, ExtCtrls, Menus, ComCtrls, StdCtrls, Types;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Chart1: TChart;
  18.     ChartExtentLink1: TChartExtentLink;
  19.     ChartToolset1: TChartToolset;
  20.     ChartToolset1ZoomDragTool1: TZoomDragTool;
  21.     ChartToolset1ZoomDragTool2: TZoomDragTool;
  22.     ChartToolset1ZoomMouseWheelTool1: TZoomMouseWheelTool;
  23.     LineSeries: TLineSeries;
  24.     MainMenu1: TMainMenu;
  25.     MenuItem1: TMenuItem;
  26.     MenuItem10: TMenuItem;
  27.     MenuItem11: TMenuItem;
  28.     MenuItem12: TMenuItem;
  29.     MenuItem14: TMenuItem;
  30.     MenuItem15: TMenuItem;
  31.     MenuItem16: TMenuItem;
  32.     MenuItem2: TMenuItem;
  33.     MenuItem3: TMenuItem;
  34.     MenuItem4: TMenuItem;
  35.     MenuItem5: TMenuItem;
  36.     MenuItem6: TMenuItem;
  37.     MenuItem7: TMenuItem;
  38.     MenuItem8: TMenuItem;
  39.     MenuItem9: TMenuItem;
  40.     YAxisLabels: TListChartSource;
  41.     XAxisLabels: TListChartSource;
  42.     YAxisTransformation: TChartAxisTransformations;
  43.     YAxisLogTransform: TLogarithmAxisTransform;
  44.     XAxisTransformation: TChartAxisTransformations;
  45.     XAxisLogTransform: TLogarithmAxisTransform;
  46.     procedure ChartToolset1ZoomDragTool1AfterKeyDown(ATool: TChartTool;
  47.       APoint: TPoint);
  48.     procedure FormCreate(Sender: TObject);
  49.     procedure MenuItem10Click(Sender: TObject);
  50.     procedure MenuItem11Click(Sender: TObject);
  51.     procedure MenuItem12Click(Sender: TObject);
  52.     procedure MenuItem14Click(Sender: TObject);
  53.     procedure MenuItem15Click(Sender: TObject);
  54.     procedure MenuItem16Click(Sender: TObject);
  55.     procedure MenuItem3Click(Sender: TObject);
  56.     procedure MenuItem6Click(Sender: TObject);
  57.     procedure MenuItem7Click(Sender: TObject);
  58.     procedure MenuItem8Click(Sender: TObject);
  59.     procedure MenuItem9Click(Sender: TObject);
  60.  
  61.   private
  62.     procedure CreateData;
  63.     procedure CreateAxisLabels(ASource: TListChartSource; AMin, AMax: Double);
  64.     procedure CreateAxisLabelsTime(ASource: TListChartSource; AMin, AMax: Double);
  65.   public
  66.  
  67.   end;
  68.  
  69. var
  70.   Form1: TForm1;
  71.  
  72. implementation
  73.  
  74. {$R *.lfm}
  75.  
  76. uses
  77.   Math, TAChartUtils;
  78.  
  79. { TForm1 }
  80.  
  81. procedure TForm1.CreateData;
  82.  
  83.  
  84. begin
  85.     //Add test data
  86.  
  87.     //LineSeries.AddXY(1, 1e3);
  88.     //LineSeries.AddXY(5, 1e2);
  89.     //LineSeries.AddXY(10,1e1);
  90.     //LineSeries.AddXY(15,1e0);
  91.     //LineSeries.AddXY(20, 1e-1);
  92.     //LineSeries.AddXY(25, 1e-2);
  93.     //LineSeries.AddXY(30, 1e-3);
  94.     LineSeries.AddXY(35,1e-4);
  95.  
  96.  
  97.     LineSeries.ListSource.Sorted := true;
  98. end;
  99.  
  100.  
  101. procedure TForm1.CreateAxisLabels(ASource: TListChartSource; AMin, AMax: Double);
  102. var
  103.   x: Double;
  104. begin
  105.   ASource.Clear;
  106.    x := power(10.0, floor(log10(AMin)));
  107.   while x <= AMax * 10 do begin
  108.             writeln(AMin);
  109.     ASource.Add(x, x, Format('10^%.0f', [Log10(x)]));
  110.  
  111.     x := x * 10;       writeln(x);
  112.   end;
  113. end;
  114.  
  115.  
  116. procedure TForm1.CreateAxisLabelsTime(ASource: TListChartSource; AMin, AMax: Double);
  117. var
  118.   x: Double;
  119. begin
  120.   ASource.Clear;
  121.  
  122.   x := 0;
  123.   while x <= AMax * 10 do begin
  124.  
  125.     ASource.Add(x, x, Format('10^%.0f', [Log10(x)]));
  126.  
  127.     x := x +10 ;
  128.   end;
  129. end;
  130.  
  131. procedure TForm1.FormCreate(Sender: TObject);     // default graph range
  132. var
  133.   ex: TDoubleRect;
  134. begin
  135.  
  136.   CreateData;
  137.  
  138.  
  139.   // Calculate x axis labels for the x extent
  140.   CreateAxisLabels(XAxisLabels, ex.a.x, ex.b.x);
  141.  
  142.  
  143.   // Calculate y axis labels for the y extent
  144.  
  145.        ex.b.y := 1e3;
  146.        ex.a.y := 1e-9;
  147.  
  148.        Chart1.AxisList.Axes[0].Range.UseMax := True;
  149.        Chart1.AxisList.Axes[0].Range.UseMin := True;
  150.        Chart1.AxisList.Axes[0].Range.Min := 1e-9;
  151.        Chart1.AxisList.Axes[0].Range.Max := 1e3;
  152.  
  153.        CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  154.  
  155.  
  156.        Chart1.LeftAxis.Marks.Source := YAxislabels;
  157.        Chart1.LeftAxis.Marks.Style := smsLabel;
  158.  
  159.  
  160.       // Optional: Let axes start at first and end at last label
  161.       //Chart1.BottomAxis.Range.Max := XAxisLabels.Item[XAxisLabels.Count-1]^.X;
  162.       //Chart1.BottomAxis.Range.UseMax := true;
  163.       //Chart1.BottomAxis.Range.Min := XAxisLabels.Item[0]^.X;
  164.       //Chart1.BottomAxis.Range.UseMin := true;
  165.  
  166.  
  167.       Chart1.LeftAxis.Range.Max := 1e3  ;
  168.       Chart1.LeftAxis.Range.UseMax := true;
  169.       Chart1.LeftAxis.Range.Min := 1e-9 ;
  170.       Chart1.LeftAxis.Range.UseMin := true;
  171.        //Chart1.        LineSeries[1].AddXY(2, 2, FormatDateTime('dd/mm/yyyy', strtodate('01/01/2010')),1);
  172.  
  173.  
  174. end;
  175.  
  176.  
  177.  
  178.  
  179.  
  180. procedure TForm1.MenuItem16Click(Sender: TObject);
  181. var
  182. ex: TDoubleRect;
  183. begin
  184.       ex.b.y := 1e3;
  185.       ex.a.y := 1e-0;
  186.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  187.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  188.       Chart1.LeftAxis.Marks.Style := smsLabel;
  189.       Chart1.LeftAxis.Range.Max := 1e3  ;
  190.       Chart1.LeftAxis.Range.UseMax := true;
  191.       Chart1.LeftAxis.Range.Min := 1e-0 ;
  192.       Chart1.LeftAxis.Range.UseMin := true;
  193.  
  194.  
  195. end;
  196.  
  197. procedure TForm1.ChartToolset1ZoomDragTool1AfterKeyDown(ATool: TChartTool;
  198.   APoint: TPoint);
  199. begin
  200.  
  201. end;
  202.  
  203. procedure TForm1.MenuItem3Click(Sender: TObject);
  204. begin
  205.  
  206. end;
  207. procedure TForm1.MenuItem15Click(Sender: TObject);
  208. var
  209. ex: TDoubleRect;
  210. begin
  211.       ex.b.y := 1e3;
  212.       ex.a.y := 1e-1;
  213.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  214.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  215.       Chart1.LeftAxis.Marks.Style := smsLabel;
  216.       Chart1.LeftAxis.Range.Max := 1e3  ;
  217.       Chart1.LeftAxis.Range.UseMax := true;
  218.       Chart1.LeftAxis.Range.Min := 1e-1 ;
  219.       Chart1.LeftAxis.Range.UseMin := true;
  220.  
  221. end;
  222. procedure TForm1.MenuItem14Click(Sender: TObject);
  223. var
  224. ex: TDoubleRect;
  225. begin
  226.       ex.b.y := 1e3;
  227.       ex.a.y := 1e-2;
  228.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  229.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  230.       Chart1.LeftAxis.Marks.Style := smsLabel;
  231.       Chart1.LeftAxis.Range.Max := 1e3  ;
  232.       Chart1.LeftAxis.Range.UseMax := true;
  233.       Chart1.LeftAxis.Range.Min := 1e-2 ;
  234.       Chart1.LeftAxis.Range.UseMin := true;
  235.  
  236. end;
  237.  
  238.  
  239.  
  240. procedure TForm1.MenuItem6Click(Sender: TObject);
  241. var
  242. ex: TDoubleRect;
  243. begin
  244.       ex.b.y := 1e3;
  245.       ex.a.y := 1e-3;
  246.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  247.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  248.       Chart1.LeftAxis.Marks.Style := smsLabel;
  249.       Chart1.LeftAxis.Range.Max := 1e3  ;
  250.       Chart1.LeftAxis.Range.UseMax := true;
  251.       Chart1.LeftAxis.Range.Min := 1e-3 ;
  252.       Chart1.LeftAxis.Range.UseMin := true;
  253.  
  254. end;
  255.  
  256. procedure TForm1.MenuItem7Click(Sender: TObject);
  257. var
  258. ex: TDoubleRect;
  259. begin
  260.      ex.b.y := 1e3;
  261.      ex.a.y := 1e-4;
  262.      CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  263.      Chart1.LeftAxis.Marks.Source := YAxislabels;
  264.      Chart1.LeftAxis.Marks.Style := smsLabel;
  265.      Chart1.LeftAxis.Range.Max := 1e3  ;
  266.      Chart1.LeftAxis.Range.UseMax := true;
  267.      Chart1.LeftAxis.Range.Min := 1e-4 ;
  268.      Chart1.LeftAxis.Range.UseMin := true;
  269.  
  270. end;
  271.  
  272. procedure TForm1.MenuItem8Click(Sender: TObject);
  273. var
  274. ex: TDoubleRect;
  275. begin
  276.      ex.b.y := 1e3;
  277.      ex.a.y := 1e-5;
  278.      CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  279.      Chart1.LeftAxis.Marks.Source := YAxislabels;
  280.      Chart1.LeftAxis.Marks.Style := smsLabel;
  281.      Chart1.LeftAxis.Range.Max := 1e3  ;
  282.      Chart1.LeftAxis.Range.UseMax := true;
  283.      Chart1.LeftAxis.Range.Min := 1e-5 ;
  284.      Chart1.LeftAxis.Range.UseMin := true;
  285. end;
  286.  
  287. procedure TForm1.MenuItem9Click(Sender: TObject);
  288. var
  289. ex: TDoubleRect;
  290. begin
  291.     ex.b.y := 1e3;
  292.     ex.a.y := 1e-6;
  293.     CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  294.     Chart1.LeftAxis.Marks.Source := YAxislabels;
  295.     Chart1.LeftAxis.Marks.Style := smsLabel;
  296.     Chart1.LeftAxis.Range.Max := 1e3  ;
  297.     Chart1.LeftAxis.Range.UseMax := true;
  298.     Chart1.LeftAxis.Range.Min := 1e-6 ;
  299.     Chart1.LeftAxis.Range.UseMin := true;
  300. end;
  301. procedure TForm1.MenuItem10Click(Sender: TObject);
  302. var
  303. ex: TDoubleRect;
  304. begin
  305.       ex.b.y := 1e3;
  306.       ex.a.y := 1e-7;
  307.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  308.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  309.       Chart1.LeftAxis.Marks.Style := smsLabel;
  310.       Chart1.LeftAxis.Range.Max := 1e3  ;
  311.       Chart1.LeftAxis.Range.UseMax := true;
  312.       Chart1.LeftAxis.Range.Min := 1e-7 ;
  313.       Chart1.LeftAxis.Range.UseMin := true;
  314.  
  315. end;
  316.  
  317. procedure TForm1.MenuItem11Click(Sender: TObject);
  318. var
  319. ex: TDoubleRect;
  320. begin
  321.       ex.b.y := 1e3;
  322.       ex.a.y := 1e-8;
  323.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  324.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  325.       Chart1.LeftAxis.Marks.Style := smsLabel;
  326.       Chart1.LeftAxis.Range.Max := 1e3  ;
  327.       Chart1.LeftAxis.Range.UseMax := true;
  328.       Chart1.LeftAxis.Range.Min := 1e-8 ;
  329.       Chart1.LeftAxis.Range.UseMin := true;
  330.  
  331. end;
  332.  
  333. procedure TForm1.MenuItem12Click(Sender: TObject);
  334. var
  335. ex: TDoubleRect;
  336. begin
  337.       ex.b.y := 1e3;
  338.       ex.a.y := 1e-9;
  339.       CreateAxisLabels(YAxisLabels, ex.a.y, ex.b.y);
  340.       Chart1.LeftAxis.Marks.Source := YAxislabels;
  341.       Chart1.LeftAxis.Marks.Style := smsLabel;
  342.       Chart1.LeftAxis.Range.Max := 1e3  ;
  343.       Chart1.LeftAxis.Range.UseMax := true;
  344.       Chart1.LeftAxis.Range.Min := 1e-9 ;
  345.       Chart1.LeftAxis.Range.UseMin := true;
  346. end;
  347.  
  348.  
  349.  
  350. end.
  351.  

wp

  • Hero Member
  • *****
  • Posts: 13333
Re: Tchart with log scale and needed real time on X-axis
« Reply #1 on: December 19, 2023, 07:02:49 pm »
Log on the y axis, I assume, but also on the x axis? This does not make much sense for hh:nn:ss when this should be understood as the time diplayed on a clock. Or do you mean a time difference, for example the elapsed time after start of an experiment?

And what do you understand as "real-time"? A live display of data coming in? Should this be a scrolling window, or should always the full curve be displayed?

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #2 on: December 19, 2023, 07:23:18 pm »
The Y axis get data from my serial port (not implemented yet, and the graph must be a scrolling window with actual time. So a real time graph so that I can monitor my vacuum gauges in time.

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #3 on: December 19, 2023, 07:31:19 pm »
Something like this see picture,

Thaddy

  • Hero Member
  • *****
  • Posts: 18676
  • Jungle wars. And failing health it seems.
Re: Tchart with log scale and needed real time on X-axis
« Reply #4 on: December 19, 2023, 07:58:14 pm »
So what you really mean is the time on X and the Log scaled values on Y....
I thought the education at the RUG was much better than you question suggests. Should I doubt you or your lecturers...
Be more precise and the answers will be easier to formulate.
And quite simple.
Right now you are somewhat confusing both wp and me  ;D
« Last Edit: December 19, 2023, 08:07:17 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #5 on: December 19, 2023, 08:28:07 pm »
What is this .. is this a forum to be crude to people! Anyway I not give lectures at all. If you can not understand my question simply stay a way here!

wp

  • Hero Member
  • *****
  • Posts: 13333
Re: Tchart with log scale and needed real time on X-axis
« Reply #6 on: December 19, 2023, 11:27:18 pm »
What is this .. is this a forum to be crude to people! Anyway I not give lectures at all. If you can not understand my question simply stay a way here!
Stop, stop, stop... Thaddy is known to be rude, and I don't like his answer. But I also dislike your comment. You do not need to give a lecture in your post, but you really should describe the issue in way that every user can understand it. Your description was way too short and unclear. So, please accept being asked for clarification.

Anyway... Find in the attachment a mini project which simulates a pump-down process, calculates some fictitious pressure values when a timer fires and creates a log plot of pressure vs time. I still do not understand what you mean as "real graph", so I dropped a TChartLiveView on the form which automates scrolling of the chart when the measurement has been running for a certain time (given by the ChartLiveView.ViewPortSize - note that TDateTime values are given in units of days). If you don't want this (or if your Lazarus is v2.0.x or older) comment the ChartLiveView lines in the code, then the left end of the chart will stay fixed and the chart will be compressed more and more with every data value arriving.

Automatic labeling of the log axis is a bit hard to control in Lazarus before v3.0, therefore I added a TListsource containing the power-of-ten values. Assigned to the Marks.Source of the Chart's LeftAxis you get a nice log axis this way.

Study the attached project, and read the documentation and some tutorials.
« Last Edit: December 21, 2023, 06:11:40 pm by wp »

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #7 on: December 21, 2023, 05:47:37 pm »
Hi WP, group,

Thanks for the example this is exactly what I need!


Best regards,
Johan



Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #8 on: December 22, 2023, 02:56:48 pm »
My project is not totally ready but this is what I made so far......

Best,

Johan


Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #9 on: December 23, 2023, 08:57:43 am »

Hi WP, others,

Now I am nearly done there is one small thing I like to change and that is the log labels this is a bit to small.

I looked everywhere but I can not find how I can make the labels bit bigger it looks some HTML....

See attachment.

Best,

Johan


wp

  • Hero Member
  • *****
  • Posts: 13333
Re: Tchart with log scale and needed real time on X-axis
« Reply #10 on: December 23, 2023, 12:37:45 pm »
there is one small thing I like to change and that is the log labels this is a bit to small.
You mean the entire label, or just the exponent?

There are several options:
  • Change the size of the Chart.LeftAxis.Marks.LabelFont; the default value of 0 means: "take the value from the OS". This affects the entire label.
  • Switch to non-html labels ("1E2" rather than "102"). For this, you must switch Chart.LeftAxis.Marks.TextFormat to tfNormal and replace the HTML code used where the axis labels are created:
Code: Pascal  [Select][+][-]
  1.   // Define the labels for the log axis (this is better than the automatic label
  2.   // creation).
  3.   for i := -10 to 3 do
  4.     PressureLabels.Add(i, 10.0**i, '1E'+IntToStr(i)); //'10<sup>' + IntToStr(i) + '</sup>');
  5.   Chart1.LeftAxis.Marks.TextFormat := tfNormal;
  • Increase the size fraction of the exponent. Unfortunately this number is hard-coded in the TADrawUtils unit, const SUBSUP_SIZE_MULTIPLIERE, which is currently 70 (percent of the labelfont size). You may also have to adapt the SUP_OFFSET_MULTIPLIER which is the vertical offset of the exponent (currently -5, negative because the offset is shifted toward smaller y values). Then recompile the TAChartLazarusPkg (go to "Package" > "Open package file", load "tachartlazaruspkg.lpk" from folder "components/tacharts" of your Lazarus installation and click compile). But note that modifying Lazarus sources is not a recommended practice since your changes will be gone when you install another Lazarus version... I think I'll have to find a way to make this configurable.

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #11 on: December 23, 2023, 01:52:26 pm »
Thanks WP,

Awesome now my labels are perfect!

See attachment picture.

Best regards,
Johan

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #12 on: December 23, 2023, 05:12:09 pm »
Hi group, WP,

I like to export my measuring data to a file.csv is there a easy way to do this?

I need some fields such as  date, time and my vacuum variables (gauges)

So something like this:


Date           Time      Gauge1        Gauge2      Gauge3     Gauge4
23-12-23    17:10    1.00E-5        5.60E-2      2.80E-8     1.0E3

Best,

Johan


wp

  • Hero Member
  • *****
  • Posts: 13333
Re: Tchart with log scale and needed real time on X-axis
« Reply #13 on: December 23, 2023, 07:43:35 pm »
Hi group, WP,

I like to export my measuring data to a file.csv is there a easy way to do this?

I need some fields such as  date, time and my vacuum variables (gauges)

So something like this:


Date           Time      Gauge1        Gauge2      Gauge3     Gauge4
23-12-23    17:10    1.00E-5        5.60E-2      2.80E-8     1.0E3
There are many possibilities. Basically you construct strings containing the individual values and put a separator character between them. The separator often is a comma, semi-colon or tab character. Then save these strings, line by line, to a text file, or add each string to a TStringList which you can save by calling its SaveToFile method.

Here is a solution based on an "old-fashioned" text file. If the data arrive at a low-enough rate (tenths of a second) I'd recommend to close the file after writing each record so that the measured data are kept even if your application might crash or hang (sh... happens).

Code: Pascal  [Select][+][-]
  1. type
  2.   TCSVFile = class
  3.   private
  4.     FFileName: String;
  5.     FCommentChar: Char;
  6.     FSeparator: Char;
  7.   public
  8.     constructor Create(FileName: String);
  9.     procedure Prepare(Comment: String; ColHeaders: array of string);
  10.     procedure SaveRecord(Values: array of string);
  11.     property CommentChar: Char read FCommentChar write FCommentChar;
  12.     property Separator: Char read FSeparator write FSeparator;
  13.   end;
  14.  
  15. constructor TCSVFile.Create(FileName: String);
  16. begin
  17.   inherited Create;
  18.   FFileName := FileName;
  19.   FCommentChar := '#';
  20.   FSeparator := #9;
  21. end;
  22.  
  23. procedure TCSVFile.Prepare(Comment: String; ColHeaders: array of string);
  24. var
  25.   F: TextFile;
  26.   i: Integer;
  27. begin
  28.   AssignFile(F, FFileName);
  29.   Rewrite(F);
  30.   WriteLn(F, FCommentChar, Comment);
  31.   Write(F, FCommentChar, ColHeaders[0]);
  32.   for i := 1 to High(ColHeaders) do
  33.     Write(F, FSeparator, ColHeaders[i]);
  34.   WriteLn(F);
  35.   CloseFile(F);
  36. end;
  37.  
  38. procedure TCSVFile.SaveRecord(Values: array of string);
  39. var
  40.   F: TextFile;
  41.   i: Integer;
  42. begin
  43.   AssignFile(F, FFileName);
  44.   Append(F);
  45.   Write(F, Values[0]);
  46.   for i := 1 to High(Values) do
  47.     Write(F, FSeparator, Values[i]);
  48.   WriteLn(F);
  49.   CloseFile(F);
  50. end;

Although not needed absolutely, a dedicated class helps to keep the code together. Before the measurement begins you create an instance of this TCSVFile with the filename as parameter. Then call the Prepare method in which you can define a comment describing the experiment and in which you define the headers of each data column. These lines are preceded by a "comment character" indicating that they contain no data values; again, this is not necessary, but helpful if you want to plot your data later by tools such as gnuplot. Then, when the data arrive you call the SaveRecord method which gets an array of strings, one for each column; the strings must be prepared by the calling routine. Finally, when the measurement ends you must destroy the TCSVFile instance.

See the attached modified demo of my previous post.

Johan Holstein

  • Jr. Member
  • **
  • Posts: 67
Re: Tchart with log scale and needed real time on X-axis
« Reply #14 on: December 23, 2023, 08:26:41 pm »
Wow WP, awesome and for sure very happy with your code !

Thank you t I wish you happy holidays from the Netherlands.

Best regards,

Johan

 

TinyPortal © 2005-2018