Recent

Author Topic: [solved] Tool: HintTool - "invalid type cast"  (Read 618 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1308
[solved] Tool: HintTool - "invalid type cast"
« on: December 19, 2025, 05:21:03 pm »
There is a TAChart with a huge amount of series: TLineSeries.
Today I addes an OHLC series.
On mouse over I read "invalid type cast". Not surprising, because I refer to TLineSeries and nothing else.
How can I work around?
Ideal would be to have BOTH of the series in my hint: the values of the active line series and that of the OHTLC as well.

Code: Pascal  [Select][+][-]
  1. procedure TFrame_cot_dis.Tool_HintHint(ATool: TDataPointHintTool;
  2.   const APoint: TPoint; var AHint: String);
  3. var ser: TLineSeries;
  4.     idx: Integer;
  5.     yidx: Integer;
  6.     ttl: String;
  7.     s: string;
  8. begin
  9.   with TDatapointHintTool(ATool) do begin
  10.     ser := Series as TLineSeries;
  11.     idx := PointIndex;
  12.     yidx := YIndex;
  13.   end;
  14.  
  15.   if ser <> nil then begin
  16.     if yidx > 0 then
  17.       ttl := TLineSeries(ser).Styles.Styles[yidx].Text
  18.     else begin
  19.       ttl := ser.Title;
  20.       s := Zeit.StringDoubleWirdString(FloatToStr(ser.GetXValue(idx)));
  21.       AHint := ttl +': '+ FloatToStrF(ser.GetYValue(idx), ffFixed, 4, 0)+ ' (' + s + ')';
  22.         end
  23.     end else AHint := '';
  24. end;
« Last Edit: January 03, 2026, 10:11:55 am by Nicole »

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: Tool: HintTool - "invalid type cast"
« Reply #1 on: December 20, 2025, 12:04:38 am »
Hmm
Code: Pascal  [Select][+][-]
  1. Series as TSeriesClass;
  2.  

don't know but maybe using the base class will help, not sure if that is too low of a check..


Jamie
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13431
Re: Tool: HintTool - "invalid type cast"
« Reply #2 on: December 20, 2025, 01:10:36 am »
Code: Pascal  [Select][+][-]
  1.   with TDatapointHintTool(ATool) do begin
  2.     ser := Series as TLineSeries;
  3. ...
A hard-cast by means of the "as" operator raises an exception if the types do not match. Better to check the type first and then do a manual cast:
Code: Pascal  [Select][+][-]
  1. var
  2.   lineSer: TLineSeries;
  3.   ohlcSer: TOHLCSeries;
  4. ...
  5.   with TDatapointHintTool(ATool) do begin
  6.     if Series is TLineSeries then
  7.       lineSer := TLineSeries(Series)
  8.     else if series is TOHLCSeries then
  9.       ohlcSer := TOHLCSeries(Series)
  10.     else
  11.       exit;
  12. ...

Now, how to combine the hints: I am assuming that all series share the same x values, or in other words: For every data point index every series sees the same x value. In this case you get the point index from the tool (ATool.PointIndex), and now you can query the values that you want to combine in the hint from all series needed even when they are not hit by the tool

Example (untested):
Code: Pascal  [Select][+][-]
  1. procedure TFrame_cot_dis.Tool_HintHint(ATool: TDataPointHintTool;
  2.   const APoint: TPoint; var AHint: String);
  3. var
  4.   idx: Integer;
  5.   O, H, L, C: Double;
  6.   volume: Double;
  7.   dt: TDateTime;
  8. begin
  9.   idx := ATool.PointIndex;
  10.   dt := OHLCSeries.XValue[idx];
  11.   O := OHLCSeries.YValues[idx, OHLCSeries.YIndexOpen];
  12.   H := OHLCSeries.YValues[idx, OHLCSeries.YIndexHigh];
  13.   L := OHLCSeries.YVAlues[idx, OHLCSeries.YIndexLow];
  14.   C := OHLCSeries.YValues[idx, OHLCSeries.YIndexClose];
  15.   volume := VolumeSeries.YValue[idx];  // Assuming that this is a TLineSeries displaying the volume of transactions on this date
  16.   AHint := Format(
  17.     'Date: %s' + LineEnding +
  18.     'Open: %.2n $'+LineEnding+
  19.     'High: %.2n $' + LineEnding +
  20.     'Low: %.2n $' + LineEnding +
  21.     'Close: %.2n $' + LineEnding +
  22.     'Volume: %.2n $', [
  23.     ShortDateToStr(dt),
  24.     O, H, L, C,
  25.     volume
  26.   ]);
  27. end;

Nicole

  • Hero Member
  • *****
  • Posts: 1308
Re: Tool: HintTool - "invalid type cast"
« Reply #3 on: January 03, 2026, 10:11:35 am »
Thank you so very much!

Here is my version and a second one, case somebody needs it on a rainy day.
No, the second one is not from me neither, but also from WP long ago:

Code: Pascal  [Select][+][-]
  1. // gibt die Werte der Kurven unter dem Cursor aus
  2. procedure TFrame_cot_dis.Tool_HintHint(ATool: TDataPointHintTool;
  3.   const APoint: TPoint; var AHint: String);
  4. var lineSer: TLineSeries;
  5.     ohlcSeries: TOpenHighLowCloseSeries;
  6.     idx: Integer;
  7.     O, H, L, C: Double;
  8.     volume: Double;
  9.     dt: TDateTime;
  10.     yidx, YWert: Integer;
  11.     ttl: String;
  12.     s: string;
  13. begin
  14.   with TDatapointHintTool(ATool) do begin  // Vorteil zu "with.. as" => keine Exception
  15.     if Series is TLineSeries then begin
  16.       lineSer := TLineSeries(Series);
  17.       idx := PointIndex;
  18.       YWert := YIndex;
  19.       if lineser <> nil then begin
  20.         if yidx > 0 then
  21.             ttl := TLineSeries(lineser).Styles.Styles[yidx].Text
  22.           else begin
  23.             ttl := lineser.Title;
  24.             s := Zeit.StringDoubleWirdString(FloatToStr(lineser.GetXValue(idx)));
  25.             AHint := ttl +': '+ FloatToStrF(lineser.GetYValue(idx), ffFixed, 4, 0)+ ' (' + s + ')';
  26.               end
  27.           end else AHint := '';
  28.     end
  29.     else if series is TOpenHighLowCloseSeries then begin
  30.       ohlcSeries := TOpenHighLowCloseSeries(Series);
  31.       idx := ATool.PointIndex;
  32.       dt := OHLCSeries.XValue[idx];
  33.       O := OHLCSeries.YValues[idx, OHLCSeries.YIndexOpen];
  34.       H := OHLCSeries.YValues[idx, OHLCSeries.YIndexHigh];
  35.       L := OHLCSeries.YVAlues[idx, OHLCSeries.YIndexLow];
  36.       C := OHLCSeries.YValues[idx, OHLCSeries.YIndexClose];
  37.       //VolumeSeries.YValue[idx];  // Assuming that this is a TLineSeries displaying the volume of transactions on this date
  38.       AHint := Format(
  39.         'Date: %s' + LineEnding +
  40.         'Open: %.2n'+LineEnding+
  41.         'High: %.2n' + LineEnding +
  42.         'Low: %.2n' + LineEnding +
  43.         'Close: %.2n'    // 'Volume: %.2n $'
  44.         , [
  45.         DateToStr(dt),
  46.         O, H, L, C
  47.         // ,volume
  48.       ]);
  49.  
  50.     end
  51.     else exit; end;
  52.  
  53. end;

Code: Pascal  [Select][+][-]
  1. // verschiebt das Ausgabefenster vom Mauscursor weg
  2. procedure TFrame_cot_dis.Tool_HintHintLocation(ATool: TDataPointHintTool;
  3.   AHintSize: TSize; var APoint: TPoint);
  4. begin
  5.   APoint.Y := APoint.Y - AHintSize.CY;
  6. end;

 

TinyPortal © 2005-2018