0

How can I get series index from cursor position when I click TChart?

Thank you.

Marjan Venema
  • 19,136
  • 6
  • 65
  • 79
Alex Tiger
  • 377
  • 3
  • 22
  • So, if I have a lot of Series as array I need to cycle all the series and print out the counter if I have >-1 value got from the Series[I].Clicked(x, Y) function? – Alex Tiger Dec 25 '13 at 11:27

2 Answers2

3

From your clicked event, you will get the X,Y mouse position.

var
  SeriesIndex: Integer;
begin
  SeriesIndex := Series1.Clicked(X,Y);
  if (SeriesIndex <> -1) then
  begin
    // Do something with SeriesIndex
  end;
  ...
end;

It is also possible to assign an OnClickSeries event to the chart.

procedure TForm1.Chart1ClickSeries(Sender: TCustomChart;
  Series: TChartSeries; ValueIndex: Integer; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
LU RD
  • 34,438
  • 5
  • 88
  • 296
2

The Series' Clicked(X,Y) function returns -1 if the series isn't under the (X,Y) position (in pixels). If the series is under the (X,Y) position (in pixels), it returns the index of the point under the series.

Here you have a simple example using the OnMouseMove event:

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.View3D:=false;

  for i:=0 to 2 do
    Chart1.AddSeries(TBarSeries).FillSampleValues(3);
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var seriesIndex, valueIndex: Integer;
begin
  Caption:='No series under the mouse';

  for seriesIndex:=Chart1.SeriesCount-1 downto 0 do
  begin
    valueIndex:=Chart1[seriesIndex].Clicked(X,Y);
    if valueIndex>-1 then
      Caption:='Series under the mouse. SeriesIndex: ' + IntToStr(seriesIndex) + ', ValueIndex: ' + IntToStr(valueIndex);
  end;
end;
Yeray
  • 5,009
  • 1
  • 13
  • 25
  • Could you re-post your answer [`here`](http://stackoverflow.com/q/29105928/960757), please ? It's a better place for it ;-) – TLama Mar 19 '15 at 10:01