[Legend] Auto-repositionning when chart is resized ?
[Legend] Auto-repositionning when chart is resized ?
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 ?
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 ?
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
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.
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 |
Instructions - How to post in this forum |
Hi Narcis,
My problem is to implement this method :
As there is no "Position" property, i tried to use "calcXPos" and "posAxis" instead, but the legend is definitely not moving correctly.
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
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
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.
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 |
Instructions - How to post in this forum |
-
- Site Admin
- Posts: 14730
- Joined: Mon Jun 09, 2003 4:00 am
- Location: Banyoles, Catalonia
- Contact:
Hi bertron,
The above code equivalent in Delphi is:
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 |
Instructions - How to post in this forum |
Thanks for the code, but it definitely doesn't work
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 :
In chart1.onResize
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.
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 ;
Code: Select all
chart1.legend.shapeBounds.TopLeft.X := chart1.TopAxis.CalcXPos(legendX) ;
chart1.legend.shapeBounds.TopLeft.Y := chart1.TopAxis.CalcYPos(legendY) ;
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
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
Hi,
you should be able to do this using the following code :
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;
Pep Jorge
http://support.steema.com
http://support.steema.com