Auto Other Slice

TeeChart FireMonkey (Windows,OSX,iOS & Android) for Embarcadero RAD Studio, Delphi and C++ Builder (XE5+)
Post Reply
realsol
Newbie
Newbie
Posts: 70
Joined: Mon Feb 27, 2017 12:00 am

Auto Other Slice

Post by realsol » Mon Apr 03, 2017 3:39 pm

I don't care about displaying an other slice in my pie chart unless it isn't currently being displayed in the legend. Is there some way to have the Other Slice automatically create itself, but only if all categories don't fit in the legend? Thanks.

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Auto Other Slice

Post by Yeray » Tue Apr 04, 2017 12:04 pm

Hello,

You may have to polish it, but here it is a simple example trying to do it:

Code: Select all

uses Math;

type TPieSeriesAccess=class(TPieSeries);

procedure TForm1.Chart1BeforeDrawChart(Sender: TObject);

  function SlicesCount: Integer;
  var i, nOther: Integer;
  begin
    nOther:=0;

    if Series1.OtherSlice.Style = poBelowValue then
      for i:=0 to Series1.Count-1 do
        if Series1.YValue[i]<=Series1.OtherSlice.Value then
           Inc(nOther);

    result:=Series1.Count-nOther;

    if nOther>0 then Inc(result);
  end;

  procedure RecalcOtherSlices;
  var i: Integer;
      tmp: Double;
  begin
    if Series1.OtherSlice.Style = poNone then
    begin
      Series1.OtherSlice.Style:=poBelowValue;
      Series1.OtherSlice.Value:=Series1.YValues.MinValue;
    end;

    tmp:=Series1.YValues.MaxValue;
    for i:=0 to Series1.Count-2 do
      if Series1.YValue[i] > Series1.OtherSlice.Value then
         tmp:=Min(tmp, Series1.YValue[i]);

    Series1.OtherSlice.Value:=tmp;

    TPieSeriesAccess(Series1).DoBeforeDrawChart;
  end;

begin
  if (Chart1.Legend.Items.Count > 0) then
  begin
    if Series1.OtherSlice.Style<>poNone then
       Series1.Delete(Series1.Count-1);

    Series1.OtherSlice.Style:=poNone;

    Chart1.Canvas.AssignFont(Chart1.Legend.Font);
    while Chart1.Legend.Top+(SlicesCount*(Chart1.Canvas.FontHeight) + 7)+10 > Chart1.Height do
      RecalcOtherSlices;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Series1.FillSampleValues(10);
end;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply