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

代码展示内容:
从数据库坐标信息表中读取坐标数据,根据读入的坐标数据绘制折线图。

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

代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
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();cmd.CommandText = "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();}}
}

效果图如下:

C#连接SQL Server数据库绘制折线图相关推荐

  1. 通过JDBC-ODBC连接SQL Server数据库

    通过JDBC-ODBC连接SQL Server数据库 由于ODBC驱动程序被广泛应用,建立这种桥连接数据库之后,使得JDBC拥有能够访问所有数据库的的能力,这里是实现配置数据库,并测试是否可以通过JD ...

  2. 条码打印软件如何连接SQL Server数据库制作条形码

    有的小伙伴用条码打印软件制作条形码时,条形码数据没有保存在TXT或者Excel表中,而是保存在数据库中,那就无法选择TXT或者Excel数据源导入条形码数据了,这样的话,在条码打印软件可以选择连接相应 ...

  3. python连接sql server数据库(pyodbc)

    用python操作ms sql server,有好几种方法: (1)利用pymssql (2)利用pyodbc 这里讲import pyodbc来操作sql server database. pyod ...

  4. 标签打印软件如何连接SQL Server数据库打印产品标签

    这两天小编遇到有人咨询说自己的产品信息在数据库中,怎么把产品数据导入到标签打印软件中制作成产品标签.其实,实现这个功能是非常简单的,我们可以在标签打印软件中直接连接相关数据库,调用数据库中的产品信息即 ...

  5. VS2019连接SQL Server数据库

    VS2019连接SQL Server数据库 -web程序设计 ASP.NET 1. 安装SQL Server 可参考链接:SQL Server 2019 安装教程 2. 打开VS2019,创建项目 3 ...

  6. visual studio2019连接SQL Server数据库,增删改查详细教程(C#代码)

    visual studio2019连接SQL Server数据库,增删改查详细教程(C#代码) 工具: 1.Visual Studio 2019 2.SQL Server数据库(我使用的2008) 操 ...

  7. 【ASP.NET】VS2015连接SQL Server数据库,实现登录、注册

    文章目录 1. 实验目标 2. 难点 3. 问题 3.1 SQL Server 3.2 Demo练习 3.3 编写页面时的错误 4. 总结 5. 页面 6. 主要代码 [ASP.NET]VS2015连 ...

  8. VS连接SQL server数据库

    目录 连接数据库 使用dataGridView控件显示表中的数据. 实现基本CRUD操作 连接数据库 打开vs,点击 视图,打开sql资源管理器,添加SQL Server 输入服务器名称,用户名,密码 ...

  9. (续)Visual Studio 连接SQL Server数据库,代码连接

    (续)Visual Studio 连接SQL Server数据库,代码连接(参考项目https://gitee.com/qmagician/book-management-system) 昨天已经将数 ...

最新文章

  1. 单例模式的几种实现方式
  2. Zookeeper_原生API操作(一)
  3. org/eclipse/jetty/server/Handler : Unsupported major.minor version 52.0
  4. JEECG第二期深入使用培训(报名截止2014-06-21)
  5. 【干货】数字经济百项场景.pdf(附下载链接)
  6. xxx is not in the sudoers file. This incident will be reported
  7. Spring MVC(三) 数据转换、格式化、校验
  8. Finalshell软件安装使用
  9. 能打开2D、3D图文件的小工具abviewer
  10. python2 python3 print_python2和python3中print有什么区别
  11. 对抗样本(五)DeepFool
  12. 北京铁路安检全面升级 四大站特警持枪巡逻
  13. fc模拟器安卓版_【SFC】魂斗罗3-异形战争模拟器情怀通关2020_EVOS
  14. 鸿蒙手机 OS 等开发必备工具,华为 DevEco Studio 2.1
  15. mysql怎么启用sa用户_安装SQL SERVER开启SA用户登录的方法
  16. 我的世界服务器怎么解压文件,我的世界整合包压缩包解压及使用
  17. Hadoop 常用的命令
  18. GO 学习笔记——第五天 / 异常,文本文件处理
  19. l7sa008b故障代码_奥克斯空调故障代码大全
  20. 读书笔记——上瘾:让用户养成使用习惯的四大产品逻辑

热门文章

  1. AI工具(ChatGPT)常用指令,持续更新...
  2. COT、COT-SC、TOT 大预言模型思考方式||底层逻辑:prompt设定
  3. C++、Python、Java的MySQL数据库操作
  4. 手绘知识点——指针运算变量的内存分配原理
  5. HINSTANCE 转换 int 正确方法(C++)
  6. Weblogic 常见漏洞分析与利用
  7. 【barcode】 基于Jbarcode开源库生成条形码,提供添加备注信息的解决方案
  8. So easy Spring事务回滚机制
  9. 开源|如何利用Tensorflow实现语义分割全卷积网络(附源码)
  10. 智能车竞赛技术报告 | 节能信标组 - 安徽工业大学 - 摸鱼大队