Page 1 of 1

Legend box positioning

Posted: Thu May 01, 2008 2:15 pm
by 6928489
Hi:

I would like to locate the legend box in the upper right of my graphs. I found the "left" and "top" attributes, but since the legend auto-sizes the width by how wide the text elements are, and there's no "width" attribute, I'm not sure how to proceed.

Do you have any suggestions?

Thanks,

Matt

Posted: Fri May 02, 2008 8:00 am
by yeray
Hi Matt,

These properties give you the four legend corners:

Code: Select all

TChart1.Legend.ShapeBounds.Left
TChart1.Legend.ShapeBounds.Top
TChart1.Legend.ShapeBounds.Right
TChart1.Legend.ShapeBounds.Bottom

Posted: Fri May 02, 2008 10:42 am
by 6928489
Thanks, I'll try that. But then what is TChart1.Legend.left and TChart1.Legend.top ?

Posted: Fri May 02, 2008 10:48 am
by 6928489
Sorry, I have another related question: When these properties are set, are they used before or after the legend box is sized based on the width of the text elements?

Thanks,

Matt

Posted: Mon May 05, 2008 8:54 am
by yeray
Hi Matt,

Yes, legend box is sized based on the width of the text elements. So, if you want to have the legend always at the same distance of the right of the chart, I recommend you to use them to obtain legend's width. And then, you could set legend's left property to move it. Something like this:

Code: Select all

Private Sub TChart1_OnResize()
  TChart1.Environment.InternalRepaint

  With TChart1.Legend
    .Left = TChart1.ChartBounds.Right - (.ShapeBounds.Right - .ShapeBounds.Left) - 5
  End With
End Sub

Posted: Mon May 05, 2008 3:33 pm
by 6928489
Hi:

This method moved the legend, but causes the legend to be painted on top of the full width graph, which is ugly.

Normally the graph is sized to fit next to the legend. Is there a way to control this behaviour also?

Thanks,

Matt

Posted: Tue May 06, 2008 9:50 am
by yeray
Hi Matt,

If you want to set a right margin according to the legend width, you could do as follows:

Code: Select all

Private Sub Form_Load()
  With TChart1
    .AddSeries scBar
  
    .Series(0).FillSampleValues 25
  
    .Legend.Top = 0

    TChart1_OnResize
    .Environment.InternalRepaint
    .Panel.MarginUnits = muPixels
  End With
  
  With TChart1.Legend.ShapeBounds
    TChart1.Panel.MarginRight = .Right - .Left + 15
  End With
End Sub

Private Sub TChart1_OnResize()
  TChart1.Environment.InternalRepaint

  With TChart1.Legend.ShapeBounds
    TChart1.Legend.Left = TChart1.ChartBounds.Right - (.Right - .Left) - 5
  End With
End Sub