This project is read-only.

How to restrict zoom on the graph

Apr 22, 2010 at 9:47 PM

I have a simple graph in the xaml file as follows

 <d3:ChartPlotter Name="myPlotter">    

  </d3:ChartPlotter>


When I run the application, I can zoom in/out  to any extent and I want to restrict the zoom feature. 

I am looking for the code/method than can restrict me from zooming the graph too much.  I tried looking around and reading the posts but was not able to figure out. If somebody can help me with the sample code, it will be much appreciated.

Thanks

 

 

Apr 23, 2010 at 4:10 PM

Hi dolph,

You will want to use ViewportRestrictions. I think in your case, you want to set a MinimalSizeRestriction and a MaximalSizeRestriction.

_chartPlotter.Viewport.Restrictions.Add(new MinimalSizeRestriction(new DataRect());
_chartPlotter.Viewport.Restrictions.Add(new MaximalSizeRestriction(new DataRect());

With the DataRects containing your zoom restrictions. Let me know if this helps.

Apr 23, 2010 at 4:19 PM

Thank you for the response. I am not sure if I understand the solution you provided.

With my My default code as above, my X axis is from 0 to 1 and my Y axis is from 0 to 1.   Now if I have to control zoom such that my x axis at the max  is 50 and min is 0, and same for Y axis, how do I provide this restriction in the code above?

Right now with the solution you provided, I don't know how to achieve the above.

Apr 23, 2010 at 4:33 PM

You can create your own restriction as follows:

 

public class HWZoomRestriction : ViewportRestriction
    {
        private double? _minW = null;
        private double? _minH = null;

        public HWZoomRestriction(double? width, double? height)
            : base()
        {
            _minW = width;
            _minH = height;
        }

        public double? MinWidth
        {
            get { return _minW; }
            set
            {
                if (_minW != value)
                {
                    _minW = value;
                    RaiseChanged();
                }
            }
        }

        public double? MinHeight
        {
            get { return _minH; }
            set
            {
                if (_minH != value)
                {
                    _minH = value;
                    RaiseChanged();
                }
            }
        }

        public override DataRect Apply(DataRect previousDataRect, DataRect proposedDataRect, Viewport2D viewport)
        {
            DataRect newRect = new DataRect();
            if (MinWidth.HasValue && proposedDataRect.Width < MinWidth.Value)
            {
                newRect.XMin = previousDataRect.XMin;
                newRect.Width = MinWidth.Value;
            }
            else
            {
                newRect.XMin = proposedDataRect.XMin;
                newRect.Width = proposedDataRect.Width;
            }
            if (MinHeight.HasValue && proposedDataRect.Height < MinHeight.Value)
            {
                newRect.YMin = previousDataRect.YMin;
                newRect.Height = MinHeight.Value;
            }
            else
            {
                newRect.YMin = proposedDataRect.YMin;
                newRect.Height = proposedDataRect.Height;
            }
            return newRect;
        }
    }

 

And then add that restriction to the Viewport:

_chartPlotter.Viewport.Restrictions.Add(
new HWZoomRestriction(minWidth, minHeight));

Apr 23, 2010 at 5:27 PM
Edited Apr 23, 2010 at 5:27 PM

Hello everybody!

 

I want to inform you that in our internal code we have renamed Restrictions to Constraints, so please be ready to do the same :)

 

Best regards,

Mikhail.

Apr 23, 2010 at 6:43 PM

sascharu, thanks for your response and code, but it dosen't restrict me from zooming.  It only changed the value of the X and Y axis for me (when I passed minWidth and minHeight as 10, the X axis went from 1 to 10 and Y axis went from 1 to 10 and I could still zoom in and out).

 

Mikhail, thanks for the update but I would also appreciate an answer for my question if possible. Why is it so hard to restrict zooming on a graph? Is this not a very basic feature to restrict the amount of zoom?

Apr 23, 2010 at 7:21 PM

If you set your Viewport.Domain to the same constraints as the HWZoomRestriction, then the control would not zoom. 

 

_chartPlotter.Viewport.Domain = new DataRect(x, y, minWidth, minHeight);
_chartPlotter.Viewport.Restrictions.Add(new HWZoomRestriction(minWidth, minHeight));

 

If you don't have the Viewport.Domain set, then you can zoom out. If you don't have Restrictions/Constraints set, then you can zoom in.

Hope this helps.

 

Sasha.

Jun 17, 2010 at 1:40 PM

Is it possible to make Viewport.Domain a dependency property so I can bind my rectangle to it in xaml? I have tried with no luck. Please help!! Thanks.