Page 1 of 1

Retrieving (x) value index of mouse position.

Posted: Fri Mar 08, 2013 12:03 pm
by 16559682
I have a line series where the bottom (x) axis is defined as DateTime. Within the Chart.MouseMove event I would like to get the index of the line series. The MouseMove gives me x/y coordinates but I see no method to transform the these into the respective index.

Something like a (fictive) Chart.BottomAxis.CalcXValueIndex(X) method.

Any idea?

Re: Retrieving (x) value index of mouse position.

Posted: Fri Mar 08, 2013 5:17 pm
by yeray
Hi,

The Axes have the CalcPosValue and CalcPosPoint functions.
- CalcPosValue takes a Double (value in the axis) and returns an Integer (pixel position).
- CalcPosPoint takes an Integer (pixel position) and returns a Double (value in the axis).

Re: Retrieving (x) value index of mouse position.

Posted: Sat Mar 09, 2013 8:04 am
by 16559682
CalcPosPoint returns the DateTime value of the x axis. I am looking for the ItemIndex within the series in order to access the underlying data object.

e.g. Series values
ItemIndex x-Value y-value
0 01.01.2013 35.00
1 02.01.2013 38.00
2 03.01.2013 37.50
3 04.01.2013 39.00
4 05.01.2013 38.80

Re: Retrieving (x) value index of mouse position.

Posted: Mon Mar 11, 2013 11:27 am
by yeray
Hi Christian,

Given an X pixel, a series may have or may not have a value drawn in that x-pixel. You could loop your series values to see if any of its values are drawn in a given X pixel. Ie:

Code: Select all

  for i:=0 to Chart1[0].Count-1 do
    if (Chart1[0].CalcXPos(i) = X) then
      valueIndex:=i;
You may also want to find the nearest point given an x pixel position. A complete example of how to do something similar to this could be found in the features demo project, concretely in the "All features\Welcome !\Chart styles\Standard\Line(Strip)\Interpolating Line series"

Re: Retrieving (x) value index of mouse position.

Posted: Mon Mar 11, 2013 2:17 pm
by 16559682
Thank you. With that info I could write a helper function for that task. If someone has a similar problem here it is...

Code: Select all

function GetXValueIndex(ASeries: TCustomLineSeries; X: Integer): Integer;
var
  i: Integer;
  Value: Double;
begin
  Value := ASeries.ParentChart.BottomAxis.CalcPosPoint(X);
  for i := ASeries.FirstValueIndex to ASeries.LastValueIndex do begin
    if ASeries.XValue[i] > Value then begin
      Result := i;
      Exit;
    end;
  end;

  // Nothing found so return the last index
  Result := ASeries.LastValueIndex;
end;