Aditya Choudhary
Aditya Choudhary

Reputation: 63

ZOOM Out function in MS Chart c#

I have the following Winforms code:

void chart1_MouseWheel(object sender, MouseEventArgs e)
        {
            double xMin = chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
            double xMax = chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum;
            if (e.Delta < 0)
            {   //chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
                //chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
            }

            if (e.Delta > 0)
            {
                double posXStart = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin)/2 ;
                double posXFinish = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin)/2;
                chart1.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish);
            }
        }

The Zoom In function is working but when e.Delta < 0, I need the Zoom Out function based on the above code.

Upvotes: 3

Views: 8662

Answers (2)

Ben
Ben

Reputation: 3391

As Baddack points out you can use the ZoomReset(1) method to go back one step in the zoom history. However, if you use ZoomReset(0) you can reset all zoom operations without having to turn the history saving off.

Upvotes: 0

Baddack
Baddack

Reputation: 2053

try

chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(1);
chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset(1);

If you set the saveState to true when you are zooming, the ZoomReset(1) will go back to the last zoom state. Or if you set the saveState to false, the ZoomReset(1) will just zoom all the way back out. Here is my code, I do mine with the mouse click, but I'm sure you can get it working with scroll wheel:

    private void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
    {
        double startX, endX, startY, endY;

        if (chart1.ChartAreas[0].CursorX.SelectionStart > chart1.ChartAreas[0].CursorX.SelectionEnd)
        {
            startX = chart1.ChartAreas[0].CursorX.SelectionEnd;
            endX = chart1.ChartAreas[0].CursorX.SelectionStart;
        }
        else
        {
            startX = chart1.ChartAreas[0].CursorX.SelectionStart;
            endX = chart1.ChartAreas[0].CursorX.SelectionEnd;
        }
        if (chart1.ChartAreas[0].CursorY.SelectionStart > chart1.ChartAreas[0].CursorY.SelectionEnd)
        {
            endY = chart1.ChartAreas[0].CursorY.SelectionStart;
            startY = chart1.ChartAreas[0].CursorY.SelectionEnd;
        }
        else
        {
            startY = chart1.ChartAreas[0].CursorY.SelectionStart;
            endY = chart1.ChartAreas[0].CursorY.SelectionEnd;
        }

        if (startX == endX && startY == endY)
        {
            return;
        }

        chart1.ChartAreas[0].AxisX.ScaleView.Zoom(startX, (endX - startX), DateTimeIntervalType.Auto, true);
        chart1.ChartAreas[0].AxisY.ScaleView.Zoom(startY, (endY - startY), DateTimeIntervalType.Auto, true);
    }

Upvotes: 4

Related Questions