Page 1 of 1

Customizing PieSlice labels

Posted: Mon Oct 23, 2006 10:07 pm
by 9336044
For a PieChart, TChart provides the ability to show a label for each pie slice showing the value, the percent, the percent + total, etc. I have been asked to customize the labels for the slices, introducing two new options.

Specifically, I have been asked to allow a label in the format of LABEL VALUE PERCENT TOTAL. Currently, I can provide all of the desired info in a single label with the exception of the Value. Is there a not-too-difficult way to implement this? Any samples you can point me to, or sample code snippets would be greatly appreciated.

The second item I have been asked to do is allow users to define a mask for displaying data. Many times, the data in our charts is measured in millions or billions of units. What our users would like to have is the ability to tell TChart to show a value like 1,234,567 as 1.2M for 1.2 million. Similarly, they want to use K for thousands, so our example becomes 1,235K.

Of course, users being users, they aren't satisfied with a simple request. They also want to be able to specify a number of decimal places, rounding as appropriate. So the 1,234,567 could correctly be displayed as 1.23M, 1.235M, 1,234,567.000, or 1,234.6K. We convinced the users that any mask applied would have to be applied to all slices; that is, a single pie of 3 slices would not have one slice unmasked, one masked for thousands, and another masked for millions.

The important thing for the users, though, is the masked value MUST work with the various label styles TChart currently allows.

Is such a thing possible?

Thanks.

Rich

Posted: Tue Oct 24, 2006 9:17 am
by narcis
Hi Rich,

Yes, this is possible, you can customize the series marks in the OnGetMarkText event. On that event you can also "mask" the series values to be displayed in the marks. Here you have an example:

Code: Select all

procedure TForm8.FormCreate(Sender: TObject);
var i: Integer;
begin
  Randomize;

  for i := 0 to 10 do
    Series1.AddPie(random*10000, 'Point ' + IntToStr(i), clTeeColor);

  Series1.Marks.Style:=smsPercentTotal;
end;

function TForm8.MaskValue(Val: Double; Format: String): String;
begin
  if Format='0.0K' then
    result:=FloatToStrF(Val/1000, ffGeneral, 1, 1) + 'K'
  else
    result:=FloatToStr(Val);
end;

procedure TForm8.Series1GetMarkText(Sender: TChartSeries; ValueIndex: Integer;
  var MarkText: string);
begin
  MarkText:=Sender.Labels[ValueIndex] + ', ' +
            MaskValue(Sender.YValue[ValueIndex],'0.0K') + ', ' +
            MarkText;
end;