FirstVisibleIndex and LastVisibleIndex while zooming

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

Re: FirstVisibleIndex and LastVisibleIndex while zooming

Post by bairog » Tue Aug 13, 2019 5:31 am

Christopher wrote:
Mon Aug 12, 2019 11:41 am
Try setting the Labels separation to zero, e.g.
Up to 60 points it's working:
Image
But for example for 70 points (when labels start to overlap each other) - chart itself performs labels thinning (my function Bottom_GetAxisDrawLabel is disabled here):
Image

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

Re: FirstVisibleIndex and LastVisibleIndex while zooming

Post by Christopher » Tue Aug 13, 2019 7:58 am

Hello,

okay, well, another variation on an earlier technique seems to work in these circumstances, i.e.

Code: Select all

    public partial class Form1 : Form
    {
        //max visible labels
        private const Int32 maxDisplayedLabelsForBottomaAxis = 20;
        List<string> _labels = new List<string>();
        int _count;


        public Form1()
        {
            var rnd = new Random();

            InitializeComponent();

            tChart1.Zoomed += TChart1_Zoomed;
            tChart1.UndoneZoom += TChart1_UndoneZoom;
            tChart1.AfterDraw += TChart1_AfterDraw;

            _count = 4000;

            for (int i = 0; i < _count; i++)
            {
                line1.Add(i, rnd.Next(5));
                _labels.Add($"Point NoClue #{rnd.Next()}");

                if (ShouldDrawIndex(i, 0, _count))
                {
                    tChart1.Axes.Bottom.Labels.Items.Add(i, _labels[i]);
                }
            }
        }

        private void TChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            this.Text = $"Number of labels = {tChart1.Axes.Bottom.Labels.Items.Count}";
        }

        private void TChart1_UndoneZoom(object sender, EventArgs e)
        {
            tChart1.Axes.Bottom.Labels.Items.Clear();

            for (int i = 0; i < _count; i++)
            {
                if (ShouldDrawIndex(i, 0, _count))
                {
                    tChart1.Axes.Bottom.Labels.Items.Add(i, _labels[i]);
                }
            }
        }

        private bool ShouldDrawIndex(int index, int firstVisible, int lastVisible)
        {
            if ((lastVisible - firstVisible) > maxDisplayedLabelsForBottomaAxis)
            {
                var drawEvery = ((lastVisible - 1 - firstVisible) / maxDisplayedLabelsForBottomaAxis) + 1;
                return index % drawEvery == 0;
            }

            return true;
        }

        private void TChart1_Zoomed(object sender, EventArgs e)
        {
            tChart1.Axes.Bottom.Labels.Items.Clear();

            var xvalues = line1.XValues.Value.Where((x, i) => i < _count);

            var min = line1.XValues.IndexOf(xvalues.Where(x => x >= tChart1.Axes.Bottom.Minimum).First());
            var max = line1.XValues.IndexOf(xvalues.Where(x => x <= tChart1.Axes.Bottom.Maximum).Last());

            for (int i = min; i <= max; i++)
            {
                if(ShouldDrawIndex(i, min, max))
                {
                    tChart1.Axes.Bottom.Labels.Items.Add(i, _labels[i]);
                }
            }
        }
    }
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: FirstVisibleIndex and LastVisibleIndex while zooming

Post by bairog » Tue Aug 13, 2019 10:07 am

Christopher wrote:
Tue Aug 13, 2019 7:58 am
okay, well, another variation on an earlier technique seems to work in these circumstances, i.e.
So we've finally returned to using Zoomed and UndoneZoom events :) Looks like it's working now, many thanks.

P. S. As I can see this code is in fact manually calculating FirstVisibleIndex and LastVisibleIndex. First time i was surprised by line:

Code: Select all

var xvalues = line1.XValues.Value.Where((x, i) => i < _count);
As I expected line1.XValues.Value to have exactly _count items.
Could you explain please, why that collection has more items despite of the fact that I've added _count points?

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

Re: FirstVisibleIndex and LastVisibleIndex while zooming

Post by Christopher » Tue Aug 13, 2019 10:38 am

bairog wrote:
Tue Aug 13, 2019 10:07 am
Looks like it's working now, many thanks.
You're welcome.
bairog wrote:
Tue Aug 13, 2019 10:07 am
Could you explain please, why that collection has more items despite of the fact that I've added _count points?
The Value field is public but is for internal use. If you are uncomfortable using it, you can use the more standard API calls:

Code: Select all

            List<double> xvalues = new List<double>();

            for (int i = 0; i < _count; i++)
            {
                xvalues.Add(line1.XValues[i]);
            }

            //var xvalues = line1.XValues.Value.Where((x, i) => i < _count);
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

Post Reply