Page 1 of 1

How calculate YValue from other series??

Posted: Mon Sep 12, 2005 3:26 pm
by 9237515
Hi,

D7 and Teechart Pro Vcl 7.04

exemple sample , you have too serie
serie1 with
point 1 = 10:00 15.5
point 2 = 11:00 23.3
point 3= 12:00 27.0

and Serie 2 with
point 1 = 08:30 42
point 2 = 15:00 24

how take for the retreive graphic value from series 2 at each point serie 1

serie 1 serie 2
point 1 = 10:00 15.5 ?
point 2 = 11:00 23.3 ?
point 3= 12:00 27.0 ?



if you have a example or the best practice ??

Posted: Mon Sep 12, 2005 3:34 pm
by narcis
Hi MD,

Assuming both series have the same number of points you can use the following code in the TChart's MouseMove event.

Code: Select all

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  ValueIndex: Integer;
begin
  ValueIndex:=Series1.Clicked(X,Y);

  if (ValueIndex <> -1) then
    Chart1.Title.Text[0]:=FloatToStr(Series2.XValue[ValueIndex]) + ', ' +
                          FloatToStr(Series2.YValue[ValueIndex]);
end;

Posted: Mon Sep 12, 2005 5:18 pm
by 9237515
hi

tank,

But not same number of point in each séries, this is difficult in my PB

I find the kind linear or extrapolation solution !!!!

Posted: Tue Sep 13, 2005 10:29 am
by narcis
Hi MD,

Then interpolating the series is necessary. You can do something like the code below. I'm also going to post the full project at [url]news://www.steema.net/steema.public.attachments[/url] newsgroup.

Code: Select all

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;

function TForm1.InterpolateLineSeries(Series: TChartSeries;XValue: Double): Double;
begin
  result:=InterpolateLineSeries(Series,Series.FirstDisplayedIndex,Series.LastValueIndex,XValue);
end;


procedure TForm1.ChartTool1Change(Sender: TCursorTool; x, y: Integer;
  const XValue, YValue: Double; Series: TChartSeries; ValueIndex: Integer);
var
  i: Integer;
begin
  With Chart1.Title.Text do
  begin
    Clear;
    for i:=0 to Chart1.SeriesCount - 1 do
        Add(Chart1[i].Name + ': Y('+FloatToStr(XValue)+')= ' +
            FloatToStr(InterpolateLineSeries(Chart1[i],XValue))+#13#10);
  end;
end;

Posted: Tue Sep 13, 2005 7:46 pm
by 9237515
Ok, i found this good example who function with
same number of point in each series.

But how take with not same number of point ???

Posted: Wed Sep 14, 2005 9:44 am
by narcis
Hi MD,

It works fine for me here using different number of points for each series, try populating your series doing something like:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i:=0 to Chart1.SeriesCount - 1 do Chart1[i].FillSampleValues(10+1*i);
end;
Then the code I posted will interpolate each series value according to the current TCursorTool position and will display the interpolation on TChart's title.

Posted: Wed Sep 14, 2005 5:23 pm
by 9237515
Ok tank

this functionne with other points, but the good formule
is // y=((y2-y1)*(x-x1)) / (x2-x1) +y1

not // y=(y2-y1)/(x2-x1)*(x-x1)+y1

bye

Posted: Thu Sep 15, 2005 7:12 am
by narcis
Hi MD,

this is what is actually calculated:

Code: Select all

result:=dy*(XValue - Series.XValues.Value[Index-1])/dx + Series.YValues.Value[Index-1]