Recent

Author Topic: DatePointHintTool - invalid typ cast  (Read 1058 times)

Nicole

  • Hero Member
  • *****
  • Posts: 742
DatePointHintTool - invalid typ cast
« on: February 01, 2023, 08:30:52 pm »
I have this code in the onHint-Event

Code: Pascal  [Select][+][-]
  1. var ser: TOpenHighLowCloseSeries;
  2.     ser_Vol: TLineSeries;
  3.     ser_Oi: TBarSeries;
  4.     punkt: Integer;
  5.     o, h, l, c: double;
  6.     s: String;
  7.     d: double;
  8.     dat: TDate;
  9.  
  10. ....
  11.  
  12.  
  13.   with TDatapointHintTool(ATool) do begin   /
  14.      ser := ATool.Series as TOpenHighLowCloseSeries; // works fine
  15.  
  16.   //these 2 give me "invalid typ cast"
  17.   ser_Vol := ATool.Series as TLineSeries;
  18.   ser_oi := ATool.Series as  TBarSeries;  
  19.    punkt := PointIndex;
  20.   end;
  21. .............
  22.  


So of 3 types, the first one works fine.
The others show "invalid type cast" or give me an protection error on mouse over.
Not sure, the thing changes.

Yes, the mistake can be elsewhere.
To my mind it is in those construction above.
If yes, does anybody have the clou "what"?

If you ask, if I see the 2 problem series on my chart - no.
This would suggest, that the error is somewhere else.
But how to search for it?

I have the illusion, that I did everything the same as in all other TCharts.
And the illusion, that I did the with the 2 series the same as with the first, the working one.
 :'(

wp

  • Hero Member
  • *****
  • Posts: 10649
Re: DatePointHintTool - invalid typ cast
« Reply #1 on: February 01, 2023, 10:56:08 pm »
ATool.Series is always the series which is clicked by the tool. So, when you clicked the OpenHighLowCloseSeries the cast "ATool.Series as TOpenHighLowCloseSeries" is correct. But when you have in the same procedure a LineSeries and cast the same clicked ATool.Series to a TLineSeries it must fail - because the clicked series simply is not a LineSeries.

If you want to handle different series types in the same event handler you should not use the "as" operator. Check the series type with "is" instead and then do a soft cast - this does not raise an exception:

Code: Pascal  [Select][+][-]
  1. var
  2.   ser: TOpenHighLowCloseSeries;
  3.   ser_vol: TLineSeries;
  4.   ser_Oi: TBarSeries;
  5.   tool: TDatapointHintTool;
  6. ...
  7.   tool := TDatapointHintTool(ATool);
  8.   if tool.Series is TOpenHighLowCloseSeries then
  9.     ser := TOpenHighLowCloseSeries(tool.Series)
  10.   else if tool.Series is TLineSeries then
  11.     ser_vol := TLineSeries(tool.Series)
  12.   else if tool.Series is TBarSeries then
  13.     ser_Oi := TBarSeries(tool.Series);

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #2 on: February 03, 2023, 02:46:29 pm »
Thank you for the answer.
For me this attempt ended in a disaster, my source did not start any more and I have to go back to a backup. Somehow this was not just an error-line, but I had to delete a bundle of graphics starting with the tool (did not help) and the charts.

Adding one by one again of my frame I found the line, which is the ► problem

How to work around?
Thanks.

 
Code: Pascal  [Select][+][-]
  1.      var
  2.       ser: TOpenHighLowCloseSeries;
  3.       ser_vol: TLineSeries;
  4.       ser_Oi: TBarSeries;
  5.       tool: TDatapointHintTool;
  6.  
  7.  
  8. begin
  9.  
  10. ►  //      tool := TDatapointHintTool(ATool);
  11.       //if tool.Series is TOpenHighLowCloseSeries then
  12.       //  ser := TOpenHighLowCloseSeries(tool.Series)
  13.       //else if tool.Series is TLineSeries then
  14.       //  ser_vol := TLineSeries(tool.Series)
  15.       //else if tool.Series is TBarSeries then
  16.       //  ser_Oi := TBarSeries(tool.Series);
  17. end;
  18.  
  19.  

this is the error message

Code: Text  [Select][+][-]
  1. [Window Title]
  2. Fehler
  3.  
  4. [Content]
  5. Projekt project_x hat Exception-Klasse »External: ACCESS VIOLATION« ausgelöst mit der Meldung:
  6. Access violation reading from address $0000000000000008.
  7.  
  8.  
  9.  In Datei 'tagraph.pas' in Zeile 1361:
  10. Result := Point(XGraphToImage(AGraphPoint.X), YGraphToImage(AGraphPoint.Y));
  11.  
  12. [Ok]

and this the jump
Code: Text  [Select][+][-]
  1. function TChart.GraphToImage(const AGraphPoint: TDoublePoint): TPoint;
  2. begin
  3.   Result := Point(XGraphToImage(AGraphPoint.X), YGraphToImage(AGraphPoint.Y));
  4. end;


wp

  • Hero Member
  • *****
  • Posts: 10649
Re: DatePointHintTool - invalid typ cast
« Reply #3 on: February 03, 2023, 03:03:58 pm »
In which context is this code used? In a handler for a DataPointHintTool it should work. In case of other handlers you should check the tool class which has the Series property. The first tool in the tools hierarchy which has a Series property is the TDataPointTools:
Code: Pascal  [Select][+][-]
  1. var
  2.   ser: TOpenHighLowCloseSeries;
  3.   ser_vol: TLineSeries;
  4.   ser_Oi: TBarSeries;
  5.   tool: TDatapointHintTool;
  6. begin
  7.   if not (ATool is TDatapointTool) then
  8.     exit;
  9.  
  10.   tool := TDatapointTool(ATool);
  11.   if tool.Series is TOpenHighLowCloseSeries then
  12.     ser := TOpenHighLowCloseSeries(tool.Series)
  13.   else if tool.Series is TLineSeries then
  14.     ser_vol := TLineSeries(tool.Series)
  15.   else if tool.Series is TBarSeries then
  16.     ser_Oi := TBarSeries(tool.Series);
  17.   ...
  18. end;

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #4 on: February 03, 2023, 04:48:53 pm »
it is in the OnHint-Event in the DataPointHintTool.

Same problem, same error message.
I wrote the text below now
Pls be aware, that I filled in the word "hint" because the compiler complained.

Code: Pascal  [Select][+][-]
  1. Var    tool: TDatapointHintTool;
  2.  
  3.  
  4. begin
  5.  
  6.     if not (ATool is TDataPointHintTool) then
  7.     exit;
  8.  
  9.     tool := TDatapointHintTool(ATool);    

PS: The Supergau came back. The code does compile, but it does not run any more.
Not even the // in front of the var-declaration helped, no exit helped.

I think, I need my backup again. ;-((
« Last Edit: February 03, 2023, 04:52:20 pm by Nicole »

wp

  • Hero Member
  • *****
  • Posts: 10649
Re: DatePointHintTool - invalid typ cast
« Reply #5 on: February 03, 2023, 06:57:33 pm »
Try to extract the problematic code into a small test project which you can upload here. I can't help you with the code fragments shown so far.

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #6 on: February 04, 2023, 11:08:53 am »
It feels like a glass, which is filling up with bugs. If the content is low, nothing happens. No hint, no warning.
If it is full, all will be spilled at once without chance to watch.

This are the reasons why I think so:
I had code which allowed me to start my software. I "added lines" one by one. After the glass "spilled", I removed the lines again, line by line and could not get back to a working version again. The code must have been the same. All added lines were removed again(!), - but the app did not start any more.

The source code MUST have been the same.

And worst of all:
The stuff compiles and then the form does not load.

What made the difference in the code:
Those series which were NOT Topenhighlowclose. The Tseries and Tbar. And the OnHint event.
The moment I try to grab thosee 2 by OnHint - the "glass spills" sooner or later.

A demo will not be possible. I take the data from a database. And adding dummy data will be too huge a difference.
I am aware, you are just guessing and have low chances by using a glass-sphere.

Do you have any ideas?
My only idea at the moment is, to have no volume information and use only open, high, low, close, - which works fine in onHint, if I leave them alone there.


wp

  • Hero Member
  • *****
  • Posts: 10649
Re: DatePointHintTool - invalid typ cast
« Reply #7 on: February 04, 2023, 03:42:13 pm »
Then I can only give you general recommendations: Use the debugger. Build yourself a Debug-IDE (Tools > "Configure Build Lazarus", select "Debug IDE"), or add an option "-wg3" (without quotes) in "Additions and overrides" of the Project options -- these will allow you to step into LCL and third-party units (except for FCL/RTL - for this you would need a "Debug-FPC"). If the form which does not load is the main form, add a break point to the "begin" line of the project file (lpr) and step one line by one, step into the line in which an error is raised or in which the main form is created. Likewise if the form in created by a button/menu/action click: Set a breakpoint on this line in the OnClick handler, and then step into the form creation, later into the form show procedures etc.

I don't see why you should not be able to find the issue.

I doubt that it has anything to do with TAChart, though. Attaching a demo which loads the Google share prices from yahoo-finance for the past 100 days and displays the open/high/low/close prices along with the trading volume in a paned chart (one pane for OHLC, the other one for volume). I added a toolset with datapoint hint and datapoint crosshair tools to display a hint window for each data value hit by the mouse (the hint window is flickering a lot - I would prefer a stationary hint panel somewhere outside the chart which does not overlap with the plotted data, by the way).

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #8 on: February 04, 2023, 05:38:16 pm »
Your answer is really valuable to me. No matter, if I can solve the issue.
It explains how to debug and I cannot even tell you, how eager I am to open the yahoo thing!

At the moment I pay a fortune for data. This is ok to me as the quality their high and the programmers there are more than ok. Never the less I always wanted to have an emergency alternative.

Not sure, if anybody is interested to read Yahoo by Java.
If yes, here is it
https://github.com/buchen/portfolio/tree/master/name.abuchen.portfolio/src/name/abuchen/portfolio

I tried, - but somehow Java is not mine.

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #9 on: February 04, 2023, 06:54:56 pm »
to add:
Setting the property "toolset" in my 2 TCharts to "none" helped me to start my software again.

Now you may think "ok, so the OnHint event is the problem.
This is not that easy.
It looked like this


Code: Pascal  [Select][+][-]
  1. procedure TFrame_Zeichne.AusgabeChartpunktHint(ATool: TDataPointHintTool; const APoint: TPoint; var AHint: String);
  2.       //var
  3.       //ser: TOpenHighLowCloseSeries;
  4.       //ser_vol: TLineSeries;
  5.       //ser_Oi: TBarSeries;
  6.   //    tool: TDatapointHintTool;
  7.  
  8.  
  9. begin
  10. //
  11. //    if not (ATool is TDataPointHintTool) then
  12. //    exit;
  13. //
  14. //    tool := TDatapointHintTool(ATool);
  15.  
  16. //      tool := TDatapointHintTool(ATool);
  17.       //if tool.Series is TOpenHighLowCloseSeries then
  18.       //  ser := TOpenHighLowCloseSeries(tool.Series)
  19.       //else if tool.Series is TLineSeries then
  20.       //  ser_vol := TLineSeries(tool.Series)
  21.       //else if tool.Series is TBarSeries then
  22.       //  ser_Oi := TBarSeries(tool.Series);
  23. end;

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #10 on: February 07, 2023, 07:52:39 pm »
bad news:
The bug tricks me.
One start the hint works like it should, very nice! - next start the whole frame refuses the start of my software as such and shows the error I posted. The error occurs, when the tab with the Chart-Frame is shown. If the PageControl has a different frame active, the app start.

Same problem, if I only have my TOpenHighLowCloseSeries in the onHint-event.
So the combination with TBarSeries is not the problem.

Setting the toolbar property in my chart to "none" (means deleting the component from the property field always helpen.
So the problem is in the OnHint-Event of the tool.
But nothing special in this event. It is even there, if there is just an "exit" inside.
Sometimes.

How to grab?!
Nobody knows, if on next start, it will work or fail.

I am PRETTY sure, that OnHint is the problem.

wp

  • Hero Member
  • *****
  • Posts: 10649
Re: DatePointHintTool - invalid typ cast
« Reply #11 on: February 07, 2023, 08:11:21 pm »
I can only repeat: Without a compilable project I cannot help you.

Since you work with frames and since I know that there are sometimes problems with frames I rewrote the financial demo of one of the previous posts so that it uses frames. But again - no issue.
« Last Edit: February 07, 2023, 09:11:04 pm by wp »

Nicole

  • Hero Member
  • *****
  • Posts: 742
Re: DatePointHintTool - invalid typ cast
« Reply #12 on: February 08, 2023, 07:29:32 pm »
Thank you so much, I will try it.

You said, you think, the issue may sit behind the computer. I agree, that this is very often the case.

The tool is the problem quite sure.  But in the meanwhile I wonder, if there may be a problem by interaction of TWO tools. I tried very hard to solve and added and added and removed etc.... If the connection to the tool is somehow DOUBLE-defined this would fit to the disaster-crashes. May be there is not just a property pointing at ONE tool, but depth of the branches a second property pointing to a different address. Then both addresses contain some further points at nil and ....

Hope, I will find the reason and post it.

 

TinyPortal © 2005-2018