gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:05:40 25 4
gpt4 key购买 nike

为了简短起见,我查看了 Microsoft 提供的 “WinFormsChartSamples”。我想知道的是如何为图表控件启用缩放和滚动。此处显示的示例非常简短。

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++

最佳答案

我认为您实际上真的在寻找这个:

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;

关于c# - 为什么自动缩放/滚动不适用于我的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10997701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com