如何检索 .Net WinForms Chart Control 中的选定范围? [英] How to retrieve the selected range in the .Net WinForms Chart Control?

查看:19
本文介绍了如何检索 .Net WinForms Chart Control 中的选定范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the C# built-in Winforms Chart control (System.Windows.Forms.DataVisualization.Charting.Chart) with its built-in ability to let the user select a range. What I'd like to do is to read back what range the user has selected. Surely there must be some easy way to do this, but I haven't been able to find it.

光标是这样启用的:

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

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

I am aware that I can make the chart zoom when the user selects a range by enabling ca.AxisX.ScaleView.Zoomable, but I don't want the picture to change: instead I am using the chart as a way to display information and let the user select a range of X values for which I then do some extra processing.

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

I tried hooking to chart1.SelectionRangeChanged and that indeed fires every time the range is changed - I just can't seem to get the selection range from the CursorEventArg I get back. It has "NewSelectionStart" and "NewSelectionEnd" fields, but those are NaN, disappointingly. I tried looking at the various properties of the chart and the axes, but didn't find anything that sounded promising.

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

Further investigation reveals the ChartArea.CursorX.SelectionStart property which sounds like exactly what I need... except that it's NaN too. I don't know whether this is normal or I'm hitting some sort of bug?

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

So, how can I figure out what range the user selected?

推荐答案

好的,我想通了.这是独家新闻:

OK, well, I figured it out. Here's the scoop:

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

There's a SelectionRangeChang*ing* event, and when that one runs the ChartArea.CursorX.SelectionStart and ChartArea.CursorX.SelectionEnd fields have correct values in them. But the user hasn't released the mouse button yet, so you should just store them.

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

When the user releases the mouse button, the SelectionRangeChang*ed* event fires. Somehow it's designed in such a way that SelectionStart and SelectionEnd are reset to NaN (just like the NewSelectionStart and NewSelectionEnd fields in the event parameters). What you have to do is to use the values that you squirreled away from the other event handler now that you know the time is right to use them.

所以你有它!希望这个答案能让其他人免于浪费时间.

So there you have it! Hopefully this answer will save someone else from wasting time.

这篇关于如何检索 .Net WinForms Chart Control 中的选定范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆