Improvements with TFitSeries lazarus / 55-TAChart ---------------------------------------------------------------------- wp · 2012-08-15 12:33 When working at the updated fitting tutorial (http://wiki.lazarus.free pascal.org/TAChart_Tutorial:_Plotting_Moore%27s_Law) I came across a few deficiencies of TFitSeries: GetFitFunctionString so far gets only a single formatting parameters. This is not good for the example in the tutorial where the fitting parameters for the exponential fit are a= 4.4E-275 and b=0.32. Of course, a general format specifier like "%.9g" can be used, but b would be much easier to read if the format specifier were something like "%.2f". But this makes the a to zero, and this would make the entire function zero. Using individual format specifiers is a solution. The attached patch overloads the GetFitFunctionString with a version where an array of formatting strings can be passed for each fit parameter. I added code to replace an empty format specifier string by '.9g'. In the new tutorial, the data are entered at design-time to a ListChartSource. Without doing anything else it seems that the fit is not executed. Maybe something is wrong with notification at designtime because the fit is not updated also when the data points are edited. But I think it would not be harmful if TFitSeries had an overridden Loaded procedure calling ExecFit (this is not included in the patch): Code: [Select]procedure TFitSeries.Loaded; begin   inherited Loaded;   ExecFit; end; ---------------------------------------------------------------------- Ask · 2012-08-16 17:37 QuoteGetFitFunctionString so far gets only a single formatting parameters.I have implemented a solution in r32258. It is quite different from yours, but achieves the same result. Here is a story: First, I have applied your patch and noticed that ParamsToEquation function is essentially duplicated. To fix that, I have first used "is nested" functions, but ran into a FPC bug. I then reimplemented using "pointer passing" idiom, but passing open string array as untyped pointers turned out to be a messy proposition. So I decided to go all the way and implement "correct", even if heavy, solution. I do agree that 120+ lines of new code is probably an overkill for such a simple feature, but it does give some additional flexibility and nice syntax. Also, I had some fun with using nested classes and interfaces, and hit a few CodeTools bugs I have marked previous API as deprecated for now. I think I'll remove ParamsToEquation since it probably has zero users. I am not sure about GetFitEquationString -- if somebody uses this function, please tell your opinion. Quotereplace an empty format specifier string by '.9g'I disagree. Why would a user pass an empty format specifier (as opposed to omitting it)? If he does, I think it is clearly a bug and it is better to signal it with the exception then hide. I did add '.9g' as the default value for NumFormat, so to get the default equation it is should be enough to write just "Label1.Caption := FitSeries1.EquationText" Quotesomething is wrong with notification at designtime becauseShould be fixed in  r38261. Quoteoverridden Loaded procedure calling ExecFitNot harmful, but may hide the real bug. Should be fixed in r38260. ---------------------------------------------------------------------- wp · 2012-08-16 19:47 I am impressed. Works perfectly. QuoteI am not sure about GetFitEquationString -- if somebody uses this function, please tell your opinion I am using it in the new LogAxis tutorial, but I will probably drop it - there are a lot of 1.0RC1 users out there, and I don't want to confuse them with the new syntax they are not having, the tutorial is still quite long to explain both. QuoteLabel1.Caption := FitSeries1.EquationText" In the tutorial I was testing this assignment within a Format statement, and this crashes at runtime due to an "invalid format index": Code: [Select]  with Chart1FitSeries1 do     ShowMessage(Format(       'Fit result: a = %g, b = %g'#13'Equation: %s', [       Param[0], Param[1],       EquationText       //GetFitEquationString('%.9g', 'x', 'y')     ])); QuoteI had some fun with using ... Glad to hear that. ---------------------------------------------------------------------- wp · 2012-08-16 19:54 Ah, I forgot this: For the legend you are calling this Code: [Select]t := Format(Legend.Format, [Title, Index, EquationText.NumFormat('%f').Get]);  This means that the equation text does not get the new individual format specifiers, and as I see it, there is not way to interfere with it. I would appreciate if a series had an event OnGetLegendtext overriding the default text. This would allow to use individual format specifiers. ---------------------------------------------------------------------- Ask · 2012-08-16 20:36 QuoteI am using it in the new LogAxis tutorial, but I will probably drop itAgreed. QuoteIn the tutorial I was testing this assignment within a Format statement, and this crashes at runtime due to an "invalid format index"Yes, maybe I have gone too far into C++ land here EquationText returns an object with IEquationText interface, which has .Get function to extract actual text. Usually there is no need to call .Get, because I have defined automatic conversion from IEquationText to string. Format function, however, accepts an array of arbitrary types so does not trigger the conversion. Use EquationText.Get -- like in GetFitEquationString code you cited. Quoteevent OnGetLegendtext overriding the default textIt is called Legend.OnCreate, see wiki.lazarus.freepascal.org/TAChart_documentation#Legend_item_text and "legend" demo. BTW, your tutorial progresses nicely. However, I disagree with the new name. The fact that the plot represents Moore's Law is a nice detail, but not important enough to deserve a place in the title. Please use something like "TAChart Tutorial: list source, logarithmic axis, fitting" or similar -- so that user can immediately see the topic from the link. ---------------------------------------------------------------------- wp · 2012-08-16 21:11 QuotePlease use something like "TAChart Tutorial: list source, logarithmic axis, fitting" or similar -- so that user can immediately see the topic from the link. Agreed.