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,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
Go
在golang中发起http请求以获取访问域名的ip地址实例(使用net, httptrace库)
这只是追踪我们的行程的简单方法,不过希望你跟着探险家的脚步,即使是在互联网的隧道中,也可以找到你想去的地方。接下来就是你的探险之旅了,祝你好运!
110 26
|
8月前
|
Java 物联网 C#
C#/.NET/.NET Core学习路线集合,学习不迷路!
C#/.NET/.NET Core学习路线集合,学习不迷路!
341 0
|
3月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
133 0
|
7月前
|
开发框架 搜索推荐 算法
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
221 18
|
7月前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
208 12
|
7月前
|
开发框架 人工智能 .NET
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
|
7月前
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
|
7月前
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
|
7月前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
370 5
|
7月前
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
277 13

热门文章

最新文章