Enabling mouse wheel zooming in a Microsoft Chart Control
Asked Answered
M

2

7

how to enable zooming in Microsoft chart control by using Mouse wheel

I have the below code, i need to know how to make this event? in which class it is..

private void chData_MouseWheel(object sender, MouseEventArgs e)
{
    try
    {
        if (e.Delta < 0)
        {
            chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset();
            chart1.ChartAreas[0].AxisY.ScaleView.ZoomReset();
        }

        if (e.Delta > 0)
        {
            double xMin = chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
            double xMax = chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum;
            double yMin = chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum;
            double yMax = chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum;

            double posXStart = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin) / 4;
            double posXFinish = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin) / 4;
            double posYStart = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin) / 4;
            double posYFinish = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin) / 4;

            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish);
            chart1.ChartAreas[0].AxisY.ScaleView.Zoom(posYStart, posYFinish);
        }
    }
    catch { }            
}
Maskanonge answered 28/5, 2013 at 8:13 Comment(0)
S
5

I think the above answer should be,

chData.MouseWheel += new MouseEventHandler(chData_MouseWheel);

But according to what I found out, chart's mouse-wheel doesn't work as long as you don't set focus on the chart control in your code. So I used mouse-enter of the chart control to set focus to the chart and mouse leave event of the chart control to set the control back to its parent.

So you need to add below lines to your code, bind the mouse leave and mouse enter events of the chart control correspondingly plus add the above line too.

    private void chartTracking_MouseEnter(object sender, EventArgs e)
    {
        this.chartTracking.Focus();
    }

    private void chartTracking_MouseLeave(object sender, EventArgs e)
    {
        this.chartTracking.Parent.Focus();
    }
Schoolgirl answered 11/7, 2014 at 23:17 Comment(0)
S
3

What you have is an handler method for the MouseWheel event. You need to attach your handler method to the MouseWheel event for the chart control. From the method signature, I assume that your chart control is named chData, so you could use the following code in your form's constructor:

chData.MouseWheel += new EventHandler(chData_MouseWheel);

Of course, you could also associate the handler with the event at design time. To do that, use the Properties Window and click the lightning bolt in the toolbar to switch to the "Events" view. Then find the MouseWheel event, click the drop-down arrow, and select your handler method's signature. This will cause the designer to write the above code into the code-behind file for your form.

Aside from that, there's a giant red flag in your code: an empty catch block. If you aren't handling an exception or doing anything with it, then you should not be catching it. This isn't Pokemon, there's no reward for catching them all.

Steinke answered 22/7, 2013 at 12:47 Comment(1)
4 years later, still a laugh to be had.\Ximenes

© 2022 - 2024 — McMap. All rights reserved.