Howto set axis title posistion to the end of the axis?

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Howto set axis title posistion to the end of the axis?

Post by bairog » Fri Jan 11, 2019 9:37 am

Hello.
I'm using TeeChart Pro 4.2018.12.17.
As far as I can see I cannot set axis title position via TeeChart Editor:
Image
By default it is located in the middle of the axis (to the left of labels in case of left axis and below labels in case of bottom axis):
Image
But I want it to be at the end of axis (in a row with labels): above "1.9" label for the left axis and to the right of "04.04.2018/1/1" label for the bottom axis.
How can I do this?
Thank you.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Howto set axis title posistion to the end of the axis?

Post by Christopher » Mon Jan 14, 2019 8:20 am

Hello,
bairog wrote:
Fri Jan 11, 2019 9:37 am
How can I do this?
One way would be draw your custom text directly to the Chart, e.g.

Code: Select all

		private void InitializeChart()
		{
			Bar bar = new Bar(tChart1.Chart);
			bar.FillSampleValues();
			tChart1.AfterDraw += TChart1_AfterDraw1;
		}

		private void TChart1_AfterDraw1(object sender, Graphics3D g)
		{
			Rectangle leftRect = tChart1.Axes.Left.AxisRect();
			string leftTitle = "left title";
			g.TextOut(leftRect.X - Utils.Round(g.TextWidth(leftTitle)), leftRect.Y, leftTitle);

			Rectangle bottomRect = tChart1.Axes.Bottom.AxisRect();
			string bottomTitle = "bottom title";
			g.TextOut((bottomRect.X + bottomRect.Width) - Utils.Round(g.TextWidth(bottomTitle)), bottomRect.Y, bottomTitle);
		}
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Re: Howto set axis title posistion to the end of the axis?

Post by bairog » Mon Jan 14, 2019 1:37 pm

Christopher wrote:
Mon Jan 14, 2019 8:20 am
One way would be draw your custom text directly to the Chart, e.g.
  1. I've slightly modified your code to make axis title be above all axis labels and to respect tChart1.Axis.Left.Title.Font setting (which is Calibri, 12pt, Bold in my case):

    Code: Select all

    g.TextOut((tChart1.Axis.Left.Title.Font, bottomRect.X + bottomRect.Width) - Utils.Round(g.TextWidth(bottomTitle)), bottomRect.Y - 15, bottomTitle);
    Looks like that approach ignores the fact that left axis title font is bold:
    Image
  2. Just interesting - why do you use Utils.Round(g.TextWidth(bottomTitle)) instead of Math.Round(g.TextWidth(bottomTitle))?

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Howto set axis title posistion to the end of the axis?

Post by Christopher » Mon Jan 14, 2019 3:06 pm

Hello,
bairog wrote:
Mon Jan 14, 2019 1:37 pm
Looks like that approach ignores the fact that left axis title font is bold:
You can rectify this by setting the Font properties in the AfterDraw event, e.g.

Code: Select all

		private void TChart1_AfterDraw1(object sender, Graphics3D g)
		{
			g.Font.Bold = true;
			g.Font.Name = "Calibri";
			g.Font.Size = 12;

			Rectangle leftRect = tChart1.Axes.Left.AxisRect();
			string leftTitle = "left title";
			g.TextOut(leftRect.X - Utils.Round(g.TextWidth(leftTitle)), leftRect.Y, leftTitle);

			Rectangle bottomRect = tChart1.Axes.Bottom.AxisRect();
			string bottomTitle = "bottom title";
			g.TextOut((bottomRect.X + bottomRect.Width) - Utils.Round(g.TextWidth(bottomTitle)), bottomRect.Y, bottomTitle);
		}
bairog wrote:
Mon Jan 14, 2019 1:37 pm
Just interesting - why do you use Utils.Round(g.TextWidth(bottomTitle)) instead of Math.Round(g.TextWidth(bottomTitle))?
Using Math.Round would mean having to code an additional cast to Int32, e.g.

Code: Select all

g.TextOut(leftRect.X - (int)Math.Round(g.TextWidth(leftTitle)), leftRect.Y, leftTitle);
Utils.Round is simply a little sugar to save having to type out those extra five characters :)
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

bairog
Advanced
Posts: 128
Joined: Fri Dec 07, 2018 12:00 am

Re: Howto set axis title posistion to the end of the axis?

Post by bairog » Tue Jan 15, 2019 4:05 pm

Thx, it's working now.

Post Reply