1. 程式人生 > >利用C#實現條形圖、餅圖的繪製(二)

利用C#實現條形圖、餅圖的繪製(二)

  1. using System;  
  2. using System.Data;  
  3. using System.Drawing;  
  4. using System.Drawing.Text;  
  5. using System.Drawing.Drawing2D;  
  6. using System.Collections.Generic;  
  7. namespace GraphDrawing  
  8. {  
  9.     class PieGraph  
  10.     {  
  11.         #region Member fields  
  12.         /// <summary>  
  13.         /// The color list for each bar drawing  
  14.         /// </summary>  
  15.         private List<Color> m_colorList;  
  16.         /// <summary>  
  17.         /// The data for graph drawing  
  18.         /// </summary>  
  19.         private DataTable m_dataTable;  
  20.         /// <summary>  
  21.         /// The width of graph  
  22.         /// </summary>  
  23.         private int m_width;  
  24.         /// <summary>  
  25.         /// The height of graph  
  26.         /// </summary>  
  27.         private int m_height;  
  28.         /// <summary>  
  29.         /// The legend width of graph  
  30.         /// </summary>  
  31.         private int m_legendWidth;  
  32.         #endregion  
  33.         #region Public properties  
  34.         /// <summary>  
  35.         /// The title of graph  
  36.         /// </summary>  
  37.         public string GraphTitle { get; set; }  
  38.         /// <summary>  
  39.         /// The font format of graph  
  40.         /// </summary>  
  41.         public string FontFormat { get; set; }  
  42.         #endregion  
  43.         /// <summary>  
  44.         /// Constructor with arguments  
  45.         /// </summary>  
  46.         /// <param name="width">the graph width</param>  
  47.         /// <param name="height">the graph height</param>  
  48.         /// <param name="legendWidth">the graph legend width</param>  
  49.         /// <param name="dataTable">the graph data</param>  
  50.         public PieGraph(int width, int height, int legendWidth, DataTable dataTable)  
  51.         {  
  52.             m_width = width;  
  53.             m_height = height;  
  54.             m_legendWidth = legendWidth;  
  55.             m_dataTable = dataTable;  
  56.             m_colorList = Utils.GetColorList();  
  57.         }  
  58.         /// <summary>  
  59.         /// Generate Pie graph  
  60.         /// </summary>  
  61.         /// <returns>bitmap</returns>  
  62.         public Bitmap DrawPieGraph()  
  63.         {  
  64.             int iPieDiameter = m_width < (m_height + 150) ? (m_width - 250) : (m_height - 150);  
  65.             // Calculate the sum  
  66.             float fSum = 0;  
  67.             foreach (DataRow row in m_dataTable.Rows)  
  68.             {  
  69.                 fSum += Convert.ToSingle(row[1]);  
  70.             }  
  71.             // Create an object of Graphics  
  72.             Bitmap bitmap = new Bitmap(m_width, m_height);  
  73.             Graphics graph = Graphics.FromImage(bitmap);  
  74.             // Set the attribute of bar and text  
  75.             graph.ScaleTransform(1, 1);  
  76.             graph.SmoothingMode = SmoothingMode.Default;  
  77.             graph.TextRenderingHint = TextRenderingHint.AntiAlias;  
  78.             // Set the canvas and the border  
  79.             graph.Clear(Color.White);  
  80.             graph.DrawRectangle(Pens.Green, 0, 0, m_width - 5, m_height - 5);  
  81.             // Draw the graph title  
  82.             graph.DrawString(GraphTitle, new Font(FontFormat, 14), Brushes.Black, new PointF(7, 35));  
  83.             // Draw the pie graph  
  84.             float fCurrentAngle = 0;  
  85.             float fTotalAngle = 0;  
  86.             for (int i = 0; i < m_dataTable.Rows.Count; i++)  
  87.             {  
  88.                 fCurrentAngle = Convert.ToSingle(m_dataTable.Rows[i][1]) / fSum * 360;  
  89.                 graph.FillPie(new SolidBrush(m_colorList[i]), 50, 75, iPieDiameter, iPieDiameter, fTotalAngle, fCurrentAngle);  
  90.                 graph.DrawPie(Pens.Black, 50, 75, iPieDiameter, iPieDiameter, fTotalAngle, fCurrentAngle);  
  91.                 fTotalAngle += fCurrentAngle;  
  92.             }   
  93.             // Draw the legend of graph  
  94.             bitmap = DrawLegend(bitmap);  
  95.             return bitmap;  
  96.         }  
  97.         /// <summary>  
  98.         /// Generate the legend  
  99.         /// </summary>  
  100.         /// <param name="graph"></param>  
  101.         /// <returns></returns>  
  102.         private Bitmap DrawLegend(Bitmap graph)  
  103.         {  
  104.             Bitmap bitmap = new Bitmap(250, m_height);  
  105.             Graphics objGraphic = Graphics.FromImage(bitmap);  
  106.             Graphics graphic = Graphics.FromImage(graph);  
  107.             int i, x;  
  108.             for (i = 0, x = m_legendWidth; i < m_dataTable.Rows.Count; i++)  
  109.             {  
  110.                 //Draw the bar  
  111.                 SolidBrush brush = new SolidBrush(m_colorList[i]);  
  112.                 objGraphic.FillRectangle(brush, 10, m_height - 90 - x, m_legendWidth, m_legendWidth);  
  113.                 string drawString = m_dataTable.Rows[i][0].ToString() + " - " + m_dataTable.Rows[i][1].ToString();  
  114.                 Font drawFont = new Font(FontFormat, 8);  
  115.                 SolidBrush drawBrush = new SolidBrush(Color.Black);  
  116.                 objGraphic.DrawString(drawString, drawFont, drawBrush, m_legendWidth * 2, m_height - 90 - x);  
  117.                 //x axis spacing by m_legendWidth + 5  
  118.                 x += m_legendWidth + 5;  
  119.             }  
  120.             // Draw the string and the rectangle for the legend  
  121.             graphic.DrawRectangle(new Pen(Color.Purple, 1), m_width - 180, m_height - 85 - x, 160, x + 10);  
  122.             graphic.DrawString("Legend", new Font(FontFormat, 11, FontStyle.Bold), Brushes.Purple, new PointF(m_width - 180, m_height - 110 - x));  
  123.             graphic.DrawImage(bitmap, m_width - 180, 10);  
  124.             return (graph);  
  125.         }  
  126.     }  
  127. }