C# 绘制分布图

C# 绘制分布图

效果图:
在这里插入图片描述
代码:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();                                                      //创建Graphics对象
            g.Clear(Color.WhiteSmoke);                                                   //设置背景色
            Pen p = new Pen(Color.Blue);                                                        //绘制画笔
            //设置用到的字体
            Font fontO = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
            Font fontT = new System.Drawing.Font("华文新魏", 16, FontStyle.Regular);
            Point pointStart = new Point(0, 0);                                                        //绘制边框与显示字体
            Size sizeWindows = new Size(this.Width - 8, this.Height - 34);                       //创建Size对象
            Rectangle rect = new Rectangle(pointStart, sizeWindows);                      //创建Rectangle对象
            g.DrawRectangle(p, rect);                                                      //绘制矩形
            Brush brus = new SolidBrush(Color.Red);                                              //创建笔刷
            g.DrawString("某网站人气指数曲线分析", fontT, brus, this.Width / 2.00f - 150, 10.00f);
            //绘制网格线
            int x = this.Width / 10;
            int y = this.Height / 14;
            int z = this.Width / 10;
            int k = y * 12;
            //X
            for (int i = 0; i < 12; i++)
            {
                g.DrawLine(p, x, y * 3 - 10, x, y * 12);                                                 //绘制水平线条
                x = x + (this.Width - 34) / 14;
            }
            //X轴
            String[] n = {" 1月", " 2月", " 3月", " 4月", " 5月", " 6月", " 7月",
     " 8月", " 9月", "10月", "11月", "12月"};                                        //绘制月份
            x = this.Width / 10 - 16;
            for (int i = 0; i < 12; i++)
            {
                g.DrawString(n[i].ToString(), fontO, Brushes.Red, x, y * 12);                 //设置文字内容及输出位置
                x = x + (this.Width - 34) / 14;
            }
            //Y
            for (int i = 0; i < 12; i++)
            {
                g.DrawLine(p, z, k, x + 10, k);                                                      //绘制垂直线条
                k = k - (y * 12) / 16;
            }
            //Y轴
            int h = k;
            String[] m = {"5565","5650","4565", "4000", "3565", "3000", "2565", "2000", "1565", "1000",
     "  565"};                                                             //绘制Y轴显示的文字
            k = y * 12;
            for (int i = 0; i < 11; i++)
            {
                g.DrawString(m[10 - i].ToString(), fontO, Brushes.Red, z - 35, k - y);            //开始绘制文字
                k = k - (y * 12) / 16;
            }
            int[] Count = new int[12];
            Pen mypen = new Pen(Color.Red, 2);                                             //创建画笔
            Point[] points = new Point[12];
            x = this.Width / 10;
            k = y * 12;
            //SqlConnection Con = new SqlConnection("Server=mrwxk\\wangxiaoke;DataBase=db_TomeOne;Uid=sa;Pwd=");
            //string cmdtxt2 = "SELECT * FROM tb_reticulation";                              //声明SQL语句
            //SqlCommand Com1 = new SqlCommand(cmdtxt2, Con);                               //创建SqlCommand对象
            //SqlDataAdapter da = new SqlDataAdapter();                                          //创建SqlDataAdapter对象
            //da.SelectCommand = Com1;
            //DataSet ds = new DataSet();                                                         //创建DataSet对象
            //da.Fill(ds);                                                              //Fill方法填充DataSet对象
            int j = 0;
            //for (j = 0; j < 12; j++)
            //{
            //    //与Y轴数生成有关(y * 12)/16因为起始为565
            //    Count[j] = Convert.ToInt32(ds.Tables[0].Rows[0][j + 2].ToString()) * (y * 12) / 16 / 565;
            //}
            //设置绘制曲线的坐标数组
            //points[0].X = x; points[0].Y = k - Count[0];
            //x = x + (this.Width - 34) / 14;
            //points[1].X = x; points[1].Y = k - Count[1];
            //x = x + (this.Width - 34) / 14;
            //points[2].X = x; points[2].Y = k - Count[2];
            //x = x + (this.Width - 34) / 14;
            //points[3].X = x; points[3].Y = k - Count[3];
            //x = x + (this.Width - 34) / 14;
            //points[4].X = x; points[4].Y = k - Count[4];
            //x = x + (this.Width - 34) / 14;
            //points[5].X = x; points[5].Y = k - Count[5];
            //x = x + (this.Width - 34) / 14;
            //points[6].X = x; points[6].Y = k - Count[6];
            //x = x + (this.Width - 34) / 14;
            //points[7].X = x; points[7].Y = k - Count[7];
            //x = x + (this.Width - 34) / 14;
            //points[8].X = x; points[8].Y = k - Count[8];
            //x = x + (this.Width - 34) / 14;
            //points[9].X = x; points[9].Y = k - Count[9];
            //x = x + (this.Width - 34) / 14;
            //points[10].X = x; points[10].Y = k - Count[10];
            //x = x + (this.Width - 34) / 14;
            //points[11].X = x; points[11].Y = k - Count[11];
            g.DrawLines(mypen, points);                                                        //绘制折线
        }
C#绘制正态分布效果图可以使用 MathNet.Numerics 库来生成正态分布的数据,并使用 System.Windows.Forms.DataVisualization.Charting 库来绘制图表。 以下是一个绘制正态分布效果图的示例代码: ```C# using System; using System.Windows.Forms.DataVisualization.Charting; using MathNet.Numerics.Distributions; // 创建一个窗体应用程序 public class NormalDistributionForm : Form { private Chart chart; public NormalDistributionForm() { // 初始化窗体和图表控件 this.Text = "Normal Distribution"; this.ClientSize = new System.Drawing.Size(600, 400); this.chart = new Chart(); this.chart.Parent = this; this.chart.Dock = DockStyle.Fill; // 生成正态分布的数据 double[] data = new double[1000]; Normal.Samples(data, 0, 1); // 添加数据到图表中 Series series = this.chart.Series.Add("Normal Distribution"); series.ChartType = SeriesChartType.Line; for (int i = 0; i < data.Length; i++) { series.Points.AddXY(i, data[i]); } // 设置图表的样式 ChartArea area = this.chart.ChartAreas[0]; area.AxisX.MajorGrid.Enabled = false; area.AxisY.MajorGrid.Enabled = false; area.AxisX.Title = "X"; area.AxisY.Title = "Density"; area.AxisX.Minimum = 0; area.AxisX.Maximum = data.Length; area.AxisY.Minimum = -0.5; area.AxisY.Maximum = 1.0; } static void Main() { Application.Run(new NormalDistributionForm()); } } ``` 在这个示例代码中,我们使用 MathNet.Numerics 库中的 Normal 类生成了 1000 个均值为 0,标准差为 1 的正态分布随机数,并将它们添加到了图表中。然后,我们使用 System.Windows.Forms.DataVisualization.Charting 库中的 Chart 控件来绘制这些数据的图表,并设置了图表的样式。 运行这个示例代码,就可以看到一个绘制正态分布效果图的窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值