Page 1 of 1

Position of center of pie when showing multiple pieseries

Posted: Wed Aug 10, 2011 9:29 am
by 10555193
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;

Re: Position of center of pie when showing multiple pieseries

Posted: Wed Aug 10, 2011 12:23 pm
by yeray
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;

Re: Position of center of pie when showing multiple pieseries

Posted: Wed Aug 10, 2011 12:48 pm
by 10555193
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.

Re: Position of center of pie when showing multiple pieseries

Posted: Wed Aug 10, 2011 1:05 pm
by yeray
Hello Michael,

I'm glad to hear it!