gpt4 book ai didi

winforms - 当用户使用 DataVisualization.Charting.Chart 单击折线图时获取数据点

转载 作者:行者123 更新时间:2023-12-04 12:20:05 27 4
gpt4 key购买 nike

我正在使用 DataVisualization.Charting.Chart (winform),当用户单击 MouseDown 事件中的折线图时,我需要获取数据点索引。

我知道有一个 HitTest 函数接受 x & y,但是对于折线图,我们只需要验证 x,如果我们扫描 y(0 到图的高度),它会起作用,但性能太差了。

最佳答案

一种方法是启用光标

chartArea1.CursorX.IsUserEnabled = true;
chartArea1.CursorX.IsUserSelectionEnabled = true;
// set selection color to transparent so that range selection is not drawn
chartArea1.CursorX.SelectionColor = System.Drawing.Color.Transparent;

并处理 CursorPositionChanged 事件。
private void chart1_CursorPositionChanged(object sender, CursorEventArgs e)
{
// find a point (this series only has Y values, so using position as index works
// for a series with actual X values, you'd need to Find the closest point
DataPoint pt = chart1.Series[0].Points[(int)Math.Max(e.ChartArea.CursorX.Position - 1, 0)];
// do what is need with the data point
pt.MarkerStyle = MarkerStyle.Square;
}

这显然假设您的 ChartArea 中有一个系列。

关于winforms - 当用户使用 DataVisualization.Charting.Chart 单击折线图时获取数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8724085/

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