2012-12-09 70 views
1

我正在尝试在MouseWheel事件期间获取鼠标的位置。在MouseMove事件中,我已成功使用HitTest并以这种方式关闭了我的业务,​​但由于某些原因,在MouseWheel事件期间,我的HitTest始终为HitTest数据点返回-1值。任何人都可以帮我解决这个问题吗?我将在下面包含我的代码。
我试图完成的是鼠标滚轮的基本放大事件。我想查看光标的位置,然后在任一侧添加当前可查看图表的1/4。在鼠标滚轮事件期间获取光标在图表中的位置

private void chData_MouseWheel(object sender, MouseEventArgs e) 
    { 
     try 
     { 
      HitTestResult pos = chData.HitTest(e.X, e.Y); 

      if (e.Delta < 0) 
      { 
       chData.ChartAreas[0].AxisX.ScaleView.ZoomReset(); 
       chData.ChartAreas[0].AxisY.ScaleView.ZoomReset(); 
      } 
      if (e.Delta > 0) 
      { 
       double xMin = chData.ChartAreas[0].AxisX.ScaleView.ViewMinimum; 
       double xMax = chData.ChartAreas[0].AxisX.ScaleView.ViewMaximum; 
       double yMin = chData.ChartAreas[0].AxisY.ScaleView.ViewMinimum; 
       double yMax = chData.ChartAreas[0].AxisY.ScaleView.ViewMaximum; 

       double posXStart = pos.PointIndex - (xMax - xMin)/4; 
       double posXFinish = pos.PointIndex + (xMax - xMin)/4; 
       double posYStart = pos.PointIndex - (yMax - yMin)/4; 
       double posYFinish = pos.PointIndex + (yMax - yMin)/4; 

       chData.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish); 
       chData.ChartAreas[0].AxisY.ScaleView.Zoom(posYStart, posYFinish); 
      } 
     } 
     catch { } 

    } 

我的图表被称为chData的方式。我希望这只是一个简单的错字。
在此先感谢!

+0

所以我已经知道PointIndex只有在你实际上是在一个数据点上时才起作用。我需要的是即使我不在数据点上,也知道我在哪个x轴值。任何人有任何想法? – tmwoods

回答

0

好吧,经过几天的实验后,我终于找到了一些可行的方法。
我将包括我的整个代码,因为我知道我无法在任何地方找到此解决方案,所以我希望这可能会在未来让其他人受益。

private void chData_MouseWheel(object sender, MouseEventArgs e) 
    { 
     try 
     { 
      if (e.Delta < 0) 
      { 
       chData.ChartAreas[0].AxisX.ScaleView.ZoomReset(); 
       chData.ChartAreas[0].AxisY.ScaleView.ZoomReset(); 
      } 

      if (e.Delta > 0) 
      { 
       double xMin = chData.ChartAreas[0].AxisX.ScaleView.ViewMinimum; 
       double xMax = chData.ChartAreas[0].AxisX.ScaleView.ViewMaximum; 
       double yMin = chData.ChartAreas[0].AxisY.ScaleView.ViewMinimum; 
       double yMax = chData.ChartAreas[0].AxisY.ScaleView.ViewMaximum; 

       double posXStart = chData.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) - (xMax - xMin)/4; 
       double posXFinish = chData.ChartAreas[0].AxisX.PixelPositionToValue(e.Location.X) + (xMax - xMin)/4; 
       double posYStart = chData.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) - (yMax - yMin)/4; 
       double posYFinish = chData.ChartAreas[0].AxisY.PixelPositionToValue(e.Location.Y) + (yMax - yMin)/4; 

       chData.ChartAreas[0].AxisX.ScaleView.Zoom(posXStart, posXFinish); 
       chData.ChartAreas[0].AxisY.ScaleView.Zoom(posYStart, posYFinish); 
      } 
     } 
     catch { }    
    } 

那么这段代码的基本功能是放大光标所在的图表。它基本上将轴的最大/最小值限制为您当前查看的一半,但以光标为中心。
我知道有很多方法可以优化,但暂时我认为这样做足以让人开始。
我希望这有助于!

相关问题