Page 1 of 1

Extra legend tool position

Posted: Mon Feb 23, 2009 5:31 am
by 10545622
Hi,

How is it possible to position correctly the Extra legend tool when the window is resized ? I tried with the Chart OnResize event but this event is never fired.

How is it possible to modify the width of this Extra legend and to add a right margin (actually equal to 0) ?. I would like to have the same width like the normal legend.

Best regards

Posted: Mon Feb 23, 2009 6:34 am
by 10046032
Hello,

Try using the OnGetLegendRect event, I use this to custom place my legend :

Code: Select all

procedure TForm1.Chart1GetLegendRect(Sender: TCustomChart; var Rect: TRect);
var w, h: Integer;
begin
    w:=Sender.Legend.Width;
    h:=Sender.Legend.Height;
    Sender.Legend.CustomPosition:=true;
    Rect.Left:=Sender.ChartRect.Right-w-3;
    Rect.Top:=Sender.ChartRect.Top+3;
    Rect.Right:=Rect.Left+w;
    Rect.Bottom:=Rect.Top+h;
end;
Regards

Posted: Mon Feb 23, 2009 10:21 am
by yeray
Hi stsl,
stsl wrote:How is it possible to position correctly the Extra legend tool when the window is resized ?
stsl wrote:How is it possible to modify the width of this Extra legend and to add a right margin (actually equal to 0) ?. I would like to have the same width like the normal legend.
I'd like to recommend you something similar than what johnnix did, but considering that you have a "normal" legend and a extra legend tool and you don't want them to be superposed, you could do something like this:

Code: Select all

procedure TForm1.Chart1GetLegendRect(Sender: TCustomChart;
  var Rect: TRect);
begin
  Rect.Left := Chart1.Legend.Left;
  Rect.Right := Chart1.Legend.Left + Chart1.Legend.Width;
end;

procedure TForm1.Chart1Resize(Sender: TObject);
begin
  Chart1.Draw;
  ChartTool1.Legend.Top := Chart1.Legend.Top + Chart1.Legend.Height + 5;
end;
stsl wrote:I tried with the Chart OnResize event but this event is never fired.
As you see, I've added the OnResize event on the sample project we discussed about in this thread and it seems to work fine. Please, could you send us a simple example project we can run "as-is" to reproduce the problem here? You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Posted: Tue Feb 24, 2009 9:00 am
by 10545622
Hi, it works very well.

I made a mistake in my code, it was the reason why the OnResize was not fired (i loaded a template and lost my design time event)

Best regards

Posted: Tue Feb 24, 2009 11:00 am
by yeray
Hi stsl,

I'm happy to see that you solved it!

Posted: Mon Mar 02, 2009 1:02 pm
by 10545622
Hi,

I have an other little problem with my Extra legend tool (one legend + one Extra legend Tool). I don't know how to find the extra legend tool width if this width is bigger than the normal legend width.
I would like to have the two legend with the same width equal to the max(legend width, extra legend width).

Best regards

Posted: Mon Mar 02, 2009 3:02 pm
by yeray
Hi stsl,

As OnGetLegendRect is executed for all the legends, you could get the biggest there doing something like this:

Code: Select all

procedure TForm1.Chart1GetLegendRect(Sender: TCustomChart;
  var Rect: TRect);
begin
  if ((Rect.Right - Rect.Left) > MaxLegendWidth) then MaxLegendWidth := Rect.Right - Rect.Left;

  Rect.Left := Chart1.Legend.Left;
  Rect.Right := Chart1.Legend.Left + MaxLegendWidth;
end;
Of course you should define as global integer the MaxLegendWidth and initialize it to zero at OnCreate, for example.

Posted: Mon Mar 02, 2009 4:25 pm
by 10545622
Hi,

I tried this solution with a long text in the second legend (long group serie title). The Chart panel is not correctly resized. The 2 legends are truncated.

Regards

Posted: Mon Mar 02, 2009 8:52 pm
by 10545622
I sent a file from your upload page (Test TimeGraph.7z).
In my sample, there is an other problem when i resize the chart too quickly (i joined a png file to see my 2 problems)

Best regards

Posted: Wed Mar 04, 2009 11:58 am
by yeray
Hi stsl,

I've sent to your mail your project with the changes I made to solve these issues.

Posted: Wed Mar 04, 2009 1:06 pm
by 10545622
Thanks, it's really better.
But the legend is now in front of the right axis. How is possible to have the the 2 legends between the labels of the right axis and the right side of the chart panel (with a little margin) like the default legend position of a new chart ?

Best regards

Posted: Thu Mar 05, 2009 8:37 am
by yeray
Hi stsl,

It's because setting a custom legend position the chart margin is recalculated considering all the chartrect width.

You'll solve it setting a dynamic right margin respect the legend. For example:

At the end of ChartGetLegendRect:

Code: Select all

Chart.MarginRight := Chart.Width-Chart.Legend.Left;
And at OnCreate:

Code: Select all

Chart.MarginUnits := muPixels;