TeeChart获取系列Y值或给定X值的索引 [英] TeeChart Get Series Y value or index by given X value
问题描述
我遇到以下问题:我将Delphi XE3与TeeChart配合使用,我想通过给定的X值检索Y值或意甲的值索引。我的意向是一个在X轴上带有日期的时间序列。我知道图表上的日期,我想显示与此日期最接近的对应Y值。
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.
TChart或TChartSeries组件是否有任何方法或功能来实现这一目标?还是我需要遍历系列直到到达选定的日期?
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?
无法使用CursorPostion方法,因为光标可能在任何地方。
It is not possible to use the CursorPostion methods, because the cursor could be anywhere.
预先感谢您的帮助。
推荐答案
您可以使用<$ TChartValueList
的c $ c> Locate 方法来获取相应数据条目的索引。
You can use Locate
method of TChartValueList
to get index of appropriate data entry.
帮助示例:
tmp:=LineSeries1.XValues.Locate(EncodeDate(2007,1,1));
if tmp<>-1 then ...
编辑:此方法适用于
如果对X值进行了排序(默认模式),则可以在XValues中使用二进制搜索来快速找到最接近的值。
例如,您可以修改此代码返回最接近的值索引,而不是返回 -1
,或对两个相邻值使用线性插值(如果适用)。
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.
//assumes A.Order = loAscending (default)
function FindClosestIndex(const Value: Double; A: TChartValueList): Integer;
var
ahigh, j, alow: integer;
begin
// extra cases
if A.Count = 0 then
Exit(-1);
if Value <= A.First then
Exit(0);
if Value >= A.Last then
Exit(A.Count - 1);
// binary search
alow := 0;
ahigh := A.Count - 1;
while ahigh - alow > 1 do begin
j := (ahigh + alow) div 2;
if Value <= A[j] then
ahigh := j
else
alow := j;
end;
// choose the closest from ahigh, alow
Result := ahigh - Ord(A[ahigh] - Value >= Value - A[alow])
end;
这篇关于TeeChart获取系列Y值或给定X值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!