C#中通过TeeChart实现鼠标点击获取多曲线的坐标值

C#通过TeeChart实现鼠标点击获取多曲线的坐标值

背景

近期在使用TeeChart进行多曲线展示的时候,客户要求实现,鼠标在曲线图上移动时要跟随鼠标显示一条竖线,在点击鼠标的时候把竖线与各条曲线的交叉点的值读取出来。经过一番研究终于实现了。效果如下:

在这里插入图片描述

实现代码

本例中虽然放了3个按钮,其实只有button3有代码,用于生成2条测试曲线数据,代码如下:

private void button3_Click(object sender, EventArgs e)
{
    Random r = new Random();
    r.Next();
    FastLine[] list = new FastLine[2];          //曲线集合
    list[0] = new FastLine();
    list[1] = new FastLine();
    for(int i= 0;i< 50;i ++)
    {
        list[0].Add(i, r.Next(30));
        list[1].Add(i, 15 + r.Next(15));
    }
    this.tChart1.Series.Add(list[0]);
    this.tChart1.Series.Add(list[1]);

    Steema.TeeChart.Tools.CursorTool cursorTool = new Steema.TeeChart.Tools.CursorTool();       //添加光标
    cursorTool.Style = Steema.TeeChart.Tools.CursorToolStyles.Vertical;                         //设置光标样式,以竖线方式展示
    cursorTool.FollowMouse = true;                                                              //设置光标跟随鼠标移动
   
    this.tChart1.Tools.Add(cursorTool);
}

另外给tChart控件添加MouseClick事件,用于在鼠标点击时获取各曲线的坐标值,代码如下:

private void tChart1_MouseClick(object sender, MouseEventArgs e)
{
    if (this.tChart1.Series.Count  > 0)
    {
        int xPoints = (int)Math.Round(this.tChart1.Series[0].XScreenToValue(e.X));
        Console.WriteLine("X = {0}, Y1 = {1}, Y2 = {2}", xPoints, this.tChart1.Series[0][xPoints].Y, this.tChart1.Series[1][xPoints].Y);
    }
}

猜你喜欢

转载自blog.csdn.net/zlbdmm/article/details/88103850