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

  

WinFrom柱形图的更多相关文章

  1. winfrom 隐藏任务栏(win7)

    1:新建winfrom 窗体应用程序 2:拖入contextMenuStrip.notifyIcon 2个控件 3:如图 4:code: 注意:复制控件事件要注册 using System; usin ...

  2. winfrom 文字滚动

    winfrom 文字滚动 http://www.codeproject.com/Articles/6913/Creating-a-professional-looking-GDI-drawn-cust ...

  3. MSChart绘图控件中折线图和柱形图画法

    首先在前台拖入一个名为chart1的MSChart控件 //折线图 string strLegend = "Legend1"; Legend lg = new Legend(str ...

  4. Web API应用架构在Winform混合框架中的应用(3)--Winfrom界面调用WebAPI的过程分解

    最近一直在整合WebAPI.Winform界面.手机短信.微信公众号.企业号等功能,希望把它构建成一个大的应用平台,把我所有的产品线完美连接起来,同时也在探索.攻克更多的技术问题,并抽空写写博客,把相 ...

  5. AChartEngine 安卓折线图 柱形图等利器

    http://www.eoeandroid.com/thread-548233-1-6.html 最近公司项目中要用到折线图,状态类型的图标要用到折线图,柱形图等,并且能够动态显示,在网上找了许多de ...

  6. C# Winfrom 页面传值

    2个窗体 Parent,Children 代码: Parent public partial class Parent : Form { public string parentValue = &qu ...

  7. WinFrom 登录窗体 密码保存效果

    WinFrom 登录窗体 保存密码效果 开发CS程序的程序员都会遇到 今天突然想把这个功能加到我的项目中 之后总结下 不多说 上图   如果关闭程序 下次在登录的时候 用户名.密码会自动保留下来  一 ...

  8. BackgroundWorker实现的winfrom中实现异步等待加载图片显示

    BackgroundWorker简介    BackgroundWorker在winfrom中有对应控件,该有三个事件:DoWork .ProgressChanged 和 RunWorkerCompl ...

  9. Winfrom中ListBox绑定List数据源更新问题

    Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...

随机推荐

  1. [RHEL] RHEL7.0 下 Postfix + Dovecot 实现邮件发送

    RHEL7.0 下 Postfix + Dovecot 实现邮件发送 一.前言 大家都对邮件服务(mail service)很感兴趣嘛.我在自己 博客站 预言了自己会实战一次,访问量一天到十几(毕竟平 ...

  2. [CISCO] 简单配置 Telnet 服务

    [CISCO] 简单配置 Telnet 服务 一.Introduction Telnet 协议是一种应用层协议,使用于网际网路及区域网中,使用虚拟终端机的形式,提供双向.以文字字串为主的互动功能.属于 ...

  3. php脚本cli 模式运行

    参考文章 http://rapheal.sinaapp.com/2013/11/20/php_zend_hello_world/ http://www.douban.com/note/33788568 ...

  4. Mac下使用crontab来实现定时任务

    说明: 1.Linux和Mac下操作crontab都是一致的 2.配置文件都在/etc/crontab下,如果没有就创建. 3.测试发现直接使用crontab -e命令创建的定时任务是放在临时文件夹的 ...

  5. WCF系列教程之WCF中的会话

    本文参考自http://www.cnblogs.com/wangweimutou/p/4516224.html,纯属读书笔记,加深记忆 一.WCF会话简介 1.在WCF应用程序中,回话将一组消息相互关 ...

  6. hibernate关联关系的crud2

    hibernate关联关系的CRUD操作,解释都在注释里了,讲了fetchType.cascade. User类: package com.oracle.hibernate; import javax ...

  7. Java多线程(三)锁对象和线程池

    1:锁(Lock) 1.1       java提供了一个锁的接口,这个锁同样可以达到同步代码块的功能,API文档上说使用锁比使用synchronized更加灵活. 1.2       如何使用这个“ ...

  8. rspec中的let和let!区别

    文档 https://relishapp.com/rspec/rspec-core/v/2-5/docs/helper-methods/let-and-let 从上面文档中得出 let 1 只会在一个 ...

  9. 前端思想实现:面向UI编程_____前端框架设计开发

    引子,我去小说看多了,写博客竟然写引子了!!!不过,没引子不知道怎么写了.言归正传吧,前端这个职业,也就这几年刚刚火起来的职业,以前那个混乱的年代,前端要么是UI设计师代劳解决问题,要么就是后端程序员 ...

  10. R语言统计字符串的字符数ncahr函数

    函数计算字符数量,包括在一个字符串的空格的个数. 语法 nchar()函数的基本语法是: nchar(x) 以下是所使用的参数的说明: x - 向量输入. 示例 result <- nchar( ...