[Legend] Auto-repositionning when chart is resized ?

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
bertrod
Advanced
Posts: 151
Joined: Wed Sep 07, 2005 4:00 am

[Legend] Auto-repositionning when chart is resized ?

Post by bertrod » Mon Feb 20, 2006 10:42 am

Hello,

I would like my legend (which has a custom position) to be repositionned correctly when the chart is resized.

I found this topic (see it here) about the subject, but i couldn't make this code work with Delphi.

Can you give me some help with this ?

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Feb 20, 2006 10:45 am

Hi bertrod,

You can try implementing the OnAfterDraw code in the OnResize event or in the OnResize event call Chart1.Draw; to force the chart being drawn and that your changes are applied.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

bertrod
Advanced
Posts: 151
Joined: Wed Sep 07, 2005 4:00 am

Post by bertrod » Mon Feb 20, 2006 11:06 am

Hi Narcis,

My problem is to implement this method :

Code: Select all

Private Sub TChart1_OnAfterDraw() 
    TChart1.Legend.Left = TChart1.Axis.Right.Position - (TChart1.Legend.ShapeBounds.Right - TChart1.Legend.ShapeBounds.Left) 
    TChart1.Legend.Top = TChart1.Axis.Top.Position 
End Sub 
As there is no "Position" property, i tried to use "calcXPos" and "posAxis" instead, but the legend is definitely not moving correctly.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Feb 20, 2006 11:46 am

Hi bertrod,

This is why I suggested you to make a call to Chart1.Draw;. You can make it after initializing your chart and in the form OnResize event.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

bertrod
Advanced
Posts: 151
Joined: Wed Sep 07, 2005 4:00 am

Post by bertrod » Mon Feb 20, 2006 1:23 pm

Ok for the moment to call the method, but my problem is to calculate the new pixel coordinates of the legend :cry: Which function must I use ?, as the property 'Position' doesn't exist in the Delphi version.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Mon Feb 20, 2006 2:13 pm

Hi bertron,

The above code equivalent in Delphi is:

Code: Select all

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  Chart1.Legend.CustomPosition:=true;
  Chart1.Legend.Left:=Chart1.Axes.Right.PosAxis-(Chart1.Legend.ShapeBounds.Right-Chart1.Legend.ShapeBounds.Left);
  Chart1.Legend.Top:=Chart1.Axes.Top.PosAxis;
end;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

bertrod
Advanced
Posts: 151
Joined: Wed Sep 07, 2005 4:00 am

Post by bertrod » Mon Feb 20, 2006 2:55 pm

Thanks for the code, but it definitely doesn't work :cry:

So I decided to try another solution :
1. When I create or move the legend, I store the TopLeft position of the legend, using axis coordinates.

So, I know that my legend is fixed in the axis coordinate (X, Y) on the chart.

2. When the chart is resized, I recalculate the pixel coordinate from the axis coordinates (X, Y).

But.... as before, this doesn't work.

Here is my code :

In formCreate :

Code: Select all

  with chart1.legend do
    begin
      //default legend position
      CustomPosition := true ;
      shapeBounds.TopLeft.X := 80 ;
      shapeBounds.TopLeft.Y := 30 ;

      //legendX and Y are the axis coordinate of the legend
      legendX := chart1.TopAxis.CalcPosPoint(shapeBounds.TopLeft.X) ;
      legendY := chart1.LeftAxis.CalcPosPoint(shapeBounds.TopLeft.Y) ;
    end ;
In chart1.onResize

Code: Select all

chart1.legend.shapeBounds.TopLeft.X := chart1.TopAxis.CalcXPos(legendX) ;
chart1.legend.shapeBounds.TopLeft.Y := chart1.TopAxis.CalcYPos(legendY) ;
I guess I'm doing something wrong with the "Calc...." functions, but I must say the help file is not very clear about the differences between those function : CalcXPos, CalcPosPoint, etc.

bertrod
Advanced
Posts: 151
Joined: Wed Sep 07, 2005 4:00 am

Post by bertrod » Wed Feb 22, 2006 1:28 pm

I'm sorry to make a little "up", but I really tried everything and my legend doesn't want to move correctly after the chart is resized.

I tried to replaced it in the 'afterdraw', but I noticed that the afterdraw method is called BEFORE the chart is resized. So I can't move the legend in the afterdraw method.

I would need some code if possible to help me :oops:

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Fri Feb 24, 2006 5:06 pm

Hi,

you should be able to do this using the following code :

Code: Select all

with Series1 do
begin
  VertAxis:=aBothVertAxis;
  HorizAxis:=aBothHorizAxis;
end;

With Chart1 do
begin
    View3D := false;
    Legend.LegendStyle := lsSeries;
    Legend.CustomPosition := true;
    Axes.Right.Labels:=false;
    Axes.Top.Labels:=false;
    Axes.Right.Axis.Width:=1;
    Axes.Top.Axis.Width:=1;
    Draw;
end;
end;

procedure TForm1.Chart1AfterDraw(Sender: TObject);
begin
  Chart1.Legend.Left := Chart1.Axes.right.PosAxis- (Chart1.Legend.ShapeBounds.Right -
    Chart1.Legend.ShapeBounds.Left);
  Chart1.Legend.Top := Chart1.Axes.top.PosAxis;
end;

Post Reply