Asp.Net实例:C# 绘制统计图(三) ——扇形统计图的绘制

简介:
扇形统计图的绘制
效果图 :
完整代码 :
扇形统计图的绘制
private void CreateImage()
{
//
把连接字串指定为一个常量
SqlConnection Con = new SqlConnection("Server=(Local);
Database=committeeTraining;Uid=sa;Pwd=**");
Con.Open();
string cmdtxt = selectString; // "select * from ##Count"; //
//SqlCommand Com = new SqlCommand(cmdtxt, Con);
DataSet ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);
Da.Fill(ds);
Con.Close();
float Total = 0.0f, Tmp;
// 转换成单精度。也可写成 Convert.ToInt32
Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
// Total=Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
//
设置字体, fonttitle 为主标题的字体
Font fontlegend = new Font("verdana", 9);
Font fonttitle = new Font("verdana", 10, FontStyle.Bold);
// 背景宽
int width = 350;
int bufferspace = 15;
int legendheight = fontlegend.Height * 10 + bufferspace; //
高度
int titleheight = fonttitle.Height + bufferspace;
int height = width + legendheight + titleheight + bufferspace;//
白色背景高
int pieheight = width;
Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);
// 加上各种随机色
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (int i = 0; i < 2; i++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
// 创建一个 bitmap 实例
Bitmap objbitmap = new Bitmap(width, height);
Graphics objgraphics = Graphics.FromImage(objbitmap);
// 画一个白色背景
objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
// 画一个亮黄色背景  
objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);
// 以下为画饼图 ( 有几行 row 画几个 )
float currentdegree = 0.0f;
// 画通过人数
objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree,
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360);
currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360;
// 未通过人数饼状图
objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree,
((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360);
currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) - 
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360;

//
以下为生成主标题
SolidBrush blackbrush = new SolidBrush(Color.Black);
SolidBrush bluebrush = new SolidBrush(Color.Blue);
string title = " 
机关单位成绩统计饼状图 : "
+ "\n \n\n";
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
objgraphics.DrawString(title, fonttitle, blackbrush,
new Rectangle(0, 0, width, titleheight), stringFormat);
// 列出各字段与得数目
objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50);
objgraphics.DrawString("---------------- 统计信息 ------------------", 
fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1);
objgraphics.DrawString("
统计单位 : " + this.ddlTaget.SelectedItem.Text, 
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1);
objgraphics.DrawString("
统计年份 : " + this.ddlYear.SelectedItem.Text, 
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1);
objgraphics.DrawString("
统计期数 : " + this.ddlSpan.SelectedItem.Text, 
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1);
objgraphics.FillRectangle((SolidBrush)colors[1], 5,height - legendheight + fontlegend.Height * 8 + 1, 10, 10);
objgraphics.DrawString("
报名总人数 : " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])), 
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1);
objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10);
objgraphics.DrawString("
通过总人数 : " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])), 
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1);
objgraphics.DrawString("
未通过人数 : " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) - 
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1);
objgraphics.DrawString(" 通过率 : " + Convert.ToString((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / 
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend, 
blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1);
Response.ContentType = "image/Jpeg";
objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();
}
 




本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/202146,如需转载请自行联系原作者

目录
相关文章
|
6月前
|
开发框架 .NET C#
ASP.Net c# 正则表达式 子表达式 group
ASP.Net c# 正则表达式 子表达式 group
36 0
|
10月前
|
开发框架 前端开发 .NET
C# ASP.NET Core开发学生信息管理系统(一)
C# ASP.NET Core开发学生信息管理系统(一)
282 0
|
2月前
|
开发框架 中间件 .NET
C# .NET面试系列七:ASP.NET Core
## 第一部分:ASP.NET Core #### 1. 如何在 controller 中注入 service? 在.NET中,在ASP.NET Core应用程序中的Controller中注入服务通常使用<u>依赖注入(Dependency Injection)</u>来实现。以下是一些步骤,说明如何在Controller中注入服务: 1、创建服务 首先,确保你已经在应用程序中注册了服务。这通常在Startup.cs文件的ConfigureServices方法中完成。例如: ```c# services.AddScoped<IMyService, MyService>(); //
76 0
|
2月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
108 5
|
4月前
|
开发框架 .NET 定位技术
asp.net core3.1 c# 生成三种sitemap
asp.net core3.1 c# 生成三种sitemap
17 0
|
4月前
|
开发框架 .NET Java
ASP.NET Core高级编程--C#基本特性(一)
本文章简略介绍C#的部分特性
|
9月前
|
SQL 开发框架 .NET
基于ASP.NET实现的排课系统(C#课程设计)
基于ASP.NET实现的排课系统(C#课程设计)
77 0
|
5月前
|
数据采集 开发框架 监控
C# ASP.NET 实验室 检验中心 医疗LIS源码
LIS系统整体流程 检验项目申请:医生通过HIS或者病历系统开具检验项目检验申请单(内容包括病人信息及检验项目),申请信息自动传到护士站。如果不和HIS和病历系统对接,可以通过LIS系统自身的检验申请模块,实现申请单电子化,申请信息自动传到护士站。
33 0
|
7月前
|
开发框架 .NET 数据库
asp.net企业费用报销管理信息系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio
asp.net 企业费用报销管理信息系统是一套完善的web设计管理系统,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。开发环境为vs2010,数据库为sqlserver2008,使 用c#语言开发 应用技术:asp.net c#+sqlserver 开发工具:vs2010 +sqlserver
56 0
|
10月前
|
开发框架 人工智能 前端开发
Visual Studio Code安装C#开发工具包并编写ASP.NET Core Web应用
Visual Studio Code安装C#开发工具包并编写ASP.NET Core Web应用
196 0