当前位置:网站首页>Getting started with scottplot tutorial: getting and displaying values at the mouse
Getting started with scottplot tutorial: getting and displaying values at the mouse
2022-07-28 14:36:00 【Xintang laoben】
1 Effect display

ScottPlot It is a very good open source chart Library , Official routines are also relatively rich , This article gives a simple example of how to get the value at the mouse .
2 establish Demo engineering
Create a blank Winform Desktop applications

introduce ScottPlot library

Right click solution , Click on Nuget Package management , Search full name ScottPlot:

After the import is completed, the toolbox of the design window will appear FromsPlot Control , Drag and drop it into the design interface , Here are two more controls :
Button Used to generate random data by clicking ,Label Used to display the value of the mouse position , The initial position is arbitrary , In the program, we let it follow the mouse to display in the appropriate position .

Changed in the figure Label Background color and border style , It looks better like this .
3 Code editing
Double click the added refresh Button You can quickly add monitoring and enter code editing mode .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScottPlotTest
{
public partial class Form1 : Form
{
public const int MAX_LOG_LENGHT = 1000;
private double[] values = new double[MAX_LOG_LENGHT];
private int dataCnt = 0;
private ScottPlot.Plottable.SignalPlot signalPlot;
public Form1()
{
InitializeComponent();
/*
* 1. Set data source , Customize X Axis labels
*/
var plt = formsPlot1.Plot;
plt.Title(" Real time curve ");
plt.SetAxisLimitsY(0, 1600);
plt.SetAxisLimitsX(0, 20);
signalPlot = plt.AddSignal(values);
signalPlot.MaxRenderIndex = dataCnt;
// Generate X Axis labels
double[] labelPositions = new double[MAX_LOG_LENGHT];
string[] labelsXAxis = new string[MAX_LOG_LENGHT];
for (int i = 0; i < MAX_LOG_LENGHT; i++)
{
labelsXAxis[i] = i.ToString();
labelPositions[i] = i;
}
plt.XTicks(labelPositions, labelsXAxis);
formsPlot1.Refresh();
// Mouse movement monitoring
formsPlot1.MouseMove += FormsPlot1_MouseMove;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
/*
* 2. Auto fit window size
*/
formsPlot1.Location = new Point(12, 51);
formsPlot1.Width = Size.Width - 48;
formsPlot1.Height = Size.Height - 108;
}
private void FormsPlot1_MouseMove(object sender, MouseEventArgs e)
{
/*
* 3. Calculate the value at the mouse and pass Label Show
*/
label1.Visible = false;
Point mouseLocation = e.Location;
double xCoordinate = formsPlot1.Plot.GetCoordinateX(mouseLocation.X);
int xAxis = Math.Abs((int)Math.Round(xCoordinate));
if (xAxis < dataCnt)
{
int y = (int)formsPlot1.Plot.GetPixelY(values[xAxis]);
if (Math.Abs(mouseLocation.Y - y) < 16)
{
label1.Text = "Y:" + y.ToString()
+ "\nX:" + xAxis.ToString();
// adjustment label To the right place
mouseLocation.Y += label1.Height;
mouseLocation.X += 16;
label1.Location = mouseLocation;
label1.Visible = true;
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
/*
* 4. Generate random data
*/
GetData();
formsPlot1.Plot.AxisAuto();
formsPlot1.Refresh();
}
private void GetData()
{
Random rand = new Random();
values[dataCnt] = 1000 + rand.Next(-100, 100);
signalPlot.MaxRenderIndex = dataCnt;
dataCnt++;
if(MAX_LOG_LENGHT == dataCnt)
{
dataCnt = 0;
}
}
}
}
The code did 4 thing :
1. Set data source ; Customize X Axis labels , Show only integers .
2. Auto fit window size .
3. Calculate the value at the mouse and pass Label Show .
4. Generate random data .
We focus on how to get the value at the mouse , adopt GetCoordinateX() obtain X The value of the icon curve coordinate corresponding to the axis mouse pixel , Then take the nearest integer , This number is the index of the data , Here you get X-Y Value of pair . But we need the mouse to point to the data point before it displays , So the next step is through GetPixelY() obtain Y The pixel value corresponding to the axis coordinate value , In the distance 16 Display within pixels Label, Otherwise, it will not show .

4END
边栏推荐
- String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
- [server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
- It's so hot that solar power can't take off? Hello, head
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- 基础架构之日志管理平台及钉钉&邮件告警通知
- Thrift 序列化协议浅析
- Node文件操作
- C # read INI file and key value pair operation
- pix2pix
- 卡方分布和伽马函数(Chi-Square Distribution)
猜你喜欢

As a programmer, how to manage time efficiently?

九、uni-popup用法 下拉框底部弹窗效果

Thrift 序列化协议浅析

开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
C # 7 methods to obtain the current path

Summarize the knowledge points of the ten JVM modules. If you don't believe it, you still don't understand it

Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)

Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie

Development and definition of software testing

超好用的手机录屏软件推荐
随机推荐
卡方分布和伽马函数(Chi-Square Distribution)
Thoughts on the construction of some enterprise data platforms
Mobile phone scrolling screenshot software recommendation
TDengine 助力西门子轻量级数字化解决方案
MySQL development skills - View
[ecmascript6] set and map
2022 low voltage electrician examination questions and answers
Cv:: mat conversion to qimage error
Metersphere -- Open Source continuous testing platform
C语言库函数getchar()怎么使用
9、 Uni popup usage popup effect at the bottom of the drop-down box
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
OKR与GRAD
多线程顺序运行有几种方法?
Iterator iterator interface
UI开发中所遇到的各种坑
为什么jq的匿名函数 外部可以访问到里面的方法
bgp实验
Thesis study -- masked generative disintegration
聊天室功能的实现