locked
How to zoom in and zoom out the point graph X and Y axis in c# windows application. RRS feed

  • Question

  • I want a chart control to zoom in and zoom out the point graph in c# windows form application
    Monday, November 25, 2019 12:31 PM

All replies

  • Hi varun_bwazz,
    To achieve your requirement, I use the event MouseWheel to zoom in and zoom out the point graph.
    I made a code example and you can refer to it.

    int[] x = { 1, 2, 3 };
    int[] y = { 4, 5, 6 };
    private void Form1_Load(object sender, EventArgs e)
    {
        chart1.Series.Add("My Data");
        for (int i = 0; i < x.Length; i++)
        {
            DataPoint dp = new DataPoint();
            dp.SetValueXY(x[i], y[i]);
            chart1.Series["My Data"].Points.Add(dp);
            chart1.Series["My Data"].ChartType = SeriesChartType.Point;
            chart1.MouseWheel += Chart1_MouseWheel;
            chart1.Focus();
        }
    }
    
    private void Chart1_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta > 0)
        {
    
            Axis ax = chart1.ChartAreas[0].AxisX;
            ax.ScaleView.Size = double.IsNaN(ax.ScaleView.Size) ?
                                (ax.Maximum - ax.Minimum) / 2 : ax.ScaleView.Size /= 2;
            Axis ay = chart1.ChartAreas[0].AxisY;
            ay.ScaleView.Size = double.IsNaN(ay.ScaleView.Size) ?
                                (ay.Maximum - ay.Minimum) / 2 : ay.ScaleView.Size /= 2;
        }
        else {
    
            Axis ax = chart1.ChartAreas[0].AxisX;
            ax.ScaleView.Size = double.IsNaN(ax.ScaleView.Size) ?
                                ax.Maximum : ax.ScaleView.Size *= 2;
            if (ax.ScaleView.Size > ax.Maximum - ax.Minimum)
            {
                ax.ScaleView.Size = ax.Maximum;
                ax.ScaleView.Position = 0;
            }
            Axis ay = chart1.ChartAreas[0].AxisY;
            ay.ScaleView.Size = double.IsNaN(ay.ScaleView.Size) ?
                                ay.Maximum : ay.ScaleView.Size *= 2;
            if (ay.ScaleView.Size > ay.Maximum - ay.Minimum)
            {
                ay.ScaleView.Size = ay.Maximum;
                ay.ScaleView.Position = 0;
            }
        }
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 26, 2019 6:02 AM