Page 1 of 1

Legend with check boxes--when are they clicked?

Posted: Thu Dec 31, 2009 6:56 pm
by 10546565
I have a legend with check boxes turned on--I need to know when one of the check boxes is clicked. How can I know? (I can use the OnCLick event for the chart, but is there anything more narrow?)

Re: Legend with check boxes--when are they clicked?

Posted: Mon Jan 04, 2010 11:34 am
by yeray
Hi Ed,

The most accurate I can think right now would be something as following:

Code: Select all

uses series, TeCanvas;

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

  for i:=0 to 4 do
  begin
    Chart1.AddSeries(TPointSeries.Create(self));
    Chart1[i].FillSampleValues();
  end;

  Chart1.Legend.CheckBoxes:=true;
end;

procedure TForm1.Chart1ClickLegend(Sender: TCustomChart;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var i: Integer;
    tmpPnt: TPoint;
    tmpRect: TRect;
begin
  tmpPnt.X:=X;
  tmpPnt.Y:=Y;

  for i:=0 to Chart1.SeriesCount-1 do
  begin
    tmpRect.Left:=Chart1.Legend.Left;
    tmpRect.Right:=Chart1.Legend.Left + Chart1.Legend.Width - 1;
    tmpRect.Bottom:=Chart1.Legend.Item[i].Top + Chart1.Legend.Item[i].SymbolRect.Bottom - Chart1.Legend.Item[i].SymbolRect.Top + 3;

    if (i = 0) then tmpRect.Top:=Chart1.Legend.Item[i].Top-3
    else tmpRect.Top:=Chart1.Legend.Item[i].Top-2;

    if PtInRect(tmpRect,tmpPnt) then
    begin
      showmessage('series ' + IntToStr(i+1));
      break;
    end;
  end;
end;