1、C#绘制各种图形代码01.using System;02.using System.Collections.Generic;03.using System.Linq;04.using System.Text;05./06.using System.Drawing;07./08.namespace DatePrint09.10./ 11./根据统计数据,输出各种统计图形,包括饼状图、曲线分析图、柱形图、多组数据曲线分析图;12./统计图形统一大小:600*420;13./开发人员:#;14./开发时间:#;15./ 16.public class CountImage 17.18.#region
2、 /颜色,画框,19./ 20./生成随机颜色21./ 22./ 23.private static Color GetRandomColor(int seed)24.25.Random random = new Random(seed);26.int r = 0;27.int g = 0;28.int b = 0;29.r = random.Next(0, 230);30.g = random.Next(0, 230);31.b = random.Next(0, 235);32.Color randomcolor = Color.FromArgb(r, g, b);33.return ran
3、domcolor;34.35./ 36./绘制区域框,框何其阴影37./ 38./ 图形39./ 矩形框对象40./ 图形41.private static Bitmap DrawRectangle(Bitmap image,Rectangle rect)42.43.Bitmap Image=image;44.Graphics g = Graphics.FromImage(Image);45.g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;46.g.SmoothingMode = System.Draw
4、ing.Drawing2D.SmoothingMode.AntiAlias;47.try 48.49.Rectangle rn =new Rectangle(rect.X +3,rect.Y +3,rect.Width,rect.Height);50.SolidBrush brush1 = new SolidBrush(Color.FromArgb(233, 234, 249);51.SolidBrush brush2 = new SolidBrush(Color.FromArgb(221, 213, 215);52./53.g.FillRectangle(brush2, rn);54.g.F
5、illRectangle(brush1, rect);55.return Image;56.57.finally 58.59.g.Dispose();60.61.62.#endregion63.#region /绘制图例框,绘制扇形64./ 65./绘制图例信息66./ 67./ 图像68./ 第一个矩形框69./ 颜色70./ 图例说明文字71./ 文字样式72./ 图例说明序号73./ 图形74.private static Bitmap DrawDes(Bitmap image,Rectangle rect,Color c,string DesStr,Font f,int i)75.76
6、.Bitmap Image = image;77.Graphics g = Graphics.FromImage(Image);78.g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;79.g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;80.try 81.82.SolidBrush brush = new SolidBrush(c);83./84.Rectangle R =new Rectangle(rect.X,re
7、ct.Y +18*i,rect.Width,rect.Height);85.Point p = new Point(rect.X + 12, rect.Y + 18 * i);86./颜色矩形框87.g.FillRectangle(brush,R);88./文字说明89.g.DrawString(DesStr, f, new SolidBrush(Color.Black), p);90.return Image;91.92.finally 93.94.g.Dispose();95.96.97./绘制扇形98.private static Bitmap DrawPie(Bitmap image,
8、 Rectangle rect, Color c, int Angle1, int Angle2)99.100.Bitmap Image = image;101.Graphics g = Graphics.FromImage(Image);102.g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;103.g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;104.try 105.106.SolidBrush brush =
9、new SolidBrush(c);107./108.Rectangle R = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);109.g.FillPie(brush, R, Angle1, Angle2);110.return Image;111.112.finally 113.114.g.Dispose();115.116.117.#endregion118.#region/绘制基本图形119./ 120./生成图片,统一设置图片大小、背景色,图片布局,及标题121./ 122./ 图片123.private static B
10、itmap GenerateImage(string Title)124.125./图片大小:宽度、高度126.int width = 600;127.int height = 420;128./标题129.Point PTitle=new Point(30,20);130.Font f1 = new Font(宋体, 10, FontStyle.Bold);131./线132.Point PLine1=new Point(20,40);133.Point PLine2=new Point(390,40);134.Pen pen = new Pen(new SolidBrush(Color.F
11、romArgb(8,34,231),1.5f);135./主区域,主区域图形136.Rectangle RMain1=new Rectangle(20,55,410,345);137.Rectangle RMain2=new Rectangle(25,60,400,335);138./图例区域139.Rectangle RDes1=new Rectangle(440,55,150,345);140./图例说明141.string Des=图例说明:;142.Font f2 = new Font(新宋体, 9, FontStyle.Bold);143.Point PDes=new Point(4
12、45,65);144./图例信息,后面的x坐标上累加20145.Rectangle RDes2=new Rectangle(445,90,10,10);146.Bitmap image = new Bitmap(width, height);147./148.Graphics g = Graphics.FromImage(image);149.g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;150.g.SmoothingMode = System.Drawing.Drawing2D.SmoothingM
13、ode.AntiAlias;151.try 152.153./设置背景色、绘制边框154.g.Clear(Color.White);155.g.DrawRectangle(new Pen(Color.Black), 0, 0, width - 1, height - 1);156./绘制标题、线157.g.DrawString(Title, f1, new SolidBrush(Color.Black),PTitle);158.g.DrawLine(pen, PLine1, PLine2);159.160./主区域161.image = DrawRectangle(image, RMain1)
14、;162./图例区域163.image = DrawRectangle(image, RDes1);164./“图例说明”165.g.DrawString(Des, f2, new SolidBrush(Color.Black), PDes);166./return167.return image;168.169.finally 170.171.g.Dispose();172.173.174.#endregion175.#region /绘制饼状图*176./ 177./计算数值综合178./ 179./ 180./ 181.private static decimal Sum(decimal
15、 Value)182.183.decimal t=0;184.foreach (decimal d in Value)185.186.t += d;187.188.return t;189.190./ 191./计算各项比例192./ 193./ 194./ 195.private static decimal GetItemRate(decimal Value)196.197.decimal sum = Sum(Value);198.decimal ItemRate = new decimalValue.Length;199.for (int i = 0; i Value.Length; i
16、+)200.201.ItemRatei = Valuei / sum;202.203.return ItemRate;204.205./ 206./根据比例,计算各项角度值207./ 208./ 209./ 210.private static int GetItemAngle(decimal ItemRate)211.212.int ItemAngel = new intItemRate.Length;213.for (int i = 0; i ItemRate.Length; i+)214.215.decimal t=360*ItemRatei;216.ItemAngeli = Conve
17、rt.ToInt32(t);217.218.return ItemAngel;219.220./ 221./绘制饼图(主要是分析不同类型的数值所占比例),参数有图的标题,项目名称,项目的数值,名称和数值都是长度对应的222./ 223./ 图的标题224./ 项目名称225./ 项目的数值226./ Bitmap图形227.public static Bitmap GetPieImage(string Title,stringItemName,decimalItemValue)228.229.Bitmap image = GenerateImage(Title);230./231./主区域图形
18、232.Rectangle RMain = new Rectangle(35, 70, 380, 300);233./图例信息234.Rectangle RDes = new Rectangle(445, 90, 10, 10);235.Font f = new Font(新宋体, 9, FontStyle.Bold);236.Graphics g = Graphics.FromImage(image);237.g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;238.g.SmoothingMode =
19、System.Drawing.Drawing2D.SmoothingMode.AntiAlias;239.try 240.241./分析数据,绘制饼图和图例说明242.decimal ItemRate = GetItemRate(ItemValue);243.int ItemAngle = GetItemAngle(ItemRate);244.int Angle1 = 0;245.int Angle2 = 0;246.int len = ItemValue.Length;247.Color c = new Color();248./3D249.g.DrawPie(new Pen(Color.B
20、lack), RMain, 0F, 360F);250.g.DrawPie(new Pen(Color.Black),new Rectangle(RMain.X,RMain.Y +10,RMain.Width, RMain.Height), 0F, 360F);251.g.FillPie(new SolidBrush (Color.Black), new Rectangle(RMain.X, RMain.Y +10, RMain.Width, RMain.Height), 0F, 360F);252./绘制253.for (int i = 0; i len; i+)254.255.Angle2
21、 = ItemAnglei;256./if (c != GetRandomColor(i)257.c = GetRandomColor(i);258.SolidBrush brush=new SolidBrush(c);259.string DesStr=ItemNamei+(+(ItemRatei*100).ToString(.00)+%+)+ItemValuei.ToString(.00);260./261.DrawPie(image, RMain, c, Angle1, Angle2);262.Angle1 += Angle2;263.DrawDes(image, RDes, c, De
22、sStr, f, i);264.265.return image;266.267.finally 268.269.g.Dispose();270.271.272.#endregion273.#region /获取Y轴坐标数据274./*275.坐标轴实现算法描述:276.* X轴坐标根据项目数量把X轴均等分,有效长度350,277.* Y轴有效长度280,平分为10个等分,即有十个点;278.* Y轴的数值算法:第一个点位最小值,然后每个等分所对应的值是(最大值-最小值)/9,279.*/280./ 281./获取Y轴坐标的点分布值282./ 283./ 项目数值284./ Y轴点的数量285
23、./ 图形286.private static int GetYValue(decimal ItemValue,int YCount)287.288.int len = ItemValue.Length;289.int Value = new intYCount;290.int Max = Convert.ToInt32(ItemValue.Max();291.int Min = Convert.ToInt32(ItemValue.Min();292.int Distance = Convert.ToInt32(Max-Min)/(YCount-1);293.for (int i = 0; i
24、 YCount; i+)294.295.Valuei = Min + Distance * i;296.297./ValueYCount - 1 = Max;298.return Value;299.300.#endregion301.#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.3
25、34.335.336.337.338.339.340./ 项目数值/ 图像private static Bitmap DrawCoordinate(Bitmap image,stringItemName,decimal/坐标轴Point P0 = new Point(60, 360);Point Px = new Point(420, 360);Point Py = new Point(60, 65);Pen pen=new Pen(Color.Black);/箭头Point Py1=new Point(58,70);Point Py2=new Point(62,70);Point Px1=n
26、ew Point(415,358);Point Px2=new Point(415,362);/Y,X Value/y 280-10int YCount = 10;/Y轴点的数量int YDistance = Convert.ToInt32(280/YCount) ;/Y轴点击的距离int YValue = GetYValue(ItemValue, YCount);int len = 3;/短线的长度int XCount = ItemName.Length;/X轴点的数量int XDistance = Convert.ToInt32(350/XCount);/X轴点间的距离/Font f = new Font(新宋体, 8, FontStyle.Bold);/ImageGraphics g = Graphics.FromImage(image);g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1