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


WinFrom柱形图的更多相关文章
- winfrom 隐藏任务栏(win7)
1:新建winfrom 窗体应用程序 2:拖入contextMenuStrip.notifyIcon 2个控件 3:如图 4:code: 注意:复制控件事件要注册 using System; usin ...
- winfrom 文字滚动
winfrom 文字滚动 http://www.codeproject.com/Articles/6913/Creating-a-professional-looking-GDI-drawn-cust ...
- MSChart绘图控件中折线图和柱形图画法
首先在前台拖入一个名为chart1的MSChart控件 //折线图 string strLegend = "Legend1"; Legend lg = new Legend(str ...
- Web API应用架构在Winform混合框架中的应用(3)--Winfrom界面调用WebAPI的过程分解
最近一直在整合WebAPI.Winform界面.手机短信.微信公众号.企业号等功能,希望把它构建成一个大的应用平台,把我所有的产品线完美连接起来,同时也在探索.攻克更多的技术问题,并抽空写写博客,把相 ...
- AChartEngine 安卓折线图 柱形图等利器
http://www.eoeandroid.com/thread-548233-1-6.html 最近公司项目中要用到折线图,状态类型的图标要用到折线图,柱形图等,并且能够动态显示,在网上找了许多de ...
- C# Winfrom 页面传值
2个窗体 Parent,Children 代码: Parent public partial class Parent : Form { public string parentValue = &qu ...
- WinFrom 登录窗体 密码保存效果
WinFrom 登录窗体 保存密码效果 开发CS程序的程序员都会遇到 今天突然想把这个功能加到我的项目中 之后总结下 不多说 上图 如果关闭程序 下次在登录的时候 用户名.密码会自动保留下来 一 ...
- BackgroundWorker实现的winfrom中实现异步等待加载图片显示
BackgroundWorker简介 BackgroundWorker在winfrom中有对应控件,该有三个事件:DoWork .ProgressChanged 和 RunWorkerCompl ...
- Winfrom中ListBox绑定List数据源更新问题
Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...
随机推荐
- SpringMVC中重定向传参数的方法
在spring的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到.但是,这样使用的是forward方式,浏览器的地址栏是不变的,如 ...
- saltstack 动态pillar实现
简介 pillar支持的数据存储方式有很多,mysql, mogo,json等.本篇介绍关于http存储方式. 首先简要说明整个流程:salt-master会去一个指定http发送get请求获取一个j ...
- Python3之urllib模块
简介 urllib是python的一个获取url(Uniform Resource Locators,统一资源定位符),可以用来抓取远程的数据. 常用方法 (1)urlopen urllib.requ ...
- JavaIO系统
为了方便记忆,特将IO中涉及的类进行整理如下: 1.File类 提供了目录操作,查看文件属性等. 2.java IO类层次 面向字节流的类为InputStream.OutputStream:面向字符流 ...
- Scrapyd API的安装
安装好了Scrapyd之后,我们可以直接请求它提供的API来获取当前主机的Scrapy任务运行状况.比如,某台主机的IP为192.168.1.1,则可以直接运行如下命令获取当前主机的所有Scrapy项 ...
- Altium Designer安装孔周围放置圆形Polygon Pour Cutout
1. 在Keep-Out层画2个圆, 中间的圆用作安装孔, 外圆做为禁止覆铜层. 这样做的好处是,放好安装孔后, 外面禁止覆铜层也覆不上铜, 防止螺钉与覆铜接触. 2. 选中外圆, Tools -& ...
- java中Filter过滤器处理中文乱码的方法
注意问题:在学习用selvert的过滤器filter处理中文乱码时,在filter配置初始化时用了utf-8处理中文乱码,而在提交的jsp页面中却用了gbk.虽然两种都可以出来中文乱码,但是却造成了处 ...
- PHP之string之explode()函数使用
explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...
- mysql 索引数据结构及原理
原文:http://www.uml.org.cn/sjjm/201107145.asp 1 索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子 ...
- UVM序列篇之二:sequence和item(上)
无论是自驾item,穿过sequencer交通站,通往终点driver,还是坐上sequence的大巴,一路沿途观光,最终跟随导游停靠到风景点driver,在介绍如何驾驶item和sequence,遵 ...