温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:我的小书坊源码(三层实现)
当前文件:
MyBookShop/App_Code/BusinessLogicLayer/Chart.cs,打开代码结构图
MyBookShop/App_Code/BusinessLogicLayer/Chart.cs,打开代码结构图1using System; 2
using System.IO; 3
using System.Data; 4
using System.Drawing; 5
using System.Drawing.Text; 6
using System.Drawing.Drawing2D; 7
using System.Drawing.Imaging; 8
9
using MyBookShop.DataAccessHelper; 10
//该源码下载自www.51aspx.com(51aspx.com) 11
12
namespace MyBookShop.BusinessLogicLayer 13
{ 14
/// <summary> 15
/// 一个通用的画饼图的类 16
/// </summary> 17
public class Chart 18
{ 19
private const int SIDE_LENGTH=400; 20
private const int PIE_DIAMETER=200; 21
22
/// <summary> 23
/// 画饼图方法 24
/// </summary> 25
/// <param name="title">饼图标题</param> 26
/// <param name="subTitle">子标题</param> 27
/// <param name="width">宽</param> 28
/// <param name="height">高</param> 29
/// <param name="dt">数据来源表</param> 30
/// <param name="targetFile">生成的图形保存目标文件</param> 31
/// <param name="descColumn">文字说明列</param> 32
/// <param name="dataColumn">显示在统计图上的数据列名</param> 33
public static void DrawPie(string title,string subTitle,int width,int height,DataTable dt,string targetFile,string descColumn,string dataColumn) 34
{ 35
//创建一个位图对象 36
Bitmap bm=new Bitmap(width,height); 37
Graphics g=Graphics.FromImage(bm); 38
39
//将图象"缩放"到指定面积 40
g.ScaleTransform(Convert.ToSingle(width)/SIDE_LENGTH,Convert.ToSingle(height)/SIDE_LENGTH); 41
42
//画饼图所在的线框 43
g.Clear(Color.White); 44
g.DrawRectangle(Pens.Black,0,0,SIDE_LENGTH-1,SIDE_LENGTH-1); 45
46
//画标题title,副标题subTitle 47
g.DrawString(title,new Font("宋体",14),Brushes.Black,new PointF(5,5)); 48
g.DrawString(subTitle,new Font("宋体",12),Brushes.Black,new PointF(7,35)); 49
50
//画饼图 51
float curAngle=0; //当前所画到的角度 52
float totalAngle=0; //总的角度 53
float sumData=0; //计算所要统计的数据之和 54
55
//计算所要统计的数据之和 56
foreach(DataRow dr in dt.Rows) 57
{ 58
if(dr[dataColumn]!=DBNull.Value) 59
sumData+=Convert.ToSingle(dr[dataColumn]); 60
} 61
62
//循环画出每一个扇形,以及其说明 63
int colorIndex=0; //颜色 64
float percent=0; //每一个种类的百分比 65
g.DrawRectangle(Pens.Black,100,270,300,130); //说明线框 66
PointF boxOrigin=new PointF(110,275); //说明标签起始位置 67
PointF textOrigin=new PointF(135,275); //说明文字起始位置 68
69
//循环画出每一个扇形 70
foreach(DataRow row in dt.Rows) 71
{ 72
if(row[dataColumn]!=System.DBNull.Value) 73
{ 74
colorIndex++; 75
76
//扇形 77
curAngle=Convert.ToSingle(row[dataColumn])/sumData*360; 78
g.FillPie(new SolidBrush(GetColor(colorIndex)),100,60,PIE_DIAMETER,PIE_DIAMETER,totalAngle,curAngle); 79
g.DrawPie(Pens.Black,100,60,PIE_DIAMETER,PIE_DIAMETER,totalAngle,curAngle); 80
totalAngle+=curAngle; 81
82
//标签 83
g.FillRectangle(new SolidBrush(GetColor(colorIndex)),boxOrigin.X,boxOrigin.Y,20,10); 84
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10); 85
86
//说明 87
percent=Convert.ToSingle(row[dataColumn])/sumData*100; 88
g.DrawString( 89
row[descColumn].ToString()+"("+percent.ToString("0.0")+"%)", 90
new Font("宋体",12),Brushes.Black,textOrigin 91
); 92
93
boxOrigin.Y+=20; 94
textOrigin.Y+=20; 95
} 96
} 97
98
//将生成的图保存到target 99
bm.Save(targetFile,ImageFormat.Gif); 100
101
//释放资源 102
bm.Dispose(); 103
g.Dispose(); 104
} 105
106
/// <summary> 107
/// 生成颜色的方法 108
/// </summary> 109
/// <param name="itemIndex"></param> 110
/// <returns></returns> 111
public static Color GetColor(int idx) 112
{ 113
idx*=60; 114
int r=idx%255; 115
int g=((255/3)+idx)%255; 116
int b=((255*2/3)+idx)%255; 117
return Color.FromArgb(r,g,b); 118
} 119
} 120
}





}