当前位置:网站首页>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
边栏推荐
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- 2022高处安装、维护、拆除考试题库及在线模拟考试
- 8、 Picker usage drop-down box selection effect
- AFNetworking速成教程
- 9、 Uni popup usage popup effect at the bottom of the drop-down box
- [ecmascript6] proxy and reflection
- 一些企业数据平台建设的思考
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- Node文件操作
- Development and definition of software testing
猜你喜欢
![[ecmascript6] set and map](/img/64/dd6ffc5f0faf881b990e609cf62343.png)
[ecmascript6] set and map

Cv:: mat conversion to qimage error

OKR and grad
C# 获取当前路径7种方法

Metersphere -- Open Source continuous testing platform

2022 high altitude installation, maintenance, removal of examination question bank and online simulated examination

Mobile phone scrolling screenshot software recommendation

Hcip day 11

How to effectively conduct the review meeting (Part 1)?

成为绿色数据中心新样板,东莞华为云数据中心是怎样炼成的?
随机推荐
IP black and white list
String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
[ecmascript6] class
超好用的手机录屏软件推荐
As a programmer, how to manage time efficiently?
分集技术简略
Super resolution reconstruction based on deep learning
【LeetCode】1331. 数组序号转换
pix2pix
2022年熔化焊接与热切割考题及在线模拟考试
Unittest executes runtestcase prompt <_ io. Textiowrapper name= '< stderr>' mode=W encoding=UTF-8 > solution
How to effectively conduct the review meeting (Part 1)?
这3款在线PS工具,得试试
Bulk Rename Utility
2022 safety officer-a certificate operation certificate examination question bank simulated examination platform operation
[leetcode] 1331. Array sequence number conversion
C# 获取当前路径7种方法
Install mysql5.7.36 in CentOS
Leetcode 0142. circular linked list II
八、picker用法 下拉框选择效果