TAChart: TNearestPointParams - Debug Errors (UPDATE) lazarus / 55-TAChart ---------------------------------------------------------------------- ChrisP · 2024-05-25 06:02 Hello, I am an absolute beginner in Lazarus and Delphi and have problems with the following procedure: I have attached the complete Pas file. Basically, three lines should be displayed in a diagram. When the mouse moves over a line, a tooltip should display additional information and a vertical line should be displayed across the entire diagram to show the position of the courser. Unfortunately, I cannot find a working solution. Maybe you can help me to get the code working. Thank you! Best regards Chris ---------------------------------------------------------------------- cdbc · 2024-05-25 06:33 Hi Off the bat, it sounds like a 'renaming' issue... e.g.: renaming a class variable/instance, then codetools can't find the properties... edit: ...or a missing unit in your uses-clause? Regards Benny ---------------------------------------------------------------------- ChrisP · 2024-05-25 06:38 @cdbc Thank you very much for your idea. But can you see where the "renaming" error is caused in my code?  I've been working on the code for hours. I can't find the solution. ---------------------------------------------------------------------- dsiders · 2024-05-25 06:42 Quote from: ChrisP on May 25, 2024, 08:02:51 amHello, I am an absolute beginner in Lazarus and Delphi and have problems with the following procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormMeasurement.ShowMouseInfo(X, Y: Integer);var  params: TNearestPointParams;  res: TNearestPointResults;  Distance: Double;begin  params.Point := Point(X, Y);  params.Radius := 8;  params.Distance := MaxDouble;   if LineSeriesSelection.Active then  begin    if LineSeriesSelection.GetNearestPoint(params, res) then    begin      Distance := Hypot(res.FValue.X - X, res.FValue.Y - Y);    if Distance < 15 then        LabelResultSelS.Caption := Format('Selection Sort: (%.0f, %.0f)', [res.FValue.X, res.FValue.Y]);    end;  end;   if LineSeriesQuick.Active then  begin    if LineSeriesQuick.GetNearestPoint(params, res) then    begin      Distance := Hypot(res.FValue.X - X, res.FValue.Y - Y);      if Distance < 15 then        LabelResultQuS.Caption := Format('Quick Sort: (%.0f, %.0f)', [res.FValue.X, res.FValue.Y]);    end;  end;   if LineSeriesShell.Active then  begin    if LineSeriesShell.GetNearestPoint(params, res) then    begin      Distance := Hypot(res.FValue.X - X, res.FValue.Y - Y);      if Distance < 15 then        LabelResultShS.Caption := Format('Shell Sort: (%.0f, %.0f)', [res.FValue.X, res.FValue.Y]);    end;  end;end;  I receive the following feedback (errors) from the debugger: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i formmeasurement.pas(352,10) Error: identifier idents no member "Point"formmeasurement.pas(353,10) Error: identifier idents no member "Radius"formmeasurement.pas(354,10) Error: identifier idents no member "Distance" I have attached the complete Pas file. Basically, three lines should be displayed in a diagram. When the mouse moves over a line, a tooltip should display additional information and a vertical line should be displayed across the entire diagram to show the position of the courser. Unfortunately, I cannot find a working solution. Maybe you can help me to get the code working. Thank you! Best regards Chris You need to use the member names as defined in the record structures: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TNearestPointParams = record    FDistFunc: TPointDistFunc;    FOptimizeX: Boolean;    FPoint: TPoint;    FRadius: Integer;    FTargets: TNearestPointTargets;  end;   TNearestPointResults = record    FDist: Integer;    FImg: TPoint;    FIndex: Integer;        // Point index    FXIndex: Integer;       // Index to be used in Source.GetX()    FYIndex: Integer;       // Index to be used in Source.GetY()    FValue: TDoublePoint;  end; ---------------------------------------------------------------------- cdbc · 2024-05-25 06:45 Hi Kudos Don, well spotted. Regards Benny ---------------------------------------------------------------------- ChrisP · 2024-05-25 07:14 @dsiders @dcbc I think that will get me further! Thanks for your help! ---------------------------------------------------------------------- ChrisP · 2024-05-25 08:05 Sorry, I need your help again. I have now adjusted the code and am getting similar errors again. The following procedures are now affected: Maybe someone can take another look at this? Thank you very much! Chris ---------------------------------------------------------------------- wp · 2024-05-25 14:14 The function GetNearestPoint of TLineSeries (and all descendants of TBasicPointSeries) uses parameters of type TNearestPointParams and TNearestPointResult which are declared in unit TACustomSeries. You, however, re-declare these records in your own FormMeasurement unit - this causes a conflict. Remove this declaration and put the "F" in front of the record elements in your code. I don't know why the author of this used the unusual "F-notation" here, maybe to indicated that this function primarily is meant to be used for internal purposes. But on the other hand, the function is public and nobody can prevent you from using it.... Did you consider using the ChartTools for your purpose? I think that all you want to create in your code is already there: a TDatapointCrosshairTools to follow the data points with the mouse and to display a crosshair cursor, a TDatapointHintTool to display a popup hint window for each data point over which the mouse hovers. There is a tutorial covering the usage of charttools: https://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Chart_Tools. In the attachment, find a simple demo showing the TDatapointCrosshairTool and TDatapointHintTool in action. ---------------------------------------------------------------------- wp · 2024-05-26 16:36 Why do you think these are valid properties in TAChart? They aren't. TLineSeries, for example, has not Hint property (it descends from TComponent, for having a Hint it should descent from TControl). Do these properties exist in Delphi's TeeChart? Be aware that TAChart was developed without having total compatibility with TeeChart in mind - much is similar, but much is different... To get the value range of an axis (that's what you mean with axis.Minimum and axis.Maximum) you can query Chart.LogicalExtent which returns a DoubleRect with point A refering to the coordinates of the lower left corner and point B to the upper right corner of chart, i.e. Chart.LogicalExtent.a.y is the minimum of the y axis, Chart.LogicalExtent.b.y its maximum. BTW, the easiest way to draw a vertical (or horizontal) line is to add a TConstantLine series and set its "Linestyle" (= lsHorizontal, lsVertical) and Position properties. A TAChart axis also has no method GraphToCoord. The conversion between graph and image coordinages is done by TChart methods: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TChart = class(...)    ...    function GraphToImage(const AGraphPoint: TDoublePoint): TPoint;    function ImageToGraph(const APoint: TPoint): TDoublePoint;    function XGraphToImage(AX: Double): Integer;     function XImageToGraph(AX: Integer): Double;     function YGraphToImage(AY: Double): Integer;    function YImageToGraph(AY: Integer): Double;    ...There is further complication when you use axis transformations - but I don't want to go into details here. If you want to display a hint you must use the Chart's Hint property, or use a custom hint - see the TDataPointHintTool code for some example (or TCustomGrid in unit grids which also provides a custom hint). ---------------------------------------------------------------------- wp · 2024-05-27 10:22 Probably because TAChart does all the essential work in its paint procedure, among it the calculation of the scaling parameters between graph and image coordinates - the error message clearly indicates that the scaling parameters have not yet been calculated because the chart has not yet been drawn at this exact moment. You could query the Chart property ScalingValid before you call XImageToGraph. If not valid, make the chart redraw itself, Chart.Invalidate (or, if this is too late, Chart.Refresh or Chart.Repaint): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormMeasurement.DrawVerticalLineAndTooltips(X: Integer);var  ChartX: Double;  ...begin  if not Chart.ScalingValid then     Chart.Repaint;  ChartX := Chart.XImageToGraph(X);  ... ---------------------------------------------------------------------- wp · 2024-05-28 07:30 The DatapointHintTool fires two events before the hint window is shown: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   OnHintPositon        (= procedure (ATool: TDataPointHintTool; var APoint: TPoint) )  OnHintLocationEvent  (= procedure (ATool: TDataPointHintTool; AHintSize: TSize; var APoint: TPoint) ) APoint here is the position of the upper left corner of the hint window. If you want to show the hint above the proposed point use the AHintSize parameter to correct the position (TSize = record CX, CY: Integer; end); ---------------------------------------------------------------------- ChrisP · 2024-05-28 08:42 I have adapted the code again (see attachment and below). Unfortunately, it still does not work. I now get AccessViolations when moving the mouse over the diagram after generating the lines and closing the module: The complete code is attached again ---------------------------------------------------------------------- wp · 2024-05-28 09:18 No, it's not complete code (*), you don't show what's happening in the OnHint, OnHintPosition, OnHintLocation (you need only one of these...) handlers. The OnHint handler in reply #1, for example will crash when the mouse goes into the chart without hitting the data point, because then ATool.Series is nil, maybe even ATool. Always check for nil in these handlers. (*) Rather than presenting such individual procedure it would be better if you'd upload a complete compilable project because then the compile and debugger would help me to find these issues. But PLEASE: Do not upload your complete project, separate the issue into a smaller project which makes it easier for me to reproduce errors. For uploading a project, pack the .pas, .lfm, .lpi, .lpr and possible data files into a common zip and use the "Attachment and other options" section of the forum. Do NOT include any files generated by the compiler, most importantly the executable, because there is an upload size limit. ---------------------------------------------------------------------- ChrisP · 2024-05-28 09:40 You're right, of course. I have now uploaded the project (smaller project) as a zip file: I think this helps to better understand and solve the problem. The aim is to make the diagram look neat and the AccessViolations disappear. In addition, the tooltips in the diagram should not appear on contact with the mouse pointer but on all diagram lines as soon as they intersect the vertical line. I have been failing at this all the time. I hope you can help me with this? ---------------------------------------------------------------------- wp · 2024-05-28 14:22 I fixed a lot of bugs (related to TAChart only - there are more) and attached the resulting project. See my comments in the code for explanations. Next time when you send code please upload it to the forum where it will be available as long as the forum exists; your code, however, will be lost when the cloud provider or you decide to remove it. Your upload was quite large because you had included the lib and .git folders as well as the sqlite3.dll and project binary. If you exclude them the upload is only 28 KB and fits well into the forum's upload limit of 500 KB. ---------------------------------------------------------------------- ChrisP · 2024-05-29 05:35 Many thanks for your efforts. The errors are finally gone! :-) But do you have an idea to add the functionality that the vertical line shows the tooltips at all intersections when crossing the horizontal lines in the diagram? Somehow that hasn't worked yet. The tooltips are still displayed individually when the mouse pointer touches a diagram line. However, this should happen when touching the vertical line and for all displayed diagram lines. Is there also a way to customize the diagram so that if different diagram lines are displayed and the values are far apart (e.g. line 1 is in the Y-axis range 0 - 500000 ms and line 2 is in the Y-axis range 0-200 ms) that this is dynamically taken into account and the diagram becomes clearer? So that the Y-axis is divided in the scaling? Otherwise the other displayed diagram lines are “lost” in the view. Perhaps you have another idea? And one more point: I know it has nothing to do with TAChart.  But when closing the module, the GRID in the MainMenu module should actually update itself and select the previously selected data set. Instead, the GRID is emptied and no data set is displayed or selected. What is the reason for this? It has worked accordingly in the past. ---------------------------------------------------------------------- wp · 2024-05-29 15:51 Quote from: ChrisP on May 29, 2024, 07:35:15 amBut do you have an idea to add the functionality that the vertical line shows the tooltips at all intersections when crossing the horizontal lines in the diagram? Somehow that hasn't worked yet. The tooltips are still displayed individually when the mouse pointer touches a diagram line. However, this should happen when touching the vertical line and for all displayed diagram lines. If you'd turn on the data point markers of the LineSeriesSelection  (LineSeriesSelection.ShowPoints := true) you'd see that the horizontal pieces of that series consist of many data points having the same y value. Therefore, the behaviour of the DatapointHintTool and DatapointCrosshairTool is correct, they follow the data points and are displayed for every value along the horizontal segments. If you don't want this you have two options: /1/ When you add a new value to the series check the y value added at the previous step and skip this value if it has the same y value: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormMessung.DrawChart(SortTimesSelection, SortTimesQuick, SortTimesShell: array of Integer);var  i: Integer;begin  LineSeriesSelection.BeginUpdate;  try    LineSeriesSelection.Clear;  for i := 0 to High(SortTimesSelection) do      if i > 0 and (SortTimesSelection[i] <> SortTimesSelection[i-1]) then        LineSeriesSelection.AddXY(i, SortTimesSelection[i]);  finally    LineseriesSelection.EndUpdate;  end;   ... /2/ When positioning the hint text scan the data values in the vicinity of the currently proposed data point index and find the last value at the same y level: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFormMessung.DataPointCrossHairToolCustomDraw(ATool: TDatapointDrawTool; ADrawer: IChartDrawer);// BTW, there is a small bug in the following code which occasionally crashes the application - I want you to find it yourself...var  series: TLineSeries;  P: TDoublePoint;  i: Integer;  y: Double;begin  if ATool.Series is TLineSeries then  begin    series := TLineSeries(ATool.Series);    y := series.YValue[ATool.PointIndex];    i := ATool.PointIndex;    while (i < series.Count) and (series.YValue[i] = y) do      inc(i);    P := DoublePoint(series.XValue[i], series.YValue[i]);    ToolTipPosition := Chart.GraphToImage(P);  end;end; Quote from: ChrisP on May 29, 2024, 07:35:15 amIs there also a way to customize the diagram so that if different diagram lines are displayed and the values are far apart (e.g. line 1 is in the Y-axis range 0 - 500000 ms and line 2 is in the Y-axis range 0-200 ms) that this is dynamically taken into account and the diagram becomes clearer? So that the Y-axis is divided in the scaling? Otherwise the other displayed diagram lines are “lost” in the view. You may want to read https://wiki.lazarus.freepascal.org/TAChart_Tutor ial:_Dual_y_axis,_Legend which covers exactly the same situation (just ignore the part on UserdefinedChartsource, add the data points to the series as usual). Quote from: ChrisP on May 29, 2024, 07:35:15 amBut when closing the module, the GRID in the MainMenu module should actually update itself and select the previously selected data set. Instead, the GRID is emptied and no data set is displayed or selected. What is the reason for this? It has worked accordingly in the past. I don't know. It's your code, find it out: Use the debugger and study what is happening and why. ---------------------------------------------------------------------- ChrisP · 2024-06-03 06:33 Thank you again! Quote/1/ When you add a new value to the series check the y value added at the previous step and skip this value if it has the same y value: Update2: I have now got it to run without error. However, the scaling does not work properly. The left Y-axis is scaled in the same way as the right Y-axis. So nothing has changed in the view. In addition, the tooltips still do not work as they should. ---------------------------------------------------------------------- ChrisP · 2024-06-06 06:27 I have made some progress. Now I only have the following problems: 1. the vertical line (controlled by the mouse cursor) should display a tooltip at all intersection points when it comes into contact with the diagram lines. 2. The course of the diagram lines looks strange. Each diagram should actually show the number of processed (sorted) data over time. Somehow this still looks very “wild”. Can someone here perhaps help me and take a look at this? I have attached the corresponding files. Thanks again for your help! ---------------------------------------------------------------------- CM630 · 2024-06-06 07:45 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i formmessung.pas(92,20) Error: Identifier not found "IDataExch"Google says nothing about that. Maybe if you have attached the LPI, LPR, etc. files it would compile. ---------------------------------------------------------------------- cdbc · 2024-06-06 08:28 Hi @CM630: It's just an elaborate callback interface he uses to communicate between forms... Regards Benny ---------------------------------------------------------------------- wp · 2024-06-06 14:06 Sorry I can't help you. Please post a full compilable project (incl lpi, lpr), which you have tested to show the issue. Finally, I somehow was able to create a project which I could compile after removal of the IDataExch things. But now the chart does not show any data, and everything is disabled. Probably due the missing IDataExch... ---------------------------------------------------------------------- ChrisP · 2024-06-07 05:41 Sorry. You are right, of course. I have packed the program executable (incl. lpi, lpr). Unfortunately, the size limit of the forum only allows an upload of 500 KB. Therefore again via an external link: It would be nice if you could help me with these problems again: 1. the vertical line (controlled by the mouse pointer) should display a tooltip at all intersection points when it comes into contact with the diagram lines. 2. the course of the diagram lines looks strange. Each chart should actually show the number of processed (sorted) data over time. Somehow this still looks very “wild”. It should therefore show an ascending line over time for all three sortings. Or am I wrong? The problems relate only to formmessung.pas and formmessung.lfm, more precisely the chart integrated there. Many thanks again! ---------------------------------------------------------------------- wp · 2024-06-07 20:20 I'm sorry I am not willing to help you. It is a huge progam, which I don't even know how to use. And why did you include the exe in the download? and lots unit folders, all of ccr, although they are not needed at all. ---------------------------------------------------------------------- ChrisP · 2024-06-08 05:50 @wp No problem. You've already helped me a lot. I'll try to work it out myself somehow. I'm making progress step by step. About the download file: I just wanted everything to be together and executable. ---------------------------------------------------------------------- CM630 · 2024-06-08 21:54 My advice - create a standalone project, containg only the part of code that is related to your question. For example - one TAChart, some small code to draw whatever to you want, etc. Then upload all source files (pas, frm, lpi, lpr...). (lps files are not needed, the subfolders generated by the compiler are useless). Some screenshots might be usefull. So when s.o. downloads your snippet he/she could get straight to the issue - click the LPI, the project is open. Also, ultra compression of 7zip might create several times smaller files than pure Zip.