Position of center of pie when showing multiple pieseries

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
Michael Heinrich
Newbie
Newbie
Posts: 20
Joined: Thu Feb 04, 2010 12:00 am

Position of center of pie when showing multiple pieseries

Post by Michael Heinrich » Wed Aug 10, 2011 9:29 am

Hello,

I am trying to figure out how to calculate the center of a pie in a chart that holds multiple pie series. I would like to place an annotation at that position with that tells the user what pie he's looking at.
What I have started is the following code. I am not sure when the CircleXCenter actually changes as they are all (0,0) for all my series in the chart. I've looked at the drawing of the pie in the sourcecode but it uses the canvas for it's coordinates. How do I position an annotation at the pie's center?

Code: Select all

  lTool := TAnnotationTool( SomeChart.Tools.Add( TAnnotationTool ) );
  lTool.Text := SomePieSeries.Title;
  lTool.Left := SomePieSeries.CircleXCenter;
  lTool.Top := SomePieSeries.CircleYCenter;

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

Re: Position of center of pie when showing multiple pieseries

Post by Yeray » Wed Aug 10, 2011 12:23 pm

Hello Michael,

Maybe you are using CircleXCenter and CircleYCenter values before the Pies are drawn so that values are still incorrect. Try adding a Draw call to force a chart repaint.
The following example seems to work fine for me here:

Code: Select all

//...
  private
    { Private declarations }
    procedure Pie1BeforeDrawValues(Sender: TObject);
    procedure Pie2BeforeDrawValues(Sender: TObject);
    procedure Pie3BeforeDrawValues(Sender: TObject);
    procedure Pie4BeforeDrawValues(Sender: TObject);
//...
uses Series, TeeTools;

var margin: Integer;

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
  margin:=10;
  Chart1.Legend.Visible:=false;

  for i:=0 to 3 do
  begin
    Chart1.AddSeries(TPieSeries).FillSampleValues();
    with Chart1.Tools.Add(TAnnotationTool) as TAnnotationTool do
      Text:='Pie nº ' + IntToStr(i);
  end;

  Chart1[0].BeforeDrawValues:=Pie1BeforeDrawValues;
  Chart1[1].BeforeDrawValues:=Pie2BeforeDrawValues;
  Chart1[2].BeforeDrawValues:=Pie3BeforeDrawValues;
  Chart1[3].BeforeDrawValues:=Pie4BeforeDrawValues;

  Chart1.Draw;
end;

procedure TForm1.Pie1BeforeDrawValues(Sender: TObject);
begin
  Chart1.ChartRect:=Rect(margin, margin, Chart1.Width div 2, Chart1.Height div 2);
end;

procedure TForm1.Pie2BeforeDrawValues(Sender: TObject);
begin
  Chart1.ChartRect:=Rect(Chart1.Width div 2, margin, Chart1.Width - margin, Chart1.Height div 2);
end;

procedure TForm1.Pie3BeforeDrawValues(Sender: TObject);
begin
  Chart1.ChartRect:=Rect(margin, Chart1.Height div 2, Chart1.Width div 2, Chart1.Height - margin);
end;

procedure TForm1.Pie4BeforeDrawValues(Sender: TObject);
begin
  Chart1.ChartRect:=Rect(Chart1.Width div 2, Chart1.Height div 2, Chart1.Width - margin, Chart1.Height - margin);
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var i: Integer;
begin
  for i:=0 to Chart1.SeriesCount-1 do
    with (Chart1.Tools[i] as TAnnotationTool) do
    begin
      Left:=(Chart1[i] as TPieSeries).CircleXCenter - (Chart1.Canvas.TextWidth(Text) div 2);
      Top:=(Chart1[i] as TPieSeries).CircleYCenter - (Chart1.Canvas.TextHeight(Text) div 2);
    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

Michael Heinrich
Newbie
Newbie
Posts: 20
Joined: Thu Feb 04, 2010 12:00 am

Re: Position of center of pie when showing multiple pieseries

Post by Michael Heinrich » Wed Aug 10, 2011 12:48 pm

Hello Yeray,

Adding an explicit draw before positioning the textlabels did the trick. I then added your afterdraw event mechanism to place the labels which fixed the redrawing after resizing the program window.

Thanks,
Michael.

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

Re: Position of center of pie when showing multiple pieseries

Post by Yeray » Wed Aug 10, 2011 1:05 pm

Hello Michael,

I'm glad to hear it!
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