C#使用Chart绘制曲线

作者:金增辉 时间:2023-03-12 19:08:56 

本文实例为大家分享了C#使用Chart绘制曲线的具体代码,供大家参考,具体内容如下

新建一个控制台应用程序,程序名:WindowsFormsApp2,将下面的代码拷贝进去即可

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;
using System.Windows.Forms.DataVisualization.Charting;
namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
          
            
            timer1.Interval = 5000;
            textBox1.Text = "0";
      
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // 设置曲线的样式
            Series series = chart1.Series[0];
            // 画样条曲线(Spline)
            series.ChartType = SeriesChartType.Spline;
            // 线宽2个像素
            series.BorderWidth = 2;
            // 线的颜色:红色
            series.Color = System.Drawing.Color.Red;
            // 图示上的文字
            series.LegendText = "动态曲线";

            // 测试代码 数据转换
            int values = Convert.ToInt16(textBox1.Text);

            // 在chart中显示数据
            int x = 0;
            series.Points.AddXY(x, values);
            x++;
            if (x == 100) x = 0;

            // 设置显示范围
            ChartArea chartArea = chart1.ChartAreas[0];
            chartArea.AxisX.Minimum = 0;
            chartArea.AxisX.Maximum = System.Double.NaN;
            chartArea.AxisY.Minimum = 0d;
            chartArea.AxisY.Maximum = System.Double.NaN;  //自动去判断

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

小编分享一段收藏的代码:C#更新数组实现动态曲线:

private void timer定时_Tick(object sender, EventArgs e)
        {
            if (Flash == 0)          //当刷新时
            {
                Pen middleLine = new Pen(Color.White);
                SolidBrush blackBrush = new SolidBrush(Color.Black);   //定义了一个单色的黑色画笔
                graphic = Graphics.FromImage(pictureBox图像.Image);
                graphic.FillRectangle(blackBrush, 0, 0, 500, 251);     //将画布填充为黑色矩形            
                Point pt1 = new Point(0, 125);          //中间线的两点
                Point pt2 = new Point(500, 125);
                graphic.DrawLine(middleLine, pt1, pt2);        //画中间线
            }
 
 
            Random random = new Random();
            pointList = new Point[50];    //实例化坐标数组*
            int data = random.Next(-800, 800);   //生成一个随机数
           // Point p;       //申明一个坐标变量
            for (int i = 1; i <50 ; i++)         //更新数组
            {
                N[i - 1] = N[i];
                pointList[i - 1] = pointList[i];      //更新点的坐标
            }
            N[49] = data;
            pointList[49].X = 49 * time;
            pointList[49].Y = data;
                 
            if (Flash == 0)
            {
                if (Flash == 0)
                {
                    int k1 = 0, k2 = 0;
                    for (int i = 0; i < 49; i++)
                    {
                        k1 = (int)(lineD - N[i] / times);
                        k2 = (int)(lineD - N[i + 1] / times);
                        graphic.FillEllipse(new SolidBrush(Color.Red), (i * time), k1, 4, 4); //填充边框所定义的椭圆内部,由椭圆边框左上角坐标和椭圆宽度和高度组成
                        //graphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0), 1), new Point(i * time, k1), new Point((i + 1) * time, k2));   //两点之间连线
                        pointList[i].X = i * time;  pointList[i].Y = k1; 
                    }
                    graphic.FillEllipse(new SolidBrush(Color.Red), (49 * time), k2, 4, 4);
                    graphic.DrawCurve(new Pen(Color.FromArgb(255, 255, 0), 1), pointList, 0.5f);
                       
                }
                pictureBox图像.Refresh();  //刷新picturebox
            }
}

来源:https://blog.csdn.net/qq_18975227/article/details/109226563

标签:C#,Chart,曲线
0
投稿

猜你喜欢

  • SpringMVC的源码解析

    2022-10-05 20:12:18
  • 20个非常实用的Java程序代码片段

    2022-02-08 10:35:44
  • Android实现多个连续带数字圆圈效果

    2021-09-10 02:46:23
  • Android开发之模仿微信打开网页的进度条效果(高仿)

    2021-09-02 10:17:04
  • Spring Bean自动装配入门到精通

    2023-11-03 07:29:09
  • 探讨Java中的深浅拷贝问题

    2023-01-07 01:12:44
  • Java多线程之Semaphore实现信号灯

    2023-09-19 23:04:51
  • 永久解决idea git log乱码的问题

    2022-01-10 06:13:13
  • Android中RecyclerView 滑动时图片加载的优化

    2021-07-29 09:58:12
  • 分享Spring Cloud OpenFeign 的五个优化技巧

    2022-06-23 08:53:30
  • Java统计字符串中字符出现次数的方法示例

    2023-11-25 08:23:02
  • 浅析java中next与nextLine用法对比

    2022-01-11 01:02:53
  • Maven+Tomcat8 实现自动化部署的方法

    2023-01-03 06:44:20
  • Java中的字节流文件读取教程(一)

    2023-08-14 08:14:47
  • 新手初学Java流程控制

    2023-08-23 08:12:34
  • Java中\\n和\\r区别

    2023-01-16 10:38:53
  • Spring MVC全局异常实例详解

    2021-11-19 13:51:26
  • 浅谈Java线程并发知识点

    2021-10-20 13:11:13
  • Java面向对象程序设计:抽象类,接口用法实例分析

    2023-03-08 23:56:07
  • Mybatis 如何开启控制台打印sql语句

    2023-08-02 02:01:53
  • asp之家 软件编程 m.aspxhome.com