Xichan
Xichan

Reputation: 95

ZedGraph how to detect zoom in or out?

I have been using ZedGraph for a while but I was just frustrated after hours of search I couldn't find how to distinguish detect if the zoom event was "zoom in" or "zoom out" or "zoom all out?

Actually I'mm making my own version of zoom in and out for sampling specific data in each case and I really need to find a way to detect the type of zoom event.

If any one has an idea, please help me out.

Thanks.

Upvotes: 2

Views: 4402

Answers (3)

user3667377
user3667377

Reputation: 19

I found a solution today. I took out the GraphPane variable to be global variable in form1 and also added a flag of my own:

GraphPane myPane;
bool inv = false;

Then in the constructor i register to the zoom event of the ZedGraph control:

zedGraphControl1.ZoomEvent += new ZedGraphControl.ZoomEventHandler(MyZoomEvent);

This is the zoom event code:

private void MyZoomEvent(ZedGraphControl control, ZoomState oldState,

                    ZoomState newState)
        {
            inv = myPane.IsZoomed;
        }

Now i have CreateGraph method in my case i call it from a timer tick event every second in this method i check if my flag is false or true:

private void CreateGraph(ZedGraphControl z1)
        {
            myPane = z1.GraphPane;
            if (myPane != null)
            {
                myPane.CurveList.Clear();
                myPane.GraphObjList.Clear();
                myPane.Title.Text = "Histograms";
                myPane.XAxis.Title.Text = "Bar Number";
                myPane.YAxis.Title.Text = "Value";

                myPane.XAxis.Scale.MaxAuto = false;
                myPane.XAxis.Scale.MinAuto = false;
                myPane.YAxis.Scale.MaxAuto = false;
                myPane.YAxis.Scale.MinAuto = false;

                myPane.XAxis.Scale.Min = 0;
                myPane.XAxis.Scale.Max = 255;
                myPane.YAxis.Scale.Min = 0;
                myPane.YAxis.Scale.Max = 255;

                PointPairList list = new PointPairList();


                for (int i = 0; i < Core.graphValues.Count; i++)
                {
                    double x = (double)i;
                    double y = (double)Core.graphValues[i];
                    double z = 0;
                    list.Add(x, y, z);
                }
                LineItem myCurve = myPane.AddCurve("Top 1000 Averages",
               list, Color.Red, SymbolType.None);

                if (inv != true)
                {
                    z1.Invalidate();
                }
                z1.AxisChange();
            }

        }

For me it's working perfect. I tested it in any of the zoom's options all zooms out and in out and it's working.

Upvotes: 0

Xichan
Xichan

Reputation: 95

Earlier I was detecting the zoom in or out by checking if zedGraphControl1.GraphPane.XAxis.Scale.Max value is less then the number of points in the chart. If its less then zoom in was intended and vice versa. However that was not fool proof as sometimes the zoom out was detected as zoom in.

After much search all I found was this: http://sourceforge.net/p/zedgraph/discussion/392232/thread/d33cfd8c/

So using a similar technique I improved the detection by calculating the area of the selected zoom area when the user zooms in using left-click and drag (using new values of zedGraphControl1.GraphPane.XAxis.Scale.Max and zedGraphControl1.GraphPane.XAxis.Scale.Min ) and comparing this area with the area of the whole chart (using previous values of zedGraphControl1.GraphPane.XAxis.Scale.Max and zedGraphControl1.GraphPane.XAxis.Scale.Min).

Like here:

private void zedGraphControl1_ZoomEvent(ZedGraphControl sender, ZoomState oldState, ZoomState newState)
{

    if (((lastYAxisMax - lastYAxisMin) * (lastXAxisMax - lastXAxisMin)) > ((sender.GraphPane.XAxis.Scale.Max - sender.GraphPane.XAxis.Scale.Min) * (sender.GraphPane.YAxis.Scale.Max - sender.GraphPane.YAxis.Scale.Min)))
    {
        zoomIn = true;
        zoomOut = false;
    }
    else
    {
        zoomIn = false;
        zoomOut = true;
    }
}

That's really disappointing to know that there is no simple way to do that but so far this is working fine because users selected area cannot be equal or greater than the chart area.

Hope this helps someone.

Upvotes: 4

SanVEE
SanVEE

Reputation: 2060

According to my understanding there is no specific option to find Zoom In/Out or Zoom all Out, but there is a work around for this.

Currently it will only say what state it is….

private void zedGraphControl1_ZoomEvent(ZedGraphControl sender, ZoomState oldState, ZoomState newState)
    {
       if(newState.Type == ZoomState.StateType.Pan ){ }
       else if (newState.Type == ZoomState.StateType.Zoom){ }
       else if (newState.Type == ZoomState.StateType.Scroll){ }
       else if (newState.Type == ZoomState.StateType.WheelZoom){ }
    }

If you add an event handler for Context Menu Builder (zedGraphControl1_ContextMenuBuilder) and add the following code aswell.

bool isSubscriptionEnabled = false;

    private void zedGraphControl1_ContextMenuBuilder(ZedGraphControl sender, ContextMenuStrip menuStrip, Point mousePt, ZedGraphControl.ContextMenuObjectState objState)
    {
        // Remove default options
        menuStrip.Items.RemoveByKey("unzoom");
        menuStrip.Items.RemoveByKey("undo_all");

        // custom context menu items
        ToolStripMenuItem unZoom_toolStripMenuItem = new ToolStripMenuItem();
        ToolStripMenuItem unZoomAll_toolStripMenuItem = new ToolStripMenuItem();

        // 1.Un Zoom
        unZoom_toolStripMenuItem.Name = "unZoom";
        unZoom_toolStripMenuItem.Tag = "unzoom";
        unZoom_toolStripMenuItem.Text = "Un Zoom*";

        // 2.Undo All Zoom/Pan
        unZoomAll_toolStripMenuItem.Name = "undoZoomAll";
        unZoomAll_toolStripMenuItem.Tag = "undozoomall";
        unZoomAll_toolStripMenuItem.Text = "Undo All Zoom/Pan*";

        // Add the above 2 menu strip items
        menuStrip.Items.Insert(4, unZoom_toolStripMenuItem);
        menuStrip.Items.Insert(5, unZoomAll_toolStripMenuItem);

        if (!isSubscriptionEnabled)
        {
            // The following event handler help us to notify user click event on context menu
            menuStrip.ItemClicked += new ToolStripItemClickedEventHandler(menuStrip_ItemClicked);
            isSubscriptionEnabled = true;
        }
    }

    private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
        if (e.ClickedItem.ToString() == "Un Zoom*")
        {
            // Add your custom task
            MessageBox.Show("Un Zoom");

        }
        else if (e.ClickedItem.ToString() == "Undo All Zoom/Pan*")
        {
            // Add your custom task
        }            
    }

Hope that helps.

Upvotes: 0

Related Questions