Recent

Author Topic: TAChart: TNearestPointParams - Debug Errors (UPDATE)  (Read 6891 times)

ChrisP

  • New Member
  • *
  • Posts: 14
TAChart: TNearestPointParams - Debug Errors (UPDATE)
« on: May 25, 2024, 08:02:51 am »
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
« Last Edit: June 09, 2024, 02:46:07 pm by ChrisP »

cdbc

  • Hero Member
  • *****
  • Posts: 1838
    • http://www.cdbc.dk
Re: TAChart: TNearestPointParams - Debug Errors
« Reply #1 on: May 25, 2024, 08:33:19 am »
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?
<your sample is not compilable>
Regards Benny
« Last Edit: May 25, 2024, 08:42:05 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ChrisP

  • New Member
  • *
  • Posts: 14
Re: TAChart: TNearestPointParams - Debug Errors
« Reply #2 on: May 25, 2024, 08:38:09 am »
@cdbc
Thank you very much for your idea.
But can you see where the "renaming" error is caused in my code?  :o
I've been working on the code for hours. I can't find the solution.

dsiders

  • Hero Member
  • *****
  • Posts: 1347
Re: TAChart: TNearestPointParams - Debug Errors
« Reply #3 on: May 25, 2024, 08:42:24 am »
Hello,

I am an absolute beginner in Lazarus and Delphi and have problems with the following procedure:
Code: Pascal  [Select][+][-]
  1. procedure TFormMeasurement.ShowMouseInfo(X, Y: Integer);
  2. var
  3.   params: TNearestPointParams;
  4.   res: TNearestPointResults;
  5.   Distance: Double;
  6. begin
  7.   params.Point := Point(X, Y);
  8.   params.Radius := 8;
  9.   params.Distance := MaxDouble;
  10.  
  11.   if LineSeriesSelection.Active then
  12.   begin
  13.     if LineSeriesSelection.GetNearestPoint(params, res) then
  14.     begin
  15.       Distance := Hypot(res.FValue.X - X, res.FValue.Y - Y);
  16.       if Distance < 15 then
  17.         LabelResultSelS.Caption := Format('Selection Sort: (%.0f, %.0f)', [res.FValue.X, res.FValue.Y]);
  18.     end;
  19.   end;
  20.  
  21.   if LineSeriesQuick.Active then
  22.   begin
  23.     if LineSeriesQuick.GetNearestPoint(params, res) then
  24.     begin
  25.       Distance := Hypot(res.FValue.X - X, res.FValue.Y - Y);
  26.       if Distance < 15 then
  27.         LabelResultQuS.Caption := Format('Quick Sort: (%.0f, %.0f)', [res.FValue.X, res.FValue.Y]);
  28.     end;
  29.   end;
  30.  
  31.   if LineSeriesShell.Active then
  32.   begin
  33.     if LineSeriesShell.GetNearestPoint(params, res) then
  34.     begin
  35.       Distance := Hypot(res.FValue.X - X, res.FValue.Y - Y);
  36.       if Distance < 15 then
  37.         LabelResultShS.Caption := Format('Shell Sort: (%.0f, %.0f)', [res.FValue.X, res.FValue.Y]);
  38.     end;
  39.   end;
  40. end;
  41.  

I receive the following feedback (errors) from the debugger:

Code: Pascal  [Select][+][-]
  1. formmeasurement.pas(352,10) Error: identifier idents no member "Point"
  2. formmeasurement.pas(353,10) Error: identifier idents no member "Radius"
  3. 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][+][-]
  1. TNearestPointParams = record
  2.     FDistFunc: TPointDistFunc;
  3.     FOptimizeX: Boolean;
  4.     FPoint: TPoint;
  5.     FRadius: Integer;
  6.     FTargets: TNearestPointTargets;
  7.   end;
  8.  
  9.   TNearestPointResults = record
  10.     FDist: Integer;
  11.     FImg: TPoint;
  12.     FIndex: Integer;        // Point index
  13.     FXIndex: Integer;       // Index to be used in Source.GetX()
  14.     FYIndex: Integer;       // Index to be used in Source.GetY()
  15.     FValue: TDoublePoint;
  16.   end;
  17.  
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

cdbc

  • Hero Member
  • *****
  • Posts: 1838
    • http://www.cdbc.dk
Re: TAChart: TNearestPointParams - Debug Errors
« Reply #4 on: May 25, 2024, 08:45:24 am »
Hi
Kudos Don, well spotted.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ChrisP

  • New Member
  • *
  • Posts: 14
Re: TAChart: TNearestPointParams - Debug Errors
« Reply #5 on: May 25, 2024, 09:14:01 am »
@dsiders @dcbc
I think that will get me further! 8-)
Thanks for your help!

ChrisP

  • New Member
  • *
  • Posts: 14
Re: TAChart: TNearestPointParams - Debug Errors
« Reply #6 on: May 25, 2024, 10:05:24 am »
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
« Last Edit: June 09, 2024, 02:45:45 pm by ChrisP »

wp

  • Hero Member
  • *****
  • Posts: 12598
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #7 on: May 25, 2024, 04:14:25 pm »
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

  • Hero Member
  • *****
  • Posts: 12598
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #8 on: May 26, 2024, 06:36:59 pm »
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][+][-]
  1. type
  2.   TChart = class(...)
  3.     ...
  4.     function GraphToImage(const AGraphPoint: TDoublePoint): TPoint;
  5.     function ImageToGraph(const APoint: TPoint): TDoublePoint;
  6.     function XGraphToImage(AX: Double): Integer;
  7.     function XImageToGraph(AX: Integer): Double;
  8.     function YGraphToImage(AY: Double): Integer;
  9.     function YImageToGraph(AY: Integer): Double;
  10.     ...
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

  • Hero Member
  • *****
  • Posts: 12598
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #9 on: May 27, 2024, 12:22:44 pm »
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][+][-]
  1. procedure TFormMeasurement.DrawVerticalLineAndTooltips(X: Integer);
  2. var
  3.   ChartX: Double;
  4.   ...
  5. begin
  6.   if not Chart.ScalingValid then
  7.     Chart.Repaint;
  8.   ChartX := Chart.XImageToGraph(X);
  9.   ...

wp

  • Hero Member
  • *****
  • Posts: 12598
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #10 on: May 28, 2024, 09:30:18 am »
The DatapointHintTool fires two events before the hint window is shown:
Code: Pascal  [Select][+][-]
  1.   OnHintPositon        (= procedure (ATool: TDataPointHintTool; var APoint: TPoint) )
  2.   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

  • New Member
  • *
  • Posts: 14
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #11 on: May 28, 2024, 10:42:23 am »
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
« Last Edit: June 09, 2024, 02:45:16 pm by ChrisP »

wp

  • Hero Member
  • *****
  • Posts: 12598
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #12 on: May 28, 2024, 11:18:47 am »
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

  • New Member
  • *
  • Posts: 14
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #13 on: May 28, 2024, 11:40:42 am »
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?
« Last Edit: June 09, 2024, 02:44:47 pm by ChrisP »

wp

  • Hero Member
  • *****
  • Posts: 12598
Re: TAChart: TNearestPointParams - Debug Errors (UPDATE)
« Reply #14 on: May 28, 2024, 04:22:22 pm »
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.

 

TinyPortal © 2005-2018