using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using DrawChart;
using System.Drawing.Imaging;
namespace WebApplication4
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected WebApplication4.DataSet1 dataSet11;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
sqlDataAdapter1.Fill(dataSet11);
DrawPieChart(dataSet11,500,300,"饼图测试");
}
}
/// <summary>
/// 生成饼图
/// </summary>
/// <param name="dsData">用于生成饼图的数据集,数据集的第一个字段为分组的字
段,第二个字段为要统计的字段的分组求和</param>
/// <param name="iWidth">图片的宽度</param>
/// <param name="iHeight">图片的高度</param>
/// <param name="sCaption">图片的标题</param>
/// <returns>图片的流</returns>
void DrawPieChart(DataSet dsData, int iWidth, int iHeight, String
sCaption)
{
// 创建一个新的图片
Bitmap oBitMap = new Bitmap(iWidth, iHeight);
// 创建一个图型容器
Graphics oGraphics = Graphics.FromImage(oBitMap);
// 创建一个画笔用于画线和边缘
Pen oPen = new Pen(Color.Black, 1);
// 创建一个实心的刷子,用这个刷子填充容器
SolidBrush oBrush = new SolidBrush(Color.White);
oGraphics.FillRectangle(oBrush, 0, 0, oBitMap.Width,
oBitMap.Height);
// 在图片的上部居中现实一个图片的标题
DrawText(oGraphics, 0, 0, oBitMap.Width, 20, sCaption,
FontStyle.Bold, 10, "", StringAlignment.Center, 0);
// 填充颜色数组
Color[] aColors = GetColorArray();
// 设置图片标题的高度
int iCaptionHeight = 30;
// 设置饼图距离顶部的高度
int iPieTopOffset = iCaptionHeight;
// 计算饼图的大小,并且要保证说明列的宽度是整个图片宽度的40%
// 同时生成饼图的阴影宽度为5个像素
int iPieDiameter = oBitMap.Height - iCaptionHeight - 6;
if (iPieDiameter > (oBitMap.Width * 0.6))
{
iPieDiameter = (int)(oBitMap.Width * 0.6);
iPieTopOffset = ((oBitMap.Height - iPieDiameter) / 2) + iCaptionHeight;
}
// 为饼图和阴影背景定义四边形的区域
Rectangle oRectPie = new Rectangle(0, iPieTopOffset,
iPieDiameter, iPieDiameter);
Rectangle oRectShadow = new Rectangle(5, iPieTopOffset + 5,
iPieDiameter, iPieDiameter);
// 定义阴影的颜色
Color cShadow = Color.FromArgb(153, 153, 153);
// 用阴影色填充阴影区域
oBrush.Color = cShadow;
oGraphics.FillEllipse(oBrush, oRectShadow);
// 从数据集中获得所有行
DataRowCollection colRows = dsData.Tables[0].Rows;
// 计算说明列距离左边距的距离
int iKeyBoxLeft = iPieDiameter + 45;
// 计算说明列的垂直间隔
int iKeyBoxSpace = (oBitMap.Height - iCaptionHeight - 15) /
(colRows.Count - 1);
// 定义一些变量
int iRowIndex = 0; // 当前行的索引
float fSliceStart = 0; // 扇区的起始度数
float fSliceDegrees; // 扇区的度数
int iKeyBoxTop; // 说明列距离顶部的垂直距离
Double fTotalValue = 0; // 要统计字段的值的总和
// 定义一个变量表示当前行的值
Double fSliceValue;
// 通过遍历表中的所有的行计算出要统计的字段的值的总和
foreach (DataRow oERow in colRows)
{
try
{
fTotalValue += Convert.ToDouble(oERow[1]);
}
catch
{
//
}
}
// 再次遍历所有行并开始画图
foreach (DataRow oERow in colRows)
{
// 计算说明行的位置
iKeyBoxTop = iCaptionHeight + (iKeyBoxSpace * iRowIndex);
// 填充说明的阴影
oBrush.Color = cShadow;
oGraphics.FillRectangle(oBrush, iKeyBoxLeft + 3, iKeyBoxTop + 3, 15,
14);
// 从数据行里取得数据
try
{
sCaption = Convert.ToString(oERow[0]);
fSliceValue = Convert.ToDouble(oERow[1]);
}
catch
{
sCaption = "Error";
fSliceValue = 0;
}
// 把数据转换成角度
fSliceDegrees = (float)((fSliceValue / fTotalValue) * 360);
// 设置扇形的颜色
oBrush.Color = aColors[iRowIndex];
// 画扇区
oGraphics.FillPie(oBrush, oRectPie, fSliceStart, fSliceDegrees);
oGraphics.DrawPie(oPen, oRectPie, fSliceStart, fSliceDegrees);
// 画说明
oGraphics.FillRectangle(oBrush, iKeyBoxLeft, iKeyBoxTop, 15, 14);
oGraphics.DrawRectangle(oPen, iKeyBoxLeft, iKeyBoxTop, 15, 14);
// 画标题
DrawText(oGraphics, iKeyBoxLeft + 22, iKeyBoxTop,
oBitMap.Width - iKeyBoxLeft + 22, 14, sCaption,
FontStyle.Bold, 9, "", null, 0);
// 为下一次循环作准备
fSliceStart += fSliceDegrees;
iRowIndex += 1;
}
// 显示图片
oBitMap.Save(Response.OutputStream, ImageFormat.Gif);
// 释放所占用的资源
oPen.Dispose();
oBrush.Dispose();
oGraphics.Dispose();
oBitMap.Dispose();
}
void DrawText(Graphics oGraphics, int iTop, int iLeft, int iWidth,
int iHeight, String sText, object eFontStyle,
int iFontSize, String sColor, object eAlign,
StringFormatFlags eFlag)
{
// 设置默认值
if (eFontStyle == null) eFontStyle = FontStyle.Regular;
if (iFontSize == 0) iFontSize = 8;
if (sColor == "") sColor = "Black";
if (eAlign == null) eAlign = StringAlignment.Near;
// 为标题创建一个区域
RectangleF oRect = new RectangleF(iTop, iLeft, iWidth, iHeight);
// 为标题文本设置字体
Font oFont = new Font("Arial", iFontSize, (FontStyle)eFontStyle);
// 设置文本的样式
StringFormat oFormat = new StringFormat(eFlag);
oFormat.Alignment = (StringAlignment)eAlign;
// 总是在矩形区域里水平和垂直居中显示文本
oFormat.LineAlignment = StringAlignment.Center;
// 画文本
SolidBrush oBrush = new SolidBrush(Color.FromName(sColor));
oGraphics.DrawString(sText, oFont, oBrush, oRect, oFormat);
}
Color[] GetColorArray()
{
// 定义一个包含20个颜色的数组
Color[] aColors = new Color[20];
// 使用浏览器能够安全显示的颜色填充颜色数组
aColors[0] = Color.FromArgb(204, 0, 0); // red
aColors[1] = Color.FromArgb(255, 153, 0); // orange
aColors[2] = Color.FromArgb(255, 255, 0); // yellow
aColors[3] = Color.FromArgb(0 ,255, 0); // green
aColors[4] = Color.FromArgb(0, 255, 255); // cyan
aColors[5] = Color.FromArgb(51, 102, 255); // blue
aColors[6] = Color.FromArgb(255, 0, 255); // magenta
aColors[7] = Color.FromArgb(102, 0, 102); // purple
aColors[8] = Color.FromArgb(153, 0, 0); // dark red
aColors[9] = Color.FromArgb(153, 153, 0); // khaki
aColors[10] = Color.FromArgb(0, 102, 0); // dark green
aColors[11] = Color.FromArgb(51, 51, 102); // dark blue
aColors[12] = Color.FromArgb(102, 51, 0); // brown
aColors[13] = Color.FromArgb(204, 204, 204); // light gray
aColors[14] = Color.FromArgb(0, 0, 0); // black
aColors[15] = Color.FromArgb(102, 204, 255); // sky
aColors[16] = Color.FromArgb(255, 204, 255); // pink
aColors[17] = Color.FromArgb(255, 255, 204); // chiffon
aColors[18] = Color.FromArgb(255, 204, 204); // flesh
aColors[19] = Color.FromArgb(153, 255, 204); // pale green
return aColors;
}
region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataSet11 = new WebApplication4.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=Z001;packet
size=4096;user id=sa;data source=Z001;persist security" +
" info=True;initial catalog=Northwind;password=1";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table",
"Employees", new System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("City", "City"),
new
System.Data.Common.DataColumnMapping("Expr1", "Expr1")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT City, COUNT(EmployeeID) AS
Expr1 FROM Employees GROUP BY City";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
}
endregion
}
}
浙公网安备 33010602011771号