delphibar-chartmouseeventteechart

Show hint on each bar of each series TChart -Delphi


In my project I've got several TCharts with 2 series and 2 bars in each series. [enter image description here]1

On each of them I need to show the value of the series bars on mouse hoovering over them. I'm using onMouseMove event:

    procedure TfrmBoard.chartCountMouseMove(Sender: TObject; Shift:
        TShiftState; X, Y: Integer);
    var
      SeriesIndex  : Integer;
    begin
      with Tcontrol(Sender) do
      begin
        seriesIndex := (Sender as TChart).series[0].clicked(X,Y);
        ShowHint := SeriesIndex <> -1;
    
        if ShowHint  then
           Hint := FloatToStr((Sender as TChart).series[0].YValues.Value[seriesIndex]);
    
      end;

end;

This code shows the values of the first series in the chart, but not of the second.

If I add a loop, the event will show values of the second series only.

procedure TfrmBoard.chartCountMouseMove(Sender: TObject; Shift:
    TShiftState; X, Y: Integer);
var
  SeriesIndex  : Integer;
  i: Integer;
begin
  with Tcontrol(Sender) do
  begin
    for I := 0 to (Sender as TChart).SeriesCount - 1 do
    begin

    seriesIndex := (Sender as TChart).series[i].clicked(X,Y);
    ShowHint := SeriesIndex <> -1;

    if ShowHint  then
       Hint := FloatToStr((Sender as TChart).series[i].YValues.Value[seriesIndex]);
    end;

  end;

end; 

Can somebody help how to show values for both series?


Solution

  • You have to break loop when ShowHint becomes True, otherwise it might be overwritten with next False values

    if ShowHint  then begin
       Hint := FloatToStr((Sender as TChart).series[i].YValues.Value[seriesIndex]);
       Break;
    end;
    

    By the way, does your TChart have event OnMouseEnter for SeriesXX objects?