首页

C# 多折线图表分析图示例(含数据库脚本)

c#

2020-9-10

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Case06_11
{
    public partial class Form1 : Form
    {
        private string connectString = "server=.;Trusted_Connection=true;database=SQL_Data";
        public Form1()
        {
            InitializeComponent();
        }

        private void CreateImage(int ID)
        {
            int height = 440, width = 600;
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //清空图片背景色
                g.Clear(Color.White);

                Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
                Font font1 = new System.Drawing.Font("宋体", 12, FontStyle.Regular);
                Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);

                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
                g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
                Brush brush1 = new SolidBrush(Color.Blue);
                Brush brush2 = new SolidBrush(Color.SaddleBrown);

                string str = "SELECT * FROM 职工统计 WHERE Years="   ID   "";
                SqlConnection Con = new SqlConnection(connectString);
                Con.Open();
                SqlCommand Com = new SqlCommand(str, Con);
                SqlDataReader dr = Com.ExecuteReader();
                dr.Read();
                if (dr.HasRows)
                {
                    g.DrawString(""   ID   "年公司内部人员统计表", font1, brush1, new PointF(160, 30));
                }
                dr.Close();
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);

                Pen mypen = new Pen(brush, 1);
                Pen mypen2 = new Pen(Color.Red, 2);
                //绘制线条
                //绘制纵向线条
                int x = 60;
                for (int i = 0; i < 12; i  )
                {
                    g.DrawLine(mypen, x, 80, x, 340);
                    x = x   40;
                }
                Pen mypen1 = new Pen(Color.Blue, 2);
                g.DrawLine(mypen1, x - 480, 80, x - 480, 340);

                //绘制横向线条
                int y = 106;
                for (int i = 0; i < 9; i  )
                {
                    g.DrawLine(mypen, 60, y, 540, y);
                    y = y   26;
                }
                g.DrawLine(mypen1, 60, y, 540, y);

                //x轴
                String[] n = {"  一月", "  二月", "  三月", "  四月", "  五月", "  六月", "  七月",
                     "  八月", "  九月", "  十月", "十一月", "十二月"};
                x = 35;
                for (int i = 0; i < 12; i  )
                {
                    g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置
                    x = x   40;
                }

                //y轴
                String[] m = {"900人", " 800人", " 700人", "600人", " 500人", " 400人", " 300人", " 200人",
                     " 100人"};
                y = 100;
                for (int i = 0; i < 9; i  )
                {
                    g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //设置文字内容及输出位置
                    y = y   26;
                }

                int[] Count1 = new int[12];
                int[] Count2 = new int[12];
                string[] NumChr = new string[12];
                string cmdtxt2 = "SELECT * FROM 职工统计 WHERE Years="   ID   "";
                SqlCommand Com1 = new SqlCommand(cmdtxt2, Con);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = Com1;
                DataSet ds = new DataSet();
                da.Fill(ds);
                int j = 0;
                for (int i = 0; i < 12; i  )
                {
                    NumChr[i] = ds.Tables[0].Rows[0][i   1].ToString();
                }
                for (j = 0; j < 12; j  )
                {
                    Count1[j] = Convert.ToInt32(NumChr[j].Split('|')[0].ToString()) * 26 / 100;
                }
                for (int k = 0; k < 12; k  )
                {
                    Count2[k] = Convert.ToInt32(NumChr[k].Split('|')[1].ToString()) * 26 / 100;
                }

                //显示折线效果
                SolidBrush mybrush = new SolidBrush(Color.Red);
                Point[] points1 = new Point[12];
                points1[0].X = 60; points1[0].Y = 340 - Count1[0];
                points1[1].X = 100; points1[1].Y = 340 - Count1[1];
                points1[2].X = 140; points1[2].Y = 340 - Count1[2];
                points1[3].X = 180; points1[3].Y = 340 - Count1[3];
                points1[4].X = 220; points1[4].Y = 340 - Count1[4];
                points1[5].X = 260; points1[5].Y = 340 - Count1[5];
                points1[6].X = 300; points1[6].Y = 340 - Count1[6];
                points1[7].X = 340; points1[7].Y = 340 - Count1[7];
                points1[8].X = 380; points1[8].Y = 340 - Count1[8];
                points1[9].X = 420; points1[9].Y = 340 - Count1[9];
                points1[10].X = 460; points1[10].Y = 340 - Count1[10];
                points1[11].X = 500; points1[11].Y = 340 - Count1[11];
                g.DrawLines(mypen2, points1);  //绘制折线

                Pen mypen3 = new Pen(Color.Black, 2);
                Point[] points2 = new Point[12];
                points2[0].X = 60; points2[0].Y = 340 - Count2[0];
                points2[1].X = 100; points2[1].Y = 340 - Count2[1];
                points2[2].X = 140; points2[2].Y = 340 - Count2[2];
                points2[3].X = 180; points2[3].Y = 340 - Count2[3];
                points2[4].X = 220; points2[4].Y = 340 - Count2[4];
                points2[5].X = 260; points2[5].Y = 340 - Count2[5];
                points2[6].X = 300; points2[6].Y = 340 - Count2[6];
                points2[7].X = 340; points2[7].Y = 340 - Count2[7];
                points2[8].X = 380; points2[8].Y = 340 - Count2[8];
                points2[9].X = 420; points2[9].Y = 340 - Count2[9];
                points2[10].X = 460; points2[10].Y = 340 - Count2[10];
                points2[11].X = 500; points2[11].Y = 340 - Count2[11];
                g.DrawLines(mypen3, points2);  //绘制折线

                //绘制标识
                g.DrawRectangle(new Pen(Brushes.Red), 150, 370, 250, 50);  //绘制范围框
                g.FillRectangle(Brushes.Red, 250, 380, 20, 10);  //绘制小矩形
                g.DrawString("试用员工人数", font2, Brushes.Red, 270, 380);
                g.FillRectangle(Brushes.Black, 250, 400, 20, 10);
                g.DrawString("正试员工人数", font2, Brushes.Black, 270, 400);

                this.panel1.BackgroundImage = image;
            }
            catch
            {
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(connectString))
            {
                SqlDataAdapter da = new SqlDataAdapter("select Years from 职工统计", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                this.comboBox1.DataSource = dt.DefaultView;
                this.comboBox1.DisplayMember = "Years";
                this.comboBox1.ValueMember = "Years";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.CreateImage(Convert.ToInt32(this.comboBox1.Text));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
Case05_11/.vs/Case06_11/v16/.suo , 35840
Case05_11/.vs/Case06_11/v16/Server/sqlite3/db.lock , 0
Case05_11/.vs/Case06_11/v16/Server/sqlite3/storage.ide , 512000
Case05_11/.vs/Case06_11/v16/TestStore/0/000-0000.testlog , 20971556
Case05_11/.vs/Case06_11/v16/TestStore/0/testlog.manifest , 24
Case05_11/Backup/Case06_11/Case06_11.csproj , 3764
Case05_11/Backup/Case06_11/Form1.Designer.cs , 5883
Case05_11/Backup/Case06_11/Form1.cs , 8409
Case05_11/Backup/Case06_11/Form1.resx , 5814
Case05_11/Backup/Case06_11/Program.cs , 526
Case05_11/Backup/Case06_11/Properties/AssemblyInfo.cs , 1374
Case05_11/Backup/Case06_11/Properties/Resources.Designer.cs , 2868
Case05_11/Backup/Case06_11/Properties/Resources.resx , 5612
Case05_11/Backup/Case06_11/Properties/Settings.Designer.cs , 1094
Case05_11/Backup/Case06_11/Properties/Settings.settings , 249
Case05_11/Backup/Case06_11.sln , 917
Case05_11/Case06_11/Case06_11.csproj , 3957
Case05_11/Case06_11/Form1.Designer.cs , 5883
Case05_11/Case06_11/Form1.cs , 8375
Case05_11/Case06_11/Form1.resx , 5814
Case05_11/Case06_11/Program.cs , 488
Case05_11/Case06_11/Properties/AssemblyInfo.cs , 1374
Case05_11/Case06_11/Properties/Resources.Designer.cs , 2853
Case05_11/Case06_11/Properties/Resources.resx , 5612
Case05_11/Case06_11/Properties/Settings.Designer.cs , 1109
Case05_11/Case06_11/Properties/Settings.settings , 249
Case05_11/Case06_11/bin/Debug/Case06_11.exe , 14336
Case05_11/Case06_11/bin/Debug/Case06_11.pdb , 28160
Case05_11/Case06_11/obj/Debug/Case06_11.Form1.resources , 180
Case05_11/Case06_11/obj/Debug/Case06_11.Properties.Resources.resources , 180
Case05_11/Case06_11/obj/Debug/Case06_11.csproj.FileListAbsolute.txt , 1363
Case05_11/Case06_11/obj/Debug/Case06_11.csproj.GenerateResource.cache , 847
Case05_11/Case06_11/obj/Debug/Case06_11.csprojAssemblyReference.cache , 424
Case05_11/Case06_11/obj/Debug/Case06_11.exe , 14336
Case05_11/Case06_11/obj/Debug/Case06_11.pdb , 28160
Case05_11/Case06_11/obj/Debug/DesignTimeResolveAssemblyReferences.cache , 745
Case05_11/Case06_11/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 7140
Case05_11/Case06_11/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll , 3584
Case05_11/Case06_11.sln , 1132
Case05_11/Case06_11.suo , 15872
Case05_11/UpgradeLog.htm , 41102
SQL_Data_Data.MDF , 1441792
SQL_Data_Data.sql , 52698
SQL_Data_Log.LDF , 1048576
没有账号? 忘记密码?

社交账号快速登录