C# winform绘制Chart曲线图滚轮放大缩小方法,以及鼠标滚轮无效解决办法

C# winform绘制Chart曲线图滚轮放大缩小方法,以及鼠标滚轮无效解决办法

滚动方法如下

private void chart1_MouseWheel(object sender, MouseEventArgs e)
        {
    
    
            //将鼠标位置转换到chart控件坐标系中
            double mouse_x = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
            double mouse_y = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
            //限制鼠标位置在chart的视图区内
            if (mouse_x < chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum
                && mouse_x > chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum
                && mouse_y < chart1.ChartAreas[0].AxisY.ScaleView.ViewMaximum
                && mouse_y > chart1.ChartAreas[0].AxisY.ScaleView.ViewMinimum)
            {
    
    
                double x_new_size = 0;//X轴的新ScaleView.Size值
                if (e.Delta > 0)//滚轮向上放大
                {
    
    
                    if (chart1.ChartAreas[0].AxisX.ScaleView.Size > 0)//设置最大放大值
                    {
    
    
                        x_new_size = chart1.ChartAreas[0].AxisX.ScaleView.Size * 0.95;//放大5%
                    }
                }
                else//滚轮向下缩小
                {
    
    
                    if (chart1.ChartAreas[0].AxisX.ScaleView.Size < (chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum))//不能无限缩小,缩小的极限就是显示全图
                    {
    
    
                        x_new_size = chart1.ChartAreas[0].AxisX.ScaleView.Size * 1.05;//缩小5%
                        if (x_new_size >= (chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum))//最大的Size就是整个Y轴
                        {
    
    
                            x_new_size = chart1.ChartAreas[0].AxisX.Maximum - chart1.ChartAreas[0].AxisX.Minimum;
                        }
                    }
                }
                if (x_new_size > 0)
                {
    
    
                    double x_mousenew;
                    double x_mouseold = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
                    chart1.ChartAreas[0].AxisX.ScaleView.Size = x_new_size;
                    x_mousenew = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
                    double newposition = x_mouseold - x_mousenew;
                    chart1.ChartAreas[0].AxisX.ScaleView.Position += newposition;
                    //限制视图在最大最小值之间,防止视图溢出
                    if (chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum < chart1.ChartAreas[0].AxisX.Minimum)
                        chart1.ChartAreas[0].AxisX.ScaleView.Position = chart1.ChartAreas[0].AxisX.Minimum;
                    if (chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum > chart1.ChartAreas[0].AxisX.Maximum)
                        chart1.ChartAreas[0].AxisX.ScaleView.Position = chart1.ChartAreas[0].AxisX.Maximum - (chart1.ChartAreas[0].AxisX.ScaleView.ViewMaximum - chart1.ChartAreas[0].AxisX.ScaleView.ViewMinimum);
                }
            }
        }

解决办法:需要关注图表控件,以便触发鼠标滚轮事件。可以在鼠标进入控件时设置焦点,并在鼠标离开时将焦点返回给父控件。
在这里插入图片描述

private void chart1_MouseLeave(object sender, EventArgs e)
        {
    
    
            if (chart1.Focused)
                chart1.Parent.Focus();
        }

        private void chart1_MouseEnter(object sender, EventArgs e)
        {
    
    
            if (!chart1.Focused)
                chart1.Focus();
        }

猜你喜欢

转载自blog.csdn.net/m0_65636467/article/details/135355150