简体   繁体   English

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

[英]Why does auto Zoom/Scroll not work for my Chart?

to make it short I checked on the "WinFormsChartSamples" provided by Microsoft. 为了简短起见,我检查了Microsoft提供的“ WinFormsChartSamples” What I wanted to know is how to enable zooming and scrolling for Chartcontrols. 我想知道的是如何为Chartcontrols启用缩放和滚动。 The example which is shown there is pretty short. 此处显示的示例非常简短。

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;

...

I tried this and nothing happened, no zooming and no scrolling. 我试过了,没有任何反应,没有缩放,也没有滚动。 I tried two things: 我尝试了两件事:

  1. In Form1.Designer.cs I added that information to the chart. 在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. I tried to add it directly into the constructor in Form1.cs. 我试图将其直接添加到Form1.cs的构造函数中。

Perhaps it is important to mention that I am using OpenFileDialog in order to add data to the series: 也许重要的是要提到我正在使用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);
                }

            }
        }

Any advice is welcome, thanks in advance, 欢迎任何建议,在此先感谢,

BC++ BC ++

I think you were actually really looking for this: 我认为您实际上是在寻找这个:

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

used in conjunction with what you already have should work well, which should look like this: 与您已有的东西结合使用应该可以正常工作,看起来应该像这样:

// 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;

Have a look here: http://archive.msdn.microsoft.com/mschart There is an example there which does zooming/scrolling and much, much more! 在这里看看: http : //archive.msdn.microsoft.com/mschart那里有一个示例,它可以进行缩放/滚动等等! :) :)

To enable easy zooming, add a trackbar and use it to zoom: 要启用轻松缩放,请添加一个轨迹栏并将其用于缩放:

    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)
    }

the "maximium - value" is to so that the higher the trackbar value, the fewer points are shown (closer zoom) “最大值-值”应为,以便跟踪栏值越高,显示的点越少(缩放越近)

and make sure that in designer the 'chart1->ChartAreas->Axes->(whichever axes)->scaleview->zoomable' is set to true 并确保在设计器中将“ chart1-> ChartAreas-> Axes->(任意轴)-> scaleview-> zoomable”设置为true

A scroll bar will normally appear when a datapoint exceeds the scaleview size of an axis, if it has been set (scrolling doesn't really work reliably if left at 'auto'), if it hasn't, set it, if a scrollbar doesn't appear, a trackbar can yet again be used: 如果已设置数据点,则滚动条通常会出现在数据点超过轴的比例视图大小的情况下(如果设置为“自动”,则滚动不会真正可靠地工作);如果未设置,则进行设置;如果滚动条没有出现,可以再次使用跟踪栏:

    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)
    }

Make sure you set the "Maximum" in the trackbars to a nice high number (eg 5000) and "Value" to what you desire it to load at. 确保将轨迹栏中的“最大值”设置为一个很高的数字(例如5000),并将“值”设置为希望加载的值。

Have yet to notice too much of a difference between "trackBar_Scroll" and "trackBar_ValueChanged", except "ValueChanged" works if the trackbar is moved by the program or user mouse click, whereas "Scoll" only works if moved by users mouse click. 尚未注意到“ trackBar_Scroll”和“ trackBar_ValueChanged”之间的太大差异,除了“ ValueChanged”在通过程序或用户单击鼠标移动跟踪栏时有效,而“ Scoll”仅在通过用户单击鼠标移动时有效。

Anything I missed? 我错过了什么?

My users dislikes the standard behavior of the mschart zooming and scrolling. 我的用户不喜欢mschart缩放和滚动的标准行为。 That's why I implement a mouse based zoom/scroll that use dragging and mousewheel on axises. 这就是为什么我实现基于鼠标的缩放/滚动,并在轴上使用拖动和鼠标滚轮的原因

The source code is here: https://bitbucket.org/controlbreak/mschartfriend 源代码在这里:https://bitbucket.org/controlbreak/mschartfriend

It is very simple and short, you can change it very quickly if you need. 它非常简单且简短,您可以根据需要快速更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM