Page 1 of 1

Determine Height of Legend

Posted: Mon Jan 05, 2015 7:13 pm
by 13052810
Hi,

I'm using version: TeeChart Pro v2013.0.1.4.131120

I have a Legend positioned at the bottom of the chart.
I'm trying to figure out the height of the legend based on the same scale as the chart.
What I'm seeing is that if I have the Legend set to invisible, the Chart is a specific height.
When I make the legend visible the Chart reduces in height to accommodate the legend.
I need to have the TChart get bigger in height, keeping the chart/grid the same vertical size.
The value of the Legend.Height is not the same scale as the TChart.height.
How can I determine the Legend height with respect to the TChart Height?

Thanks

Re: Determine Height of Legend

Posted: Wed Jan 07, 2015 8:57 am
by yeray
Hi Tirby,

This simple example uses the ChartRect and the Legend properties (Left, Top and Right&Bottom or Width&Height) to draw a rectangle around both them.

Code: Select all

Private Sub Form_Load() 
  TChart1.AddSeries scLine
  TChart1.Series(0).FillSampleValues
  
  TChart1.Legend.Alignment = laBottom
End Sub

Private Sub TChart1_OnAfterDraw()
  With TChart1.Canvas
    .Brush.Style = bsClear
    .Pen.Color = vbRed
    
    .Rectangle TChart1.GetChartRect.Left, TChart1.GetChartRect.Top, TChart1.GetChartRect.Right, TChart1.GetChartRect.Bottom
    .Rectangle TChart1.Legend.Left, TChart1.Legend.Top, TChart1.Legend.Left + TChart1.Legend.Width, TChart1.Legend.Top + TChart1.Legend.Height
  End With
End Sub
I'm not sure about what are you exactly trying to achieve, but these properties may help you.
Otherwise, please don't hesitate to let us know and we'll try to help you.

Re: Determine Height of Legend

Posted: Wed Jan 07, 2015 3:11 pm
by 13052810
Yeray,

Thank you for reviewing this.

On the picture below, I have place 2 screen-shots side by side.
I aligned the tops of the grids.
The picture on the left has the Legend set to invisible.
The picture on the right has the Legend set to visible.
Note the positions of the "0" lines.
The difference is highlighted by the purple lines and arrows.

What I need to have happen is regardless of the visible state of the Legend, the Height of the grid needs to remain the same.
Is this more clear now?

Thanks!

Re: Determine Height of Legend

Posted: Thu Jan 08, 2015 2:53 pm
by yeray
Hello,

Then, you should set the legend CustomPostition to true and set both the legend position and the chart margin manually. Ie:

Code: Select all

  TChart1.Aspect.View3D = False
  
  Dim i As Integer
  For i = 0 To 4
    TChart1.AddSeries scLine
    TChart1.Series(0).FillSampleValues
  Next i
  
  TChart1.Legend.Alignment = laBottom
  TChart1.Legend.CustomPosition = True
  
  TChart1.Environment.InternalRepaint
  TChart1.Legend.PositionUnits = puPixels
  TChart1.Legend.Top = TChart1.ChartBounds.Bottom - 30
  TChart1.Legend.Left = 80
  
  TChart1.Panel.MarginBottom = 10