使用winform的GDI+绘制图表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Test
{
    public partial class Form1 : Form
    {

        private List<int[]> dotList = new List<int[]>();  // 保存每个点的坐标

        private int PART = 25;  // X轴点的距离

        private int num = 0;  // 标记当前点数

        private int XYoffset = 20;
        private int DelNumPos=0;
        private int NumCount=0;

        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        // 绘制
        private void updateShow()
        {
            int width = this.pictureBox1.Size.Width;
            int height = this.pictureBox1.Size.Height;
            Bitmap codeImage = new Bitmap(width, height);  // 制作图片大小的画布
            Graphics g = Graphics.FromImage(codeImage);

            // 绘制XY轴  
            Pen axisPen = new Pen(Color.Black, 3); // 使用黑色细线绘制轴  
            g.DrawLine(axisPen, 0, height-XYoffset , width, height- XYoffset); // X轴,位于图片底部  
            g.DrawLine(axisPen, XYoffset, 0, XYoffset, height); // Y轴,位于图片左侧  


            PointF[] points = new PointF[this.dotList.Count];  //点坐标列表
            //绘制点,并连成线段
            for (int i = 0; i < this.dotList.Count; i++)
            {
                PointF pointF = new PointF(this.dotList[i][0], this.dotList[i][1]);
                points[i] = pointF;
            }


            Pen pen = new Pen(Color.Red, 2);  // 创建绘笔
            g.DrawLines(pen, points);  // 绘制线段          

            //Y轴数值显示
            float YValueOffset = pictureBox1.Size.Height / 8;
            float YLength = pictureBox1.Size.Height-YValueOffset;
            Font font = new Font("宋体", 14, FontStyle.Regular, GraphicsUnit.Pixel);
            Brush brush = Brushes.Red;
            Brush backgroundBrush = Brushes.White; // 假设背景色是白色 
            for (int i=1;i<=8;i++) 
            {
                PointF numberPosition = new PointF(0, YLength); 
                g.DrawString(((int.Parse(this.textBox3.Text)/8)*i).ToString(), font, brush, numberPosition);
                YLength -= YValueOffset;
            }


            //X轴数值显示
            float XValueOffset = pictureBox1.Size.Width / num;
            float XLength = 0 + XValueOffset;
            if (DelNumPos < (this.pictureBox1.Size.Width / PART))
            {
                for (int i = 0; i < num; i++)
                {
                    PointF numberPosition = new PointF(this.dotList[i][0], pictureBox1.Size.Height - XYoffset);
                    g.DrawString((i+NumCount).ToString(), font, brush, numberPosition);
                    XLength += XValueOffset;
                    DelNumPos++;
                }

              
            }
            else
            {
                //清除数字
                for (int i = 0; i < num; i++)
                {
                    Rectangle number5Rect = new Rectangle(this.dotList[i][0], pictureBox1.Size.Height - XYoffset, 5, 5);
                    g.FillRectangle(backgroundBrush, number5Rect);
                }

                NumCount++;
                for (int i = 0; i < num; i++)
                {
                    PointF numberPosition = new PointF(this.dotList[i][0], pictureBox1.Size.Height - XYoffset);
                    g.DrawString((i+NumCount).ToString(), font, brush, numberPosition);
                    XLength += XValueOffset;
                }
                DelNumPos = 0;
                NumCount++;
            }
          

            this.pictureBox1.Image = codeImage;
         
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int[] site = new int[2];

            if (num < (this.pictureBox1.Size.Width / PART))
            { 
                num += 1;             
            }
            else
            {
                //把后一位左边往前移
                this.dotList.RemoveAt(0);
                foreach (int[] dot in this.dotList)
                {
                    dot[0] -= PART;
                }
            }

            site[0] = num * PART+XYoffset;//X轴位置点


            int minY = int.Parse(this.textBox2.Text);
            int maxY = int.Parse(this.textBox3.Text);
            Random rd = new Random();
            int random = rd.Next(minY, maxY);

            site[1] = ((maxY-random) * (this.pictureBox1.Size.Height / (maxY - minY)))- XYoffset;//Y轴位置点

            this.dotList.Add(site);
            this.textBox1.Text = (random+1).ToString();
            if (this.dotList.Count > 1)
                this.updateShow();

        }
      
        

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }

        private  void TextChange(object sender, EventArgs e)
        {
            textBox2.Text = "1";
        }
    }
}

在这里插入图片描述

相关推荐

  1. 使用Python绘制各种图表

    2024-07-10 01:04:06       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 01:04:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 01:04:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 01:04:06       58 阅读
  4. Python语言-面向对象

    2024-07-10 01:04:06       69 阅读

热门阅读

  1. 整车行业APS项目难点(我的APS项目九)

    2024-07-10 01:04:06       23 阅读
  2. 7月07日,每日信息差

    2024-07-10 01:04:06       25 阅读
  3. 定义变量和声明变量、定义类和声明类

    2024-07-10 01:04:06       25 阅读
  4. 2024第三届中国医疗机器人大会第一轮通知

    2024-07-10 01:04:06       16 阅读
  5. 反向业务判断逻辑

    2024-07-10 01:04:06       21 阅读
  6. 决策树构建精要:算法步骤与实现细节

    2024-07-10 01:04:06       21 阅读
  7. 我们为什么要学数据库?

    2024-07-10 01:04:06       20 阅读
  8. redis的setnx实现分布式锁

    2024-07-10 01:04:06       30 阅读
  9. Bert 变种, T5模型

    2024-07-10 01:04:06       21 阅读