Lazarus

Programming => Graphics and Multimedia => TAChart => Topic started by: torbente on June 12, 2018, 04:10:44 pm

Title: Tchart with 2 lines and time interval
Post by: torbente on June 12, 2018, 04:10:44 pm
Hi everyone. Im very new with charts, this is my first try.
I need create a tchart with 2 lines series, each one using a different Y axis (left and rigth)

I created both series and added an aditional axis and changed its alinment to rigth.
Then, i selected AxiIndexY left for the first serie, and AxiIndexY rigth for the secodn one, but both Y axis are showing the same scale. How i could implement this?

Thanks.
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 12, 2018, 05:14:43 pm
http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Dual_y_axis,_Legend
Title: Re: Tchart with 2 lines and time interval
Post by: torbente on June 13, 2018, 11:41:59 pm
http://wiki.lazarus.freepascal.org/TAChart_Tutorial:_Dual_y_axis,_Legend

Great, thanks.
Just another Question: is there candles support in Tchart?
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 14, 2018, 12:32:20 am
You mean CandleStick? Yes, use TOpenHighLowCloseSeries and set its Mode to mCandleStick. There is a demo in folder components/tachart/demo/financial of your Lazarus installation.
Title: Re: Tchart with 2 lines and time interval
Post by: torbente on June 16, 2018, 04:08:53 pm
You mean CandleStick? Yes, use TOpenHighLowCloseSeries and set its Mode to mCandleStick. There is a demo in folder components/tachart/demo/financial of your Lazarus installation.

Thanks again.
Now all is working, but sometimes i get an "access violation" (the chart refresh every few seconds) Im doing some tests, and if i delete this line

Code: Pascal  [Select][+][-]
  1. form6.currentmarket.clear; // clear the series to set new data

It never happens, but the series is redrawn manytimes and finally beguns unusefull.
Im trying to intercept the error with a try... except but i do not know how to call it.

 
Code: Pascal  [Select][+][-]
  1.   Try
  2.    form6.currentmarket.clear;
  3.    except
  4.    On E:error do
  5.       begin
  6.       proceder := false;
  7.       end;
  8.    end;

Any ideas?
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 16, 2018, 04:44:17 pm
The information that you provide is too unclear to give you more detailed help. What is "currentmarket"? Why are you calling form6 explicitely? What is happening before the .Clear?. I only can repeat: try to reduce the issue to a small project which shows the issue and which you can upload here. When doing this you normally find the issue by yourself, or - if not - I have something to work with.
Title: Re: Tchart with 2 lines and time interval
Post by: torbente on June 16, 2018, 05:31:03 pm
The information that you provide is too unclear to give you more detailed help. What is "currentmarket"? Why are you calling form6 explicitely? What is happening before the .Clear?. I only can repeat: try to reduce the issue to a small project which shows the issue and which you can upload here. When doing this you normally find the issue by yourself, or - if not - I have something to work with.

currentmarket is a OpenHighLowlCloseserie. I call form6 since the chart is in another form (updatechart function is in the project main unit, not in a form) Before .Clear, the function verify is there is new data to update the series: if it found new data, it updates the chart (calling .Clear first), if not, nothing happens.
I will create the small project as you suggested to verify.
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 16, 2018, 05:39:24 pm
currentmarket is a OpenHighLowlCloseserie.
Since OpenHighLowCloseSeries requires several y values (4 to be specific) I could imagine that you did not set the YCount of the chartsource accordingly. For this interal ListChartSource, this is done automatically, but maybe you are using an external ChartSource - all these details are important, and become clear when you post a compilable demo.
Title: Re: Tchart with 2 lines and time interval
Post by: torbente on June 16, 2018, 08:03:11 pm
Here is the demo. It complies ok and run perfect, but sometimes it throws an "access violation", sometimes after a few time running (between 15 seconds and 5 minutes)

http://www.mediafire.com/file/a37769u7sfvbec1/Lazarus%20-%20Chart%20Demo.rar

PS if i delete the .Clear call, the app never hangs.
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 16, 2018, 11:48:42 pm
You are working with threads, but you are accessing the gui from the thread. This is absolutely forbidden. Read this: http://wiki.freepascal.org/Multithreaded_Application_Tutorial (at least up to including "The Thread Class"), and https://www.getlazarus.org/forums/viewtopic.php?t=52.

I am attaching a version of your program without a thread (the easy way, absolutely no problems, but a bit slaggy when you drag the window), and another version with a download thread. The latter one implements a simple thread which just downloads the json string and returns it to the main thread for analysis and plotting.

BTW: It is normally not a good idea to provide sources on some cloud servers because the sources may not be available any more some time in the future, but they are an important ingredient of the posted question. You can upload source code here to the forum under "Attachments and other options". Pack the .pas, .lfm, .lpi and .lpr files (plus maybe data files needed) into a common zip - don't add the compiler-generated files .exe, .dcu, .res, .ico etc. otherwise the upload will be too large (max is 250 kB),
Title: Re: Tchart with 2 lines and time interval
Post by: torbente on June 17, 2018, 01:51:42 am
Thanks for all your suggestions and tips. I will work to implement your idea to my app but with another aproach (update a global array with the data from the thread, and update the chart with the array from the main thread)
Just the last same question: how i could intercept errors on charts? E:????

PS: another question: can i force the Y axis to use always complete numbers? IE 0,00000001, not a notational one
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 17, 2018, 12:31:53 pm
how i could intercept errors on charts? E:????
Unit TAChartUtils declares the basic chart Exception: EChartError
Code: Pascal  [Select][+][-]
  1. type
  2.   EChartError = class(Exception);
  3.   EChartIntervalError = class(EChartError);
  4.   EListenerError = class(EChartError);
  5.   EDrawDataError = class(EChartError);  
But exceptions are an annoyance when working in the IDE, so better avoid them. They are errors which must be avoided not be abused to avoid checking for error cases.

another question: can i force the Y axis to use always complete numbers? IE 0,00000001, not a notational one
What is a "complete number"? What is a "notational" number?
Title: Re: Tchart with 2 lines and time interval
Post by: torbente on June 17, 2018, 01:50:26 pm
number : 0,00000001
notation : 1E-8
Title: Re: Tchart with 2 lines and time interval
Post by: wp on June 17, 2018, 02:49:19 pm
Chart.LeftAxis.Marks.Format This format string is used by the Format() function to convert a number to string. The default '%0:.9g" is very general, it switches to exponential notation when the number is less than some threshold. Use "f" instead of "g" to have nonexponential notation all the time. Replace "9" by the number of decimals places required.

But be warned: You'll get the same labels along the axis if you select the number of decimal places too small for your data. So "%0:.3f" will give you '0.000' at all labels.
TinyPortal © 2005-2018