• ScottPlot入门教程:获取和显示鼠标处的数值


    1效果展示

    ScottPlot是个非常不错的开源图表库,官方的例程也比较丰富,本文简单举例说明如何获取鼠标处的数值。

    2创建Demo工程

    创建空白的Winform桌面程序

     引入ScottPlot库

    右键解决方案,点击Nuget程序包管理,搜索完整名ScottPlot:

     引入完成之后设计窗口的工具箱中会出现FromsPlot控件,将其拖放到设计界面中,这里再加两个控件:

    Button用于点击产生随机数据,Label用于显示鼠标位置的数值,初始位置随意,程序里我们让它跟随鼠标在适当的位置显示。

     图中更改了Label的背景色和边框样式,这样看起来好看些。

     3代码编辑

    双击添加的刷新Button可快速添加监听并进入代码编辑模式。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. namespace ScottPlotTest
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public const int MAX_LOG_LENGHT = 1000;
    15. private double[] values = new double[MAX_LOG_LENGHT];
    16. private int dataCnt = 0;
    17. private ScottPlot.Plottable.SignalPlot signalPlot;
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. /*
    22. * 1.设置数据源,自定义X轴标签
    23. */
    24. var plt = formsPlot1.Plot;
    25. plt.Title("实时曲线");
    26. plt.SetAxisLimitsY(0, 1600);
    27. plt.SetAxisLimitsX(0, 20);
    28. signalPlot = plt.AddSignal(values);
    29. signalPlot.MaxRenderIndex = dataCnt;
    30. //生成X轴标签
    31. double[] labelPositions = new double[MAX_LOG_LENGHT];
    32. string[] labelsXAxis = new string[MAX_LOG_LENGHT];
    33. for (int i = 0; i < MAX_LOG_LENGHT; i++)
    34. {
    35. labelsXAxis[i] = i.ToString();
    36. labelPositions[i] = i;
    37. }
    38. plt.XTicks(labelPositions, labelsXAxis);
    39. formsPlot1.Refresh();
    40. //鼠标移动监听
    41. formsPlot1.MouseMove += FormsPlot1_MouseMove;
    42. }
    43. protected override void OnSizeChanged(EventArgs e)
    44. {
    45. base.OnSizeChanged(e);
    46. /*
    47. * 2.自动适应窗口大小
    48. */
    49. formsPlot1.Location = new Point(12, 51);
    50. formsPlot1.Width = Size.Width - 48;
    51. formsPlot1.Height = Size.Height - 108;
    52. }
    53. private void FormsPlot1_MouseMove(object sender, MouseEventArgs e)
    54. {
    55. /*
    56. * 3.计算鼠标处的值并通过Label显示
    57. */
    58. label1.Visible = false;
    59. Point mouseLocation = e.Location;
    60. double xCoordinate = formsPlot1.Plot.GetCoordinateX(mouseLocation.X);
    61. int xAxis = Math.Abs((int)Math.Round(xCoordinate));
    62. if (xAxis < dataCnt)
    63. {
    64. int y = (int)formsPlot1.Plot.GetPixelY(values[xAxis]);
    65. if (Math.Abs(mouseLocation.Y - y) < 16)
    66. {
    67. label1.Text = "Y:" + y.ToString()
    68. + "\nX:" + xAxis.ToString();
    69. //调整label到合适位置
    70. mouseLocation.Y += label1.Height;
    71. mouseLocation.X += 16;
    72. label1.Location = mouseLocation;
    73. label1.Visible = true;
    74. }
    75. }
    76. }
    77. private void Button1_Click(object sender, EventArgs e)
    78. {
    79. /*
    80. * 4.生成随机数据
    81. */
    82. GetData();
    83. formsPlot1.Plot.AxisAuto();
    84. formsPlot1.Refresh();
    85. }
    86. private void GetData()
    87. {
    88. Random rand = new Random();
    89. values[dataCnt] = 1000 + rand.Next(-100, 100);
    90. signalPlot.MaxRenderIndex = dataCnt;
    91. dataCnt++;
    92. if(MAX_LOG_LENGHT == dataCnt)
    93. {
    94. dataCnt = 0;
    95. }
    96. }
    97. }
    98. }

    代码做了4件事:

    1.设置数据源;自定义X轴标签,只显示整数。

    2.自动适应窗口大小。

    3.计算鼠标处的值并通过Label显示。

    4.生成随机数据。

    我们重点看如何获取鼠标处的值,通过GetCoordinateX()获取X轴鼠标像素点对应的图标曲线坐标的值,然后取最近的整数,这个数就是数据的索引,这里就得到了X-Y对的值。但我们需要鼠标指到数据点附近时才显示,所以接下来通过GetPixelY()获取Y轴坐标数值对应的像素值,在距离16个像素范围内就显示Label,否则就不显示。

     4END

  • 相关阅读:
    Linux之Shell基础入门
    BCryptPasswordEncoder的使用及原理
    Verlog-串口发送-FPGA
    leetcode.无重复字符的最长字串(刷题日记)
    开咖啡店该如何做好管理?实现快速盈利
    .NET 开源工作流: Slickflow流程引擎高级开发(十) -- BpmnJS流程设计器集成
    【学习笔记】《模式识别》2:聚类分析
    最重要的技术深入学习
    Spring Boot2.x 学习导航大纲-Boot整套前端由0到1(关注收藏本博文即可)
    JavaScript 解构的 5 个有趣用途
  • 原文地址:https://blog.csdn.net/qq_40692629/article/details/125993555