Page 1 of 1

Drawing Centred Text

Posted: Thu Sep 12, 2013 3:45 am
by 15663432
I am drawing text on the TChart Canvas using the g.RotateLabel(Convert.ToInt32(X1), Convert.ToInt32(Y1), string, rotation) function

For test purposes, I am setting the rotation to zero. To use this function, the text must be perfectly centred horizontally and vertically.

I am trying to find the best method to centre the text that will work with all fonts. Should I use Visual Studio font metrics or textheight and textwidth methods? Testing the various methods, I get different results.
In Private Sub TChart1_AfterDraw(ByVal sender As Object, ByVal g As Steema.TeeChart.Drawing.Graphics3D) Handles TChart1.AfterDraw
g.Font.Name = SymbolFont(i)
g.Font.Size = SymbolSize(i)
g.Font.Italic = SymbolItalic(i)
g.Font.Bold = SymbolBold(i)
g.Font.Underline = False 'SymbolUnderline(i)
g.Font.Color = SymbolColor(i)
g.Font.Unit = GraphicsUnit.Pixel

text_hgt = g.TextWidth(Chr(SymbolLabel(i)))
text_wid = g.TextWidth(Chr(SymbolLabel(i)))

X1 = X1 - (text_wid / 2)
Y1 = Y1 + (text_hgt / 2)
This seems to give the best results, but I have no idea why?
Also, the text_hgt does not work for all fonts, only certain fonts.
I need a method that will centre the text vertically for all fonts, horizontally works OK.

I have also tried:

Dim Font1 = New Steema.TeeChart.Drawing.ChartFont
Font1 = g.Font
text_hgt = g.TextHeight(Font1, Chr(SymbolLabel(i)))

But, the text_hgt does not centre the text vertically.

text_hgt = g.Font.Size does not work either.

I am thinking g.RotateLabel(Convert.ToInt32(X1), Convert.ToInt32(Y1), string, rotation) function does not work properly on the Tchart canvas?

Re: Drawing Centred Text

Posted: Thu Sep 12, 2013 7:48 am
by 15663432
I have found the solution by using:

text_hgt = g.Font.DrawingFont.Height