2012-11-27 152 views

回答

20

你会想使用MouseWheel事件。确保你的图表可缩放。例如:

chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true; 

然后访问MouseWheel事件:

private void chData_MouseWheel(object sender, MouseEventArgs e) 
    { 
     try 
     { 
      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); 
      } 
     } 
     catch { }    
    } 

有可能是这样做的更清洁的方式,但它是。 e.Delta属性告诉你已经完成了多少个轮子的“滚动”,并且可能有用。如果您完全滚动出来,使用此代码就可以恢复到图表的原始大小。

希望这会有所帮助!

+0

这对我来说真棒。谢谢! – crocboy

+0

这不适合我。图表的Mousewheel事件不会触发。 –

+2

显然,你必须这样做首先为它工作'无效friendChart_MouseLeave(对象发件人,EventArgs的){ 如果 (friendChart.Focused) friendChart.Parent.Focus(); } void friendChart_MouseEnter(object sender,EventArgs e) if(!friendChart.Focused) friendChart.Focus(); }'[Mousewheel event not firing](http://stackoverflow.com/questions/13782763/mousewheel-event-not-firing) –

相关问题