当前位置:网站首页>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
边栏推荐
- Hcip day 12
- These three online PS tools should be tried
- MiniTest--小程序自动化测试框架
- [ecmascript6] class
- Many "double first-class" universities have launched the research guarantee and prediction name!
- Thesis study -- masked generative disintegration
- @Solution to DS ('slave') multi data source compatible transaction problem
- 8、 Picker usage drop-down box selection effect
- 【LeetCode】 贴纸拼词(动态规划)
- MySQL development skills - View
猜你喜欢
随机推荐
文件批量重命名工具Bulk Rename Utility
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
用友BIP CRM新品发布,赋能大中型企业营销增长
Factory mode and constructor mode
超好用的手机录屏软件推荐
MySQL development skills - View
HCIP第十一天
How to effectively conduct the review meeting (Part 1)?
9、 Uni popup usage popup effect at the bottom of the drop-down box
Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
2022 melting welding and thermal cutting examination questions and online simulation examination
基础架构之日志管理平台及钉钉&邮件告警通知
String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
成为绿色数据中心新样板,东莞华为云数据中心是怎样炼成的?
Qt中QTableView设置分页显示的三种方法[通俗易懂]
Excel VBA 免密查看VBE加密代码
JS instantiation method
Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
Thrift 序列化协议浅析
多所“双一流”大学,保研预报名启动!









