Page 1 of 1

Curve Fitting...

Posted: Sun Jan 22, 2012 12:46 pm
by 10047857
Hi

I have a scattered point series on a chart and I want to fit a curve to it - this should be simple right?

I add a statistics tool to the chart and assign it to the point series and check the Curve fitting check box. When I run the code the line series which as been added automatically by the addition of the tool is still not visible.

Is there a step I'm missing here?

I am using v8.06 and have searched the user base and found mention of tutorial 7 on this subject - which I can't find - in fact the .hlp file is not readable using Windows 7. Is there any chance there is a .pdf of the help?

Bruce.

Re: Curve Fitting...

Posted: Mon Jan 23, 2012 10:24 am
by yeray
Hi Bruce,
Metman wrote:I have a scattered point series on a chart and I want to fit a curve to it - this should be simple right?

I add a statistics tool to the chart and assign it to the point series and check the Curve fitting check box. When I run the code the line series which as been added automatically by the addition of the tool is still not visible.

Is there a step I'm missing here?
The following code works fine for me here.
If you still have problems with it, please don't hesitate to let us know.

Code: Select all

uses TeeSeriesStats, CurvFitt, Series;

procedure TForm1.FormCreate(Sender: TObject);
var tmp: TCurveFittingFunction;
begin
  Chart1.View3D:=false;

  Chart1.AddSeries(TPointSeries).FillSampleValues;
  Chart1[0].Title:='Source';

  with Chart1.Tools.Add(TSeriesStatsTool) as TSeriesStatsTool do
  begin
    Series:=Chart1[0];

    tmp:=TCurveFittingFunction.Create(Self);
    tmp.ParentSeries:=Chart1.AddSeries(TFastLineSeries);
    tmp.ParentSeries.DataSource:=Chart1[0];
    tmp.ParentSeries.FunctionType:=tmp;
    tmp.ParentSeries.Title:='Curve Fitting';
  end;
end;
Metman wrote:I am using v8.06 and have searched the user base and found mention of tutorial 7 on this subject - which I can't find - in fact the .hlp file is not readable using Windows 7. Is there any chance there is a .pdf of the help?
Regarding the hlp files:
http://www.teechart.net/support/viewtop ... s+7#p49181

Re: Curve Fitting...

Posted: Mon Jan 23, 2012 11:37 am
by 10047857
Hi Yeray

Thanks for the quick reply - yes that code snippet works - but although I have been programming for years there is more than a little magic going on in it even though its not many lines long!

It must be possible to do this through the tool dialog without resorting to doing it dynamically?

I notice when I look at previous replies to this problem they always supply users with a dynamic solution - that's why I would just appreciate it if you could maybe talk me through doing it this way?

By the way I did download the Win help and I can now read the TeeChart help. The trouble with that it's fairly basic and as far as I can see does it talk you through adding the stats tool through the IDE - has anyone ever thought of producing an Ebook for TeeChart.

Bruce.

Re: Curve Fitting...

Posted: Mon Jan 23, 2012 2:43 pm
by yeray
Hi Bruce,
Metman wrote:Thanks for the quick reply - yes that code snippet works - but although I have been programming for years there is more than a little magic going on in it even though its not many lines long!

It must be possible to do this through the tool dialog without resorting to doing it dynamically?

I notice when I look at previous replies to this problem they always supply users with a dynamic solution - that's why I would just appreciate it if you could maybe talk me through doing it this way?
We usually try to share and comment the examples in code because it's easier to miss something when commenting a list of steps to follow at designtime, etc. A code snipped or a complete example is the best way we've found to ensure both sides are talking about the same.
I'm not sure to understand what is a "dynamic solution".
I can see the Curve fitting both using the code above in a new application with just a chart on the form, and also adding a Point series with some random values, adding the Statistics tool, setting the tool series to be the point series and checking the "Curve fitting" checkbox. The only difference I see is that doing it at design time the function is drawn in a TPointSeries while I used a TFasTLineSeries by code.
I'm using v8.08 in D2010 here.
Metman wrote:By the way I did download the Win help and I can now read the TeeChart help. The trouble with that it's fairly basic and as far as I can see does it talk you through adding the stats tool through the IDE - has anyone ever thought of producing an Ebook for TeeChart.
I've just emailed you the printable version of the manual.

Re: Curve Fitting...

Posted: Mon Jan 23, 2012 4:11 pm
by 10047857
Hi Yeray

What I suppose I mean by 'dynamic solution' is I want to add the series and the curve fitting tool at design time - your solution is dynamic because the curve fitting class is instantiated at run time when the form is created.

I can add a curve fitting tool to my graph at design time and point it at a series but I can't make the curve fitting series display!

Your code snippet works fine but I want to be able to do it at design time and not have to dynamically add any code.

This is the very simple procedure I use to plot the scattered point data - the point series displays fine but no curve is there a method I have to call to refresh the curve fitting and redraw the fast line?

Code: Select all

  procedure PlotGraph(grid: TAdvStringGrid);
  var
    r : integer;
    t : double;
  begin
    Series1.Clear;

    with grid do
    begin
      for r:=2 to RowCount-1 do
      begin
        t:=StrToFloatDef(Cells[2,r],missing);

        if t<>missing then
        begin
          Series1.AddXY(Ints[1,r],t);
        end;
      end;
    end;
  end;

Thanks for the RTF even though it is a little out of data.

Bruce.

Re: Curve Fitting...

Posted: Tue Jan 24, 2012 12:25 pm
by yeray
Hi Bruce,

That's what I do in Delphi 2010 + TeeChart Pro v8.08.
At design time:
- Create a new Delphi project
- Add a Chart on the form.
- Set the Chart to 2D.
- Add a Point series.
- Set the point series values to "manual" (Series\Data Source).
- Add an Statistics tool and assign Series1 to it.
- Select "Curve fitting" checkbox in the statistics tool.

With this, when I run the application, I see exactly the same I see at designtime:
Design.png
Design.png (20.25 KiB) Viewed 15818 times
Now, if you add some more values to Series1, for example adding a button that does this:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  for i:=0 to 9 do
    Series1.Add(Series1.YValues.MinValue + Random(Round(Series1.YValues.Range)));
end;
When you run this button Series1 has new values, but the function(s) in the Statistics tool don't know that so you have to refresh their source.
In the case above, we know Series1 (the source) has the index 0 and the Series2 (the series linked to the Curve fitting function) has the index 1. Then you should call the following at the end of Button1Click:

Code: Select all

  Chart1[1].CheckDataSource;
Does it solve your doubts?

Re: Curve Fitting...

Posted: Wed Jan 25, 2012 8:45 am
by 10047857
Hi Yeray

Sorry for not replying yesterday. Thanks for your steps to add a curve they work fine but it still does not fix my problem although I can now plot a curve!
The embedded image (1) shows you the problem - I plot a scattered temperature series for the years between 1878 and 2012 from a list of randomly sorted values like this:
1878 12.5
1969 11.7
1990 11.0
1898 11.4
1918 11.4
1884 11.4
1899 10.1
1901 10.1
1976 10.9
1902 10.0
1937 11.7
1981 10.4
etc etc...
Because the chart auto-scales the real values are huddled up on the right whilst the best fit curve starts at 0 and plots 25 values on the extreme left with Y values that don't bear any relation to those of the temperature values which are always around 10-13.
I then decided to save the data and load it manually as space delimited text (file attached) and got the results you see in the second screen shot (2).

Should the series be sorted for the curve fitting to work and why are the results so high?

Bruce.
screen shot1.png
screen shot1.png (10.04 KiB) Viewed 15826 times
screen shot2.png
screen shot2.png (12.06 KiB) Viewed 15831 times

Re: Curve Fitting...

Posted: Wed Jan 25, 2012 9:27 am
by yeray
Hi Bruce,

I'm trying to reproduce the problem with an as simple as possible example, but it seems to work fine for me here. Could you please try it?

Code: Select all

uses CurvFitt, Series;

procedure TForm1.FormCreate(Sender: TObject);
var curveFitFunct: TCurveFittingFunction;
    curveFitSEries: TFastLineSeries;
begin
  Chart1.View3D:=false;

  with Chart1.AddSeries(TPointSeries) do
  begin
    Title:='Source';
    AddXY(1878, 12.5);
    AddXY(1969, 11.7);
    AddXY(1990, 11.0);
    AddXY(1898, 11.4);
    AddXY(1918, 11.4);
    AddXY(1884, 11.4);
    AddXY(1899, 10.1);
    AddXY(1901, 10.1);
    AddXY(1976, 10.9);
    AddXY(1902, 10.0);
    AddXY(1937, 11.7);
    AddXY(1981, 10.4);
  end;

  curveFitFunct:=TCurveFittingFunction.Create(Self);
  curveFitSeries:=Chart1.AddSeries(TFastLineSeries) as TFastLineSeries;
  curveFitSeries.FunctionType:=curveFitFunct;
  with curveFitSeries do
  begin
    DataSource:=Chart1[0];
    Title:='Curve Fitting';
  end;
end;
CurveFit.png
CurveFit.png (11.26 KiB) Viewed 15822 times
If it gives you the same result than me, maybe you can find the relevant difference between this example and your application.
I you don't find it, please, try to arrange a simple example project we can run as-is to reproduce the problem here.
Thanks in advance.

Re: Curve Fitting...

Posted: Wed Jan 25, 2012 11:51 am
by 10047857
Hi Yeray

Your simple project works OK but I think I now know why the curve never appears - it simply can't fit it to the data - its too scattered.

See what you think of this project that I've attached - this has all the data points and not just a few - and the curve still does not get plotted.

You may well be able to get something to plot if you play around with the parameters...

Bruce.

Re: Curve Fitting...

Posted: Wed Jan 25, 2012 4:16 pm
by yeray
Hi,

Doing some very little changes it seem to work fine for me here
test.zip
(2.2 KiB) Downloaded 789 times
CurveFitting.png
CurveFitting.png (18.8 KiB) Viewed 15810 times

Re: Curve Fitting...

Posted: Wed Jan 25, 2012 4:38 pm
by 10047857
Hi Yeray

So all it all seemed to be down to the fact that I didn't force the curve to redraw by not calling the

Code: Select all

Chart1[1].CheckDataSource 
method - so simple - thanks for your help :D

Bruce.