C# 窗体应用程序 Chart控件显示实时曲线

news2024/10/6 6:46:54

IDE: VS2019
项目模板:C# windows 窗体应用(.NET Framework)

【参考】

  1. B站上教程C#Chart控件画折线图的使用,关于Chart控件的属性,介绍得非常详细。
  2. B站上教程C#上位机Chart控件实时曲线终极讲解,对鼠标滚轮事件等,多个事件的讲解较为详细。
  3. 工具箱中找不到Chart控件怎么办->VS/C#添加chart控件
    项目结构非常简单,一个窗体ArtDAQ.cs和一个主程序Program
    在这里插入图片描述# 【遇到的问题】
    Timer控件拖拽到设计器上,显示不出来,无法通过双击的方式给Timer控件添加事件
    手动写了一个InitializeTimer函数,在该函数中,给Timer控件的对象timer1绑定了一个事件timer1_Tick
timer1.Tick += new EventHandler(timer1_Tick);

InitializeTimer函数添加进ArtDAQ的构造函数中,然后在后面继续写timer1_Tick函数。
在这里插入图片描述
ArtDAQ.cs代码

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 ArtDAQ
{
    public partial class ArtDAQ : Form
    {
        // 设置Tiemr事件的间隔事件
        private void InitializeTimer()
        {
            // 设置Timer事件的间隔时间(在此例中为2000毫秒)
            timer1.Interval = 100;
            // 启动Timer
            // timer1.Start();
            // 绑定Tick事件
            timer1.Tick += new EventHandler(timer1_Tick);
        }

        // 构造函数
        public ArtDAQ()
        {
            InitializeComponent();
            InitializeTimer();
        }
        
        // 触发按钮
        private void button1_Click(object sender, EventArgs e)
        {
           if (timer1.Enabled == false)
            {
                // timer1.Enabled = true;
                timer1.Start();
                MessageBox.Show(timer1.Enabled.ToString());
            }
           else
            {
                timer1.Enabled = false;
            }
        }

        // Timer的事件
        // 随机数
        Random rd = new Random();       
        int x = 0;
        int y = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            y = rd.Next(0, 100+1);
            chart1.Series[0].Points.AddXY(x, y);
            if(x>=101)
            {
                timer1.Enabled = false;
                // timer1.Stop();
            }
            x++;
        }
    }
}

窗体设计文件ArtDAQ.Designer.cs


namespace ArtDAQ
{
    partial class ArtDAQ
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend3 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series7 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.DataPoint dataPoint3 = new System.Windows.Forms.DataVisualization.Charting.DataPoint(0D, 0D);
            System.Windows.Forms.DataVisualization.Charting.Series series8 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series9 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Title title3 = new System.Windows.Forms.DataVisualization.Charting.Title();
            this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
            this.SuspendLayout();
            // 
            // chart1
            // 
            chartArea3.AxisX.Interval = 5D;
            chartArea3.AxisX.Maximum = 100D;
            chartArea3.AxisX.Minimum = 0D;
            chartArea3.AxisY.Interval = 5D;
            chartArea3.AxisY.Maximum = 100D;
            chartArea3.AxisY.Minimum = 0D;
            chartArea3.CursorX.IsUserEnabled = true;
            chartArea3.CursorX.IsUserSelectionEnabled = true;
            chartArea3.Name = "ChartArea1";
            chartArea3.Position.Auto = false;
            chartArea3.Position.Height = 90F;
            chartArea3.Position.Width = 90F;
            chartArea3.Position.X = 3F;
            chartArea3.Position.Y = 10F;
            this.chart1.ChartAreas.Add(chartArea3);
            legend3.Name = "Legend1";
            this.chart1.Legends.Add(legend3);
            this.chart1.Location = new System.Drawing.Point(19, 12);
            this.chart1.Name = "chart1";
            series7.ChartArea = "ChartArea1";
            series7.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            series7.Legend = "Legend1";
            series7.Name = "Series1";
            series7.Points.Add(dataPoint3);
            series8.ChartArea = "ChartArea1";
            series8.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            series8.Legend = "Legend1";
            series8.Name = "Series2";
            series9.ChartArea = "ChartArea1";
            series9.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            series9.Legend = "Legend1";
            series9.Name = "Series3";
            this.chart1.Series.Add(series7);
            this.chart1.Series.Add(series8);
            this.chart1.Series.Add(series9);
            this.chart1.Size = new System.Drawing.Size(692, 375);
            this.chart1.TabIndex = 0;
            this.chart1.Text = "chart1";
            title3.BorderWidth = 2;
            title3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold);
            title3.ForeColor = System.Drawing.Color.CornflowerBlue;
            title3.Name = "Title1";
            title3.Text = "Chart1";
            this.chart1.Titles.Add(title3);
            // 
            // timer1
            // 
            // this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(734, 51);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(93, 47);
            this.button1.TabIndex = 1;
            this.button1.Text = "显示曲线";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // ArtDAQ
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(875, 473);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.chart1);
            this.Name = "ArtDAQ";
            this.Text = "ArtDAQ";
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        private System.Windows.Forms.Timer timer1; 
        private System.Windows.Forms.Button button1;
    }
}

【项目地址】:

链接:https://pan.baidu.com/s/1HhBg620l0nMsSQ07j8MdOQ
提取码:6wnn
–来自百度网盘超级会员V5的分享

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1607773.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

NLP任务全览:涵盖各类NLP自然语言处理任务及其面临的挑战

自然语言处理(Natural Language Processing, 简称NLP&#xff09;是计算机科学与语言学中关注于计算机与人类语言间转换的领域。NLP将非结构化文本数据转换为有意义的见解&#xff0c;促进人与机器之间的无缝通信&#xff0c;使计算机能够理解、解释和生成人类语言。人类等主要…

基于SkyEye运行Qt:著名应用程序开发框架

Qt是一个著名的跨平台的C图形用户界面应用程序开发框架&#xff0c;目前包括Qt Creator、Qt Designer等等快速开发工具&#xff0c;还支持2D/3D图形渲染、OpenGL&#xff0c;允许真正的组件编程&#xff0c;是与GTK、MFC、OWL、ATL一样的图形界面库。使用Qt开发的软件可以做到一…

excel 无法正确处理 1900-03-01 前的日期

问题由来&#xff1a;excel 用公式 TEXT(A1,"yyyy-mm-dd") 转日期时&#xff0c;当A1 的值等于59 的时候&#xff0c;返回值是1900-02-28&#xff1b;当A1 的值等于61 的时候&#xff0c;返回值是1900-03-01&#xff1b;那么当 A1的值为 60 的时候&#xff0c;返回值…

[大模型]MiniCPM-2B-chat WebDemo部署

MiniCPM-2B-chat WebDemo部署 MiniCPM-2B-chat 介绍 MiniCPM 是面壁智能与清华大学自然语言处理实验室共同开源的系列端侧大模型&#xff0c;主体语言模型 MiniCPM-2B 仅有 24亿&#xff08;2.4B&#xff09;的非词嵌入参数量。 经过 SFT 后&#xff0c;MiniCPM 在公开综合性…

开发与产品的战争之自动播放视频

开发与产品的战争之自动播放视频 起因 产品提了个需求&#xff0c;对于网站上的宣传视频&#xff0c;进入页面就自动播放。但是基于我对chromium内核的一些浅薄了解&#xff0c;我当时就给拒绝了: “浏览器不允许”。&#xff08;后续我们浏览器默认都是chromium内核的&#…

C盘越用越大?教你如何科学管理C盘空间

前言&#xff1a; 如图&#xff0c;左边是我多开的E5电脑&#xff0c;装的是LTSC2019_210707F多开封装版&#xff0c;C盘占用8.5GB&#xff0c;右边是我平常打游戏写代码的电脑&#xff0c;装的是Win11 22H2&#xff0c;C盘占用30GB。两台电脑都关闭了休眠&#xff0c;C盘的虚拟…

【鸿蒙开发】画布组件 Canvas

1. Canvas 提供画布组件&#xff0c;用于自定义绘制图形。 接口&#xff1a; Canvas(context?: CanvasRenderingContext2D) 参数&#xff1a; 参数名 参数类型 必填 默认值 参数描述 context CanvasRenderingContext2D 否 - 不支持多个Canvas共用一个CanvasRende…

GPT国内怎么用

2022年11月&#xff0c;OpenAI发布了ChatGPT&#xff0c;这标志着大型语言模型在自然语言处理领域迈出了巨大的一步。ChatGPT不仅在生成文本方面表现出了惊人的流畅度和连贯性&#xff0c;更为人工智能应用开启了全新的可能性。 ChatGPT的推出促进了人工智能技术在多个领域的广…

查看apk是64位32位(三种方法)

通过检查APK文件&#xff0c;你可以确定该APK支持的架构类型&#xff0c;包括它是为64位&#xff08;例如arm64-v8a、x86_64&#xff09;还是32位&#xff08;例如armeabi-v7a、x86&#xff09;架构准备的。Android应用程序可以包含多个不同的二进制文件&#xff0c;每个文件针…

数组和指针的联系(C语言)

数组和指针是两种不同的数据类型&#xff0c;数组是一种构造类型&#xff0c;用于存储一组相同类型的变量&#xff1b;而指针是一种特殊类型&#xff0c;专门用来存放数据的地址。数组名除了sizeof(数组名)和&数组名表示整个数组外&#xff0c;其他情况下都表示的是首元素的…

说说你对图的理解?相关操作有哪些?

一、是什么 在计算机科学中&#xff0c;图是一种抽象的数据类型&#xff0c;在图中的数据元素通常称为结点&#xff0c;V是所有顶点的集合&#xff0c;E是所有边的集合 如果两个顶点v,w&#xff0c;只能由v向w&#xff0c;而不能由w向v&#xff0c;那么我们就把这种情况叫做一…

Leetcode - 周赛393

目录 一&#xff0c;3114. 替换字符可以得到的最晚时间 二&#xff0c;3115. 素数的最大距离 三&#xff0c;3116. 单面值组合的第 K 小金额 四&#xff0c; 3117. 划分数组得到最小的值之和 一&#xff0c;3114. 替换字符可以得到的最晚时间 本题是一道模拟题&#xff0c;…

有效的括号 + 点击消除 || 匹配问题

目录 点击消除&#xff1a; 有效的括号&#xff1a; 点击消除&#xff1a; 点击消除_牛客题霸_牛客网 (nowcoder.com)https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5?tpId308&tqId40462&ru/exam/oj 如题目所述&#xff0c; 示例1&#xf…

顺丰同城急送API对接(附源码)

一、背景 最近公司让我对接顺丰同城急送的API&#xff0c;讲讲里面需要注意的几点 官方的API文档有些示例代码也不全&#xff0c;具体细节不多说&#xff0c;如果你现在也需要对接他们API&#xff0c;可以参考本篇博客再配合官方文档结合起来看&#xff0c;可以让您再开发的时…

C++面向对象程序设计-北京大学-郭炜【课程笔记(七)】

C面向对象程序设计-北京大学-郭炜【课程笔记&#xff08;七&#xff09;】 1、类型转换运算符2、自增、自减运算符的重载3、继承和派生的基本概念3.1、基本概念3.2、派生类对象的内存空间 4、继承关系和复合关系4.1、继承关系的使用4.2、复合关系的使用 5、派生类覆盖基类成员6…

【分治】Leetcode 数组中的第K个最大元素

题目讲解 数组中的第K个最大元素 算法讲解 堆排序&#xff1a;1. 寻找最后一个节点的父亲&#xff0c;依次向上遍历&#xff0c;完成小堆的建立&#xff1b;2. 从最后一个元素开始&#xff0c;和堆顶的数据做交换&#xff0c;此时最小的数据在对后面&#xff0c;然后对剩下的…

C++设计模式|创建型 4.建造者模式

1.什么是建造者模式? 建造者模式&#xff08;也被成为生成器模式&#xff09;&#xff0c;是一种创建型设计模式&#xff0c;软件开发过程中有的时候需要创建很复杂的对象&#xff0c;而建造者模式的主要思想是将对象的构建过程分为多个步骤&#xff0c;并为每个步骤定义一个…

OpenHarmony图形处理库—pyclipper [GN编译]

简介 pyclipper是图形处理库&#xff0c;用于剪裁和偏移直线和多边形。 下载安装 直接在OpenHarmony-SIG仓中搜索pyclipper并下载。 使用说明 以OpenHarmony 3.1 Beta的rk3568版本为例 将下载的pyclipper库代码存在以下路径&#xff1a;./third_party/pyclipper 修改添加依…

Java 算法篇-深入了解 BF 与 KMP 算法

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 BF 算法概述 1.1 BF 算法实际使用 2.0 KMP 算法概述 2.1 KMP 算法实际使用 2.2 相比于 BF 算法实现&#xff0c;KMP 算法的重要思想 2.3 为什么要这样设计&#x…

ATFX汇市:日元贬值导致进口物价走高,日央行或有二次加息计划

消息面&数据面&#xff1a; 日本央行行长植田和男表示&#xff0c;弱势日元可能影响通胀趋势&#xff0c;如果这样可能导致政策转变。意思是说&#xff0c;随着日元汇率逼近160.00&#xff0c;日元贬值对进口物价的影响越来越明显。如果日元继续保持贬值态势&#xff0c;日…