Page 1 of 1
Auto Other Slice
Posted: Mon Apr 03, 2017 3:39 pm
by 16880166
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.
Re: Auto Other Slice
Posted: Tue Apr 04, 2017 12:04 pm
by yeray
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;