Zoom vs Panning

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
rossmc
Newbie
Newbie
Posts: 57
Joined: Wed Apr 08, 2009 12:00 am

Zoom vs Panning

Post by rossmc » Mon Sep 28, 2009 12:26 pm

Hi

My chart is setup to allow Zoom using left mouse button.
I want user to be able to pan horizontally with the left mouse button as well. Obviously this is a problem because two different features cannot share the same left click.

I don't want to use KeyShift for either feature. My proposed 'solution' was:
1) Have Zoom.Allow = TRUE
2) If user clicks and holds left mouse down on a series, and then moves mouse (while holding button in), the chart will pan left and right.

I tried the following (the idea came from a link in another post for moving a series using mouse):
On ClickSeries event I set Zoom.Allow = False and Panning.Allow = ScrollModes.Horizontal
On MouseUp event I set the above two back; Zoom.Allow = True and Panning.Allow = ScrollModes.None

That doesn't appear to work unfortunately. Do you have any ideas for this?

Note (bug?): If I allow Zooming and Panning both with Left Mouse button, with Panning.KeyShift = Keys.Shift panning doesn't work but zooming does.

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

Re: Zoom vs Panning

Post by Yeray » Tue Sep 29, 2009 10:59 am

Hi rossmc,

The problem is that activating the panning mode and deactivating the zoom mode at OnClickSeries event it's too late, and even at OnMouseDown.
On the other hand I think you can achieve that zooming and panning (scrolling) manually. Take a look at the following example, it seems to work fine here:

Code: Select all

        public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }        
        
        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            bar1.FillSampleValues(6);

            tChart1.Zoom.Allow = false;
            tChart1.Panning.Allow = Steema.TeeChart.ScrollModes.None;

            tChart1.MouseDown += new MouseEventHandler(tChart1_MouseDown);
            tChart1.MouseUp += new MouseEventHandler(tChart1_MouseUp);
            tChart1.MouseMove += new MouseEventHandler(tChart1_MouseMove);
            tChart1.AfterDraw += new Steema.TeeChart.PaintChartEventHandler(tChart1_AfterDraw);
        }

        private int MouseDownX, MouseDownY, MouseActX, MouseActY;
        private double PreviousXMin, PreviousXMax;
        private bool Zooming, Panning;

        void tChart1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MouseDownX = e.X;
                MouseDownY = e.Y;
                Zooming = true;
                for (int i = 0; i < tChart1.Series.Count; i++)
                {
                    if (tChart1[i].Clicked(e.X, e.Y) != -1) Zooming = false;
                }
                Panning = !Zooming;

                if (Panning)
                {
                    PreviousXMin = tChart1.Axes.Bottom.Minimum;
                    PreviousXMax = tChart1.Axes.Bottom.Maximum;
                }
            }
        }

        void tChart1_MouseUp(object sender, MouseEventArgs e)
        {
            if (Zooming)
            {
                if ((e.X < MouseDownX) && (e.Y < MouseDownY))
                {
                    tChart1.Axes.Left.Automatic = true;
                    tChart1.Axes.Bottom.Automatic = true;
                    tChart1.Invalidate();
                }
                else
                {
                    tChart1.Axes.Left.SetMinMax(tChart1.Axes.Left.CalcPosPoint(e.Y), tChart1.Axes.Left.CalcPosPoint(MouseDownY));
                    tChart1.Axes.Bottom.SetMinMax(tChart1.Axes.Bottom.CalcPosPoint(MouseDownX), tChart1.Axes.Bottom.CalcPosPoint(e.X));
                }
            }
            Zooming = false;
            Panning = false;
        }

        void tChart1_MouseMove(object sender, MouseEventArgs e)
        {
            if (Zooming)
            {
                MouseActX = e.X;
                MouseActY = e.Y;
                tChart1.Invalidate();
            }
            else if (Panning)
            {
                tChart1.Axes.Bottom.SetMinMax(tChart1.Axes.Bottom.CalcPosPoint(tChart1.Axes.Bottom.CalcPosValue(PreviousXMin) + e.X - MouseDownX), tChart1.Axes.Bottom.CalcPosPoint(tChart1.Axes.Bottom.CalcPosValue(PreviousXMax) + e.X - MouseDownX));
            }
        }

        void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            if (Zooming)
            {
                tChart1.Graphics3D.Pen.Style = System.Drawing.Drawing2D.DashStyle.Dash;
                tChart1.Graphics3D.BackColor = Color.Empty;
                tChart1.Graphics3D.Rectangle(MouseDownX, MouseDownY, MouseActX, MouseActY);
            }
        }
    }
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

rossmc
Newbie
Newbie
Posts: 57
Joined: Wed Apr 08, 2009 12:00 am

Re: Zoom vs Panning

Post by rossmc » Thu Oct 08, 2009 12:40 pm

Hi Yeray

Even though I'm now not going to do this anymore, I discovered how to make this work quite easily. So in case a future VB reader wants to know how; When plotting the line I add event handlers for line.MouseEnter and line.MouseLeave.

Then:
In mouse enter event I simply set .Zoom.Allow = False and .Panning.Allow = ScrollModes.Horizontal
In mouse leave event I set .Zoom.Allow = True and .Panning.Allow = ScrollModes.None

That works.

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

Re: Zoom vs Panning

Post by Yeray » Fri Oct 09, 2009 12:01 pm

Hi rossmc,

Thanks for the feedback!
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

Post Reply