Page 1 of 1

OnHover event

Posted: Mon Dec 11, 2017 7:57 pm
by 16880166
I know that the chart and the legend react when the mouse hovers a slice. Right now, I let the user 'right-click' on the chart to display a slices value in a callout panel hint. I want this to happen automatically as they hover over the legend or slice. I have the STD license. Is there some way I can hook into that event?

Re: OnHover event

Posted: Tue Dec 12, 2017 9:18 am
by yeray
Hello,

You can use the Chart's OnMouseMove event. Ie:

Code: Select all

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Single);
var ValueIndex: Integer;
begin
  ValueIndex:=Chart1.Legend.Clicked(Round(X),Round(Y));

  if ValueIndex=-1 then
  begin
    ValueIndex:=Series1.Clicked(X, Y);
  end;

  Caption:=IntToStr(ValueIndex);
end;

Re: OnHover event

Posted: Tue Dec 12, 2017 4:21 pm
by 16880166
Thanks. I will give it a shot. It there a % property for pie slices. I would like to popup the hint showing the total and the % of the pie. I would like to somehow wrap it in the code above.

Re: OnHover event

Posted: Wed Dec 13, 2017 8:32 am
by yeray
Hello,

Find here a simple example you can take as starting point:

Code: Select all

uses Series;

var myValueIndex: Integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=False;
  Chart1.AddSeries(TPieSeries).FillSampleValues;
end;

procedure TForm1.Chart1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Single);
begin
  myValueIndex:=Chart1.Legend.Clicked(Round(X),Round(Y));

  if myValueIndex=-1 then
  begin
    myValueIndex:=Chart1[0].Clicked(X, Y);
  end;

  if myValueIndex>-1 then
     Chart1.Repaint;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var tmpX, tmpY, tmpW, tmpH: Single;
    tmpP: Double;
    tmpS: string;
begin
  if myValueIndex>-1 then
  begin
    tmpX:=Chart1.GetCursorPos.X;
    tmpY:=Chart1.GetCursorPos.Y;
    tmpP:=Chart1[0].YValue[myValueIndex]*100/Chart1[0].YValues.Total;
    tmpS:='ValueIndex: ' + IntToStr(myValueIndex) + sLineBreak +
          'Value: ' + FormatFloat(Chart1[0].ValueFormat, Chart1[0].YValue[myValueIndex]) + sLineBreak +
          'Percent: ' + FormatFloat('#0.## %', tmpP);

    with Chart1.Canvas do
    begin
      tmpW:=TextWidth(tmpS);
      tmpH:=TextHeight(tmpS);
      Rectangle(RectF(tmpX, tmpY, tmpx+tmpW, tmpY+tmpH), 0);
      TextOut(tmpX, tmpY, tmpS);
    end;
  end;
end;

Re: OnHover event

Posted: Wed Dec 13, 2017 4:05 pm
by 16880166
Thank you.