利用C#實現條形圖、餅圖的繪製(二)
阿新 • • 發佈:2018-12-09
- 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);
- }
- }
- }