Page 1 of 1

Detect which series is being clicked when clicking legend

Posted: Fri Apr 01, 2005 7:58 am
by 9235196
when user click in the legend area. Suppose i have 5 series, how to detect which series is being clicked.

thank you

Posted: Fri Apr 01, 2005 8:46 am
by narcis
Hi Herman,

You can use the chart's OnClickLegend event. Using the Clicked method you will get the series index starting at 0 and with the index you will be able to address each sereis. This is a small example on how to use this:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i:=0 to Chart1.SeriesCount-1 do
    begin
      Chart1[i].FillSampleValues();
      Chart1[i].SeriesColor:=clRed;
    end;
end;

procedure TForm1.Chart1ClickLegend(Sender: TCustomChart;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  SeriesIndex: Integer;
begin
  SeriesIndex:=Chart1.Legend.Clicked(X,Y);

  Chart1[SeriesIndex].SeriesColor:=clBlue;

  Chart1.Title.Text.Clear;
  Chart1.Title.Text.Add(IntToStr(SeriesIndex));
end;