TChart : The check/unchecked event not working in ExtraLegendTool

709 views Asked by At

I displayed an ExtraLegendTool with checkboxes but the checkbox event is not working. Here is the code to display the 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;

Please check the folowing image for more details :

enter image description here

As you can see in the image, I have 2 legends one of type 'Chart1.Legend.LegendStyle := lsSeriesGroups' and the other one is an ExtraLegend.

How can I NOT display all the blue bars for all the series groups when I uncheck the blue series in the Extralegend?

1

There are 1 answers

0
Yeray On BEST ANSWER

You can use the ExtraLegendTool Clicked() function at the chart OnClick event to get the item of the legend that has been clicked. Then, you can activate/deactivate any series you desire.
This simple example seems to work fine for me here:

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;