WinFrom柱形图
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.Data.SqlClient;
using System.Drawing.Drawing2D;
namespace MyFill
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int Sum; //声明int类型变量Sum
SqlConnection conn; //声明一个connection变量
public void CreateImage()
{
//连接SQLserver数据库
conn = new SqlConnection("server=.;database=vote;uid=sa;pwd=sa");
conn.Open();
//获取总的投票数
SqlCommand cmd = new SqlCommand("select SUM(tp) from tb_vote",conn);
Sum = (int)cmd.ExecuteScalar();
//查询整个表的数据
SqlDataAdapter sda = new SqlDataAdapter("select * from tb_vote",conn);
DataSet ds = new DataSet();
sda.Fill(ds);
//选项的投票
int TP1 = Convert.ToInt32(ds.Tables[0].Rows[0][2].ToString());
int TP2 = Convert.ToInt32(ds.Tables[0].Rows[1][2].ToString());
int TP3 = Convert.ToInt32(ds.Tables[0].Rows[2][2].ToString());
int TP4 = Convert.ToInt32(ds.Tables[0].Rows[3][2].ToString());
//名称
string Name = Convert.ToString(ds.Tables[0].Rows[0][1].ToString());
string Name1 = Convert.ToString(ds.Tables[0].Rows[1][1].ToString());
string Name2 = Convert.ToString(ds.Tables[0].Rows[2][1].ToString());
string Name3 = Convert.ToString(ds.Tables[0].Rows[3][1].ToString());
//如果使用遍历就可以使用下面这种类型的Convert.ToString(ds.Tables[0].Rows[0]["字段名称"].ToString());
//string Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"].ToString());
//获取每个选项的百分比
float tp1 = Convert.ToSingle(Convert.ToSingle(TP1)*100/Sum);
float tp2 = Convert.ToSingle(Convert.ToSingle(TP2) * 100 / Sum);
float tp3 = Convert.ToSingle(Convert.ToSingle(TP3) * 100 / Sum);
float tp4 = Convert.ToSingle(Convert.ToSingle(TP4) * 100 / Sum);
int width = 300, height = 300;
Bitmap bitmap = new Bitmap(width,height);
Graphics g = Graphics.FromImage(bitmap);
try
{
g.Clear(Color.White); //使用Clear方法使画布变成白色
//创建6个Brush对象,用于填充颜色
Brush brush1 = new SolidBrush(Color.White);
Brush brush2 = new SolidBrush(Color.Black);
Brush brush3 = new SolidBrush(Color.Red);
Brush brush4 = new SolidBrush(Color.Green);
Brush brush5 = new SolidBrush(Color.Orange);
Brush brush6 = new SolidBrush(Color.DarkBlue);
//创建2个font用于设置字体
Font f1 = new Font("Courier New", 16, FontStyle.Bold);
Font f2 = new Font("Courier New",8);
g.FillRectangle(brush1,0,0,width,height); //绘制背景图
g.DrawString("投票结果", f1, brush2, new Point(90, 20)); //绘制标题
//设置坐标
Point p1 = new Point(70, 50);
Point p2 = new Point(230, 50);
g.DrawLine(new Pen(Color.Black), p1, p2); //绘制直线
//绘制文字
g.DrawString(Name, f2, brush2, new Point(10, 80));
g.DrawString(Name1, f2, brush2, new Point(32, 110));
g.DrawString(Name2, f2, brush2, new Point(32, 140));
g.DrawString(Name3, f2, brush2, new Point(54, 170));
//绘制柱形图
g.FillRectangle(brush3,95,80,tp1,17);
g.FillRectangle(brush4, 95, 110, tp2, 17);
g.FillRectangle(brush5, 95, 140, tp3, 17);
g.FillRectangle(brush6, 95, 170, tp4, 17);
//绘制所有选项的票数显示
g.DrawRectangle(new Pen(Color.Green), 10, 210, 280, 80); //绘制范围
g.DrawString(Name +":"+ TP1.ToString() + "票", f2, brush2, new Point(15, 220));
g.DrawString(Name1 + ":" + TP2.ToString() + "票", f2, brush2, new Point(150, 220));
g.DrawString(Name2 + ":" + TP3.ToString() + "票", f2, brush2, new Point(15, 260));
g.DrawString(Name3 + ":" + TP4.ToString() + "票", f2, brush2, new Point(150, 260));
pictureBox1.Image = bitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
CreateImage();
}
}
}
数据库:
USE [AssetSys] GO /****** Object: Table [dbo].[tb_vote] Script Date: 2018/10/27 9:12:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[tb_vote]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, [TP] [int] NOT NULL, [CreateTime] [datetime] NOT NULL, [Remake] [nvarchar](50) NOT NULL, [State] [int] NOT NULL, [Sort] [int] NOT NULL, CONSTRAINT [PK_tb_vote] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[tb_vote] ADD CONSTRAINT [DF_tb_vote_CreateTime] DEFAULT (getdate()) FOR [CreateTime] GO


生命中最值得欣慰的,莫过于一觉醒来,你还在身旁

浙公网安备 33010602011771号