点击下载 最新有坐标放大功能工程。
坐标为每10s传感器采集的数据
代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
namespace TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point mouseOffset;
int times = 10;//每10s采集一次数据
int drawflag=0;
private int height = 350;
private int width = 800;
private Bitmap bitmap;
private Graphics graphics;
private float Tension = 0.5f;
//要画曲线的点y坐标值,x值为10s采集一次数据
private float[] curvebuf = new float[] { 20.0f, 30.0f, 40.0f, 35.4f, 21.6f, 32.8f,5.2f,9.9f,30f,35.3f,25.3f,21.2f,30.2f,22.8f,40.5f,5.6f,20.6f };
private void DrawCurve(float [] buf)
{
//创建位图
bitmap = new Bitmap(width, height);
//创建Graphics类对象
graphics = Graphics.FromImage(bitmap);
//清空图片背景色
//graphics.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
//填充背景
graphics.FillRectangle(Brushes.SeaGreen, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
Brush brushPoint = new SolidBrush(Color.Red);
//画图片的边框线
Pen mypenBlack = new Pen(Color.Black, 1);
graphics.DrawRectangle(mypenBlack, 40, 40, 720, 270);//矩形
graphics.DrawRectangle(mypenBlack, 40, 35, 720, 275); //矩形
Pen mypenBlue = new Pen(Color.Blue, 1);//线条
Pen mypenRed = new Pen(Color.Red, 1);
Pen mypenYellow = new Pen(Color.Yellow,1);//点颜色
//绘制线条
//绘制纵向线条
int x = 80;
for (int i = 0; i < 17; i++)
{
graphics.DrawLine(mypenBlue, x, 40, x, 310);
x = x + 40;
}
//绘制横向线条
int y = 70;
for (int i = 0; i < 8; i++)
{
graphics.DrawLine(mypenBlue, 40, y, 760, y);
y = y + 30;
}
//x轴上对应的标记
String[] n = { " 10", " 20", " 30", " 40", " 50", " 60", " 70",
" 80", " 90", " 100", "110", "120", "130", "140", "150", "160", "170"};
x = 70;
for (int i = 0; i < 17; i++)
{
graphics.DrawString(n[i].ToString(), font, Brushes.Red, x, 315); //设置文字内容及输出位置
x = x + 40;
}
//y轴上对应的标记
String[] m = {"40", "35", "30", "25", "20", "15", "10", " 5"};
y = 60;
for (int i = 0; i < 8; i++)
{
graphics.DrawString(m[i].ToString(), font, Brushes.Red, 16, y); //设置文字内容及输出位置
y = y + 30;
}
//画曲线
if (drawflag == 1)
{
int len = buf.Length;
PointF[] CurvePointF = new PointF[len];//坐标点
float pointX = 0;
float pointY = 0;
for (int i = 0; i < len; i++)
{
pointX = i * times * 4 + 80; //坐标系(0,0)实际位置为(70,310)实际工程坐标左上角为原点。
pointY = 310 - 60 * (buf[i] / 10);
CurvePointF[i] = new PointF(pointX, pointY);
graphics.FillEllipse(brushPoint, pointX-4, pointY-4, 8, 8);//画点,是以圆心为切点画的圆,所以减去半径
}
graphics.DrawCurve(mypenYellow, CurvePointF, Tension);//画曲线
label2.Text = Convert.ToString("输出点个数:"+len);
}
graphics.Dispose();
this.pictureBox1.Image = bitmap;
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Visible = false;
label1.Parent = pictureBox1;
DrawCurve(curvebuf);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f=new Form2();
f.Show();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
int ex = e.X;
int ey = e.Y;
mouseOffset.X= (e.X-40)/4;
mouseOffset.Y=(310- e.Y)/6;
if ((ex > 40 && ex < 760) && (ey > 40 && ey < 310))
{
label1.BackColor = Color.FromArgb(80, Color.White);
label1.Visible = true;
label1.Text = ("坐标值:" + "\n" + "x=" + mouseOffset.X + "\n" + "y=" + mouseOffset.Y);
label1.Location = new Point(ex+20, ey+10);
}
else
{
label1.Visible = false;
}
}
private void buttonDraw_Click(object sender, EventArgs e)
{
int lens = curvebuf.Length;
if (lens <= 1)
{
drawflag = 0;
MessageBox.Show("数据小于2!", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
drawflag = 1;
DrawCurve(curvebuf);
}
}
private void buttonClear_Click(object sender, EventArgs e)
{
drawflag = 0;
label2.Text = Convert.ToString("输出点个数:");
DrawCurve(curvebuf);
}
}
}