Page 1 of 2

centering Title on chart

Posted: Wed Nov 14, 2007 2:06 pm
by 9232632
using TeeChart v 7 in Borland Builder C++ 6. have 5 charts lined up next to each other. each as a different dynamic title. would like to center the title either over the graph or in the middle of the chart. have tried setting alignment property to Center and disabling custom location.

basically ... <left position of text>=(<chart width> - <width of title>) / 2

how do i calculate the <width of title>?

Posted: Wed Nov 14, 2007 2:48 pm
by narcis
Hi rperkins,

You can try doing something like this:

Code: Select all

  Chart1->Draw();
  Chart1->Title->Width();
The Draw call is necessary to force the chart being internally repainted and therefore drawn elements having valid property values.

Posted: Wed Nov 14, 2007 3:02 pm
by 9232632
tried the code in the previous post (Draw and Width), but the Width is not a function, but an int property.

the current code has a function that is called after the Title text is changed. this is what was there, but doesn't work correctly.

int TitleWidth = aChart->DelphiCanvas->TextWidth(aChart->Title->Caption);
aChart->Title->Left = (aChart->Width-TitleWidth)/2;

note: aChart is the chart to manipulate and it's passed in to the function.

what happens in the above is that the text does move, but is not centered.

Posted: Wed Nov 14, 2007 3:55 pm
by narcis
Hi rperkins,

What about doing something like this?

Code: Select all

void __fastcall TForm1::FormCreate(TObject *Sender)
{
		Chart1->Title->Text->Clear();
		Chart1->Title->Text->Add("This is my custom title text");
		Chart1->Draw();
		int tmpW=Chart1->DelphiCanvas->TextWidth(Chart1->Title->Text->Strings[0]);
		Chart1->Title->CustomPosition=true;
		Chart1->Title->Left=(Chart1->Width - tmpW)/2;
}

Posted: Wed Nov 14, 2007 4:37 pm
by 9232632
nope. same thing.

uploaded a file (ChartTitles.doc) to the Steema upload site.

Posted: Thu Nov 15, 2007 8:58 am
by narcis
Hi rperkins,

Another approach using ChartRect instead of whole chart dimensions.

Code: Select all

	Chart1->Title->Text->Clear();
	Chart1->Title->Text->Add("This is my custom title text");
	Chart1->Draw();
	int TitleWidth=Chart1->DelphiCanvas->TextWidth(Chart1->Title->Text->Strings[0]);
	Chart1->Title->CustomPosition=true;
	Chart1->Title->Left=Chart1->ChartRect.Left+(Chart1->ChartRect.Width() - TitleWidth)/2;

Posted: Thu Nov 15, 2007 2:05 pm
by 9232632
it appears that the TextWidth function isn't returning the correct size. doing a bit of experimentation, i've found that multiplying the TitleWidth by 1.4 always results in the correct placement over the chart. without this "fudge factor", the text is still too far to the right.

updated code is below. aChart is a parameter passed in to a local function which is used by all charts to update the title position.

String tempString = aChart->Title->Caption;

aChart->Title->Text->Clear();
aChart->Title->Text->Add(tempString);
aChart->Draw();
int TitleWidth=aChart->DelphiCanvas->TextWidth(aChart->Title->Text->Strings[0]);
TitleWidth = (int)((float)TitleWidth * 1.4 );
aChart->Title->CustomPosition=true;
aChart->Title->Left=aChart->ChartRect.Left+(aChart->ChartRect.Width() - TitleWidth)/2;

any thoughts?

Posted: Thu Nov 15, 2007 2:24 pm
by 9232632
a co-worker (more familiar with TeeChart) suggested it might have something to do with the font associated with the canvas. he suggested that before doing the TextWidth, set the font of the canvas to the font of the title. this helped, but it still wasn't centered like the "fudge factor" multiply of 1.4

font setting....

aChart->DelphiCanvas->Font = aChart->Title->Font;

Posted: Mon Nov 19, 2007 12:26 pm
by Pep
Hi,

using the following code seems to work fine here :

Chart3.Title.Left :=Chart3.ChartRect.Left + ((Chart3.Chartrect.right - chart3.ChartRect.Left) div 2) - ( Chart3.Canvas.textWidth(Chart3.Title.Caption) div 2);


Could you please test it ?

Posted: Mon Nov 19, 2007 3:47 pm
by 9232632
same thing.

here's my Borland C++ converted code - i think it matches up with the delphi code.

aChart->Title->Left=aChart->ChartRect.Left+((aChart->ChartRect.right - aChart->ChartRect.Left)/2) -
(aChart->Canvas->TextWidth(aChart->Title->Caption)/2);

i've uploaded a new document to the Steema Upload site - ChartTitle-111907.doc. this shows the titles over the bottom 5 charts. using the above code, the 2nd screen shot has the titles running off the right. using the "fudge factor" i described before, there is enough room to display the entire title centered over the chart.

not sure if it's because of the font/size/style of the title. The font is Arial, size 10, Bold, TTeeShadow, clBlue.

let me know if there's anything else you'd like me to try.

thanks,

pbt.

Posted: Mon Nov 26, 2007 12:38 pm
by Pep
Hi,

it's strange. Are you able to reproduce the same problem iwth a small new app. (doing the same, adding just some charts and assign titles for them) ?
If so, could you please send us the app. so we can reproduce it ?

Posted: Mon Nov 26, 2007 2:09 pm
by 9232632
i believe i uploaded the Borland C++ builder module (cpp, h, and dfm) file that contain the charts (for another unrelated support issue). it would be called MultiViewForm.zip. it's not in a self contained "mini-app", so it won't compile/run by itself. haven't had time to create a small test app, just time to come up with the fudge factor.

Posted: Tue Nov 27, 2007 11:11 am
by 10046032
Hello,

I think this is issue TV52012148 but it is not fixed yet, correct?

Regards

Posted: Tue Nov 27, 2007 1:25 pm
by 9232632
not sure how to see the issue #. the "other" support item has to do with visibility of the top and right bar around the chart, and no, it has not been resolved (other than using the Borland shapes).

Posted: Wed Dec 05, 2007 11:03 am
by narcis
Hi rperkins,

We have looked at the code you sent but we are unable to compile it here. It is very difficult for us finding the problem without being able to reproduce the problem here. Test we have done here worked fine using this code:

Code: Select all

Chart3.Title.Left :=Chart3.ChartRect.Left + ((Chart3.Chartrect.right - chart3.ChartRect.Left) div 2) - ( Chart3.Canvas.textWidth(Chart3.Title.Caption) div 2); 
Would you be so kind to arrange a simple example project we can run "as-is" to reproduce the problem here? This could only contain the numbers of charts you want, their titles and any other setting you feel to be relevant to the issue.

Thanks in advance.