Bar Chart self-stacked title vertical position

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Errol
Newbie
Newbie
Posts: 75
Joined: Thu Jul 11, 2013 12:00 am

Bar Chart self-stacked title vertical position

Post by Errol » Wed Dec 07, 2022 4:36 am

In my previous topic, I was asking about the correct horizontal placement of a title above a self-stacked bar chart. In this post, I would like to be able to set the vertical position of the title just above the top axis of the bar chart, and have it remain there on scroll or zoom operations. As shown in the attached images, the title scrolls off the top of the chart at present if I use CalcBarBounds, or disappear entirely if I zoom the chart.

Any suggestions much appreciated.

Best regards

Errol
Attachments
BarChart unscrolled.jpg
BarChart unscrolled.jpg (106.02 KiB) Viewed 5234 times
BarChart scrolled up.jpg
BarChart scrolled up.jpg (105.05 KiB) Viewed 5234 times
BarChart zoomed.jpg
BarChart zoomed.jpg (106.93 KiB) Viewed 5234 times

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

Re: Bar Chart self-stacked title vertical position

Post by Yeray » Fri Dec 09, 2022 6:17 pm

Hello,

Taking the code from the reply I just wrote there, I've modified it to also calculate the value for the TAnnotationTool.Top property to position them just above the ChartRect:
Project1_2022-12-09_19-17-13.png
Project1_2022-12-09_19-17-13.png (15.97 KiB) Viewed 5197 times

Code: Select all

uses Series, TeeTools;

const aWellName: array[0..2] of string = ('AT-602', 'AT-501', 'AT-401');

procedure TForm1.FormCreate(Sender: TObject);
var i, j: Integer;
begin
  Chart1.Title.Hide;
  Chart1.View3D:=False;
  Chart1.Color:=clWhite;
  Chart1.Gradient.Visible:=False;
  Chart1.Walls.Back.Color:=clWhite;
  Chart1.Walls.Back.Gradient.Visible:=False;
  Chart1.Legend.Hide;

  for i:=0 to 2 do
  begin
    with TBarSeries(Chart1.AddSeries(TBarSeries)) do
    begin
      MultiBar:=mbSelfStack;
      Marks.Hide;
      for j:=0 to 4 do
        Add(5+Random*5);
    end;

    with TAnnotationTool(Chart1.Tools.Add(TAnnotationTool)) do
    begin
      Shape.CustomPosition := true;
      Shape.Pen.Color := clWhite;
      Shape.Transparent := true;
      Shape.Font.Size := 10;
      Shape.Font.Style := Shape.Font.Style + [fsBold];
      Shape.Angle := 90;
      Text := aWellName[i];

      OnBeforeDraw:=AnnotationToolBeforeDraw;
    end;
  end;

  Chart1.MarginTop:=10;
  Chart1.Draw;
end;

procedure TForm1.AnnotationToolBeforeDraw(Sender: TObject);
var i: Integer;
begin
  for i:=0 to Chart1.Tools.Count-1 do
    if Sender = Chart1.Tools[i] then
    begin
      PlaceWellNames(i);
      Exit;
    end;
end;

procedure TForm1.PlaceWellNames(AIndex: Integer);
var
  LLeft, LWidth: integer;
  LAcross: integer;
  annot: TAnnotationTool;
  bar: TBarSeries;
begin
  if Chart1.Tools[AIndex] is TAnnotationTool then
  begin
    annot:=TAnnotationTool(Chart1.Tools[AIndex]);
    bar:=TBarSeries(Chart1[AIndex]);

    // centre of bar
    LLeft := bar.CalcXPos(0) + (bar.BarWidth div 2);
    LWidth := annot.Width;   // width of shape
    LAcross := LWidth div 2;   // half title width

    annot.PositionUnits := muPixels;
    annot.Left := LLeft - LAcross;  // this is correct for non-automatic scaling
    annot.Top := Chart1.ChartRect.Top - LAcross - 10;
  end;
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