继前一篇文章提到关于使用C#绘制条形图的思路之后,这里接着介绍绘制饼图的思路。本篇文章所涉及的源代码是在前面的基础上完成的,在本文的最后,将会提供本实例的完整代码下载地址,有兴趣的朋友可以下载。
言归正传,开始介绍饼图的绘制。其实,饼图的绘制跟条形图的绘制有颇多相似之处,其大体亦经过以下几个步骤:
(1)创建Graphics对象实例;
(2)设置图形及文本属性;
(3)设置画布及边框;
(4)绘制标题文本;
(5)循环绘制扇形图并最终组合成饼图,这里利用了FillPie方法;
(6)绘制图例。
饼图的绘制在类PieGraph中实现,以下为其代码示例:
[c-sharp] view plain copy
- using System;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Text;
- using System.Drawing.Drawing2D;
- using System.Collections.Generic;
-
- namespace GraphDrawing
- {
- class PieGraph
- {
- #region Member fields
- /// <summary>
- /// The color list for each bar drawing
- /// </summary>
- private List<Color> m_colorList;
-
- /// <summary>
- /// The data for graph drawing
- /// </summary>
- private DataTable m_dataTable;
-
- /// <summary>
- /// The width of graph
- /// </summary>
- private int m_width;
-
- /// <summary>
- /// The height of graph
- /// </summary>
- private int m_height;
-
- /// <summary>
- /// The legend width of graph
- /// </summary>
- private int m_legendWidth;
- #endregion
-
- #region Public properties
- /// <summary>
- /// The title of graph
- /// </summary>
- public string GraphTitle { get; set; }
-
- /// <summary>
- /// The font format of graph
- /// </summary>
- public string FontFormat { get; set; }
- #endregion
-
- /// <summary>
- /// Constructor with arguments
- /// </summary>
- /// <param name="width">the graph width</param>
- /// <param name="height">the graph height</param>
- /// <param name="legendWidth">the graph legend width</param>
- /// <param name="dataTable">the graph data</param>
- public PieGraph(int width, int height, int legendWidth, DataTable dataTable)
- {
- m_width = width;
- m_height = height;
- m_legendWidth = legendWidth;
- m_dataTable = dataTable;
-
- m_colorList = Utils.GetColorList();
- }
-
- /// <summary>
- /// Generate Pie graph
- /// </summary>
- /// <returns>bitmap</returns>
- public Bitmap DrawPieGraph()
- {
- int iPieDiameter = m_width < (m_height + 150) ? (m_width - 250) : (m_height - 150);
-
- // Calculate the sum
- float fSum = 0;
- foreach (DataRow row in m_dataTable.Rows)
- {
- fSum += Convert.ToSingle(row[1]);
- }
-
- // Create an object of Graphics
- Bitmap bitmap = new Bitmap(m_width, m_height);
- Graphics graph = Graphics.FromImage(bitmap);
-
- // Set the attribute of bar and text
- graph.ScaleTransform(1, 1);
- graph.SmoothingMode = SmoothingMode.Default;
- graph.TextRenderingHint = TextRenderingHint.AntiAlias;
-
- // Set the canvas and the border
- graph.Clear(Color.White);
- graph.DrawRectangle(Pens.Green, 0, 0, m_width - 5, m_height - 5);
-
- // Draw the graph title
- graph.DrawString(GraphTitle, new Font(FontFormat, 14), Brushes.Black, new PointF(7, 35));
-
- // Draw the pie graph
- float fCurrentAngle = 0;
- float fTotalAngle = 0;
- for (int i = 0; i < m_dataTable.Rows.Count; i++)
- {
- fCurrentAngle = Convert.ToSingle(m_dataTable.Rows[i][1]) / fSum * 360;
-
- graph.FillPie(new SolidBrush(m_colorList[i]), 50, 75, iPieDiameter, iPieDiameter, fTotalAngle, fCurrentAngle);
- graph.DrawPie(Pens.Black, 50, 75, iPieDiameter, iPieDiameter, fTotalAngle, fCurrentAngle);
- fTotalAngle += fCurrentAngle;
- }
-
- // Draw the legend of graph
- bitmap = DrawLegend(bitmap);
-
- return bitmap;
- }
-
- /// <summary>
- /// Generate the legend
- /// </summary>
- /// <param name="graph"></param>
- /// <returns></returns>
- private Bitmap DrawLegend(Bitmap graph)
- {
- Bitmap bitmap = new Bitmap(250, m_height);
- Graphics objGraphic = Graphics.FromImage(bitmap);
- Graphics graphic = Graphics.FromImage(graph);
-
- int i, x;
- for (i = 0, x = m_legendWidth; i < m_dataTable.Rows.Count; i++)
- {
- //Draw the bar
- SolidBrush brush = new SolidBrush(m_colorList[i]);
- objGraphic.FillRectangle(brush, 10, m_height - 90 - x, m_legendWidth, m_legendWidth);
-
- string drawString = m_dataTable.Rows[i][0].ToString() + " - " + m_dataTable.Rows[i][1].ToString();
- Font drawFont = new Font(FontFormat, 8);
- SolidBrush drawBrush = new SolidBrush(Color.Black);
-
- objGraphic.DrawString(drawString, drawFont, drawBrush, m_legendWidth * 2, m_height - 90 - x);
-
- //x axis spacing by m_legendWidth + 5
- x += m_legendWidth + 5;
- }
-
- // Draw the string and the rectangle for the legend
- graphic.DrawRectangle(new Pen(Color.Purple, 1), m_width - 180, m_height - 85 - x, 160, x + 10);
- graphic.DrawString("Legend", new Font(FontFormat, 11, FontStyle.Bold), Brushes.Purple, new PointF(m_width - 180, m_height - 110 - x));
-
- graphic.DrawImage(bitmap, m_width - 180, 10);
-
- return (graph);
- }
- }
- }
同样,在WinForm中调用亦非常简单,代码示例如下:
[c-sharp] view plain copy
- /// <summary>
- /// Draw the pie graph
- /// </summary>
- private void DrawPieGraph()
- {
- this.ReadInputData();
-
- string strTitle = txtGraphTitle.Text;
- string strFont = "Courier";
- int iWidth = picPieGraph.Width;
- int iHeight = picPieGraph.Height;
- int iLegendWidth = (iWidth - 200) / (m_dataTable.Rows.Count * 2);
-
- PieGraph pieGraph = new PieGraph(iWidth, iHeight, iLegendWidth, m_dataTable);
- pieGraph.GraphTitle = strTitle;
- pieGraph.FontFormat = strFont;
-
- // Output the graph to the picture box
- picPieGraph.Image = pieGraph.DrawPieGraph();
- }
最终的饼图绘制界面效果如下图所示:
整个绘图程序的源代码可以从以下地址下载:http://download.csdn.net/source/2386682。到此,利用C#的GDI+绘制条形图和饼图就介绍到这里,这里介绍的都是很基本的方法,并未作深入探讨,更深层次的研究有待日后继续。绘图,就是如此简单。