一、ScottPlot 5.1.x 学习案例(含核心代码)

本文实现图表上鼠标悬停,ToopTip提示坐标值,效果如下(亲测)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.1 图表上显示十字线

定义:private Crosshair Crosshair;
结合FormsPlot_MouseMove、FormsPlot_MouseEnter、FormsPlot_MouseLeave

            Crosshair = this.formsPlot.Plot.Add.Crosshair(0.0, 0.0);//十字光标线
            Crosshair.LineWidth = 2;
            Crosshair.MarkerShape = MarkerShape.OpenCircle;
            Crosshair.MarkerSize = 10;
            Crosshair.LineColor = Colors.Red;
            Crosshair.TextColor = Colors.White;

控制十字线,进入时显示,离开时隐藏

            Crosshair.VerticalLine.IsVisible = true;//显示
            Crosshair.HorizontalLine.IsVisible = true;

在这里插入图片描述

FormsPlot_MouseMove中获取坐标方法:

            // 查找最近的数据点(使用像素坐标),需要定义FindNearestPoint
            Coordinates? nearestPoint = FindNearestPoint(e.X, e.Y, searchRadiusPixels: 10);
            if (nearestPoint.HasValue)
            {
                Crosshair.Position = nearestPoint.Value;
                Crosshair.IsVisible = true;
                //如果显示提示就在这定义
                //string timeStr =DateTime.FromOADate(nearestPoint.Value.X).ToString("HH:mm:ss");
                //string priceStr=nearestPoint.Value.Y.ToString("F3")
                //string tipText = $"时间: {timeStr}\n价格: {priceStr} 元";
                //toolTip.Show(tipText, formsPlot, e.Location + new Size(10, -30));
           }else{
				Crosshair.IsVisible = false;
           }

1.2 查找坐标最近的数据点

在这里插入图片描述
在这里插入图片描述

实现曲线颜色与主题颜色一致(AntdUI)
scatterPlot.Color = ScottPlot.Color.FromHex(AntdUI.Style.Db.Primary.ToHex());

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐