TChart:check/unchecked 事件在 ExtraLegendTool 中不起作用

TChart : The check/unchecked event not working in ExtraLegendTool

我显示了带有复选框的 ExtraLegendTool,但复选框事件不起作用。 这是显示 ExtraLegend 的代码:

procedure TFRChart.TCChartAfterDraw(Sender: TObject);
begin
    if CKDisplay.Checked then
    begin
        with ExtraLegend do
        begin
             Active:= True;
             Series := TBarSeries(Self.FindComponent('SeriesTotal'));
             with Legend do
             begin
                LegendStyle := lsAuto;
                CheckBoxes  := True;
                //MaxNumRows  := 3;
                CustomPosition :=  True;
                Left:= TCChart.Legend.Left;
                Top:=  TCChart.Legend.ShapeBounds.Bottom + 10;
                Width := TCChart.Legend.Width;
                ShapeBounds.Right := TCChart.Legend.ShapeBounds.Bottom;
                DrawLegend;
             end;
        end;
    end;
end;

请查看下图了解更多详情:

如图所示,我有 2 个图例,一个是 'Chart1.Legend.LegendStyle := lsSeriesGroups' 类型,另一个是 ExtraLegend。

当我取消选中 Extralegend 中的蓝色系列时,如何不显示所有系列组的所有蓝色条?

您可以在图表的OnClick 事件中使用ExtraLegendTool Clicked() 函数来获取被点击的图例项。然后,您可以activate/deactivate任何您想要的系列。
这个简单的例子似乎对我来说很好用:

procedure TForm1.Chart1Click(Sender: TObject);
var MousePos: TPoint;
    index: Integer;
begin
  MousePos:=Chart1.GetCursorPos;
  index:=ChartTool1.Legend.Clicked(MousePos);

  while (index>-1) and (index<Chart1.SeriesCount) do
  begin
    Chart1[index].Active:=not Chart1[index].Active;
    index:=index+3;
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.SeriesGroups.Add;
  Chart1.SeriesGroups.Add;
  Chart1.SeriesGroups[0].Name:='This is Group 1';
  Chart1.SeriesGroups[1].Name:='This is Group 2';

  for i:=0 to 9 do
    with Chart1.AddSeries(TBarSeries) as TBarSeries do
    begin
      FillSampleValues(5);
      if (i<3) then
      begin
        Chart1.SeriesGroups[0].Add(Chart1[i]);
        StackGroup:=0;
      end
      else
      begin
        Chart1.SeriesGroups[1].Add(Chart1[i]);
        StackGroup:=1;
      end;

      MultiBar:=mbStacked;
    end;

  Chart1.Legend.LegendStyle := lsSeriesGroups;

  Chart1.Draw;

  with ChartTool1 do
  begin
    Active:= True;
    //Series := TBarSeries(Self.FindComponent('SeriesTotal'));
    Series := Chart1[0];
    with Legend do
    begin
      LegendStyle := lsAuto;
      CheckBoxes  := True;
      MaxNumRows  := 3;
      CustomPosition :=  True;
      Left:= Chart1.Legend.Left;
      Top:= Chart1.Legend.ShapeBounds.Bottom + 10;
      Width := Chart1.Legend.Width;
      ShapeBounds.Right := Chart1.Legend.ShapeBounds.Bottom;
      DrawLegend;
    end;
  end;
end;