为什么自动缩放/滚动不适用于我的图表?

时间:2012-06-12 13:27:24

标签: c# .net winforms mschart

为了简化,我查看了Microsoft提供的“WinFormsChartSamples”。我想知道的是如何为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中的构造函数中。

  3. 也许重要的是要提到我使用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 ++

4 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

看看这里:http://archive.msdn.microsoft.com/mschart有一个例子可以进行缩放/滚动等等! :)

答案 2 :(得分: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)
    }

“最大值 - 值”是为了使轨迹栏值越高,显示的点越少(越接近缩放)

并确保在设计师的'chart1-> ChartAreas-> Axes->(无论哪个轴) - > scaleview-> zoomable'设置为true

当数据点超过轴的缩放视图大小时,通常会出现滚动条,如果已经设置了(如果保留在'auto',则滚动不能可靠地工作),如果没有,则设置它,如果没有出现滚动条,则可以再次使用轨迹栏:

    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”仅在用户移动鼠标时有效点击。

我错过了什么?

答案 3 :(得分:0)

我的用户不喜欢mschart缩放和滚动的标准行为。这就是为什么我在轴上实现了一个基于鼠标的缩放/滚动,使用拖动和鼠标滚轮

The source code is here: https://bitbucket.org/controlbreak/mschartfriend

它非常简单,如果需要,您可以非常快速地更改它。