How to enable Zooming but fixed 0 to the center

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
Andres
Posts: 8
Joined: Mon May 22, 2017 9:40 pm

How to enable Zooming but fixed 0 to the center

Post by Andres » Mon May 29, 2017 11:14 am

]Hi there,

I am having some issues enabling zooming.

My expected behavior is :
  • Enable Zoom with the Mouse Wheel
  • Keep the 0 value position-fixed. (for the YAxis in the middle).

Code: Select all

                yAxis.SetRange(-5000, 5000);
                yAxis.ZoomingEnabled = true;
                yAxis.PanningEnabled = false;

The problem is, that the position it's not fixed, so when I use my mouse wheel the range increase/decrease but the zero-positions changes too.

Please see this example. Range used to be (-5k, 5k) . I placed my mouse on the bottom , and the range got increased (-5k, 11k) but zero was moved.


I think it must be some property but I can't find it.

Thanks,
Andrés
Attachments
range.png
range.png (59.6 KiB) Viewed 6685 times

ArctionKestutis
Posts: 553
Joined: Mon Mar 14, 2016 9:22 am

Re: How to enable Zooming but fixed 0 to the center

Post by ArctionKestutis » Mon May 29, 2017 12:01 pm

Hello Andrés,

If zooming done with mouse wheel, the zoom center is the position of mouse cursor.
If you would like center zooming with mouse wheel otherwise, you could use ZoomByFactor method and MouseWheel event. For example, centered at {x=10, y=50}

Code: Select all

                _chart.MouseWheel += _chart_MouseWheel;
                _chart.ViewXY.ZoomPanOptions.MouseWheelZooming = MouseWheelZooming.Off;

            void _chart_MouseWheel(object sender, MouseEventArgs e)
            {
                AxisX xAxis = _chart.ViewXY.XAxes[0];
                AxisY yAxis = _chart.ViewXY.YAxes[0];
                PointInt pCenter;
                double dZoomFactor;

                // convert Axis values to DIP units
                pCenter.X = (int)xAxis.ValueToCoord(10, true);
                pCenter.Y = (int)yAxis.ValueToCoord(50, true);

                // set ZoomFactor
                if (e.Delta > 0)
                    dZoomFactor = 2;
                else
                    dZoomFactor = 0.5;

                // zoom with a center point and a zoom factor
                _chart.ViewXY.ZoomByFactor(pCenter, dZoomFactor, true, true);
            }
Even more straightforward and accurate is to use Axis.SetRange() method instead of ZoomByFactor.

Hope this helps.
All the best.
Last edited by ArctionKestutis on Tue May 30, 2017 6:33 am, edited 1 time in total.

Andres
Posts: 8
Joined: Mon May 22, 2017 9:40 pm

Re: How to enable Zooming but fixed 0 to the center

Post by Andres » Mon May 29, 2017 12:44 pm

Thanks for the answer.

I am using WPF, and the Event looks like this:

Code: Select all

_chart.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(_chart_MouseWheel);
But I got an error saying that MouseEventArgs doens't containt a definition for e.Delta.


Should I implement this differently on WPF?

ArctionKestutis
Posts: 553
Joined: Mon Mar 14, 2016 9:22 am

Re: How to enable Zooming but fixed 0 to the center

Post by ArctionKestutis » Tue May 30, 2017 6:44 am

Hi Andrés,


For WPF use should subscribe to _chart.PreviewMouseWheel event.
And _chart_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e) method works just fine - no problem with e.Delta.

Please note, that I modify 1st argument for ZoomByFactor(), which actually in DIPs untis. Therefore, ValueToCoord() call should be changed accordingly (i.e. second argument is TRUE).

If you reaching PreviewMouseWheel event handler, but e.Delta is not defined, when it must be some version differences (which I doubt). Please report which LightningChart version you are using and is it non-bindable?

All the best.

Post Reply