生成各种统计图的C#方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Drawing;
//
namespace DatePrint
{
??? /// <summary>
??? /// 根據(jù)統(tǒng)計數(shù)據(jù),輸出各種統(tǒng)計統(tǒng)計圖形,包括餅狀圖、曲線分析圖、柱形圖、多組數(shù)據(jù)曲線分析圖;
??? /// 統(tǒng)計圖形統(tǒng)一大小:600*420;
??? /// 開發(fā)人員:###;
??? /// 開發(fā)時間:###;
??? /// </summary>
??? public? class CountImage
??? {
??????? #region // 顏色,畫框,
??????? /// <summary>
??????? /// 生成隨機顏色
??????? /// </summary>
??????? /// <returns></returns>
??????? private static Color GetRandomColor(int seed)
??????? {
??????????? Random random = new Random(seed);
??????????? int r = 0;
??????????? int g = 0;
??????????? int b = 0;
??????????? r = random.Next(0, 230);
??????????? g = random.Next(0, 230);
??????????? b = random.Next(0, 235);
??????????? Color randomcolor = Color.FromArgb(r, g, b);
??????????? return randomcolor;
??????? }
??????? /// <summary>
??????? /// 繪制區(qū)域框,框何其陰影
??????? /// </summary>
??????? /// <param name="image">圖形</param>
??????? /// <param name="rect">矩形框?qū)ο?lt;/param>
??????? /// <returns>圖形</returns>
??????? private static Bitmap DrawRectangle(Bitmap image,Rectangle rect)
??????? {
??????????? Bitmap Image=image;
??????????? Graphics g = Graphics.FromImage(Image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? Rectangle rn = new Rectangle(rect.X + 3, rect.Y + 3, rect.Width, rect.Height);
??????????????? SolidBrush brush1 = new SolidBrush(Color.FromArgb(233, 234, 249));
??????????????? SolidBrush brush2 = new SolidBrush(Color.FromArgb(221, 213, 215));
??????????????? //
??????????????? g.FillRectangle(brush2, rn);
??????????????? g.FillRectangle(brush1, rect);
??????????????? return Image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
??????? #region //繪制圖例框,繪制扇形
??????? /// <summary>
??????? /// 繪制圖例信息
??????? /// </summary>
??????? /// <param name="image">圖像</param>
??????? /// <param name="rect">第一個矩形框</param>
??????? /// <param name="c">顏色</param>
??????? /// <param name="DesStr">圖例說明文字</param>
??????? /// <param name="f">文字樣式</param>
??????? /// <param name="i">圖例說明序號</param>
??????? /// <returns>圖形</returns>
??????? private static Bitmap DrawDes(Bitmap image, Rectangle rect,Color c,string DesStr,Font f,int i)
??????? {
??????????? Bitmap Image = image;
??????????? Graphics g = Graphics.FromImage(Image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? SolidBrush brush = new SolidBrush(c);
??????????????? //
??????????????? Rectangle R = new Rectangle(rect.X, rect.Y + 18 * i, rect.Width, rect.Height);
??????????????? Point p = new Point(rect.X + 12, rect.Y + 18 * i);
??????????????? //?顏色矩形框
??????????????? g.FillRectangle(brush,R);
??????????????? //文字說明
??????????????? g.DrawString(DesStr, f, new SolidBrush(Color.Black), p);
??????????????? return Image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? //繪制扇形
??????? private static Bitmap DrawPie(Bitmap image, Rectangle rect, Color c, int Angle1, int Angle2)
??????? {
??????????? Bitmap Image = image;
??????????? Graphics g = Graphics.FromImage(Image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? SolidBrush brush = new SolidBrush(c);
??????????????? //
??????????????? Rectangle R = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
??????????????? g.FillPie(brush, R, Angle1, Angle2);
??????????????? return Image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
??????? #region//繪制基本圖形
??????? /// <summary>
??????? /// 生成圖片,統(tǒng)一設(shè)置圖片大小、背景色,圖片布局,及標(biāo)題
??????? /// </summary>
??????? /// <returns>圖片</returns>
??????? private static Bitmap GenerateImage(string Title)
??????? {
??????????? //圖片大小:寬度、高度
??????????? int width = 600;
??????????? int height = 420;
??????????? //標(biāo)題
??????????? Point PTitle=new Point(30,20);
??????????? Font f1 = new Font("宋體", 10, FontStyle.Bold);
??????????? //線
??????????? Point PLine1=new Point(20,40);
??????????? Point PLine2=new Point(390,40);
??????????? Pen pen = new Pen(new SolidBrush(Color.FromArgb(8,34,231)),1.5f);
??????????? //主區(qū)域,主區(qū)域圖形
??????????? Rectangle RMain1=new Rectangle(20,55,410,345);
??????????? Rectangle RMain2=new Rectangle(25,60,400,335);
??????????? //圖例區(qū)域
??????????? Rectangle RDes1=new Rectangle(440,55,150,345);
??????????? //圖例說明
??????????? string Des="圖例說明:";
??????????? Font f2 = new Font("新宋體", 9, FontStyle.Bold);
??????????? Point PDes=new Point(445,65);
??????????? //圖例信息,后面的x坐標(biāo)上累加20
??????????? Rectangle RDes2=new Rectangle(445,90,10,10);
??????????? Bitmap image = new Bitmap(width, height);
??????????? //
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? //設(shè)置背景色、繪制邊框
??????????????? g.Clear(Color.White);
??????????????? g.DrawRectangle(new Pen(Color.Black), 0, 0, width - 1, height - 1);
??????????????? //繪制標(biāo)題、線
??????????????? g.DrawString(Title, f1, new SolidBrush(Color.Black),PTitle);
??????????????? g.DrawLine(pen, PLine1, PLine2);
???????????????
??????????????? //主區(qū)域
??????????????? image = DrawRectangle(image, RMain1);
??????????????? //圖例區(qū)域
??????????????? image = DrawRectangle(image, RDes1);
??????????????? //“圖例說明”
??????????????? g.DrawString(Des, f2, new SolidBrush(Color.Black), PDes);
??????????????? //return
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
??????? #region //繪制餅狀圖************************
??????? /// <summary>
??????? /// 計算數(shù)值綜合
??????? /// </summary>
??????? /// <param name="Value"></param>
??????? /// <returns></returns>
??????? private static decimal Sum(decimal[] Value)
??????? {
??????????? decimal t=0;
??????????? foreach (decimal d in Value)
??????????? {
??????????????? t += d;
??????????? }
??????????? return t;
??????? }
??????? /// <summary>
??????? /// 計算各項比例
??????? /// </summary>
??????? /// <param name="Value"></param>
??????? /// <returns></returns>
??????? private static decimal[] GetItemRate(decimal[] Value)
??????? {
??????????? decimal sum = Sum(Value);
??????????? decimal[] ItemRate = new decimal[Value.Length];
??????????? for (int i = 0; i < Value.Length; i++)
??????????? {
??????????????? ItemRate[i] = Value[i] / sum;
??????????? }
??????????? return ItemRate;
??????? }
??????? /// <summary>
??????? /// 根據(jù)比例,計算各項角度值
??????? /// </summary>
??????? /// <param name="ItemRate"></param>
??????? /// <returns></returns>
??????? private static int[] GetItemAngle(decimal[] ItemRate)
??????? {
??????????? int[] ItemAngel = new int[ItemRate.Length];
??????????? for (int i = 0; i < ItemRate.Length; i++)
??????????? {
??????????????? decimal t=360*ItemRate[i];
??????????????? ItemAngel[i] = Convert.ToInt32(t);
??????????? }
??????????? return ItemAngel;
??????? }
??????? /// <summary>
??????? /// 繪制餅圖(主要是分析不同類型的數(shù)值所占比例),參數(shù)有圖的標(biāo)題,項目名稱,項目的數(shù)值,名稱和數(shù)值都是長度對應(yīng)的
??????? /// </summary>
??????? /// <param name="Title">圖的標(biāo)題</param>
??????? /// <param name="ItemName">項目名稱</param>
??????? /// <param name="ItemValue">項目的數(shù)值</param>
??????? /// <returns>Bitmap圖形</returns>
??????? public static Bitmap GetPieImage(string Title, string[] ItemName, decimal[] ItemValue)
??????? {
??????????? Bitmap image = GenerateImage(Title);
??????????? //
??????????? //主區(qū)域圖形
??????????? Rectangle RMain = new Rectangle(35, 70, 380, 300);
??????????? //圖例信息
??????????? Rectangle RDes = new Rectangle(445, 90, 10, 10);
??????????? Font f = new Font("新宋體", 9, FontStyle.Bold);
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? //分析數(shù)據(jù),繪制餅圖和圖例說明
??????????????? decimal[] ItemRate = GetItemRate(ItemValue);
??????????????? int[] ItemAngle = GetItemAngle(ItemRate);
??????????????? int Angle1 = 0;
??????????????? int Angle2 = 0;
??????????????? int len = ItemValue.Length;
??????????????? Color c = new Color();
??????????????? //3D
??????????????? g.DrawPie(new Pen(Color.Black), RMain, 0F, 360F);
??????????????? g.DrawPie(new Pen(Color.Black), new Rectangle(RMain.X, RMain.Y + 10, RMain.Width, RMain.Height), 0F, 360F);
??????????????? g.FillPie(new SolidBrush (Color.Black), new Rectangle(RMain.X, RMain.Y + 10, RMain.Width, RMain.Height), 0F, 360F);
??????????????? //繪制
??????????????? for (int i = 0; i < len; i++)
??????????????? {???????????????????
??????????????????? Angle2 = ItemAngle[i];
??????????????????? //if (c != GetRandomColor(i))
??????????????????????? c = GetRandomColor(i);
??????????????????? SolidBrush brush=new SolidBrush(c);
??????????????????? string DesStr=ItemName[i]+"("+(ItemRate[i]*100).ToString(".00")+"%"+")"+ItemValue[i].ToString(".00");
??????????????????? //
??????????????????? DrawPie(image, RMain, c, Angle1, Angle2);
??????????????????? Angle1 += Angle2;
??????????????????? DrawDes(image, RDes, c, DesStr, f, i);
??????????????? }
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
??????? #region //獲取Y軸坐標(biāo)數(shù)據(jù)
??????? /*
??????? 坐標(biāo)軸實現(xiàn)算法描述:
???????? * X軸坐標(biāo)根據(jù)項目數(shù)量把X軸均等分,有效長度350,
???????? * Y軸有效長度280,平分為10個等分,即有十個點;
???????? * Y軸的數(shù)值算法:第一個點位最小值,然后每個等分所對應(yīng)的值是(最大值-最小值)/9,
??????? */
??????? /// <summary>
??????? /// 獲取Y軸坐標(biāo)的點分布值
??????? /// </summary>
??????? /// <param name="ItemValue">項目數(shù)值</param>
??????? /// <param name="YCount">Y軸點的數(shù)量</param>
??????? /// <returns>圖形</returns>
??????? private static int[] GetYValue(decimal[] ItemValue,int YCount)
??????? {
??????????? int len = ItemValue.Length;
??????????? int[] Value = new int[YCount];
??????????? int Max = Convert.ToInt32(ItemValue.Max());
??????????? int Min = Convert.ToInt32(ItemValue.Min());
??????????? int Distance = Convert.ToInt32((Max-Min)/(YCount-1));
??????????? for (int i = 0; i < YCount; i++)
??????????? {
??????????????? Value[i] = Min + Distance * i;
??????????? }
??????????? //Value[YCount - 1] = Max;
??????????? return Value;
??????? }
??????? #endregion
??????? #region //建立坐標(biāo)軸
??????? /// <summary>
??????? /// 繪制坐標(biāo)軸,X、Y軸的坐標(biāo)值
??????? /// </summary>
??????? /// <param name="image">圖像</param>
??????? /// <param name="ItemName">項目名稱</param>
??????? /// <param name="ItemValue">項目數(shù)值</param>
??????? /// <returns>圖像</returns>
??????? private static Bitmap DrawCoordinate(Bitmap image,string[] ItemName, decimal[] ItemValue)
??????? {
??????????? //坐標(biāo)軸
??????????? 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=new Point(415,358);
??????????? Point Px2=new Point(415,362);
??????????? //Y,X Value
??????????? //y 280-10
??????????? int YCount = 10;//Y軸點的數(shù)量
??????????? int YDistance = Convert.ToInt32(280/YCount) ;//Y軸點擊的距離
??????????? int[] YValue = GetYValue(ItemValue, YCount);
??????????? int len = 3;//短線的長度
??????????? int XCount = ItemName.Length;//X軸點的數(shù)量
??????????? int XDistance = Convert.ToInt32(350/XCount);//X軸點間的距離
??????????? //
??????????? Font f = new Font("新宋體", 8, FontStyle.Bold);
??????????? //Image
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? //繪制坐標(biāo)軸線
??????????????? g.DrawLine(pen, P0, Px);
??????????????? g.DrawLine(pen, P0, Py);
??????????????? //箭頭
??????????????? g.DrawLine(pen, Py, Py1);
??????????????? g.DrawLine(pen, Py, Py2);
??????????????? g.DrawLine(pen, Px, Px1);
??????????????? g.DrawLine(pen, Px, Px2);
??????????????? //X
??????????????? for (int i = 1; i <= XCount; i++)
??????????????? {
??????????????????? Point pl1 = new Point(P0.X+i*XDistance,P0.Y);
??????????????????? Point pl2 = new Point(P0.X + i * XDistance, P0.Y - len);
??????????????????? string str=ItemName[i - 1];
??????????????????? Point ps = new Point(pl1.X-(str.Length*8),pl1.Y+5);
??????????????????? g.DrawLine(pen, pl1, pl2);
??????????????????? g.DrawString(str, f, new SolidBrush(Color.Black), ps);
??????????????? }
??????????????? //Y
??????????????? for (int i = 1; i <= YCount; i++)
??????????????? {
??????????????????? Point pl1 = new Point(P0.X, P0.Y-YDistance*i);
??????????????????? Point pl2 = new Point(pl1.X+len, pl1.Y);
??????????????????? string str=YValue[i-1].ToString();
??????????????????? Point ps = new Point(pl1.X - str.Length*8, pl1.Y - 5);
??????????????????? g.DrawLine(pen, pl1, pl2);
??????????????????? g.DrawString(str, f, new SolidBrush(Color.Black), ps);
??????????????? }
??????????????? //0
??????????????? g.DrawString("0",f,new SolidBrush(Color.Black),new Point(P0.X-10,P0.Y-10));
??????????????? //return
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
???????
??????? #region //獲取某個數(shù)值在坐標(biāo)系中的位置
??????? /// <summary>
??????? /// 獲取某個數(shù)值在坐標(biāo)系中的位置
??????? /// </summary>
??????? /// <param name="Value">當(dāng)前數(shù)值</param>
??????? /// <param name="ItemValue">所有數(shù)值</param>
??????? /// <returns>位置</returns>
??????? private static int GetCoordinateValue(decimal Value,decimal[] ItemValue)
??????? {
??????????? //y 280-10
??????????? //get y value
??????????? int[] YValue=GetYValue(ItemValue,10);
??????????? int Max = YValue.Max();
??????????? int Min = YValue.Min();
??????????? float AvgDis = (Max - Min) / 9;
??????????? float tt = (Convert.ToSingle(Value) - Min) / AvgDis;
??????????? int m = Convert.ToInt32(tt*28);
??????????? m = 360 - 28 - m;
??????????? //if((Convert.ToInt32(Value)) >= Max)
??????????? //??? m=80;
??????????? //else if (Convert.ToInt32(Value) <= Min)
??????????? //??? m=332;
??????????? return m;
??????? }
??????? #endregion
??????? #region //繪制曲線圖****************************
??????? /// <summary>
??????? /// 繪制曲線圖(主要是分析不同類型的數(shù)值所占比例,或者同意項目不同狀態(tài)下的值和趨勢),參數(shù)有圖的標(biāo)題,項目名稱,項目的數(shù)值,名稱和數(shù)值都是長度對應(yīng)的
??????? /// </summary>
??????? /// <param name="Title">圖的標(biāo)題</param>
??????? /// <param name="ItemName">項目名稱</param>
??????? /// <param name="ItemValue">項目的數(shù)值</param>
??????? /// <returns>Bitmap圖形</returns>
??????? public static Bitmap GetLineImage(string Title, string[] ItemName, decimal[] ItemValue)
??????? {
??????????? Bitmap image = GenerateImage(Title);
??????????? image = DrawCoordinate(image, ItemName, ItemValue);
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? int PNum = ItemName.Length;
??????????????? Point[] Pts = new Point[PNum];
??????????????? //坐標(biāo)軸
??????????????? Point P0 = new Point(60, 360);
??????????????? Point Px = new Point(420, 360);
??????????????? Point Py = new Point(60, 65);
??????????????? Pen pen = new Pen(Color.Black);
??????????????? //
??????????????? int XCount = ItemName.Length;//X軸點的數(shù)量
??????????????? int XDistance = Convert.ToInt32(350 / XCount);//X軸點間的距離
??????????????? //圖例
??????????????? Rectangle RDes = new Rectangle(445, 90, 10, 10);
??????????????? Font f = new Font("新宋體", 9, FontStyle.Bold);
??????????????? //
??????????????? for (int i = 0; i < PNum; i++)
??????????????? {
??????????????????? int x=P0.X + (i+1) * XDistance;
??????????????????? int y = GetCoordinateValue(ItemValue[i], ItemValue);
??????????????????? Pts[i] = new Point(x, y);
??????????????? }
??????????????? //把點連起來
??????????????? for (int i = 1; i < PNum; i++)
??????????????? {
??????????????????? g.DrawLine(pen, Pts[i - 1], Pts[i]);
??????????????? }
??????????????? //畫圖例說明
??????????????? Color c = GetRandomColor(3);
??????????????? for (int i = 0; i < PNum; i++)
??????????????? {
??????????????????? string str = ItemName[i] + ":" + ItemValue[i].ToString();
??????????????????? DrawDes(image, RDes, c, str, f, i);
??????????????? }
??????????????? //return
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
??????? #region //繪制柱狀圖*********************
??????? /// <summary>
??????? /// 繪制柱狀圖(主要是分析某一個項目在不同狀態(tài)下的值,獲取其發(fā)展趨勢),參數(shù)有圖的標(biāo)題,項目名稱,項目的數(shù)值,名稱和數(shù)值都是長度對應(yīng)的
??????? /// </summary>
??????? /// <param name="Title">圖的標(biāo)題</param>
??????? /// <param name="ItemName">項目名稱</param>
??????? /// <param name="ItemValue">項目的數(shù)值</param>
??????? /// <returns>Bitmap圖形</returns>
??????? public static Bitmap GetColumnImage(string Title, string[] ItemName, decimal[] ItemValue)
??????? {
??????????? Bitmap image = GenerateImage(Title);
??????????? DrawCoordinate(image,ItemName,ItemValue);
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? int PNum = ItemName.Length;
??????????????? Point[] Pts = new Point[PNum];
??????????????? //坐標(biāo)軸
??????????????? Point P0 = new Point(60, 360);
??????????????? Point Px = new Point(420, 360);
??????????????? Point Py = new Point(60, 65);
??????????????? Pen pen = new Pen(Color.Black);
??????????????? //
??????????????? int XCount = ItemName.Length;//X軸點的數(shù)量
??????????????? int XDistance = Convert.ToInt32(350 / XCount);//X軸點間的距離
??????????????? //圖例
??????????????? Rectangle RDes = new Rectangle(445, 90, 10, 10);
??????????????? Font f = new Font("新宋體", 9, FontStyle.Bold);
??????????????? //獲取值所對應(yīng)的點
??????????????? for (int i = 0; i < PNum; i++)
??????????????? {
??????????????????? int x = P0.X + (i + 1) * XDistance;
??????????????????? int y = GetCoordinateValue(ItemValue[i], ItemValue);
??????????????????? Pts[i] = new Point(x, y);
??????????????? }
??????????????? //繪制條形框圖
??????????????? Color c=GetRandomColor(0);
??????????????? for (int i = 0; i < PNum; i++)
??????????????? {
??????????????????? Rectangle rect = new Rectangle(Pts[i].X - 5, Pts[i].Y, 10, P0.Y - Pts[i].Y);
??????????????????? Font ff = new Font("新宋體", 8, FontStyle.Regular);
??????????????????? g.DrawString(ItemValue[i].ToString(".00"), f, new SolidBrush(Color.Black), new Point(Pts[i].X - 15, Pts[i].Y - 12));
??????????????????? g.DrawRectangle(new Pen(Color.Black), rect);
??????????????????? g.FillRectangle(new SolidBrush(c), rect);
??????????????? }
??????????????? //畫圖例說明
??????????????? for (int i = 0; i < PNum; i++)
??????????????? {
??????????????????? string str = ItemName[i] + ":" + ItemValue[i].ToString();
??????????????????? DrawDes(image, RDes, c, str, f, i);
??????????????? }
??????????????? //return
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? #endregion
??????? #region //繪制多組數(shù)據(jù)曲線圖********************
??????? /// <summary>
??????? /// 獲取二維數(shù)組中的最大值
??????? /// </summary>
??????? /// <param name="ItemValues"></param>
??????? /// <returns></returns>
??????? private static int GetMax(decimal[][] ItemValues)
??????? {
??????????? int Max = 0;
??????????? for (int i = 0; i < ItemValues.Length; i++)
??????????? {
??????????????? int t = Convert.ToInt32(ItemValues[i].Max());
??????????????? if (i == 0)
??????????????????? Max = t;
??????????????? if (Max <= t)
??????????????????? Max = t;
??????????? }
??????????? return Max;
??????? }
??????? /// <summary>
??????? /// 獲取二維數(shù)組中的最小值
??????? /// </summary>
??????? /// <param name="ItemValues"></param>
??????? /// <returns></returns>
??????? private static int GetMin(decimal[][] ItemValues)
??????? {
??????????? int Min = 0;
??????????? for (int i = 0; i < ItemValues.Length; i++)
??????????? {
??????????????? int t = Convert.ToInt32(ItemValues[i].Min());
??????????????? if (i == 0)
??????????????????? Min = t;
??????????????? if (Min >= t)
??????????????????? Min = t;
??????????? }
??????????? return Min;
??????? }
??????? /// <summary>
??????? /// 獲取Y軸上點的數(shù)值
??????? /// </summary>
??????? /// <param name="ItemValues"></param>
??????? /// <param name="YCount"></param>
??????? /// <returns></returns>
??????? private static int[] GetYValue(decimal[][] ItemValues, int YCount)
??????? {
??????????? int[] Value = new int[YCount];
??????????? int Max = GetMax(ItemValues);
??????????? int Min = GetMin(ItemValues);
??????????? int Distance = Convert.ToInt32((Max - Min) / (YCount - 1));
??????????? for (int i = 0; i < YCount; i++)
??????????? {
??????????????? Value[i] = Min + Distance * i;
??????????? }
??????????? //Value[YCount - 1] = Max;
??????????? return Value;
??????? }
??????? /// <summary>
??????? /// 獲取某個數(shù)值在坐標(biāo)系中的位置
??????? /// </summary>
??????? /// <param name="Value">當(dāng)前數(shù)值</param>
??????? /// <param name="ItemValue">所有數(shù)值</param>
??????? /// <returns>位置</returns>
??????? private static int GetCoordinateValue(decimal Value, decimal[][] ItemValues)
??????? {
??????????? //y 280-10
??????????? //get y value
??????????? int[] YValue = GetYValue(ItemValues, 10);
??????????? int Max = GetMax(ItemValues);
??????????? int Min = GetMin(ItemValues);
??????????? float AvgDis = (Max - Min) / 9;
??????????? float tt = (Convert.ToSingle(Value) - Min) / AvgDis;
??????????? int m = Convert.ToInt32(tt * 28);
??????????? m = 360 - 28 - m;
??????????? return m;
??????? }
??????? /// <summary>
??????? /// 繪制坐標(biāo)軸,X、Y軸的坐標(biāo)值
??????? /// </summary>
??????? /// <param name="image">圖像</param>
??????? /// <param name="ItemName">項目名稱</param>
??????? /// <param name="ItemValue">多組項目數(shù)值</param>
??????? /// <returns>圖像</returns>
??????? private static Bitmap DrawCoordinate(Bitmap image,string[] ItemName, decimal[][] ItemValues)
??????? {
??????????? //坐標(biāo)軸
??????????? 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=new Point(415,358);
??????????? Point Px2=new Point(415,362);
??????????? //Y,X Value
??????????? //y 280-10
??????????? int YCount = 10;//Y軸點的數(shù)量
??????????? int YDistance = Convert.ToInt32(280/YCount) ;//Y軸點擊的距離
??????????? int[] YValue = GetYValue(ItemValues, YCount);
??????????? int len = 3;//短線的長度
??????????? int XCount = ItemName.Length;//X軸點的數(shù)量
??????????? int XDistance = Convert.ToInt32(350/XCount);//X軸點間的距離
??????????? //
??????????? Font f = new Font("新宋體", 8, FontStyle.Bold);
??????????? //Image
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? //繪制坐標(biāo)軸線
??????????????? g.DrawLine(pen, P0, Px);
??????????????? g.DrawLine(pen, P0, Py);
??????????????? //箭頭
??????????????? g.DrawLine(pen, Py, Py1);
??????????????? g.DrawLine(pen, Py, Py2);
??????????????? g.DrawLine(pen, Px, Px1);
??????????????? g.DrawLine(pen, Px, Px2);
??????????????? //X
??????????????? for (int i = 1; i <= XCount; i++)
??????????????? {
??????????????????? Point pl1 = new Point(P0.X+i*XDistance,P0.Y);
??????????????????? Point pl2 = new Point(P0.X + i * XDistance, P0.Y - len);
??????????????????? string str=ItemName[i - 1];
??????????????????? Point ps = new Point(pl1.X-(str.Length*8),pl1.Y+5);
??????????????????? g.DrawLine(pen, pl1, pl2);
??????????????????? g.DrawString(str, f, new SolidBrush(Color.Black), ps);
??????????????? }
??????????????? //Y
??????????????? for (int i = 1; i <= YCount; i++)
??????????????? {
??????????????????? Point pl1 = new Point(P0.X, P0.Y-YDistance*i);
??????????????????? Point pl2 = new Point(pl1.X+len, pl1.Y);
??????????????????? string str=YValue[i-1].ToString();
??????????????????? Point ps = new Point(pl1.X - str.Length*8, pl1.Y - 5);
??????????????????? g.DrawLine(pen, pl1, pl2);
??????????????????? g.DrawString(str, f, new SolidBrush(Color.Black), ps);
??????????????? }
??????????????? //0
??????????????? g.DrawString("0",f,new SolidBrush(Color.Black),new Point(P0.X-10,P0.Y-10));
??????????????? //return
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
??????? /// <summary>
??????? /// 繪制多組數(shù)據(jù)的曲線圖
??????? /// </summary>
??????? /// <param name="Title">主標(biāo)題</param>
??????? /// <param name="ItemName">數(shù)據(jù)項名稱</param>
??????? /// <param name="ItemValues">多組數(shù)據(jù)集合</param>
??????? /// <param name="ItemsName">各組的名稱</param>
??????? /// <returns></returns>
??????? public static Bitmap GetMulLineImage(string Title, string[] ItemName, decimal[][] ItemValues,string[] ItemsName)
??????? {
??????????? Bitmap image = GenerateImage(Title);
??????????? image = DrawCoordinate(image, ItemName, ItemValues);
??????????? Graphics g = Graphics.FromImage(image);
??????????? g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
??????????? g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
??????????? try
??????????? {
??????????????? //繪制多條線
??????????????? for (int m = 0; m < ItemsName.Length; m++)
??????????????? {
??????????????????? int PNum = ItemName.Length;
??????????????????? Point[] Pts = new Point[PNum];
??????????????????? //坐標(biāo)軸
??????????????????? Point P0 = new Point(60, 360);
??????????????????? Point Px = new Point(420, 360);
??????????????????? Point Py = new Point(60, 65);
??????????????????? //color
??????????????????? Color c = GetRandomColor(m);
??????????????????? Pen pen = new Pen(c);
??????????????????? //
??????????????????? int XCount = ItemName.Length;//X軸點的數(shù)量
??????????????????? int XDistance = Convert.ToInt32(350 / XCount);//X軸點間的距離
??????????????????? //圖例
??????????????????? Rectangle RDes = new Rectangle(445, 90, 10, 10);
??????????????????? Font f = new Font("新宋體", 9, FontStyle.Bold);
??????????????????? //
??????????????????? for (int i = 0; i < PNum; i++)
??????????????????? {
??????????????????????? int x = P0.X + (i + 1) * XDistance;
??????????????????????? int y = GetCoordinateValue(ItemValues
-[i], ItemValues);
??????????????????????? Pts[i] = new Point(x, y);
??????????????????? }
??????????????????? //把點連起來
??????????????????? for (int i = 1; i < PNum; i++)
??????????????????? {
??????????????????????? g.DrawLine(pen, Pts[i - 1], Pts[i]);
??????????????????? }
??????????????????? //畫圖例說明
??????????????????? DrawDes(image, RDes, c, ItemsName
-, f, m);
??????????????? }
??????????????? //return
??????????????? return image;
??????????? }
??????????? finally
??????????? {
??????????????? g.Dispose();
??????????? }
??????? }
#endregion
??????? //end
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/albert-struggle/archive/2012/03/04/2379778.html
總結(jié)
以上是生活随笔為你收集整理的生成各种统计图的C#方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [置顶] OAuth工作原
- 下一篇: WinCE5.0中文模拟器SDK(VS2