Page 1 of 1

Display mark percent relative to other series total

Posted: Thu Jul 16, 2015 5:30 pm
by 9231501
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

Re: Display mark percent relative to other series total

Posted: Fri Jul 17, 2015 9:10 am
by yeray
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;