Specific Spline X value required at Y Maximum

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
swesty
Newbie
Newbie
Posts: 14
Joined: Tue Feb 17, 2009 12:00 am

Specific Spline X value required at Y Maximum

Post by swesty » Wed Feb 18, 2009 5:38 am

Hi Steema,

I have just purchased the latest VCL Pro version and have implemented a Spline function which works perfectly. My next requirement is to get a specific X value from the Y Maximum of the spline. Is this possible and if so how do I achieve this ?

Thanks,
Steve West

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Post by Yeray » Wed Feb 18, 2009 11:05 am

Hi Steve,

Here is how you could search for your function maximum:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var maxY: double;
maxYIndex, i: integer;
begin
  maxY := Series2.MaxYValue;

  for i:=0 to Series2.Count-1 do
  begin
    if (Series2.YValue[i] = maxY) then
    begin
      maxYIndex := i;
      break;
    end;
  end;

  caption := 'maxY: ' + floattostr(maxY) + '  maxYIndex: ' + inttostr(maxYIndex) + '  X: ' + floattostr(Series2.XValue[maxYIndex]);
end;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

swesty
Newbie
Newbie
Posts: 14
Joined: Tue Feb 17, 2009 12:00 am

Post by swesty » Wed Feb 18, 2009 11:46 am

Yeray,

Thanks for the reply. I should have included a little more information on how I use the spline function. I have tried the method shown in your reply but I have had no luck as the TSmoothingFunction gives me no data in my series. Here is what I have so far.......

unit testofspline ;

// procedure declarations here

var

SplineFunction : TSmoothingFunction ;
SplineSeries : Array[1..10] of TLineSeries ;
MDRPointSeries : Array[1..10] of TPointSeries ;
XatYPeak,YPeak : Real ;

procedure TTestofSplineForm.CalculateandDrawSpline(LabNoIndex) ;
var
i : integer ;
begin
MDRPointSeries[LabNoIndex] := TPointSeries.Create(Self) ;
SplineFunction := TSmoothingFunction.Create(self) ;
SplineFunction.Interpolate := true ;
SplineFunction.Factor := 8 ;

SplineSeries[LabNoIndex] := TLineSeries.Create(Self) ;
SplineSeries[LabNoIndex].SetFunction(Splinefunction) ;

MDRPointSeries.AddXY(5.5,1.85,'',clTeal) ;
MDRPointSeries.AddXY(6.5,1.90,'',clTeal) ;
MDRPointSeries.AddXY(7.5,1.95,'',clTeal) ;
MDRPointSeries.AddXY(8.5,1.91,'',clTeal) ;

SplineSeries[LabNoIndex].DataSource := MDRPointSeries[LabnoIndex] ;
SplineSeries[LabnoIndex].checkDataSource ;

YPeak := SplineSeries[LabNoIndex].MaxYValue ;
for i := 0 to SplineSeries[LabNoIndex].Count-1 do
begin
if SplineSeries[LabNoIndex].YValues = YPeak then
XatYPeak := SplineSeries[LabNoIndex].XValue ;
end;
end;


Using this code above I end up with nothing that is accessible as points in my SplineSeries which is odd because the line is drawn perfectly on the graph. What am I doing wrong ?

swesty
Newbie
Newbie
Posts: 14
Joined: Tue Feb 17, 2009 12:00 am

Post by swesty » Wed Feb 18, 2009 11:49 am

The lines shown as

MDRPointSeries.AddXY(5.5,1.85,'',clTeal) ;
MDRPointSeries.AddXY(6.5,1.90,'',clTeal) ;
MDRPointSeries.AddXY(7.5,1.95,'',clTeal) ;
MDRPointSeries.AddXY(8.5,1.91,'',clTeal) ;


should read

MDRPointSeries[LabNoIndex].AddXY(5.5,1.85,'',clTeal) ;
MDRPointSeries[LabNoIndex].AddXY(6.5,1.90,'',clTeal) ;
MDRPointSeries[LabNoIndex].AddXY(7.5,1.95,'',clTeal) ;
MDRPointSeries[LabNoIndex].AddXY(8.5,1.91,'',clTeal) ;

sorry about that was hashing code from my main project into a unit.

Regards,
Steve

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Feb 18, 2009 2:18 pm

Hi Steve,

Could you please send us a simple example project we can run "as-is" to reproduce the problem here ?

You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Thanks in advance.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

swesty
Newbie
Newbie
Posts: 14
Joined: Tue Feb 17, 2009 12:00 am

Post by swesty » Wed Feb 18, 2009 10:19 pm

Hi Narcís,

I have uploaded an app as requested. Very basic but does do what I have tried to explain in this post so far. The file I have uploaded is called TestofSplineExampleApp.zip

Cheers,
Steve

swesty
Newbie
Newbie
Posts: 14
Joined: Tue Feb 17, 2009 12:00 am

Post by swesty » Wed Feb 18, 2009 10:19 pm

I should mention I uploaded via the upload page.

Thanks,
Steve

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Post by Yeray » Thu Feb 19, 2009 11:38 am

Hi Steve,

Simply changing the order of some calls, your code works fine for me:

Code: Select all

procedure TForm1.CalculateandDrawSpline(LabNoIndex : integer) ;
var
i : integer ;
begin
  ebYPeak.Text := '' ;
  ebXatYPeak.Text := '' ;
  ebSeriesPointCount.Text := '' ;
  ebPointSeriesPointCount.Text := '' ;

  MDRPointSeries[LabNoIndex] := TPointSeries.Create(Self) ;
  SplineFunction := TSmoothingFunction.Create(self) ;

  SplineSeries[LabNoIndex] := TLineSeries.Create(Self) ;
  SplineSeries[LabNoIndex].SetFunction(Splinefunction) ;

  Chart1.AddSeries(MDRPointSeries[LabNoindex]) ;
  Chart1.AddSeries(SplineSeries[LabNoindex]) ;

  SplineFunction.Interpolate := true ;
  SplineFunction.Factor := 8 ;

  MDRPointSeries[LabNoIndex].AddXY(5.5,1.85,'',clTeal) ;
  MDRPointSeries[LabNoIndex].AddXY(6.5,1.90,'',clTeal) ;
  MDRPointSeries[LabNoIndex].AddXY(7.5,1.95,'',clTeal) ;
  MDRPointSeries[LabNoIndex].AddXY(8.5,1.91,'',clTeal) ;

  SplineSeries[LabNoIndex].DataSource := MDRPointSeries[LabnoIndex] ;
  SplineSeries[LabnoIndex].checkDataSource ;

  YPeak := SplineSeries[LabNoIndex].MaxYValue ;
  for i := 0 to SplineSeries[LabNoIndex].Count-1 do
  begin
    if SplineSeries[LabNoIndex].YValues[i] = YPeak then
    XatYPeak := SplineSeries[LabNoIndex].XValue[i] ;
  end;
  ebSeriesPointCount.Text := inttostr(SplineSeries[LabNoIndex].Count) ;
  ebYPeak.Text := FloattoStrF(YPeak,ffFixed,6,3) ;
  ebXatYPeak.Text := FloattoStrF(XatYPeak,ffFixed,4,1) ;
  ebPointSeriesPointCount.Text := inttostr(MDRPointSeries[LabNoIndex].Count) ;
end;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

swesty
Newbie
Newbie
Posts: 14
Joined: Tue Feb 17, 2009 12:00 am

Post by swesty » Thu Feb 19, 2009 11:53 am

Yeray,

Thanks for that. Perfect. Works fine now.
One thing though, I have used Delphi since Delphi 1 and your standard components as part of Delphi since I can't remember but I have never put my AddSeries before I have processed the points into the actual series. Is this standard practice ? I am sure I read examples over the years showing the AddSeries as almost the last thing to do. Doesn't matter a great deal but I am glad it works. I appreciate the speedy replies from the Steema team.

Kind Regards,
Steve

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Post by Yeray » Thu Feb 19, 2009 3:53 pm

Hi Steve,

Well, as you know, when you add a series in design time, the series is created and added to the chart immediately. Then, it's more logic doing one thing just after the other also at runtime but, anyway, I'm glad to see that those examples worked at you end. :)
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply