Page 1 of 1

Set constant number of decimals in axis

Posted: Tue Nov 18, 2008 8:55 pm
by 14048359
Hi,

is it possible to specify the axis to have a constant number of decimals for each displayed label?

for example, in this image:

http://picasaweb.google.com/marvin966ju ... 4684653906

the left axis shows a 0.5 increment, but whole values (24 for example), I would like to have 24.0 displayed.

I can not set ValueFormat to #0.0# because I want to be able to zoom more and go to up to X decimals.

I have tried the following:

Code: Select all

private void AdjustAxisValueFormat(Axis axis, int maxDecimals)
      {
         if (axis.CalcIncrement != axis.MinAxisIncrement)
         {
            string currentIncrement = ((decimal)axis.CalcIncrement).ToString();

            int indexOfSeparator =
               currentIncrement.IndexOf(".");
                  
            if (indexOfSeparator == -1)
            {
               axis.Labels.ValueFormat = "#0";
            }
            else
            {
               int numberOfDecimals = Math.Min(currentIncrement.Length - indexOfSeparator - 1, maxDecimals);
               axis.Labels.ValueFormat = "#0.".PadRight(numberOfDecimals + "#0.".Length, '0');
            }
         }
      }
which works in some cases, but not always. axis.CalcIncrement sometimes returns me a value, for example 0.1 in my example, but I end up having duplicate labels, like the following image:

http://picasaweb.google.com/marvin966ju ... 2323761746

Is there a simplier way to accomplish that? Or am I doing something wrong?

Thank you very much!

Posted: Wed Nov 19, 2008 8:59 am
by narcis
Hi Qwerty,

I'm afraid your images are not visible in the thread. You may want to post them at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

I also recommend you to read Tutorial 4 - Axis Control and Custom Numeric Format Strings. You'll find the tutorials at TeeChart's program group.

Hope this helps!

Posted: Wed Nov 19, 2008 1:42 pm
by 14048359
Hi Narcis, thanks for the response. I have updated links for the images, now you should be able to see them.

I went through the tutorial and through custom numeric format strings, but that doesn't help..

Thanks

Posted: Wed Nov 19, 2008 3:57 pm
by narcis
Hi Qwerty,

Thank you very much for posting the images again.

What about doing something like this?

Code: Select all

		public Form1()
		{
			InitializeComponent();
			InitializeChart();
		}

		private void InitializeChart()
		{
			tChart1.Aspect.View3D = false;

			Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);

			Random y = new Random();
			for (int i = 0; i < 25; i++)
			{
				line1.Add(y.Next(2));
			}

			tChart1.Axes.Left.Labels.ValueFormat = "0.0000";
			tChart1.Axes.Left.Increment = 0.0001;
		}
Using this code you'll always have 4 decimals and no duplicate labels.

Posted: Wed Nov 19, 2008 7:09 pm
by 14048359
Hi Narcis,

thank you for the reply.

I do not want to specify the number of decimals to 4 (or whatever other value).. I want to have it dynamically, exacly the same way as the graph already does. The only difference is that I want to have the same number of decimal characters for each axis label.

Having 4 decimal digits when all the displayed values are whole numbers is somewhat useless and we only want to have significative digits for the whole axis, that is why I tried the function stated in my previous message: all it did was finding the number of decimal digits and apply a valueformat according to the number found.

Here is an image of what I am doing right now (when axis.CalcIncrement gives me the correct value).

http://picasaweb.google.com/marvin966ju ... 5511325826

Notice that the left axis has a constant number of decimal digits (and shows 23.0 instead of 23). I have not applied the function on the bottom axis so it keeps the "default" behavior


Thank you

Posted: Thu Nov 20, 2008 9:47 am
by narcis
Hi Qwerty,

So do you already get what you are looking for or still have problems?
which works in some cases, but not always. axis.CalcIncrement sometimes returns me a value, for example 0.1 in my example, but I end up having duplicate labels, like the following image:

http://picasaweb.google.com/marvin966ju ... 2323761746

Is there a simplier way to accomplish that? Or am I doing something wrong?
To avoid duplicate labels you should change axis Increment accordingly. Setting it back to zero would set it as automatic.

Hope this helps!

Posted: Thu Nov 20, 2008 2:09 pm
by 14048359
Hi Narcis,

As I stated in my first post, I have a part of what I am looking for.

Please take a look at the sample project I uploaded via your upload page. The file is named Set constant number of decimals in axis.zip.

Run the program and click + button a few times. You will notice that the 4th time you click it, there are duplicate labels on the left axis, but the 5th time you click, there are no more duplicate labels and there is a constant number of decimal digits, as I am looking for.

The problem I have seen with the 4th click is that the Axis.CalcIncrement returns 1.0. However, if I comment the line where I set ValueFormat, the resulting increment is in fact 0.5, and THAT is my problem.

I would like not having to manage the increment manually. I just want to change the way it is displayed, and I feel I am really close to what I am looking for, except for a few situations.

So, Yes, I am still having problems.

Waiting for your feedback!

Thank you very much



(By the way, doing Zoom.ZoomPercent(379) zooms the graph OUT instead of zooming in... which is a strange behavior in my opinion!)

Posted: Thu Nov 20, 2008 3:25 pm
by narcis
Hi Qwerty,

Thanks for the example project. It helped me find which the problem was.

In AdjustAxisValueFormat(Axis axis, int maxDecimals) you always get an obsolete value for CalcIncrement because it is being called before chart has been repainted after zooming. Therefore axes scales and thus CalcIncrement haven't been updated. To solve this issue you just need to force the chart being internally repainted before calling AdjustAxisValueFormat(Axis axis, int maxDecimals):

Code: Select all

      public void AdjustAxesValueFormat()
      {
				tChart1.Draw();
				AdjustAxisValueFormat(this.tChart1.Axes.Left, 2);
				//AdjustAxisValueFormat(this.tChart1.Axes.Bottom, 5);
				//AdjustAxisValueFormat(this.tChart1.Axes.Right, 3);
      }
(By the way, doing Zoom.ZoomPercent(379) zooms the graph OUT instead of zooming in... which is a strange behavior in my opinion!)
Yes, this also seems strange to me and added the issue (TF02013580) to the defect list to be investigated.

Posted: Thu Nov 20, 2008 9:35 pm
by 14048359
Hi Narcis,

I first thought your solution was working, but after further tests, I came accross another situation where I have duplicate labels:



Please try the following sequence in the uploaded application:
Apply AdjustAxesValueFormat() only to bottom axis and do the following sequence:

+ + + + + + + + + -

Notice that the bottom axis labels are duplicate.. :(

Posted: Fri Nov 21, 2008 3:16 pm
by narcis
Hi Qwerty,

Thanks for the information. I could reproduce this here. In some circumstances axis.CalcIncrement returns a wrong value, as if it hadn't been updated, even calling tChart.Draw() to give it a chance to be refreshed. So I've added the issue to the defect list (TF02013584) to be investigated.

Posted: Fri Nov 21, 2008 3:19 pm
by narcis
Hi Qwerty,

For completeness, I thought that if you are looking for alternatives you'd be interested on reading this thread. Please notice this is quite a long thread (has several pages) and what you are interested in may not appear in the first posts. However, several possibilities were discussed there and it covers a lot of ground on label customization.

Hope this helps!