C# winform图表控件scottplot快速入门设置十字标尺显示刻度及数值

Hello大家好我是开箱测评小汪,在C#项目开发中使用图表控件,我们希望随着鼠标的移动动态显示鼠标位置相应的数值,所以本期课程将带介绍Scottplot图表控件如何来实现这个功能。

C# winform图表控件scottplot快速入门设置十字标尺显示刻度及数值

效果图

本期课程的要达到的目的:

1、在scottplot中实现通过十字光标线显示当前鼠标位置对应的XY轴对应的数值

环境:

Visual Studio 2019 .net5.0

实现:

添加1个复选框,4个文本显示label

程序源码:

using ScottPlot;
using ScottPlot.Plottable;
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 Scottplot_01
{
    public partial class Form1 : Form
    {
        private readonly Crosshair Crosshair;
        public Form1()
        {
            InitializeComponent();
            var plt = formsPlot1.Plot;

            // sample data
            //X轴数据
            double[] xs = DataGen.Consecutive(51);

            //Y轴数据
            double[] sin = DataGen.Sin(51);
            double[] cos = DataGen.Cos(51);

            // 2条曲线
            plt.AddScatter(xs, sin);
            plt.AddScatter(xs, cos);

            // plot参数设置
            plt.Title("标题");
            plt.XLabel("X轴");
            plt.YLabel("Y轴");

            //添加十字光标线
            this.Crosshair = this.formsPlot1.Plot.AddCrosshair(0.0, 0.0);

            
            


            //缩放进行配置

            formsPlot1.Refresh();
        }


        
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //鼠标右键拖拽缩放
            this.formsPlot1.Configuration.RightClickDragZoom = this.cbZoomable.Checked;

            //鼠标管轮缩放
            this.formsPlot1.Configuration.ScrollWheelZoom = this.cbZoomable.Checked;
        }

        private void cbLockHorizontal_CheckedChanged(object sender, EventArgs e)
        {
            //锁定水平缩放
            this.formsPlot1.Configuration.LockHorizontalAxis = this.cbLockHorizontal.Checked;
        }

        private void cbLockVertical_CheckedChanged(object sender, EventArgs e)
        {
            //锁定垂直缩放
            this.formsPlot1.Configuration.LockVerticalAxis = this.cbLockVertical.Checked;
        }


        //鼠标移动时处理十字光标
        private void formsPlot1_MouseMove(object sender, MouseEventArgs e)
        {
            if (CrosshaircheckBox.Checked) {
                ValueTuple<double, double> mouseCoordinates = this.formsPlot1.GetMouseCoordinates();
                double coordinateX = mouseCoordinates.Item1;
                double coordinateY = mouseCoordinates.Item2;
                this.XPixelLabel.Text = string.Format("{0:0.000}", e.X);
                this.YPixelLabel.Text = string.Format("{0:0.000}", e.Y);
                this.XCoordinateLabel.Text = string.Format("{0:0.00000000}", coordinateX);
                this.YCoordinateLabel.Text = string.Format("{0:0.00000000}", coordinateY);
                this.Crosshair.X = coordinateX;
                this.Crosshair.Y = coordinateY;
            }
           

            //this.XPixelLabel.Location = new System.Drawing.Point(e.X, e.Y);

            //如果设置十字光标线为不显示数值就不显示了
            if (!CrosshaircheckBox.Checked)
            {
                this.XPixelLabel.Text = "";
                this.YPixelLabel.Text = "";
                this.XCoordinateLabel.Text = "";
                this.YCoordinateLabel.Text = "";
            }

            this.formsPlot1.Refresh(false, false);
        }

        //十字功能复选框改变执行任务
        private void CrosshaircheckBox_CheckedChanged(object sender, EventArgs e)
        {
           
            if (CrosshaircheckBox.Checked) {
                this.Crosshair.VerticalLine.IsVisible = true;
                this.Crosshair.HorizontalLine.IsVisible = true;


            }
            else{
                this.Crosshair.VerticalLine.IsVisible = false;
                this.Crosshair.HorizontalLine.IsVisible = false;

            }
            this.formsPlot1.Refresh(false, false);
        }

        //鼠标进入图表中执行的任务
        private void formsPlot1_MouseEnter(object sender, EventArgs e)
        {
            if (CrosshaircheckBox.Checked)
            {
                this.Crosshair.VerticalLine.IsVisible = true;
                this.Crosshair.HorizontalLine.IsVisible = true;


            }
            else
            {
                this.Crosshair.VerticalLine.IsVisible = false;
                this.Crosshair.HorizontalLine.IsVisible = false;

            }
            
            this.formsPlot1.Refresh(false, false);
        }


        //鼠标离开图表执行的任务
        private void formsPlot1_MouseLeave(object sender, EventArgs e)
        {
            this.Crosshair.VerticalLine.IsVisible = false;
            this.Crosshair.HorizontalLine.IsVisible = false;
            this.formsPlot1.Refresh(false, false);
        }
    }
}

结束语:

本期教程为项目scottplot图表控件增加了十字光标功能,随着鼠标的移动显示十字光标对应在图表的中数值,这个功能肯定对家里来说非常有用,希望大家收藏,以备用时方便查找。下期计划给大家带来为scottplot图表控件增加动态添加数据,随着时间的移动,动态显示数据。感谢各位朋友持续关注我的文章更新。欢迎各位朋友点赞、关注、评论谢谢!

 

  • 10
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值