Page 1 of 1

TAnnotationTool Update Problem

Posted: Mon Dec 10, 2012 12:55 pm
by 16562319
hi, I have a chart with some TannotationTools on it. When i draw the chart the first time the annotations are in the correct position, when i zoom the chart the annotation are "one step back". I use the chart onafterdraw event to update the annotation position.
In the attached image you can see an example of that i obtain.

Another problem is that in the onafterdraw event i set the label for the annotation but i don't have access (the size is 1) to the size of the annotation next in the event.

i.e.:
procedure TFMain.DBChart1AfterDraw(Sender: TObject);
begin
AnnotationTool.Text := 'Some text';
AnnotationTool.Visible := True;
// here AnnotationTool.Width is 1
[...]
end;

Any idea to solve the problem?

Re: TAnnotationTool Update Problem

Posted: Mon Dec 10, 2012 3:28 pm
by yeray
Hi,

Note the OnAfterDraw event is executed once the chart has been drawn, so changing the position of any if the elements won't take effect until the next repaint.
You can change the position of the element on that event, and force a chart repaint, but this has the risk to create an endless loop.
In the following example, I only repaint the chart if the position of the Annotation is being altered.

Code: Select all

uses Series, TeeTools;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart1.View3D:=false;
  Chart1.Legend.Visible:=false;

  Chart1.AddSeries(TAreaSeries).FillSampleValues;
  Chart1.Tools.Add(TAnnotationTool);
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
var XPos, YPos: Integer;
begin
  with Chart1[0] do
  begin
    XPos:=CalcXPos(0) + (CalcXPos(Count-1)-CalcXPos(0)) div 2;
    YPos:=CalcYPosValue(YValues.MaxValue) + (CalcYPosValue(YValues.MinValue)-CalcYPosValue(YValues.MaxValue)) div 2;
  end;

  with Chart1.Tools[0] as TAnnotationTool do
  begin
    Chart1.Canvas.AssignFont(Shape.Font);
    Text:='Label in the Middle';
    XPos:=XPos-(Shape.Width div 2);
    YPos:=YPos-(Shape.Height div 2);
    if (Left<>XPos) or (Top<>YPos) then
    begin
      Left:=XPos;
      Top:=YPos;
      Chart1.Draw;
    end;
  end;
end;
I'm also using the TAnnotationTool Shape.Width and Shape.Height properties, and both seem to have correct values.