Getting Y value from X Value?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Ed Dressel
Newbie
Newbie
Posts: 28
Joined: Tue Dec 05, 2017 12:00 am

Getting Y value from X Value?

Post by Ed Dressel » Mon Apr 09, 2018 6:07 pm

I have a TCOlorLIneTool that I allow a user to drag along the X axis. I display an annotation tool with the line that I want to display the corresponding Y value for it.

The X value is a list of dates that have a corresponding integer value.

When the user drags the line, I want to update the annotation's text with the corresponding Y value.

How can I get the Y value for a corresponding X value?

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

Re: Getting Y value from X Value?

Post by Yeray » Wed Apr 11, 2018 8:59 am

Hello,

The example at All features\Welcome!\Chart Styles\Standard\Line (Strip)\Interpolating Line series in the features demo does this. The only difference is it shows the Y values at the title instead of an Annotation tool.
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

Ed Dressel
Newbie
Newbie
Posts: 28
Joined: Tue Dec 05, 2017 12:00 am

Re: Getting Y value from X Value?

Post by Ed Dressel » Wed Apr 11, 2018 2:34 pm

I don't have the demo installed, it does not come when installing the source code installs.

How do I get an example of this?

Thank you,

Ed Dressel

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

Re: Getting Y value from X Value?

Post by Yeray » Thu Apr 12, 2018 6:31 am

Hello,

We use to recommend to install the binary version before installing the source code version to get the demos and help only shipped with the former.
Alternativelly, you can download the Compiled Demos or see the code at GitHub (see the Interpolate example here).
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

Ed Dressel
Newbie
Newbie
Posts: 28
Joined: Tue Dec 05, 2017 12:00 am

Re: Getting Y value from X Value?

Post by Ed Dressel » Thu Apr 12, 2018 2:05 pm

I spent several hours trying to get that done yesterday.

Can I just get an answer to my question?

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

Re: Getting Y value from X Value?

Post by Yeray » Fri Apr 13, 2018 2:02 pm

Hello,

I'm sorry, I thought pointing you to a code example was the answer.
I've made a simple example project with just a TLineSeries with a TColorLineTool and I've copied the InterpolateLineSeries function from the mentioned example and it seems to work without problems for me here:

Code: Select all

var annotationTool: TAnnotationTool;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=False;
  Chart1.Legend.Hide;

  with Chart1.AddSeries(TLineSeries) do
  begin
    XValues.DateTime:=True;
    FillSampleValues;
  end;

  annotationTool:=Chart1.Tools.Add(TAnnotationTool) as TAnnotationTool;

  with Chart1.Tools.Add(TColorLineTool) as TColorLineTool do
  begin
    Axis:=Chart1.Axes.Bottom;
    Value:=Chart1[0].XValue[10];
    OnDragLine:=DragColorLine;
  end;
end;

procedure TForm1.DragColorLine(Sender:TColorLineTool);
var YValue: Double;
begin
  YValue:=InterpolateLineSeries(Chart1[0], Chart1[0].FirstDisplayedIndex, Chart1[0].LastValueIndex, Sender.Value);
  annotationTool.Text:=Chart1.Axes.Left.LabelValue(YValue);
  annotationTool.Left:=Chart1.Axes.Bottom.CalcPosValue(Sender.Value);
end;

function TForm1.InterpolateLineSeries(Series: TChartSeries;
  FirstIndex, LastIndex: Integer; XValue: Double): Double;
var
  Index: Integer;
  dx,dy: Double;
begin
  for Index:=FirstIndex to LastIndex do
    if Series.XValues.Value[Index]>XValue then break;

  //safeguard
  if (Index<1) then Index:=1
  else if (Index>=Series.Count) then Index:=Series.Count-1;

  // y=(y2-y1)/(x2-x1)*(x-x1)+y1
  dx:=Series.XValues.Value[Index] - Series.XValues.Value[Index-1];
  dy:=Series.YValues.Value[Index] - Series.YValues.Value[Index-1];

  if (dx<>0) then
    result:=dy*(XValue - Series.XValues.Value[Index-1])/dx + Series.YValues.Value[Index-1]
  else result:=0;
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