1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > C#连接SQL Server数据库绘制折线图

C#连接SQL Server数据库绘制折线图

时间:2024-02-01 22:30:41

相关推荐

C#连接SQL Server数据库绘制折线图

C#连接数据库绘制折线图

代码展示内容:

从数据库坐标信息表中读取坐标数据,根据读入的坐标数据绘制折线图。

C#窗口中的坐标系是原点在窗口左上角,X轴方向是自左向右,Y轴方向是自上向下的。如果想在C#窗口中绘制正常的直角坐标系,我们就需要做专门的处理。

数据库信息如下:

代码如下:

using System;using System.Collections;using System.Collections.Generic;using ponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace Test8{public partial class Form3 : Form{public Form3(){InitializeComponent();}List<int> numX = new List<int>();List<int> numY = new List<int>();private void Connection(){string constr = "server=LAPTOP-H6RRB219;database=system;uid=ss;pwd=liujia66";SqlConnection conn = new SqlConnection(constr);conn.Open();SqlCommand cmd = conn.CreateCommand();mandText = "select * from map";SqlDataReader dr = cmd.ExecuteReader();while (dr.Read()){numX.Add((int)dr[0]);numY.Add((int)dr[1]);}}private int height = 800;private int width = 350;private Bitmap bitmap;private Graphics graphics;private void DrawCurve(){//创建位图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.White, 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, 280, 390);//矩形graphics.DrawRectangle(mypenBlack, 40, 35, 280, 395); //矩形//设置画笔颜色Pen mypenBlue = new Pen(Color.Blue, 1);Pen mypenRed = new Pen(Color.Red, 1);Pen mypenYellow = new Pen(Color.Yellow, 1);//连接数据库Connection();//绘制线条int lenX = numX.Count;//绘制纵向线条//绘制纵向线条int x = 80;for (int i = 0; i < 6; i++){graphics.DrawLine(mypenBlue, x, 40, x, 430);x = x + 40;}//绘制横向线条int y = 70;for (int i = 0; i < 12; i++){graphics.DrawLine(mypenBlue, 40, y, 320, y);y = y + 30;}//x轴上对应的标记String[] n = {" 1", " 2", " 3", " 4", " 5", " 6" };x = 70;for (int i = 0; i < 6; i++){graphics.DrawString(n[i].ToString(), font, Brushes.Red, x, 435); //设置文字内容及输出位置x = x + 40;}//y轴上对应的标记String[] m = {"60", "55", "50", "45", "40", "35", "30", "25", "20", "15", "10", " 5" };y = 60;for (int i = 0; i < 12; i++){graphics.DrawString(m[i].ToString(), font, Brushes.Red, 4, y); //设置文字内容及输出位置y = y + 30;}//画曲线PointF[] CurvePointF = new PointF[lenX];float pointX = 0;float pointY = 0;for (int i = 0; i < lenX; i++){pointX = i * 40 + 40;pointY = 430 - numY[i] * 6;CurvePointF[i] = new PointF(pointX, pointY);graphics.FillEllipse(brushPoint, pointX - 2, pointY - 2, 4, 4);}graphics.DrawLines(mypenBlack, CurvePointF);graphics.Dispose();this.pictureBox1.Image = bitmap;}private void Form3_Load(object sender, EventArgs e){DrawCurve();}}}

效果图如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。