I have the following problem: I am using Delphi XE3 with TeeChart and I'd like to retrieve a Y value or the value index of a serie by a given X value. My serie is a time series with dates on the X axis. I know the date on the chart and I want to display the nearest corresponding Y value to this date.
Is there any method or function of the TChart or TChartSeries component to achieve this? Or do I need to iterate through the series until I reached the selected date?
It is not possible to use the CursorPostion methods, because the cursor could be anywhere.
Thanks in advance for your help.
A solution is an interpolation algorithm as shown at the All Features\Welcome!\Chart styles\Standard\Line(Strip)\Interpolating Lines example in the features demo. Here's the complete code for the example:
You can use
Locate
method ofTChartValueList
to get index of appropriate data entry.Example from help:
Edit: This method works for exact coincidence.
If you X-values are sorted (default mode), the you can use binary search in XValues to find the closest value quickly. For example, you might modify this code to return the closest value index instead of
-1
, or use linear interpolation (if applicable) for two neighbor values.