窗体的Designer.cs文件下的InitializeComponent()函数中添加: this.chart1.MouseWheel +=System.Windows.Forms.MouseEventHandler(this.chart1_MouseWheel); 

chart2 的鼠标滚轮事件:

 this.chart2.Click += new System.EventHandler(this.chart2_Click);
            this.chart2.DoubleClick += new System.EventHandler(this.chart2_DoubleClick);
            this.chart2.MouseClick += new System.Windows.Forms.MouseEventHandler(this.chart2_MouseClick);
            this.chart2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.chart2_MouseDown);
            this.chart2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart2_MouseMove);
            this.chart2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.chart2_MouseUp);
            this.chart2.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.chart2_MouseWheel); 

    private Series dragSeries = null; // 拖拽的Series对象
        private int dragIndex = -1; // 拖拽的Series对象的数据点索引
        private int dragOffset = 0; // 鼠标点击位置相对于数据点的Y值偏移量
                                    // 定义两个全局变量
        public bool isMouseDown = false;
        public int lastMoveX = 0, lastMoveY=0; //用于记录鼠标上次移动的点,用于判断是左移还是右移
        private void chart1_MouseDown(object sender, MouseEventArgs e)
        {
            HitTestResult result = chart1.HitTest(e.X, e.Y);
            if (result.ChartElementType == ChartElementType.DataPoint)
            {
                dragSeries = result.Series;
                dragIndex = result.PointIndex;
                dragOffset = (int)(dragSeries.Points[dragIndex].YValues[0] - chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y));
            }
            else
            {
                dragSeries = null;
            }
            isMouseDown = true;
            lastMoveX = 0; lastMoveY = 0;
        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {

            try
            {
                var areaC = chart1.ChartAreas[0];

                double axisxValue = areaC.AxisX.PixelPositionToValue(e.X);
                double axisyValue = areaC.AxisY.PixelPositionToValue(e.Y);
                textBox_chartEdit.Text = string.Format("{0:F0},{1:F0}", axisxValue, axisyValue);//
                areaC.CursorX.IsUserEnabled = true;
                areaC.CursorY.IsUserEnabled = true;//十字光标
                areaC.CursorX.SetCursorPixelPosition(new PointF(e.X, e.Y), true);
                areaC.CursorY.SetCursorPixelPosition(new PointF(e.X, e.Y), true);
            }
            catch { }

            if (dragSeries != null && dragIndex >= 0)
            {
                double yValue = 0;
                try
                {
                    yValue = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y + dragOffset);
                }
                catch (Exception err)
                {
                    // if (serialPort1.IsOpen == true)    serialPort1.Close();   
                    //MessageBox.Show(err.ToString(), "系统提示"); //不能重复打开
                }
                dragSeries.Points[dragIndex].YValues[0] = Math.Round(yValue, 2);//保留2位小数
                chart1.Invalidate();

                if (dragIndex >= 0 && dragIndex < List_temprature.Count) // List_ib.Count)
                {

                    float y = (float)Math.Round(yValue, 2);//保留2位小数
                    List_temprature[dragIndex] = y;// (float)yValue;// List_ib[dragIndex] = (float)yValue;
                    temprature_curve[dragIndex] = y;// (float)yValue;
                }
            }
            else
            {

                HitTestResult Result = new HitTestResult();
                Result = chart1.HitTest(e.X, e.Y);

                if (Result.Series != null && Result.Object != null)
                {
                    // 获取当前焦点x轴的值
                    //string xValue = ObjectUtil.GetPropertyValue(Result.Object, "AxisLabel").ToString();
                    //// 获取当前焦点所属区域名称
                    //string areaName = ObjectUtil.GetPropertyValue(Result.Object, "LegendText").ToString();
                    // 获取当前焦点y轴的值
                    if (Result.PointIndex < 0 || Result.Series.Points.Count <= Result.PointIndex)
                        return; //Result.PointIndex = 0;// https://blog.csdn.net/tingchenchen/article/details/129791012

                    double yValue = Result.Series.Points[Result.PointIndex].YValues[0];
                    // 鼠标经过时label显示
                    //label_chart.Text = "时间:" + xValue + "\n" + areaName + ":" + yValue + "ug/m^3";

                    label_chart.Location = new Point(e.X, e.Y - 20);
                    var area = chart1.ChartAreas[0];
                    double xxValue = area.AxisX.PixelPositionToValue(e.X);
                    double yyValue = area.AxisY.PixelPositionToValue(e.Y);
                    label_chart.Text = string.Format("{0:F0},{1:F1}", xxValue, yyValue);
                    chart1.Series[0].ToolTip = "X:#VALX\nY:#VALY";
                    label_chart.Visible = true;

                }
                else
                {
                    // 鼠标离开时label隐藏

                    label_chart.Visible = false;

                    //https://blog.csdn.net/baidu_41651935/article/details/118943228
                   
                    
                }

                if (isMouseDown)//鼠标拖动横坐标移动
                {
                    chartArea = chart1.ChartAreas[0];
                    MIN_Y = (int)chartArea.AxisY.Minimum;
                    MAX_Y = (int)chartArea.AxisY.Maximum;//
                    // 设置不显示chart自带的滚动条
                    chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = false;
                    chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = false;
                    // 可更改(交换)如下加减1或if条件来设置鼠标移动时曲线移动方向
                    int xMove = Math.Abs(e.X - lastMoveX);
                    int yMove = Math.Abs(e.Y - lastMoveY);

                    if (xMove > yMove)
                    {
                        //if (lastMove > 10 && e.X - lastMove > 0)
                        if (lastMoveX > 10 && e.X - lastMoveX > 0)
                        {
                            MAX_X+=xMove;   //MAX_X++; MIN_X++;
                            chart1.ChartAreas[0].AxisX.ScaleView.Position += xMove ;  // 每次移动1
                        }
                        else if (lastMoveX > 10 && e.X - lastMoveX < 0)
                        {
                             //MIN_X+=xMove;   //

                             MAX_X+=xMove;   //MAX_X--; MIN_X--;
                            chart1.ChartAreas[0].AxisX.ScaleView.Position -= xMove ; // 每次移动1
                        }
                    }
                    else
                    {
                        if (lastMoveY > 10 && e.Y - lastMoveY > 0)
                        {
                              MAX_Y+=yMove;   //MAX_Y++; MIN_Y++;
                            chart1.ChartAreas[0].AxisY.ScaleView.Position += yMove ;  // 每次移动1
                        }
                        else if (lastMoveY > 10 && e.Y - lastMoveY < 0)
                        {
                            MAX_Y-=yMove;   //MAX_Y--; MIN_Y--;
                            chart1.ChartAreas[0].AxisY.ScaleView.Position -= yMove ; // 每次移动1
                        }
                    }
                    lastMoveX = e.X;
                    lastMoveY = e.Y;

                    //chartArea.AxisX.Maximum = Math.Round(MAX_X);
                    // chartArea.AxisY.Maximum = Math.Round(MAX_Y);
                }
            }
        }
  
        private void chart1_MouseUp(object sender, MouseEventArgs e)
        {
            dragSeries = null;
            dragIndex = -1;
            isMouseDown = false;

        }

        private void chart1_MouseWheel(object sender, MouseEventArgs e)
        {
            //当e.Delta > 0时鼠标滚轮是向上滚动,e.Delta < 0时鼠标滚轮向下滚动
            int Ymax = (int)chart1.ChartAreas[0].AxisY.Maximum;
            chartArea =chart1.ChartAreas[0];
            MIN_Y =(int) chartArea.AxisY.Minimum ;
            MAX_Y = (int)chartArea.AxisY.Maximum;
            if (e.Delta > 0)//滚轮向上
            {
                ZoomIn(); //放大
                          //MessageBox.Show("鼠标向上滑动");
            }
            else
            {
                ZoomOut();//缩小
                          //MessageBox.Show("鼠标向下滑动");
            } 
        }
       public void ZoomIn()
        {
            MAX_Y += 0.1f * MAX_Y;
            if(MIN_Y>1)  MIN_Y -= 0.1f * MIN_Y;
            //chart1.ChartAreas[0].AxisY.Maximum = Math.Round(MAX_Y);
            chartArea.AxisY.Maximum = Math.Round(MAX_Y);

        }

        //沿中心缩放:缩小
        public void ZoomOut()
        {
            if(MAX_Y*0.9f > MIN_Y+1) MAX_Y -= 0.1f * MAX_Y;
            if (MIN_Y * 1.1f+1 < MAX_Y)  MIN_Y += 0.1f * MIN_Y+1;
            //chart1.ChartAreas[0].AxisY.Maximum = Math.Round(MAX_Y);
            chartArea.AxisY.Maximum = Math.Round(MAX_Y);
        }

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐