【问题标题】:How to retrieve the selected range in the .Net WinForms Chart Control?如何检索 .Net WinForms 图表控件中的选定范围?
【发布时间】:2020-03-31 23:56:06
【问题描述】:

我正在使用 C# 内置的 Winforms 图表控件 (System.Windows.Forms.DataVisualization.Charting.Chart),它具有让用户选择范围的内置功能。我想做的是回读用户选择的范围。肯定有一些简单的方法可以做到这一点,但我一直找不到。

光标是这样启用的:

var ca = chart1.ChartAreas["ChartArea1"].CursorX;
ca.CursorX.IsUserEnabled = true;
ca.CursorX.IsUserSelectionEnabled = true;

我知道当用户通过启用ca.AxisX.ScaleView.Zoomable 选择范围时,我可以使图表缩放,但我不希望图片改变:相反,我使用图表作为显示信息的一种方式,让用户选择一系列 X 值,然后我对其进行一些额外的处理。

我尝试连接到 chart1.SelectionRangeChanged,并且每次更改范围时都会触发 - 我似乎无法从返回的 CursorEventArg 中获取选择范围。它有“NewSelectionStart”和“NewSelectionEnd”字段,但令人失望的是那些是NaN。我尝试查看图表和轴的各种属性,但没有发现任何听起来很有希望的东西。

进一步调查揭示了ChartArea.CursorX.SelectionStart 属性,这听起来正是我所需要的……除了它也是 NaN。我不知道这是正常的还是我遇到了某种错误?

那么,我如何才能确定用户选择的范围?

【问题讨论】:

  • 另外,我会采纳免费图表控件的建议,让用户选择一个范围。但我更愿意使用我已经拥有的东西。

标签: c# winforms charts


【解决方案1】:

好吧,我想通了。这是独家新闻:

有一个 SelectionRangeChang*ing* 事件,当该事件运行时,ChartArea.CursorX.SelectionStartChartArea.CursorX.SelectionEnd 字段中有正确的值。但是用户还没有释放鼠标按钮,所以你应该把它们存储起来。

当用户释放鼠标按钮时,SelectionRangeChang*ed* 事件会触发。不知何故,它的设计方式是将 SelectionStart 和 SelectionEnd 重置为 NaN(就像事件参数中的 NewSelectionStartNewSelectionEnd 字段一样)。您需要做的是使用从其他事件处理程序中分离出来的值,因为您知道使用它们的时机已经成熟。

所以你有它!希望这个答案可以避免其他人浪费时间。

【讨论】:

  • 不确定您使用的是哪个版本的 .NET,但在 .NET Framework v4 中这对我来说很好。我在 SelectionRangeChanged 事件中得到了正确的值。
  • 奇怪。我想知道我是否使用了过时版本的 Chart 组件。
【解决方案2】:

除了 redtuna 在 c# 图表中设置光标:

我使用“SelectionRangeChanging”而不是“SelectionRangeChanged”来避免出现 NaN 问题:

初始化表单时

this.chart1.SelectionRangeChanging += chart1_SelectionRangeChanging;

        chart1.ChartAreas[0].CursorX.IsUserEnabled = false;         // red cursor at SelectionEnd
        chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
        chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;      // zoom into SelectedRange
        chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
        chart1.ChartAreas[0].CursorX.Interval = 0.01;               // set "resolution" of CursorX

如果选择了范围/设置了光标,会执行什么

private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
    {
        double x1 = x1 = e.NewSelectionStart; // or: chart1.ChartAreas[0].CursorX.SelectionStart;
        double x2 = e.NewSelectionEnd;        // or: x2 = chart1.ChartAreas[0].CursorX.SelectionEnd;

        double diffx1x2 = x2 - x1;
    }    

为了放大和缩小(x 轴),我刚刚添加了一个获取光标值的按钮。这样通过 mouseClick (ScaleView.Zoomable = false;) 进行缩放不会干扰我的光标定位:)

 private void button_ZoomIn(object sender, EventArgs e)
    {
        double x1 = chart1.ChartAreas[0].CursorX.SelectionStart;  // x1 = X1
        double x2 = chart1.ChartAreas[0].CursorX.SelectionEnd;    // x2 = X2

        if (x2 > x1)
        {
            // hard setting: chart1.ChartAreas[0].AxisX.Minimum = x1;
            // hard setting: chart1.ChartAreas[0].AxisX.Maximum = x2;
            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(x1,x2); // dynamic approach with scrollbar
        }
        else
        {
            chart1.ChartAreas[0].AxisX.ScaleView.Zoom(x2,x1);
        }
    }

缩小

private void button_ZoomOut(object sender, EventArgs e)
    {
        chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
    }

也可以通过mouseWheel实现缩放:how to enable zooming in Microsoft chart control by using Mouse wheel 如果您还想在图表中进行右键单击操作:How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

【讨论】:

    猜你喜欢
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多