2012-11-27 149 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) –

相關問題