Retrieving (x) value index of mouse position.

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
christianh
Newbie
Newbie
Posts: 6
Joined: Mon Jun 27, 2011 12:00 am

Retrieving (x) value index of mouse position.

Post by christianh » Fri Mar 08, 2013 12:03 pm

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?

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

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

Post by Yeray » Fri Mar 08, 2013 5:17 pm

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).
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

christianh
Newbie
Newbie
Posts: 6
Joined: Mon Jun 27, 2011 12:00 am

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

Post by christianh » Sat Mar 09, 2013 8:04 am

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

Yeray
Site Admin
Site Admin
Posts: 9612
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

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

Post by Yeray » Mon Mar 11, 2013 11:27 am

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"
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

christianh
Newbie
Newbie
Posts: 6
Joined: Mon Jun 27, 2011 12:00 am

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

Post by christianh » Mon Mar 11, 2013 2:17 pm

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;

Post Reply