首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用十字准线光标的C#图表

使用十字准线光标的C#图表
EN

Stack Overflow用户
提问于 2015-10-27 17:00:47
回答 2查看 3.6K关注 0票数 1

我正在使用C#图表类来显示曲线。该系列的类型为样条。我要做的就是在图表区域显示一个十字光标。当鼠标进入图表时,此光标的垂直线应随鼠标移动。但是水平线应该随着曲线移动,而不是随着鼠标的移动。我输入的代码是:

代码语言:javascript
复制
    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
        Point mousePoint = new Point(e.X, e.Y);
        chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint,false);
        chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint,false);
     }

此代码会使光标随鼠标移动,结果如下图所示:

使光标的水平线沿着曲线移动,曲线在这里是正弦信号。我应该知道光标的垂直线和曲线的交点。如图所示:

有什么直接的方法可以找到这一点吗?有什么需要帮忙的吗?

EN

回答 2

Stack Overflow用户

发布于 2015-10-28 01:52:09

如果您知道以下内容,则可以获得相对于图表区域顶部的y像素偏移量:

  • height = pixels
  • rangeMin/rangeMax中的图表高度=图表范围最小/最大(此处-150/150)
  • value =函数值(底部图像,大约75)

采用以下公式:

yOffset = height * (rangeMax - value) / (rangeMax - rangeMin);

您应该能够将yOffset插入到MouseMove函数中,如下所示:

代码语言:javascript
复制
private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    double yOffset = GetYOffset(chart1, e.X);
    Point mousePoint = new Point(e.X, yOffset);
    chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint,false);
    chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint,false);
}

// ChartClass chart is just whatever chart you're using
// x is used here I'm assuming to find f(x), your value
private double GetYOffset(ChartClass chart, double x)
{
    double yOffset;
    // yOffset = height * (rangeMax - value) / (rangeMax - rangeMin);
    return yOffset;
}
票数 0
EN

Stack Overflow用户

发布于 2020-11-09 19:57:47

在此函数中,我使用方法GetXOfCursor();获取光标X的位置,然后使用以下语句获取序列点:

代码语言:javascript
复制
points = _theChart.Series[i].Points;

在最后一条if语句之后,我看到了与光标位置X匹配的点,并且我计算了这两个点的平均值,因为光标截取的点是由windows图表控件生成的插值线

代码语言:javascript
复制
public string GetPointInterceptedCursorX()
        {
            string values = string.Empty;
            // Far uscire le etichette ai punti intercettati
            var xPosCursor = GetXOfCursor();
            DataPointCollection points = null;

            for (int i = 0; i < _theChart.Series.Count; i++)
            {
                if (_theChart.Series[i].BorderWidth == ChartConst.THICKNESS_LINE_ENABLED)
                {
                    points = _theChart.Series[i].Points;
                    break;
                }
            }
            if (points == null) return "No Curve Selected";
            for (int i = 0; i < points.Count - 1; i++)
            {
                if ((xPosCursor > points[i].XValue & xPosCursor < points[i + 1].XValue) | xPosCursor > points[i + 1].XValue & xPosCursor < points[i].XValue)
                {
                    var Yval = (points[i].YValues[0] + points[i + 1].YValues[0]) / 2;

                    values += $"Y= {Yval} {Environment.NewLine}";
                }
            }
            values += "X=" + " " + String.Format("{0:0.00}", xPosCursor);
            return values;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33363565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档