【问题标题】:How can I scale my Windows forms chart using the mouse wheel?如何使用鼠标滚轮缩放 Windows 窗体图表?
【发布时间】:2015-05-19 13:37:10
【问题描述】:

我正在使用 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);
            }
        }

问题出在图表绘制事件中,当我使用滚轮绘制点并将它们与线连接时,点和线消失了,我认为它们的缩放比例不正确。

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());
            }
                    }

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

【问题讨论】:

    标签: c# .net winforms


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2021-01-30
      • 1970-01-01
      相关资源
      最近更新 更多