2

简而言之,我检查了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 中的构造函数中。

也许值得一提的是,我正在使用 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

4 回答 4

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;
于 2013-01-09T20:29:09.770 回答
2

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

于 2012-06-13T11:44:05.753 回答
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

当数据点超过轴的 scaleview 大小时,通常会出现滚动条,如果它已设置(如果保留在“自动”,滚动不会真正可靠地工作),如果没有,设置它,如果滚动条没有出现,可以再次使用轨迹栏:

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

我错过了什么?

于 2012-06-13T15:09:25.373 回答
1

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

源代码在这里:https://bitbucket.org/controlbreak/mschartfriend

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

于 2012-10-18T07:02:29.933 回答