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.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
using System.Net.NetworkInformation;
using System.IO;
using System.Runtime.CompilerServices;
namespace XY
{
internal class feng
{
public static void DrawXY(Panel pan)
{
Graphics g = pan.CreateGraphics();
//整体内缩move像素
float move = 35f;
float newX = pan.Width - move;
float newY = pan.Height - move;
//绘制X轴,
PointF px1 = new PointF(move, newY);
PointF px2 = new PointF(newX, newY);
g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
//绘制Y轴
PointF py1 = new PointF(move, move);
PointF py2 = new PointF(move, newY);
g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
}
/// <summary>
/// 画出X轴上的分值线,从零开始
/// </summary>
/// <param name="pan"></param>
/// <param name="maxX"></param>
/// <param name="len"></param>
public static void DrawXLine(Panel pan, float maxX, int len, DataTable dt)
{
int h = 25;
int z = 13;
int s = 12;
float move = 35f;
float LenX = pan.Width - 2 * move;
float LenY = pan.Height - 2 * move;
Graphics g = pan.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias;
len = 90;
Pen pen = new Pen(Color.Black, 1);
for (int j = 1; j <= 24; j++)
{
for (int i = 1; i <= len; i++)
{
if (Convert.ToDouble(dt.Rows[i][s]) <= 2)
{
g.TranslateTransform(LenX * i / len + move + 1, LenY * (h - j) / h + move);
// 旋转画板
g.RotateTransform(float.Parse((dt.Rows[i][z]).ToString()));
g.TranslateTransform(-LenX * i / len - move - 1, -LenY * (h - j) / h - move);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move - 3, LenY * (h - j) / h + move + 7);//添加竖直线
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move + 1, LenY * (h - j) /h + move + 7);//添加横直线
g.ResetTransform();
}
if (2 < Convert.ToDouble(dt.Rows[i][s]) && Convert.ToDouble(dt.Rows[i][s]) <= 4)
{
g.TranslateTransform(LenX * i / len + move +1, LenY * (h - j) / h + move);
// 旋转画板
g.RotateTransform(float.Parse((dt.Rows[i][z]).ToString()));
g.TranslateTransform(-LenX * i / len - move -1, -LenY * (h - j) / h - move);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i/ len + move - 3, LenY * (h - j) / h + move + 7);//添加竖直线
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i/ len + move + 5, LenY * (h - j) / h + move - 7);
g.ResetTransform();
}
if (4 < Convert.ToDouble(dt.Rows[i][s]) && Convert.ToDouble(dt.Rows[i][s]) <= 6)
{
g.TranslateTransform(LenX * i / len + move + 1, LenY * (h - j) / h + move);
// 旋转画板
g.RotateTransform(float.Parse((dt.Rows[i][z]).ToString()));
g.TranslateTransform(-LenX * i / len - move - 1, -LenY * (h - j) / h - move);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move - 3, LenY * (h - j) / h + move + 7);//添加竖直线
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move -7, LenX * i / len + move + 5, LenY * (h - j) / h + move -7);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move -3, LenX * i / len + move + 1, LenY * (h - j) / h + move -3);//添加横直线
g.ResetTransform();
}
if (6 < Convert.ToDouble(dt.Rows[i][s]) && Convert.ToDouble(dt.Rows[i][s]) <= 8)
{
g.TranslateTransform(LenX * i / len + move + 1, LenY * (h - j) / h + move);
// 旋转画板
g.RotateTransform(float.Parse((dt.Rows[i][z]).ToString()));
g.TranslateTransform(-LenX * i / len - move - 1, -LenY * (h - j) / h - move);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move - 3, LenY * (h - j) / h + move + 7);//添加竖直线
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move + 5, LenY * (h - j) / h + move - 7);
g.DrawLine(pen, LenX * i/ len + move - 3, LenY * (h - j) / h + move - 3, LenX * i / len + move + 5, LenY * (h - j) / h + move - 3);
g.ResetTransform();
}
if (8 < Convert.ToDouble(dt.Rows[i][s]) && Convert.ToDouble(dt.Rows[i][s]) <= 10)
{
g.TranslateTransform(LenX * i / len + move + 1, LenY * (h - j) / h + move);
// 旋转画板
g.RotateTransform(float.Parse((dt.Rows[i][z]).ToString()));
g.TranslateTransform(-LenX * i / len - move - 1, -LenY * (h - j) / h - move);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move - 3, LenY * (h - j) / h + move + 7);//添加竖直线
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move + 5, LenY * (h - j) / h + move - 7);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 3, LenX * i / len + move + 5, LenY * (h - j) / h + move - 3);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move , LenX * i / len + move + 1, LenY * (h - j) / h + move );
g.ResetTransform();
}
if (10 < Convert.ToDouble(dt.Rows[i][s]) && Convert.ToDouble(dt.Rows[i][s]) <= 12)
{
g.TranslateTransform(LenX * i / len + move + 1, LenY * (h - j) / h + move);
// 旋转画板
g.RotateTransform(float.Parse((dt.Rows[i][z]).ToString()));
g.TranslateTransform(-LenX * i / len - move - 1, -LenY * (h - j) / h - move);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move - 3, LenY * (h - j) / h + move + 7);//添加竖直线
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 7, LenX * i / len + move + 5, LenY * (h - j) / h + move - 7);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move - 3, LenX * i / len + move + 5, LenY * (h - j) / h + move - 3);
g.DrawLine(pen, LenX * i / len + move - 3, LenY * (h - j) / h + move, LenX * i / len + move + 5, LenY * (h - j ) / h + move);
g.ResetTransform();
}
}
s += 4;
z += 4;
}
g.ResetTransform();
g.Dispose();
}
public static DataTable ReadCSV(string fileName)
{
DataTable dt = new DataTable();
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
//记录每次读取的一行记录
string strLine = null;
//记录每行记录中的各字段内容
string[] arrayLine = null;
//判断,若是第一次,建立表头
bool isFirst = true;
//列的个数
int dtColumns = 0;
//逐行读取CSV文件
while ((strLine = sr.ReadLine()) != null)
{
strLine = strLine.Trim();//去除头尾空格
arrayLine = strLine.Split(',');//分隔字符串,返回数组
if (isFirst) //建立表头
{
dtColumns = arrayLine.Length;//列的个数
for (int i = 0; i < dtColumns; i++)
{
dt.Columns.Add(arrayLine[i]);//每一列名称
}
isFirst = false;
}
else //表内容,另建一行
{
DataRow dataRow = dt.NewRow();//新建一行
for (int j = 0; j < dtColumns; j++)
{
if (arrayLine.Length > j)
{
dataRow[j] = arrayLine[j];
}
}
dt.Rows.Add(dataRow);//添加一行
}
}
sr.Close();
fs.Close();
return dt;
}
}
}
这一部分部分为风羽的绘制及通过datatable进行CSV文件数据的读取。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace PointToPic
{
class XYLinesFactory
{
#region 画出X轴与Y轴
/// <summary>
/// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边50像素
/// </summary>
/// <param name="pan"></param>
public static void DrawXY(Panel pan)
{
Graphics g = pan.CreateGraphics();
//整体内缩move像素
float move = 35f;
float newX = pan.Width - move;
float newY = pan.Height - move;
//绘制X轴,
PointF px1 = new PointF(move, newY);
PointF px2 = new PointF(newX, newY);
g.DrawLine(new Pen(Brushes.Black, 1), px1, px2);
//绘制Y轴
PointF py1 = new PointF(move, move);
PointF py2 = new PointF(move, newY);
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
}
#endregion
/// <summary>
/// 画出Y轴上的分值线,从零开始
/// </summary>
/// <param name="pan"></param>
/// <param name="maxY"></param>
/// <param name="len"></param>
#region 画出Y轴上的分值线,从零开始
//public static void DrawYLine(Panel pan, float maxY, int len)
//{
// float move = 30f;
// float LenX = pan.Width - 2 * move;
// float LenY = pan.Height - 2 * move;
// Graphics g = pan.CreateGraphics();
// for (int i = 0; i <= len; i++) //len等份Y轴
// {
// PointF px1 = new PointF(move, LenY * i / len + move);
// PointF px2 = new PointF(move + 4, LenY * i / len + move);
// string sx = (maxY - maxY * i / len).ToString();
// g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
// StringFormat drawFormat = new StringFormat();
// drawFormat.Alignment = StringAlignment.Far;
// drawFormat.LineAlignment = StringAlignment.Center;
// g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
// }
// Pen pen = new Pen(Color.Black, 1);
// g.DrawString("Y轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
//}
#endregion
/// <summary>
/// 画出Y轴上的分值线,从任意值开始
/// </summary>
/// <param name="pan"></param>
/// <param name="minX"></param>
/// <param name="maxx"></param>
/// <param name="len"></param>
#region 画出X轴上的分值线,从任意值开始
public static void DrawYLine(Panel pan, float minY, float maxY, int len)
{
int time1 = 38;
int miao = 0;
int fen = 54;
float move = 35f;
float LenX = pan.Width - 2 * move;
float LenY = pan.Height - 2 * move;
Graphics g = pan.CreateGraphics();
string sx = null;
len = 90;
for (int i = 0; i <= len; i++) //len等份Y轴
{
PointF px1 = new PointF(LenX * i / len + move, pan.Height - move - 4);
PointF px2 = new PointF(LenX * i / len + move, pan.Height - move);
//string sx = (maxY - (maxY - minY) * i / len).ToString();
if (time1 < 60)
{
miao=time1;
time1 += 2;
}
else
{
time1 = 0;
miao = 0;
fen += 1;
}
if(i== 0)
{
sx = ("0");
}
else
{
sx = (Convert.ToString(fen) + ":" + Convert.ToString(miao));
}
//((fen).ToString()+":"+(miao).ToString);
g.DrawLine(new Pen(Brushes.Black, 1), px1, px2);
//g.TranslateTransform(move / 1.2f, LenY * i / len + move * 1.1f);
旋转画板
//g.RotateTransform(60);
//g.TranslateTransform(-move / 1.2f, -LenY * i / len - move * 1.1f);
//g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
g.TranslateTransform(LenX * i / len + move, pan.Height - move / 1.1f);
// 旋转画板
g.RotateTransform(60);
g.TranslateTransform(-LenX * i / len - move, -pan.Height + move / 1.1f);
g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(LenX * i / len + move, pan.Height - move / 1.1f));
g.ResetTransform();
}
Pen pen = new Pen(Color.Black, 1);
//g.DrawString("时间", new Font("宋体 ", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
g.DrawString("时间", new Font("宋体 ", 10f), Brushes.Black, new PointF(pan.Width - move / 1.5f, pan.Height - move / 1.5f));
}
#endregion
/// <summary>
/// 画出X轴上的分值线,从零开始
/// </summary>
/// <param name="pan"></param>
/// <param name="maxY"></param>
/// <param name="len"></param>
#region 画出Y轴上的分值线,从零开始
public static void DrawXLine(Panel pan, float maxX, int len)
{
int high = 300;
float move = 35f;
float LenX = pan.Width - 2 * move;
float LenY = pan.Height - 2 * move;
Graphics g = pan.CreateGraphics();
len = 25;
for (int i = 1; i <= len; i++)
{
PointF py1 = new PointF(move, LenY * i / len + move);
PointF py2 = new PointF(move + 4, LenY * i / len + move);
//string sy = (maxX * i / len).ToString();
if (high == 300)
{
string sy = (high).ToString();
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
high -=20;
}
if ( i ==2&&high ==280)
{
string sy = (high).ToString();
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
high -= 20;
}
if (i > 2&& high>60)
{
string sy = (high).ToString();
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
high -= 10;
}
if (i>22&&high == 60)
{
string sy = (high).ToString();
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
high -= 15;
}
if (i>23&&high == 45)
{
string sy = (high).ToString();
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
high -= 45;
}
if (i ==25 && high == 0)
{
string sy = (high).ToString();
g.DrawLine(new Pen(Brushes.Black, 1), py1, py2);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Far;
drawFormat.LineAlignment = StringAlignment.Center;
g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
}
//string sy = ( i ).ToString();
}
Pen pen = new Pen(Color.Black, 1);
g.DrawString("(m)", new Font("宋体 ", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
}
#endregion
//#region 画出X轴上的分值线,从任意值开始
/ <summary>
/ 画出X轴上的分值线,从任意值开始
/ </summary>
/ <param name="pan"></param>
/ <param name="minX"></param>
/ <param name="maxX"></param>
/ <param name="len"></param>
//public static void DrawXLine(Panel pan, float minX, float maxX, int len)
//{
// float move = 50f;
// float LenX = pan.Width - 2 * move;
// float LenY = pan.Height - 2 * move;
// Graphics g = pan.CreateGraphics();
// for (int i = 0; i <= len; i++)
// {
// PointF py1 = new PointF(LenX * i / len + move, pan.Height - move - 4);
// PointF py2 = new PointF(LenX * i / len + move, pan.Height - move);
// string sy = ((maxX - minX) * i / len + minX).ToString();
// g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
// g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(LenX * i / len + move, pan.Height - move / 1.1f));
// }
// Pen pen = new Pen(Color.Black, 1);
// g.DrawString("X轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(pan.Width - move / 1.5f, pan.Height - move / 1.5f));
//}
//#endregion
}
}
这一部分为横纵坐标XY轴的绘制,全部代码见附件。
标签:move,using,C#,float,System,GDI,风羽图,new,pan From: https://blog.csdn.net/Stupid114514/article/details/137147029