Display mark percent relative to other series total

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
WarmBooter
Newbie
Newbie
Posts: 33
Joined: Thu Mar 18, 2004 5:00 am
Location: Brasil
Contact:

Display mark percent relative to other series total

Post by WarmBooter » Thu Jul 16, 2015 5:30 pm

I have a chart with 2 series, the first representing the total of sells, and the second representing the total of returns, grouped by product. In the second serie, I would like to show in the marks the percent of the returns based on the total of sells, ie:

Product group A
Total sells = 120
Returns = 10
The mark should display 8.33% (since 10 returns represents 8.33% of 100 sells).

Is that possible?

Carlos

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

Re: Display mark percent relative to other series total

Post by Yeray » Fri Jul 17, 2015 9:10 am

Hello Carlos,

You can calculate the value of the label to show in second series as you add values on it. Ie:

Code: Select all

uses Series;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.AddSeries(TBarSeries).Marks.Visible:=false;;
  Chart1.AddSeries(TBarSeries);

  Chart1[0].Add(120);
  Chart1[1].Add(10, FormatFloat('#0.##', 10*100/Chart1[0].YValue[0]) + '%');

  //By default the axes LabelStyle is set to talAuto and it will show the labels if any series has.
  Chart1.Axes.Bottom.LabelStyle:=talValue;
end;
You can also calculate the values for the labels later. Ie:

Code: Select all

uses Series, math;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.AddSeries(TBarSeries).Marks.Visible:=false;;
  Chart1.AddSeries(TBarSeries);

  Chart1[0].Add(120);
  Chart1[1].Add(10);

  for i:=0 to min(Chart1[0].Count-1, Chart1[1].Count-1) do
    Chart1[1].Labels[i]:=FormatFloat('#0.##', Chart1[1].YValue[i]*100/Chart1[0].YValue[i]) + '%';

  Chart1.Axes.Bottom.LabelStyle:=talValue;
end;
Finally, you can also use the second series' OnGetMarkText event to calculate the labels there:

Code: Select all

uses Series, math;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  Chart1.AddSeries(TBarSeries).Marks.Visible:=false;;
  Chart1.AddSeries(TBarSeries);

  Chart1[0].Add(120);
  Chart1[1].Add(10);

  Chart1[1].OnGetMarkText:=Series2GetMarkText;

  Chart1.Axes.Bottom.LabelStyle:=talValue;
end;

procedure TForm1.Series2GetMarkText(Sender:TChartSeries; ValueIndex:Integer; var MarkText:String);
begin
  if (Chart1.SeriesCount>1) and (ValueIndex>-1) and (ValueIndex<min(Chart1[0].Count, Chart1[1].Count)) then
    MarkText:=FormatFloat('#0.##', Chart1[1].YValue[ValueIndex]*100/Chart1[0].YValue[ValueIndex]) + '%';
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