2012-06-12 24 views
2

使其縮短我檢查了「WinFormsChartSamples」由Microsoft提供。我想知道的是如何爲Chartcontrols啓用縮放和滾動功能。這裏顯示的例子非常短。爲什麼自動縮放/滾動不適用於我的圖表?

using System.Windows.Forms.DataVisualization.Charting; 
... 

// Set automatic zooming 
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true; 
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true; 

// Set automatic scrolling 
chart1.ChartAreas["Default"].CursorX.AutoScroll = true; 
chart1.ChartAreas["Default"].CursorY.AutoScroll = true; 

... 

我試過這個,沒有發生任何事情,沒有縮放,也沒有滾動。我試了兩件事:

  1. 在Form1.Designer.cs中,我將該信息添加到圖表。

    chartArea1.Name = "ChartArea1"; 
        chartArea1.CursorX.AutoScroll = true; 
        chartArea1.CursorY.AutoScroll = true; 
    
        chartArea1.AxisX.ScaleView.Zoomable = true; 
        chartArea1.AxisY.ScaleView.Zoomable = true; 
    
        this.chart1.ChartAreas.Add(chartArea1); 
        this.chart1.Cursor = System.Windows.Forms.Cursors.Cross; 
        legend1.Name = "Legend1"; 
        this.chart1.Legends.Add(legend1); 
        this.chart1.Location = new System.Drawing.Point(297, 62); 
        this.chart1.Name = "chart1"; 
        series1.ChartArea = "ChartArea1"; 
        series1.Legend = "Legend1"; 
        series1.Name = "Series1"; 
        this.chart1.Series.Add(series1); 
        this.chart1.Size = new System.Drawing.Size(963, 668); 
        this.chart1.TabIndex = 6; 
        this.chart1.Text = "chart1"; 
    
  2. 我試着將它直接添加到Form1.cs的構造函數中。

也許一提的是,我爲了將數據添加到該系列使用的OpenFileDialog是很重要的:

private void openToolStripMenuItem_Click(object sender, EventArgs e) 
     { 

      Stream fileStream = null; 
      OpenFileDialog fDialog = new OpenFileDialog(); 
      fDialog.Title = "Open File.."; 
      //First the description of the file separated by "|" 
      fDialog.Filter = "((ASC files)| *.asc"; 
      fDialog.InitialDirectory = @"C:\"; 

      //Show Messagebox if the file was loaded (Source: MSDN - FileDialog.FilterProperty) 
      if (fDialog.ShowDialog() == DialogResult.OK) 
      { 
       MessageBox.Show("The File was loaded successfully."); 

       try 
       { 
        if ((fileStream = fDialog.OpenFile()) != null) 
        { 
         using (fileStream) 
         { 
          //Insert code for reading the stream here. 
          Spectrum newSpectrum = new Spectrum(chart1.Series.Count, fDialog.FileName, 
           fDialog.SafeFileName, DataHandler.readSpectrumFromFile(fileStream)); 

          addSpectrumToView(newSpectrum); 

         } 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 

      } 
     } 

任何意見是值得歡迎的,在此先感謝,BC

++

回答

2

看看這裏:http://archive.msdn.microsoft.com/mschart有一個例子,它可以縮放/滾動,還有更多! :)

+0

我上面貼的例子來自給定的來源! –

+1

鏈接已關閉。建議不要張貼鏈接,但也摘錄。 –

1

要啓用容易變焦,添加跟蹤條,並用它來放大:

private void trackBar1_Scroll(object sender, EventArgs e) 
    { 
     chart1.ChartAreas[0].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value; 
     chart1.ChartAreas[1].AxisX.ScaleView.Size = trackBar1.Maximum - trackBar1.Value; 
     (etc for however many chart areas you have) 
    } 

的「maximium - 值」是這樣的跟蹤條值越高,顯示少點(接近變焦)

,並確保在設計師的「chart1-> ChartAreas->軸 - >(取軸) - > scaleview->縮放」設置爲true

滾動條,通常會出現當數據點超出軸的縮放視圖尺寸(如果已設置)(滾動不重新)如果在「自動」)離開盟友可靠地工作,如果還沒有,設置它,如果滾動條不出現,一個跟蹤條可以再次使用:

private void trackBar2_ValueChanged(object sender, EventArgs e) 
    { 
     chart1.ChartAreas[0].AxisX.ScaleView.Position = trackBar2.Value; 
     chart1.ChartAreas[1].AxisX.ScaleView.Position = trackBar2.Value; 
     (etc for however many chart areas you have) 
    } 

確保您設置的「最大「在軌道條上設置一個很好的數字(例如5000)和」價值「以滿足您希望載入的內容。

「trackBar_Scroll」和「trackBar_ValueChanged」之間的差異還沒有注意到,除了「ValueChanged」在程序或用戶鼠標點擊移動軌跡條時有效,而「Scoll」僅在用戶移動時才起作用鼠標點擊。

我錯過了什麼?

4

我想你實際上真的找這樣的:

chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true; 
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true; 

與你已經擁有配合使用應該足夠了,這應該是這樣的:

// Set automatic zooming 
chart1.ChartAreas["Default"].AxisX.ScaleView.Zoomable = true; 
chart1.ChartAreas["Default"].AxisY.ScaleView.Zoomable = true; 

// Set automatic scrolling 
chart1.ChartAreas["Default"].CursorX.AutoScroll = true; 
chart1.ChartAreas["Default"].CursorY.AutoScroll = true; 

// Allow user selection for Zoom 
chart1.ChartAreas["Default"].CursorX.IsUserSelectionEnabled = true; 
chart1.ChartAreas["Default"].CursorY.IsUserSelectionEnabled = true; 
+0

這爲我解決了它! –

+0

如何在縮放模式下打開? – Doro

相關問題