An axis glitch ....

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Snarkle
Newbie
Newbie
Posts: 91
Joined: Wed Jun 30, 2010 12:00 am

An axis glitch ....

Post by Snarkle » Fri Aug 13, 2010 6:33 am

Greetings,

I have a tricky one ... my Right Axis (Which is axis.left and axes.left.otherside = true)
axisGoodBehaviour.png
axisGoodBehaviour.png (5.25 KiB) Viewed 8092 times
All working as expected ..
However when I add a custom axis to the right ... it does not adjust to allow room for the scale values. (see below)
axisBad.png
axisBad.png (12.27 KiB) Viewed 8089 times
I have set the axis to automatic

Code: Select all

            StockChart.Axes.Left.Automatic = true;
            StockChart.Axes.Bottom.Automatic = true;
But no luck there ... is there a property I am not setting that will enable the chart to adjust accordingly?
--------------------
Cheers Phil.

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: An axis glitch ....

Post by Yeray » Fri Aug 13, 2010 4:03 pm

Hi Phil,

The chart doesn't adjust the margins automatically for the Custom axes. So you have to do it manually:

Code: Select all

StockChart.Panel.MarginRight = 10;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Snarkle
Newbie
Newbie
Posts: 91
Joined: Wed Jun 30, 2010 12:00 am

Re: An axis glitch ....

Post by Snarkle » Mon Aug 16, 2010 12:37 am

Thanks Yeray,

Ok I understand .. is there a way to determine how many pixels the Custom axis' label text is ..

For example if the Axis lable is "5,000,000,000" .. can I determine how many pixels this takes up so I can adjust the axis manually ...

Cheers Phil.
--------------------
Cheers Phil.

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: An axis glitch ....

Post by Yeray » Mon Aug 16, 2010 9:11 am

Hi Phil,

Yes, you could call the following once the chart has been drawn (so you may also need to call tChart1.Draw()):

Code: Select all

            tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
            tChart1.Panel.MarginRight = tChart1.Axes.Left.MaxLabelsWidth() + tChart1.Axes.Left.Ticks.Length;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Snarkle
Newbie
Newbie
Posts: 91
Joined: Wed Jun 30, 2010 12:00 am

Re: An axis glitch ....

Post by Snarkle » Thu Nov 04, 2010 1:33 am

Hi Yeray,

I tried the supplied code ... but it doesn't seem to be working predictably.

Code: Select all

        private void StockChart_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            StockChart.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
            StockChart.Panel.MarginRight = StockChart.Axes.Left.MaxLabelsWidth() +StockChart.Axes.Left.Ticks.Length;
            Debug.Print("MarginRight: " + StockChart.Panel.MarginRight.ToString());
        }
My full code is somethign like this

Code: Select all

StockChart.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
            double maxMargin =  StockChart.Axes.Left.MaxLabelsWidth() + StockChart.Axes.Left.Ticks.Length;

            foreach (Axis axis in StockChart.Axes.Custom)
            {
                double tmp = axis.MaxLabelsWidth() + axis.Ticks.Length;
                if (maxMargin < tmp)
                {
                    maxMargin = tmp;
                }
            }

            StockChart.Panel.MarginRight = maxMargin;
But it still overestimates the Label size and not by any constant value that I could adjust the Margin value by .. i.e. 50%

When I first open my chart the axis labels render like so
flakey1.png
flakey1.png (3.65 KiB) Viewed 8021 times
When I add a custom axis with much larger labels the spacing doesnt change.(but by luck happens to fit)
flakey2.png
flakey2.png (5.66 KiB) Viewed 8018 times
When I change to another instrument that has larger labels I again have quite a gap
flakey3.png
flakey3.png (4.1 KiB) Viewed 8020 times
Does it matter that my 'Right Axis' is actually the 'Left Axis' with the 'otherside' property set to true?

I'm guessing the main problem I am having is that it is calculating a far bigger label size than is actually being drawn.
And I am assuming that I need to do this routine for EVERY vertical axis and Custom axis.
--------------------
Cheers Phil.

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

Re: An axis glitch ....

Post by Narcís » Thu Nov 04, 2010 11:29 am

Hi Phil,

What about doing something like in the example below?

Code: Select all

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

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

            tChart1.Series.Add(new Steema.TeeChart.Styles.Line()).FillSampleValues(100);
            tChart1.Series.Add(new Steema.TeeChart.Styles.Line());

            Random y = new Random();

            for (int i = 0; i < 100; i++)
            {
                tChart1[1].Add(y.Next());
            }

            tChart1.Axes.Left.OtherSide = true;
            tChart1.Axes.Left.EndPosition = 50;

            Steema.TeeChart.Axis axis1 = new Steema.TeeChart.Axis(false, true, tChart1.Chart);
            tChart1.Axes.Custom.Add(axis1);
            axis1.StartPosition = 50;

            tChart1[1].CustomVertAxis = axis1;

            CalcMargins();
        }

        private void CalcMargins()
        {
            tChart1.Draw();

            double maxWidth = 0;

            for (int i = 0; i < tChart1.Axes.Count; i++)
            {
                Steema.TeeChart.Axis a = tChart1.Axes[i];

                if (!a.Horizontal)
                {
                    double tmp = tChart1.Graphics3D.TextWidth(a.Labels.Font, a.Maximum.ToString());
                    maxWidth = Math.Max(maxWidth, tmp);
                }
            }

            tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
            tChart1.Panel.MarginRight = maxWidth;
        }
Hope this helps!
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

Snarkle
Newbie
Newbie
Posts: 91
Joined: Wed Jun 30, 2010 12:00 am

Re: An axis glitch ....

Post by Snarkle » Fri Nov 05, 2010 1:14 am

no that did the same thing ... but I might have an idea why with the last bit of code ...

double tmp = StockChart.Graphics3D.TextWidth(a.Labels.Font, a.Maximum.ToString());

Should be bullet proof if I understand it correctly.

What I suspect here is that a.Maximum can happily be a value such as 15.348578857994885959
even though it would only be displayed to 2 or 3 decimal points (i.e. 15.34 ) ... so its calculating on a string that is much much larger than the visible display value.

I'll keep playing with it .. see if I'm right ... I'll try truncating the Maximum value to a form that reflects what is being displayed.

Would axis.MaxLabelsWidth() have the same issue .. does it use StockChart.Graphics3D.TextWidth() behind the scenes ...
--------------------
Cheers Phil.

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

Re: An axis glitch ....

Post by Narcís » Fri Nov 05, 2010 9:56 am

Hi Phil,

Yes, exactly, internally MaxLabelsWidth() uses TextWidth. Having that in mind you can modify CalcMargins to convert axis maximum to a string with the same format as axis labels and then calculate its width, for example:

Code: Select all

        private void CalcMargins()
        {
            tChart1.Draw();

            double maxWidth = 0;

            for (int i = 0; i < tChart1.Axes.Count; i++)
            {
                Steema.TeeChart.Axis a = tChart1.Axes[i];

                if (!a.Horizontal)
                {
                    string maxLabel = String.Format("{0:" + a.Labels.ValueFormat + "}", a.Maximum.ToString());
                    double tmpWidth = tChart1.Graphics3D.TextWidth(a.Labels.Font, maxLabel);
                    maxWidth = Math.Max(maxWidth, tmpWidth);
                }
            }

            tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
            tChart1.Panel.MarginRight = maxWidth;
        }
Hope this helps!
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

Snarkle
Newbie
Newbie
Posts: 91
Joined: Wed Jun 30, 2010 12:00 am

Re: An axis glitch ....

Post by Snarkle » Tue Nov 09, 2010 5:32 am

closer still but not quite ...

Its getting the text formatting information and the maximum values .. but the maximum values still have decimal points in them and so even with the formatting are still beign calculated slightly larger than what is being displayed ...

Is there a way to grab the largest Label on an axis and in most cases I mean the top label on an axis ...

i.e. an axis runs from 5 - 12000 ... the max value may well be 12000.12345 ... but the label is 'nice' number of 12000 .. on which I'd like to base the scaling.

My custom axis are all set to AUTO the string mask is #,##0.### .. so the max value will get decimal places as well.
--------------------
Cheers Phil.

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

Re: An axis glitch ....

Post by Narcís » Tue Nov 09, 2010 10:03 am

Hi Phil,

The only option I can think of is using GetAxisLabel or GetNextAxisLabel events as in the example below.

Code: Select all

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

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

            tChart1.Series.Add(new Steema.TeeChart.Styles.Line()).FillSampleValues(100);
            tChart1.Series.Add(new Steema.TeeChart.Styles.Line());

            Random y = new Random();

            for (int i = 0; i < 100; i++)
            {
                tChart1[1].Add(y.Next());
            }

            tChart1.Axes.Left.OtherSide = true;
            tChart1.Axes.Left.EndPosition = 50;
            tChart1.Axes.Left.Labels.ValueFormat = "#,##0.###";

            Steema.TeeChart.Axis axis1 = new Steema.TeeChart.Axis(false, true, tChart1.Chart);
            tChart1.Axes.Custom.Add(axis1);
            axis1.StartPosition = 50;
            axis1.Labels.ValueFormat = "#,##0.###";

            tChart1[1].CustomVertAxis = axis1;

            tChart1.GetAxisLabel += new Steema.TeeChart.GetAxisLabelEventHandler(tChart1_GetAxisLabel);

            CalcMargins();
        }

        double maxLength = 0;

        void tChart1_GetAxisLabel(object sender, Steema.TeeChart.GetAxisLabelEventArgs e)
        {
            Steema.TeeChart.Axis a = (Steema.TeeChart.Axis)sender;

            if (!a.Horizontal)
            {
                double tmpWidth = tChart1.Graphics3D.TextWidth(a.Labels.Font, e.LabelText);
                maxLength = Math.Max(maxLength, tmpWidth);
            }
        }

        private void CalcMargins()
        {
            tChart1.Draw();

            //double maxWidth = 0;

            //for (int i = 0; i < tChart1.Axes.Count; i++)
            //{
            //    Steema.TeeChart.Axis a = tChart1.Axes[i];

            //    if (!a.Horizontal)
            //    {
            //        string maxLabel = String.Format("{0:" + a.Labels.ValueFormat + "}", a.Maximum.ToString());
            //        double tmpWidth = tChart1.Graphics3D.TextWidth(a.Labels.Font, maxLabel);
            //        maxWidth = Math.Max(maxWidth, tmpWidth);
            //    }
            //}

            tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
            tChart1.Panel.MarginRight = maxLength;// maxWidth;
        }
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

Snarkle
Newbie
Newbie
Posts: 91
Joined: Wed Jun 30, 2010 12:00 am

Re: An axis glitch ....

Post by Snarkle » Wed Nov 10, 2010 5:08 am

Tnx Narcis,

I ended up going back to the previous version ... but by being a little more aggressive about setting the ValueFormat string instead of leaving the custom axis as auto its working fairly well now ...

thanks for the support.
--------------------
Cheers Phil.

Post Reply