Page 1 of 1
[Legend] Auto-repositionning when chart is resized ?
Posted: Mon Feb 20, 2006 10:42 am
by 9343260
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 ?
Posted: Mon Feb 20, 2006 10:45 am
by narcis
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.
Posted: Mon Feb 20, 2006 11:06 am
by 9343260
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.
Posted: Mon Feb 20, 2006 11:46 am
by narcis
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.
Posted: Mon Feb 20, 2006 1:23 pm
by 9343260
Ok for the moment to call the method, but my problem is to calculate the new pixel coordinates of the legend
Which function must I use ?, as the property 'Position' doesn't exist in the Delphi version.
Posted: Mon Feb 20, 2006 2:13 pm
by narcis
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;
Posted: Mon Feb 20, 2006 2:55 pm
by 9343260
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 :
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.
Posted: Wed Feb 22, 2006 1:28 pm
by 9343260
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
Posted: Fri Feb 24, 2006 5:06 pm
by Pep
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;