Page 1 of 1

Getting Y value from X Value?

Posted: Mon Apr 09, 2018 6:07 pm
by 16582483
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?

Re: Getting Y value from X Value?

Posted: Wed Apr 11, 2018 8:59 am
by yeray
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.

Re: Getting Y value from X Value?

Posted: Wed Apr 11, 2018 2:34 pm
by 16582483
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

Re: Getting Y value from X Value?

Posted: Thu Apr 12, 2018 6:31 am
by yeray
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).

Re: Getting Y value from X Value?

Posted: Thu Apr 12, 2018 2:05 pm
by 16582483
I spent several hours trying to get that done yesterday.

Can I just get an answer to my question?

Re: Getting Y value from X Value?

Posted: Fri Apr 13, 2018 2:02 pm
by yeray
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;