使用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";
}
}
}
原文地址:https://blog.csdn.net/weixin_52620712/article/details/140308189
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!