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?
Getting Y value from X Value?
Re: Getting Y value from X Value?
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.
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,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
-
- Newbie
- Posts: 28
- Joined: Tue Dec 05, 2017 12:00 am
Re: Getting Y value from X Value?
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
How do I get an example of this?
Thank you,
Ed Dressel
Re: Getting Y value from X Value?
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).
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,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
-
- Newbie
- Posts: 28
- Joined: Tue Dec 05, 2017 12:00 am
Re: Getting Y value from X Value?
I spent several hours trying to get that done yesterday.
Can I just get an answer to my question?
Can I just get an answer to my question?
Re: Getting Y value from X Value?
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:
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,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |