Page 1 of 1

Legend onmouseover

Posted: Wed Mar 20, 2013 9:47 pm
by 16564695
I have a Tchart with about a dozen Tlineseries standard x,y graph, legend has item for eac series with checkbox and series title. Is there a mouseover event on Legend so that if mouse is over the legend item associated with a series I can alter the colour of that series so it stands out from others
thanks
Sean

Re: Legend onmouseover

Posted: Thu Mar 21, 2013 4:10 pm
by yeray
Hi Sean,

You can use the OnMouseMove event for it. You just have to calculate the rectangles of the according items in the legend and check if the mouse is over them.
Here is how you could do it:

Code: Select all

uses Series, TeCanvas;

var seriesIndex: Integer;

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

  for i:=0 to 11 do
  begin
    with Chart1.AddSeries(TFastLineSeries) as TFastLineSeries do
    begin
      FillSampleValues;
    end;
  end;

  seriesIndex:=-1;
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var i: Integer;
    tmpR: TRect;
begin
  if PointInRect(Chart1.Legend.RectLegend, X, Y) then
  begin
    for i:=0 to Chart1.Legend.Items.Count-1 do
    begin
      tmpR:=Rect(Chart1.Legend.Left, Chart1.Legend.Items[i].Top,
                 Chart1.Legend.Left + Chart1.Legend.Width,
                 Chart1.Legend.Items[i].Top + Chart1.Canvas.textHeight('W'));

      if PointInRect(tmpR, X, Y) then
      begin
        if (seriesIndex<>-1) and (seriesIndex<>i) then
          Chart1[seriesIndex].Pen.Width:=1;

        seriesIndex:=i;
        Chart1[seriesIndex].Pen.Width:=2;
        break;
      end;
    end;
  end
  else
  begin
    if (seriesIndex>-1) then
      Chart1[seriesIndex].Pen.Width:=1;
    seriesIndex:=-1;
  end;
end;