Page 1 of 1

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

Posted: Fri Jan 11, 2019 9:37 am
by 15685014
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.

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

Posted: Mon Jan 14, 2019 8:20 am
by Christopher
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);
		}

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

Posted: Mon Jan 14, 2019 1:37 pm
by 15685014
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))?

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

Posted: Mon Jan 14, 2019 3:06 pm
by Christopher
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 :)

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

Posted: Tue Jan 15, 2019 4:05 pm
by 15685014
Thx, it's working now.