Page 1 of 1

Legned position vs axis

Posted: Mon May 31, 2010 8:25 am
by 9529132
legend1.jpg
Default legend position
legend1.jpg (22.87 KiB) Viewed 11580 times
legend2.jpg
Expected legend position
legend2.jpg (21.33 KiB) Viewed 11577 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.

Re: Legned position vs axis

Posted: Mon May 31, 2010 10:37 am
by yeray
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

Re: Legned position vs axis

Posted: Mon May 31, 2010 1:06 pm
by 9529132
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?

Re: Legned position vs axis

Posted: Tue Jun 01, 2010 7:23 am
by yeray
Hi David,

Are you using that code at chart's AfterDraw event? Here it seems to work as expected.

Re: Legned position vs axis

Posted: Tue Jun 01, 2010 8:22 am
by 9529132
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.

Re: Legned position vs axis

Posted: Thu Jun 03, 2010 6:10 am
by 9529132
Any update on this?

Re: Legned position vs axis

Posted: Thu Jun 03, 2010 10:42 am
by yeray
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);
}

Re: Legned position vs axis

Posted: Thu Jun 03, 2010 12:48 pm
by 9529132
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.

Re: Legned position vs axis

Posted: Mon Jun 07, 2010 10:19 am
by yeray
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();

Re: Legned position vs axis

Posted: Mon Jun 07, 2010 12:41 pm
by 9529132
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.