Legned position vs axis

TeeChart for ActiveX, COM and ASP
Post Reply
David
Advanced
Posts: 203
Joined: Tue Nov 08, 2005 5:00 am

Legned position vs axis

Post by David » Mon May 31, 2010 8:25 am

legend1.jpg
Default legend position
legend1.jpg (22.87 KiB) Viewed 11578 times
legend2.jpg
Expected legend position
legend2.jpg (21.33 KiB) Viewed 11575 times
How can I adjust the legend position so that it won't waste the plotting area? legend1.jpg is what the teechart's default legend position, and legend2.jpg is what I want to realize. Would anyone please advice how to do it? Thanks.

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Legned position vs axis

Post by Yeray » Mon May 31, 2010 10:37 am

Hi David,

You should adjust the legend manually doing something like this:

Code: Select all

Private Sub AdjustLegend()
  Dim tmpWidth, tmpMargin As Integer
  tmpWidth = TChart1.Legend.ShapeBounds.Right - TChart1.Legend.ShapeBounds.Left
  tmpMargin = 10
  
  TChart1.Legend.CustomPosition = True
  TChart1.Panel.MarginUnits = muPixels
  TChart1.Panel.MarginRight = tmpWidth + tmpMargin * 2
  TChart1.Legend.Left = TChart1.ChartBounds.Right - tmpWidth - tmpMargin
End Sub

Private Sub TChart1_OnAfterDraw()
  AdjustLegend
End Sub
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

David
Advanced
Posts: 203
Joined: Tue Nov 08, 2005 5:00 am

Re: Legned position vs axis

Post by David » Mon May 31, 2010 1:06 pm

Thanks for prompt reply. I followed your advice, but it didn't work. Here is my code in C++ in AdjustLegend()

Code: Select all

	int nWidth = m_chart.GetLegend().GetShapeBounds().GetRight()-m_chart.GetLegend().GetShapeBounds().GetLeft();
	int nMgn = 10;

	m_chart.GetLegend().SetCustomPosition(TRUE);
	m_chart.GetPanel().SetMarginUnits(muPixels);
	m_chart.GetPanel().SetMarginRight(nWidth + nMgn*2);
	int nRmgn = m_chart.GetChartBounds().GetRight();
	m_chart.GetLegend().SetLeft(nRmgn - nWidth - nMgn);
Any suggestion?

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Legned position vs axis

Post by Yeray » Tue Jun 01, 2010 7:23 am

Hi David,

Are you using that code at chart's AfterDraw event? Here it seems to work as expected.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

David
Advanced
Posts: 203
Joined: Tue Nov 08, 2005 5:00 am

Re: Legned position vs axis

Post by David » Tue Jun 01, 2010 8:22 am

Yes, I did, and it didn't work. I traced step by step and the OnAfterDraw function is fired when the dialog-based app starts, but the effect wasn't right. I even tried to set nMgn = 100, but no effect at all.

I attached a simple test project using VC++.NET 2008 and hopefully you can reproduce the problem.The additional included directory in the project is "C:\Program Files\TeeChart Pro v8 ActiveX Control\Utilities\New VC Classes", and if you installed teechart into different directory, you have to change the setting project->properties->configuration property->C/C++->additional included directory.
Attachments
ChartLegendTest.rar
(131 KiB) Downloaded 605 times

David
Advanced
Posts: 203
Joined: Tue Nov 08, 2005 5:00 am

Re: Legned position vs axis

Post by David » Thu Jun 03, 2010 6:10 am

Any update on this?

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Legned position vs axis

Post by Yeray » Thu Jun 03, 2010 10:42 am

Hi David,

With the following changes it seems to work fine here.
The problem is that you set a right axis title, so the calculations made before didn't consider the margin added automatically for this text.

Code: Select all

BOOL CChartLegendTestDlg::OnInitDialog()
{
	//...

	m_chart1.GetAspect().SetView3D(FALSE);

	m_chart1.AddSeries(scFastLine);
	m_chart1.AddSeries(scFastLine);
	m_chart1.AddSeries(scFastLine);

	m_chart1.Series(1).SetVerticalAxis(aRightAxis);

	m_chart1.GetAxis().GetRight().GetTitle().SetCaption("Test");
	m_chart1.GetAxis().GetRight().GetLabels().SetSize(42);
	m_chart1.GetAxis().GetRight().GetTitle().GetFont().SetSize(12);
	m_chart1.GetAxis().GetRight().SetAutomatic(TRUE);

	m_chart1.GetLegend().SetCustomPosition(TRUE);

	m_chart1.GetEnvironment().InternalRepaint();

	return TRUE;
}

//...

void CChartLegendTestDlg::OnAfterDrawTchart1()
{
	int nWidth = m_chart1.GetLegend().GetShapeBounds().GetRight()-m_chart1.GetLegend().GetShapeBounds().GetLeft();
	int nMgn = 10;
	int axisMgn = 65;

	m_chart1.GetPanel().SetMarginUnits(muPixels);
	m_chart1.GetPanel().SetMarginRight(nWidth - axisMgn + nMgn*2);
	
	m_chart1.GetLegend().SetLeft(m_chart1.GetAxis().GetBottom().GetIEndPos() + nMgn);
}
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

David
Advanced
Posts: 203
Joined: Tue Nov 08, 2005 5:00 am

Re: Legned position vs axis

Post by David » Thu Jun 03, 2010 12:48 pm

Thanks a lot, now it works. but I found another problem and it could be a bug.

Code: Select all

	m_chart1.GetLegend().SetAlignment(laTop); 
	int nWidth = m_chart1.GetLegend().GetShapeBounds().GetRight()-m_chart1.GetLegend().GetShapeBounds().GetLeft();
with or without the SetAlignment(laTop), the nWidth always gets the same value, even though the legend width changes a lot when aligh legend items to top.

Yeray
Site Admin
Site Admin
Posts: 9602
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Legned position vs axis

Post by Yeray » Mon Jun 07, 2010 10:19 am

Hi David,

Setting the legend to be aligned on the top you'll need to force the chart to be repainted once more so that the new legend position can be used to calculate it's width.

If I add the following at the end of the OnInitDialog in the example above, the nWidth I get in the OnAfterDraw event is 170, while before it was 66.

Code: Select all

BOOL CChartLegendTestDlg::OnInitDialog()
{
   //...
   m_chart1.GetLegend().SetCustomPosition(TRUE);
   m_chart1.GetLegend().SetAlignment(laTop);

   m_chart1.GetEnvironment().InternalRepaint();
   m_chart1.GetEnvironment().InternalRepaint();
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

David
Advanced
Posts: 203
Joined: Tue Nov 08, 2005 5:00 am

Re: Legned position vs axis

Post by David » Mon Jun 07, 2010 12:41 pm

It seems I made a mistake, I put m_chart1.GetEnvironment().InternalRepaint() before m_chart1.GetLegend().SetAlignment(laTop). After I change the sequence, it works fine. It is not necessary to use internalrepaint twice.

Thanks a lot for your help.

Post Reply