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();
        }
  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薪薪代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值