C# 如何使用鼠标滚轮缩放Windows窗体图表?

C# 如何使用鼠标滚轮缩放Windows窗体图表?,c#,.net,winforms,C#,.net,Winforms,我正在使用windows窗体图表控件,这是滚轮鼠标事件: void chart1_MouseWheel(object sender, MouseEventArgs e) { if (e.Delta < 0) { chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(); chart1.ChartAreas[0].AxisY.

我正在使用windows窗体图表控件,这是滚轮鼠标事件:

void chart1_MouseWheel(object sender, MouseEventArgs e)
        {
            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);
            }
        }

如何在滚轮鼠标事件中同时注意绘制事件中绘制的点和线?

我认为在缩放时,您可能需要设置“paint ToCalculate”标志。它在第一次绘制事件期间关闭,在缩放时不会重新打开,因此不会重新缩放线条

Pen pen = new Pen(Color.Blue, 2.5f);
        SolidBrush myBrush = new SolidBrush(Color.Red);
        private void chart1_Paint(object sender, PaintEventArgs e)
        {

            if (paintToCalaculate)
            {
                Series s = chart1.Series.FindByName("dummy");
                if (s == null) s = chart1.Series.Add("dummy");
                drawPoints.Clear();
                s.Points.Clear();
                foreach (PointF p in valuePoints)
                {
                    s.Points.AddXY(p.X, p.Y);
                    DataPoint pt = s.Points[0];
                    double x = chart1.ChartAreas[0].AxisX.ValueToPixelPosition(pt.XValue);
                    double y = chart1.ChartAreas[0].AxisY.ValueToPixelPosition(pt.YValues[0]);
                    drawPoints.Add(new Point((int)x, (int)y));
                    s.Points.Clear();
                }
                paintToCalaculate = false;
                chart1.Series.Remove(s);
            }
            foreach (Point p in drawPoints)
            {
                e.Graphics.FillEllipse(Brushes.Red, p.X - 2, p.Y - 2, 4, 4);
            }
            if (drawPoints.Count > 1)
            {
                e.Graphics.DrawLines(pen, drawPoints.ToArray());
            }
                    }