centering Title on chart

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

centering Title on chart

Post by rperkins » Wed Nov 14, 2007 2:06 pm

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>?

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Nov 14, 2007 2:48 pm

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.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Wed Nov 14, 2007 3:02 pm

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.

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Nov 14, 2007 3:55 pm

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;
}
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Wed Nov 14, 2007 4:37 pm

nope. same thing.

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

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Thu Nov 15, 2007 8:58 am

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;
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Thu Nov 15, 2007 2:05 pm

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?

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Thu Nov 15, 2007 2:24 pm

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;

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Mon Nov 19, 2007 12:26 pm

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 ?

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Mon Nov 19, 2007 3:47 pm

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.

Pep
Site Admin
Site Admin
Posts: 3299
Joined: Fri Nov 14, 2003 5:00 am
Contact:

Post by Pep » Mon Nov 26, 2007 12:38 pm

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 ?

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Mon Nov 26, 2007 2:09 pm

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.

johnnix
Advanced
Posts: 192
Joined: Tue Jul 10, 2007 12:00 am

Post by johnnix » Tue Nov 27, 2007 11:11 am

Hello,

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

Regards

rperkins
Newbie
Newbie
Posts: 58
Joined: Wed May 26, 2004 4:00 am

Post by rperkins » Tue Nov 27, 2007 1:25 pm

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).

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Post by Narcís » Wed Dec 05, 2007 11:03 am

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.
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply