Page 1 of 1

Marks within Bars

Posted: Tue Apr 19, 2005 8:34 am
by 9340851
Situation: Chart with two series of kind TBarJoinSeries which are stacked 100%.

I want to display the value of each bar within the bar itself. Is that possible and how would I do this?

TeeChart is version 7.02 for Delphi 7.

Posted: Tue Apr 19, 2005 8:51 am
by narcis
Hi sm,

Yes, one easy way of doing that is setting marks arrowlength to a negative value like in the example below:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Chart1.AddSeries(TBarJoinSeries.Create(self));
  Chart1.AddSeries(TBarJoinSeries.Create(self));

  for i:=0 to Chart1.SeriesCount-1 do
    With Chart1[i] do
    begin
      FillSampleValues();
      Marks.Arrow.Visible:=false;
      Marks.ArrowLength:=-50;
    end;

  (Chart1[0] as TBarJoinSeries).MultiBar:=mbStacked100;
end;

Posted: Tue Apr 19, 2005 9:33 am
by 9340851
narcis wrote:Hi sm,

Yes, one easy way of doing that is setting marks arrowlength to a negative value like in the example below:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Chart1.AddSeries(TBarJoinSeries.Create(self));
  Chart1.AddSeries(TBarJoinSeries.Create(self));

  for i:=0 to Chart1.SeriesCount-1 do
    With Chart1[i] do
    begin
      FillSampleValues();
      Marks.Arrow.Visible:=false;
      Marks.ArrowLength:=-50;
    end;

  (Chart1[0] as TBarJoinSeries).MultiBar:=mbStacked100;
end;
Thanks! But I would still have to calculate the arrow length by myself if I I want to center the marks. Also a hardcoded value for ArrowLength would not work if the bars values can be very small (the mark would slip into another bar).

Posted: Tue Apr 19, 2005 11:01 am
by narcis
Hi sm,

Ok, you can play with series values positions in the chart, ChartRect settings, series marks positions, etc. Find an example below:

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Chart1.AddSeries(TBarJoinSeries.Create(self));
  Chart1.AddSeries(TBarJoinSeries.Create(self));

  (Chart1[0] as TBarJoinSeries).MultiBar:=mbStacked100;

  for i:=0 to Chart1.SeriesCount-1 do
    Chart1[i].FillSampleValues();

  Chart1.Draw;
  CustomSeriesMarks;
end;

procedure TForm1.CustomSeriesMarks;
var
  i,j: Integer;
  APosition:TSeriesMarkPosition;
begin

  APosition:=TSeriesMarkPosition.Create;
  try

    for j:=0 to Chart1.SeriesCount-1 do
      for i:=0 to Chart1[j].Count-1 do
      begin
        APosition.Custom:=True;
        APosition.LeftTop.X:=Chart1[j].Marks.Positions[i].LeftTop.X;

        if (j=0) then
          APosition.LeftTop.Y:=Chart1[j].CalcYPos(i) +
                            ((Chart1[j+1].CalcYPos(i) -
                              Chart1[j].CalcYPos(i)) div 2)
        else
          APosition.LeftTop.Y:=Chart1[j].CalcYPos(i) +
                            ((Chart1.ChartRect.Bottom -
                              Chart1[j].CalcYPos(i)) div 2);

        Chart1[j].Marks.Positions[i]:=APosition;
      end;

  finally
      APosition.Free;
  end;
end;