C#绘制各种图形代码.docx
《C#绘制各种图形代码.docx》由会员分享,可在线阅读,更多相关《C#绘制各种图形代码.docx(27页珍藏版)》请在冰豆网上搜索。
C#绘制各种图形代码
01.usingSystem;
02.usingSystem.Collections.Generic;
03.usingSystem.Linq;
04.usingSystem.Text;
05.//
06.usingSystem.Drawing;
07.//
08.namespaceDatePrint
09.{
10.///
11.///根据统计数据,输出各种统计图形,包括饼状图、曲线分析图、柱形图、多组数据曲线分析图;
12.///统计图形统一大小:
600*420;
13.///开发人员:
###;
14.///开发时间:
###;
15.///
16.publicclassCountImage
17.{
18.#region//颜色,画框,
19.///
20.///生成随机颜色
21.///
22.///
23.privatestaticColorGetRandomColor(intseed)
24.{
25.Randomrandom=newRandom(seed);
26.intr=0;
27.intg=0;
28.intb=0;
29.r=random.Next(0,230);
30.g=random.Next(0,230);
31.b=random.Next(0,235);
32.Colorrandomcolor=Color.FromArgb(r,g,b);
33.returnrandomcolor;
34.}
35.///
36.///绘制区域框,框何其阴影
37.///
38.///图形
39.///矩形框对象
40.///图形
41.privatestaticBitmapDrawRectangle(Bitmapimage,Rectanglerect)
42.{
43.BitmapImage=image;
44.Graphicsg=Graphics.FromImage(Image);
45.g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;
46.g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
47.try
48.{
49.Rectanglern=newRectangle(rect.X+3,rect.Y+3,rect.Width,rect.Height);50.SolidBrushbrush1=newSolidBrush(Color.FromArgb(233,234,249));51.SolidBrushbrush2=newSolidBrush(Color.FromArgb(221,213,215));
52.//
53.g.FillRectangle(brush2,rn);
54.g.FillRectangle(brush1,rect);
55.returnImage;
56.}
57.finally
58.{
59.g.Dispose();
60.}
61.}
62.#endregion
63.#region//绘制图例框,绘制扇形
64.///
65.///绘制图例信息
66.///
67.///图像
68.///第一个矩形框
69.///颜色
70.///图例说明文字
71.///文字样式
72.///图例说明序号
73.///图形
74.privatestaticBitmapDrawDes(Bitmapimage,Rectanglerect,Colorc,stringDesStr,Fontf,inti)
75.{
76.BitmapImage=image;
77.Graphicsg=Graphics.FromImage(Image);
78.g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;
79.g.SmoothingMode=System.Drawing.Drawing
2D.SmoothingMode.AntiAlias;
80.try
81.{
82.SolidBrushbrush=newSolidBrush(c);
83.//
84.RectangleR=newRectangle(rect.X,rect.Y+18*i,rect.Width,rect.Height);
85.Pointp=newPoint(rect.X+12,rect.Y+18*i);
86.//?
颜色矩形框
87.g.FillRectangle(brush,R);
88.//文字说明
89.g.DrawString(DesStr,f,newSolidBrush(Color.Black),p);
90.returnImage;
91.}
92.finally
93.{
94.g.Dispose();
95.}
96.}
97.//绘制扇形
98.privatestaticBitmapDrawPie(Bitmapimage,Rectanglerect,Colorc,intAngle1,intAngle2)
99.{
100.BitmapImage=image;
101.Graphicsg=Graphics.FromImage(Image);
102.g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;
103.g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
104.try
105.{
106.SolidBrushbrush=newSolidBrush(c);
107.//
108.RectangleR=newRectangle(rect.X,rect.Y,rect.Width,rect.Height);
109.g.FillPie(brush,R,Angle1,Angle2);
110.returnImage;
111.}
112.finally
113.{
114.g.Dispose();
115.}
116.}
117.#endregion
118.#region//绘制基本图形
119.///
120.///生成图片,统一设置图片大小、背景色,图片布局,及标题
121.///
122.///图片
123.privatestaticBitmapGenerateImage(stringTitle)
124.{
125.//图片大小:
宽度、高度
126.intwidth=600;
127.intheight=420;
128.//标题
129.PointPTitle=newPoint(30,20);
130.Fontfl=newFont("宋体",10,FontStyle.Bold);
131.//线
132.PointPLine1=newPoint(20,40);
133.PointPLine2=newPoint(390,40);
134.Penpen=newPen(newSolidBrush(Color.FromArgb(8,34,231)),1.5f);
135.//主区域,主区域图形
136.RectangleRMain1=newRectangle(20,55,410,345);
137.RectangleRMain2=newRectangle(25,60,400,335);
138.//图例区域
139.RectangleRDes1=newRectangle(440,55,150,345);
140.//图例说明
141.stringDes="图例说明:
5
142.Fontf2=newFont(‘新宋体",9,FontStyle.Bold);
143.PointPDes=newPoint(445,65);
144.//图例信息,后面的x坐标上累加20
145.RectangleRDes2=newRectangle(445,90,10,10);
146.Bitmapimage=newBitmap(width,height);
147.//
148.Graphicsg=Graphics.FromImage(image);
149.g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;
150.g.SmoothingMode=System.Drawing.Drawing
2D.SmoothingMode.AntiAlias;
151.try
152.{
153.//设置背景色、绘制边框
154.g.Clear(Color.White);
155.g.DrawRectangle(newPen(Color.Black),0,0,width-1,height-1);
156.//绘制标题、线
157.g.DrawString(Title,f1,newSolidBrush(Color.Black),PTitle);
158.g.DrawLine(pen,PLine1,PLine2);
159.
160.//主区域
161.image=DrawRectangle(image,RMain1);
162.//图例区域
163.image=DrawRectangle(image,RDes1);
164.//图“例说明”
165.g.DrawString(Des,f2,newSolidBrush(Color.Black),PDes);
166.//return
167.returnimage;
168.}
169.finally
170.{
171.g.Dispose();
172.}
173.}
174.#endregion
175.#region//绘制饼状图
************************
176.///
177.///计算数值综合
178.///
179.///
180.///
181.privatestaticdecimalSum(decimal[]Value)
182.{
183.decimalt=0;
184.foreach(decimaldinValue)
185.{
186.t+=d;
187.}
188.returnt;
189.}
190.///
191.///计算各项比例
192.///
193.///
194.///
195.privatestaticdecimal[]GetItemRate(decimal[]Value)
196.{
197.decimalsum=Sum(Value);
198.decimal[]ItemRate=newdecimal[Value.Length];
199.for(inti=0;i200.{
201.ItemRate[i]=Value[i]/sum;
202.}
203.returnItemRate;
204.}
205.///
206.///根据比例,计算各项角度值
207.///
208.///
209.///
210.privatestaticint[]GetItemAngle(decimal[]ItemRate)
211.{
212.int[]ItemAngel=newint[ItemRate.Length];
213.for(inti=0;i214.{
215.decimalt=360*ItemRate[i];
216.ItemAngel[i]=Convert.ToInt32(t);
217.}
218.returnItemAngel;
219.}
220.///
221.///绘制饼图(主要是分析不同类型的数值所占比例),参数有图的标题,项目名称,项目的数值,名称和数值都是长度对应的
222.///
223.///图的标题
224.///项目名称
225.///项目的数值
226.///Bitmap图形
227.publicstaticBitmapGetPieImage(string
Title,string[]ItemName,decimal[]ItemValue)
228.{
229.Bitmapimage=GenerateImage(Title);
230.//
231.//主区域图形
232.RectangleRMain=newRectangle(35,70,380,300);
233.//图例信息
234.RectangleRDes=newRectangle(445,90,10,10);
235.FOntf=newFont(‘新宋体",9,FontStyle.Bold);
236.Graphicsg=Graphics.FromImage(image);
237.g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;
238.g.SmoothingMode=System.Drawing.Drawing
2D.SmoothingMode.AntiAlias;
239.try
240.{
241.//分析数据,绘制饼图和图例说明
242.decimal[]ItemRate=GetItemRate(ItemValue);
243.int[]ItemAngle=GetItemAngle(ItemRate);
244.intAngle1=0;
245.intAngle2=0;
246.intlen=ItemValue.Length;
247.Colorc=newColor();
248.//3D
249.g.DrawPie(newPen(Color.Black),RMain,0F,360F);
250.g.DrawPie(newPen(Color.Black),newRectangle(RMain.X,RMain.Y+10,RMain.Width,RMain.Height),0F,360F);
251.g.FillPie(newSolidBrush(Color.Black),newRectangle(RMain.X,RMain.Y+10,RMain.Width,RMain.Height),0F,360F);
252.//绘制
253.for(inti=0;i254.{
255.Angle2=ItemAngle[i];
256.//if(c!
=GetRandomColor(i))
257.c=GetRandomColor(i);
258.SolidBrushbrush=newSolidBrush(c);
259.string
DesStr=ItemName[i]+"("+(ItemRate[i]*100).ToString(".00")+"%"+")"+ItemValue[i].ToString(".00");
260.//
261.DrawPie(image,RMain,c,Angle1,Angle2);
262.Angle1+=Angle2;
263.DrawDes(image,RDes,c,DesStr,f,i);
264.}
265.returnimage;
266.}
267.finally
268.{
269.g.Dispose();
270.}
271.}
272.#endregion
273.#region//获取Y轴坐标数据
274./*
275.坐标轴实现算法描述:
276.*X轴坐标根据项目数量把X轴均等分,有效长度350,
277.*丫轴有效长度280,平分为10个等分,即有十个点;
278.*丫轴的数值算法:
第一个点位最小值,然后每个等分所对应的值是(最大值-最小值)/9,
279.*/
280.///
281.///获取Y轴坐标的点分布值
282.///
283.///项目数值
284.///Y轴点的数量
285.///图形
286.privatestaticint[]GetYValue(decimal[]ItemValue,intYCount)
287.{
288.intlen=ItemValue.Length;
289.int[]Value=newint[YCount];
290.intMax=Convert.ToInt32(ItemValue.Max());
291.intMin=Convert.ToInt32(ItemValue.Min());
292.intDistance=Convert.ToInt32((Max-Min)/(YCount-1));
293.for(inti=0;i294.{
295.Value[i]=Min+Distance*i;
296.}
297.//Value[YCount-1]=Max;
298.returnValue;
299.}
300.#endregion
301.#region//建立坐标轴
302.///
303.///绘制坐标轴,X、Y轴的坐标值
304.///
305.///图像
306.///项目名称
307.
308.
309.
ItemValue)
310.
311.
312.
313.
314.
315.
316.
317.
318.
319.
320.
321.
322.
323.
324.
325.
326.
327.
328.
329.
330.
331.
332.
333.
334.
335.
336.
337.
338.
339.
340.///项目数值
///图像
privatestaticBitmapDrawCoordinate(Bitmapimage,string[]ItemName,decimal[]{
//坐标轴
PointP0=newPoint(60,360);
PointPx=newPoint(420,360);
PointPy=newPoint(60,65);
Penpen=newPen(Color.Black);
//箭头
PointPy1=newPoint(58,70);
PointPy2=newPoint(62,70);
PointPx1=newPoint(415,358);
PointPx2=newPoint(415,362);
//Y,XValue
//y280-10
intYCount=10;//Y轴点的数量
intYDistanee=Convert.Tolnt32(280/YCount);//Y轴点击的距离
int[]YValue=GetYValue(ItemValue,YCount);
intlen=3;//短线的长度
intXCount=ItemName.Length;//X轴点的数量
intXDistanee=Convert.Tolnt32(350/XCount);//X轴点间的距离
//
Fontf=newFont("新宋体",8,FontStyle.Bold);
//lmage
Gr