首页 > 编程语言 >C#的Winform通过GDI+以及Datatable绘制基于CSV文件的风羽图

C#的Winform通过GDI+以及Datatable绘制基于CSV文件的风羽图

时间:2024-04-02 16:01:01浏览次数:29  
标签:move using C# float System GDI 风羽图 new pan

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

相关文章

  • C#读写并修改csv文件特定列或行的值,例如cos和sin值
    usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceCSVHelper{publicpartial......
  • k8s + springcloud 微服务开发调试工具kt Connect的使用
    概览KtConnect(全称KubernetesToolkitConnect)是一款基于Kubernetes环境用于提高本地测试联调效率的小工具。通过这个工具,可以不在本地启动所有服务,只需启动当前开发的服务即可,其它服务使用的是部署在k8s集群的实例,如下图:Reference官方文档:https://github.com/alibaba/......
  • SQL语句中 DDL 、DML 、DQL 、DCL学习、看板
    DDL语句(数据定义语言)DDL(DataDefinitionLanguage)语句:数据定义语言,主要是进行定义/改变表的结构,数据类型,表之间的连接操作.常用的语句关键字有CREATE,DROP,ALTER等.CREATETABLE表名(列名1数据类型,列名2数据类型,列名3数据类型,...)​ALTERTABLE表名;......
  • 用C语言输出100到200以内的所有素数 (只能被本身或则1整除的数)
    代码如下#include<stdio.h>intmain(){//输出100到200以内的所有素数(只能被本身或则1整除的数)   inti=0,j=0;   printf("100到200以内的所有素数为:");   for(i=100;i<=200;i++)   {         for(j=2;j<i;j++)  ......
  • 2024 CleanMyMac X 4.15.2的功能介绍及如何使用
    CleanMyMacX4.15.2:释放Mac空间的强大工具随着我们使用Mac进行工作、学习和娱乐,我们的硬盘空间可能会逐渐被各种文件、应用程序和缓存数据填满。为了保持Mac的性能和效率,定期清理和优化硬盘空间变得至关重要。而CleanMyMacX4.15.2正是一款强大的Mac清理工具,它可以帮助我们......
  • 展锐平台sensorhub架构(SC9863A)
    安卓架构图: Sensorhub架构 SensorHub 是Sensor 软件运行环境,分为Sensor manager:管理sensordriver 采样和上报数据给AP, 接收AP 下发的命令;AP: 应用程序执行环境,HAL:sensorhal 层接口实现,SensorHubDriver: 接收HAL 层的命令并发给sensorhub,反馈sensorhub ......
  • 执行计划】Oracle 11gR2使用Full outer Joins执行计划完成全外连接查询
    1.创建实验表并初始化实验数据sys@ora11g>select*fromv$version;BANNER------------------------------------------------------------------------OracleDatabase11gEnterpriseEditionRelease11.2.0.1.0-ProductionPL/SQLRelease11.2.0.1.0-ProductionCORE  ......
  • HTML设置定时执行代码 JavaScript 计时事件
    1、https://www.runoob.com/js/js-timing.htmlJavaScript一个设定的时间间隔之后来执行代码我们称之为计时事件JavaScript计时事件通过使用JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。在JavaScript......
  • 高亮数显驱动芯片/点阵LED驱动IC-VK16K33B/BA SOP24/SSOP24,适用于电饭煲,温控仪,数字电
    产品品牌:永嘉微电/VINKA产品型号:VK16K33B/BA封装形式:SOP24/SSOP24概述VK16K33B/BA是一种带按键扫描接口的数码管或点阵LED驱动控制专用芯片,内部集成有数据锁存器、键盘扫描、LED驱动模块等电路。数据通过I2C通讯接口与MCU通信。SEG脚接LED阳极,GRID脚接LED阴极,可支持12SEGx8......
  • C语言程序设计—实验报告四
    C语言程序设计—实验报告四一、实验目的1.在熟练掌握if语句和switch语句的基础上,能灵活使用if语句和switch语句进行选择结构的程序设计2.学习调试程序二、实验硬、软件环境Windows计算机、Devc6.0三、实验内容及步骤项目一解题思路首先声明一个字符变量ch使用getchar()函......