Lazarus

Programming => Graphics and Multimedia => TAChart => Topic started by: Nicole on January 16, 2023, 05:09:38 pm

Title: [partly solved] TAChart - Charttools - Crosshair - add ShowHints
Post by: Nicole on January 16, 2023, 05:09:38 pm
Added as the heading says.
I can click with the crosshair-cursor into my chart, fine.
It is a line chart in this case, not sure, if it matters. And Laz 2.2.4 in Windows 7.

What must I add, that there is a small window saying e.g. "16.1.2023: myvalue = 3456"
?
Title: Re: TAChart - Charttools - Crosshair - add ShowHints
Post by: wp on January 16, 2023, 10:37:43 pm
Use a TDatapointHintTool in addition to the TDataPointCrosshairTool. Both tools can share the same key combination (Shift property) for initiation. You have best flexibility when you construct the hint text by yourself: Switch "UseDefaultHintText" of the DatapointHintTool to false and assign a handler to the OnHint event, maybe like this:

Code: Pascal  [Select][+][-]
  1. // Construct the hint text.
  2. procedure TForm1.ChartToolset1DataPointHintTool1Hint(ATool: TDataPointHintTool;
  3.   const APoint: TPoint; var AHint: String);
  4. var
  5.   ser: TLineSeries;
  6. begin
  7.   ser := ATool.Series as TLineSeries;
  8.   AHint := Format('Series "%s": x = %.2f, y = %.2f', [
  9.     ser.Title,
  10.     ser.XValue[ATool.Index],
  11.     ser.YValue[ATool.Index]
  12.   ]);
  13. end;

Alternatively - keeping UseDefaultHintText at true - you could select a predefined hint text from the Marks.Style property of each series, but set Marks.Visible to false to hide the labels permanently on the chart.

In order to avoid that the mouse cursor overlaps with the hint window I'd also recommend to move the hint window to a higher position:
Code: Pascal  [Select][+][-]
  1. // Move the hint window so that it is not overlapped by the mouse cursor.
  2. procedure TForm1.ChartToolset1DataPointHintTool1HintLocation(
  3.   ATool: TDataPointHintTool; AHintSize: TSize; var APoint: TPoint);
  4. begin
  5.   APoint.Y := APoint.Y - AHintSize.CY;
  6. end;

And I noticed that sometimes the crosshair cursor remains visible after mouse-up - hide it in the OnAfterMouseUp event:
Code: Pascal  [Select][+][-]
  1. // Avoid keeping the crosshair in the chart sometimes.
  2. procedure TForm1.ChartToolset1DataPointCrosshairTool1AfterMouseUp(
  3.   ATool: TChartTool; APoint: TPoint);
  4. begin
  5.   TDataPointCrossHairTool(ATool).Hide;
  6. end;
Title: Re: TAChart - Charttools - Crosshair - add ShowHints
Post by: Nicole on January 18, 2023, 06:01:38 pm
(Thank you for the explanation and the example. It worked at sudden for me.)

ROW Back:
I was happy too early.

1)
Unfortunately the hint does always show the same text, no matter, where you click at.

2)
And for this I am too stupid:
 AHint := Format('Series "%s": x = %.2f, y = %.2f',....

How to say DateToStr(x-axes-value) ?
Title: Re: [partly solved] TAChart - Charttools - Crosshair - add ShowHints
Post by: wp on January 21, 2023, 10:14:04 pm
1) Sorry, typo: Use ATool.PointIndex rather than ATool.Index (the latter one is the index of the HintTool in the ChartToolset collection).

2)
Code: Pascal  [Select][+][-]
  1.      AHint := Format('mySeriesName "%s": x = %s, y = %.2f', [
  2.           ser.Title,
  3.           DateToStr(ser.XValue[ATool.Index]),
  4.           ser.YValue[ATool.Index]
  5.         ]);
TinyPortal © 2005-2018