直播中
/// <summary>
/// 重載構(gòu)造函數(shù)
/// </summary>
/// <param name="a_strTitle"> </param>
/// <param name="a_objBackColor"> </param>
/// <param name="a_intWidth"> </param>
/// <param name="a_intHeight"> </param>
/// <param name="a_intChartType"> </param>
/// <param name="a_strUnit"> </param>
public MyChart(string a_strTitle , System.Drawing.Color a_objBackColor ,
int a_intWidth , int a_intHeight , ChartType a_intChartType , string a_strUnit)
{
m_arrItems = new ArrayList() ;
m_strTitle = a_strTitle ;
m_objBackColor = a_objBackColor ;
m_intWidth = a_intWidth ;
m_intHeight = a_intHeight ;
m_intChartType = a_intChartType ;
m_intTotalCount = 0 ;
m_strUnit = a_strUnit ;
}
/// <summary>
/// 添加一個(gè)新的統(tǒng)計(jì)圖項(xiàng)目
/// </summary>
/// <param name="a_objItem"> </param>
public void AddItem(object a_objItem)
{
if(a_objItem is MyClass.Util.ChartItem)
{
m_arrItems.Add(a_objItem) ;
m_intTotalCount += ((ChartItem)a_objItem).Count ;
}
else
{
throw(new Exception("對(duì)象類型錯(cuò)誤,要加入的必須是ChartItem對(duì)象")) ;
}
}
/// <summary>
/// 產(chǎn)生統(tǒng)計(jì)圖圖片
/// </summary>
/// <param name="a_strFileName">要保存的文件名 </param>
/// <remarks>
/// a_strFileName必須是絕對(duì)物理路徑
/// </remarks>
public void Create(string a_strFileName)
{
//如果沒有定義統(tǒng)計(jì)圖項(xiàng),則拋出異常
if (m_arrItems.Count == 0)
{
throw(new Exception("沒有定義統(tǒng)計(jì)圖項(xiàng)")) ;
}
//根據(jù)不同統(tǒng)計(jì)圖類型選擇函數(shù)
switch(m_intChartType)
{
case ChartType.Bar:
DrawBar(a_strFileName) ;
break;
case ChartType.Pie:
DrawPie(a_strFileName) ;
break ;
case ChartType.Curve:
DrawCurve(a_strFileName) ;
break ;
default:
DrawPie(a_strFileName) ;
break ;
}
}
/// <summary>
/// 畫條形圖
/// </summary>
/// <param name="a_strFileName">要保存的圖片名稱</param>
protected void DrawBar(string a_strFileName)
{
//繪圖準(zhǔn)備,新建一個(gè)image對(duì)象,一個(gè)graphics對(duì)象
System.Drawing.Image myBmp = new Bitmap(m_intWidth , m_intHeight ) ;
System.Drawing.Graphics g = Graphics.FromImage(myBmp) ;
//填充背景
g.FillRectangle(new System.Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_intWidth , m_intHeight) ;
//如果沒有任何回答,則顯示沒有結(jié)果
if (this.m_intTotalCount == 0)
{
g.DrawString("沒有統(tǒng)計(jì)數(shù)字!" , new Font("宋體" , m_intWidth / 14) ,
new SolidBrush(Color.Red) , (m_intWidth - m_intWidth / 8 * 6) / 2 ,
m_intHeight / 2 - m_intWidth / 8) ;
}
else
{
//寫題目
//g.DrawString(m_strTitle , new System.Drawing.Font("黑體" , m_intWidth / 30) ,
// new System.Drawing.SolidBrush(Color.Black) , (m_intWidth - m_strTitle.Length * (m_intWidth / 30))/2 , 10 ,
// System.Drawing.StringFormat.GenericDefault) ;
//畫統(tǒng)計(jì)圖項(xiàng)目的矩形
//計(jì)算每個(gè)矩形的寬度
int intWidth = m_intWidth / (m_arrItems.Count * 2 - 1) ;
//定義一個(gè)一個(gè)像素寬的黑色的筆
System.Drawing.Pen pen = new System.Drawing.Pen(new System.Drawing.SolidBrush(Color.Black) , 1) ;
for (int i = 0 ; i < m_arrItems.Count ; i ++)
{
ChartItem item = (ChartItem)m_arrItems[i] ;
//計(jì)算所占百分比
float intPercent = (float)Decimal.Divide(item.Count * 100 , m_intTotalCount) ;
//計(jì)算矩形高度
int intHeight = (int)(m_intHeight/ 3 * 2 * intPercent / 100 - 10) ;
//計(jì)算矩形坐標(biāo)
int ix = intWidth * i * 3 / 2 + intWidth ;
int iy = m_intHeight / 3 * 2 - intHeight ;
//畫矩形
g.FillRectangle(new System.Drawing.SolidBrush(item.Color) ,
ix , iy , intWidth , intHeight + 10) ;
//寫字
//計(jì)算字體大小
int intFontSize = intWidth / this.TotalCount.ToString().Length ;
//限制一下,字體大小不能超過12
intFontSize = intFontSize > 12 ? 12 : intFontSize ;
g.DrawString(item.Count.ToString() + m_strUnit ,
new System.Drawing.Font("宋體" , intFontSize) ,
new System.Drawing.SolidBrush(item.Color) ,
ix , iy - intFontSize * 2) ;
//畫圖例
//計(jì)算字體大小
intFontSize = m_intHeight / 3 / m_arrItems.Count / 2 - 1 ;
intFontSize = intFontSize > 12 ? 12 : intFontSize ;
g.FillRectangle(new System.Drawing.SolidBrush(item.Color) , 20 ,
m_intHeight / 3 * 2 + intFontSize * (i * 2 + 1) + 5 ,
intFontSize , intFontSize) ;
g.DrawString( intPercent.ToInt32().ToString() + "%" ,
new System.Drawing.Font("宋體" , intFontSize) ,
new System.Drawing.SolidBrush(item.Color) ,
20 + intFontSize ,
m_intHeight / 3 * 2 + intFontSize * (i * 2 + 1) + 5) ;
g.DrawString(item.Text ,
new System.Drawing.Font("宋體" , intFontSize) ,
new System.Drawing.SolidBrush(item.Color) , 80 ,
m_intHeight / 3 * 2 + intFontSize * (i * 2 + 1) + 5) ;
}
//畫標(biāo)志線
g.DrawLine(pen , 0 , 10 , 0 , m_intHeight / 3 * 2 + 10) ;
g.DrawLine(pen , 0 , m_intHeight / 3 * 2 + 10 , m_intWidth ,
m_intHeight / 3 * 2 + 10) ;
for (int i = 0 ; i < 10 ; i++)
{
g.DrawLine( pen , 0 , m_intHeight / 3 * 2 / 10 * i + 10 ,
5 , m_intHeight / 3 * 2 / 10 * i + 10) ;
}
//寫單位
//g.DrawString("單位:" + m_strUnit , new System.Drawing.Font("宋體" , m_intWidth/40) ,
// new SolidBrush(Color.Black) , 10 , 10) ;
//清空
pen.Dispose() ;
}
//清控對(duì)象
g.Dispose() ;
//保存為jpg格式
try
{
myBmp.Save(a_strFileName , System.Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw(new Exception("保存圖片失敗:" + e.ToString())) ;
}
myBmp.Dispose() ;
}
/// <summary>
/// 畫餅形圖
/// </summary>
/// <param name="a_strFileName"> </param>
/// <remarks>
/// 這段程序很難懂,因?yàn)樾枰鶕?jù)圖片大小決定餅圖及文字的大小,所以所有計(jì)算方法
/// 都是試驗(yàn)出來的。
/// </remarks>
protected void DrawPie(string a_strFileName)
{
//繪圖準(zhǔn)備,新建一個(gè)image對(duì)象,一個(gè)graphics對(duì)象
System.Drawing.Image myBmp = new Bitmap(m_intWidth , m_intHeight ) ;
System.Drawing.Graphics g = Graphics.FromImage(myBmp) ;
//填充背景
g.FillRectangle(new System.Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_intWidth , m_intHeight) ;
//如果沒有任何回答,則顯示沒有結(jié)果
if (this.m_intTotalCount == 0)
{
g.DrawString("沒有統(tǒng)計(jì)數(shù)字!" , new Font("宋體" , m_intWidth / 14) ,
new SolidBrush(Color.Red) , (m_intWidth - m_intWidth / 8 * 6) / 2 ,
m_intHeight / 2 - m_intWidth / 8) ;
}
else
{
//定義一個(gè)浮點(diǎn)變量,記住上一個(gè)項(xiàng)目結(jié)束的角度
int intAngel = 180 ;
//畫餅形圖
for (int i = 0 ; i < m_arrItems.Count ; i ++)
{
ChartItem item = (ChartItem)m_arrItems[i] ;
//開始時(shí)從180度開始
//計(jì)算所占百分比
float intPercent = (float)Decimal.Divide(item.Count * 100 , m_intTotalCount) ;
if (i == m_arrItems.Count - 1)
{
g.FillPie(new SolidBrush(item.Color) , 0 , 0 , m_intWidth * 2 / 3 ,
m_intHeight * 2 / 3 , intAngel , 540 - intAngel) ;
}
else
{
g.FillPie(new SolidBrush(item.Color) , 0 , 0 , m_intWidth * 2 / 3,
m_intHeight * 2 / 3 , intAngel , 360 * intPercent/100) ;
intAngel += (int)(360 * intPercent/100) ;
}
//畫餅圖右邊圖例百分?jǐn)?shù)
//計(jì)算矩形大小
int intRectSize = m_intHeight / 30 ;
//畫顏色方塊
g.FillRectangle(new SolidBrush(item.Color) , m_intWidth * 2 / 3 + intRectSize ,
intRectSize * (i * 2 + 1) + m_intHeight / 2 - m_arrItems.Count * 2 * intRectSize
,intRectSize , intRectSize) ;
//寫百分?jǐn)?shù)
g.DrawString(item.Count.ToString() + m_strUnit ,
new Font("宋體" , intRectSize) ,
new SolidBrush(item.Color) ,
m_intWidth * 2 / 3 + intRectSize * 3 ,
intRectSize * (i * 2 + 1) + m_intHeight / 2
- m_arrItems.Count * 2 * intRectSize);
//畫餅圖下方圖例文字
//計(jì)算矩形大小
intRectSize = m_intHeight / 3 / (m_arrItems.Count * 2) - 1 ;
intRectSize = intRectSize > 12 ? 12 : intRectSize ;
//畫顏色方塊
g.FillRectangle(new SolidBrush(item.Color) , intRectSize * 2 ,
intRectSize * (i * 2 + 1) + m_intHeight / 3 * 2
,intRectSize , intRectSize) ;
//寫文字
g.DrawString(intPercent.ToInt32().ToString() + "% " + item.Text ,
new Font("宋體" , intRectSize) ,
new SolidBrush(item.Color) , intRectSize * 4 ,
intRectSize * ( i * 2 + 1) + m_intHeight / 3 * 2) ;
}
}
//清控對(duì)象
g.Dispose() ;
//保存為jpg格式
try
{
myBmp.Save(a_strFileName , System.Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw(new Exception("保存圖片失敗:" + e.ToString())) ;
}
myBmp.Dispose() ;
}
/// <summary>
/// 畫折線圖
/// </summary>
/// <param name="a_strFileName"> </param>
protected void DrawCurve(string a_strFileName)
{
//繪圖準(zhǔn)備,新建一個(gè)image對(duì)象,一個(gè)graphics對(duì)象
System.Drawing.Image myBmp = new Bitmap(m_intWidth , m_intHeight ) ;
System.Drawing.Graphics g = Graphics.FromImage(myBmp) ;
//填充背景
g.FillRectangle(new System.Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_intWidth , m_intHeight) ;
//如果沒有任何回答,則顯示沒有結(jié)果
if (this.m_intTotalCount == 0)
{
g.DrawString("沒有統(tǒng)計(jì)數(shù)字!" , new Font("宋體" , m_intWidth / 14) ,
new SolidBrush(Color.Red) , (m_intWidth - m_intWidth / 8 * 6) / 2 ,
m_intHeight / 2 - m_intWidth / 8) ;
}
else
{
//定義一個(gè)一個(gè)像素寬的黑色的筆
System.Drawing.Pen pen = new System.Drawing.Pen(new System.Drawing.SolidBrush(Color.Black) , 1) ;
//畫標(biāo)志線
g.DrawLine(pen , 0 , 10 , 0 , m_intHeight / 3 * 2 + 10) ;
g.DrawLine(pen , 0 , m_intHeight / 3 * 2 + 10 , m_intWidth , m_intHeight / 3 * 2 + 10) ;
for (int i = 0 ; i < 10 ; i++)
{
g.DrawLine( pen , 0 , m_intHeight / 3 * 2 / 10 * i + 10 , 5 , m_intHeight / 3 * 2 / 10 * i + 10) ;
}
//寫單位
g.DrawString("單位:" + m_strUnit , new System.Drawing.Font("宋體" , m_intWidth/40) ,
new SolidBrush(Color.Black) , 10 , 10) ;
//畫折線
//計(jì)算寬度
int intWidth = m_intWidth / (m_arrItems.Count * 2) ;
//定義兩個(gè)變量,記住上一個(gè)點(diǎn)的x , y
int ix = 0 ;
int iy = 0 ;
for ( int i = 0 ; i < m_arrItems.Count ; i ++)
{
ChartItem item = (ChartItem)m_arrItems[i] ;
//計(jì)算所占百分比
float intPercent = (float)Decimal.Divide(item.Count * 100 , m_intTotalCount) ;
//計(jì)算點(diǎn)高度
int intHeight = (int)(m_intHeight / 3 * 2 * intPercent / 100) ;
//畫點(diǎn)
g.FillEllipse(new SolidBrush(item.Color) , intWidth * (i * 2 + 1) ,
m_intHeight / 3 * 2 - intHeight , 10 , 10) ;
//連接線
//定義筆,如果是升則為藍(lán)顏色,否則是紅色
if (iy > m_intHeight /3 * 2 - intHeight + 5)
{
pen = new Pen(new SolidBrush(Color.Blue) , 3) ;
}
else
{
pen = new Pen(new SolidBrush(Color.Red) , 3) ;
}
if (i != 0)
{
g.DrawLine(pen , ix , iy , intWidth * (i * 2 + 1) + 5 , m_intHeight /3 * 2 - intHeight + 5) ;
}
ix = intWidth * (i * 2 + 1) + 5 ;
iy = m_intHeight / 3 * 2 - intHeight + 5 ;
//畫餅圖下方圖例文字
//計(jì)算矩形大小
int intRectSize = m_intHeight / 3 / (m_arrItems.Count * 2) - 1 ;
intRectSize = intRectSize > 12 ? 12 : intRectSize ;
//畫顏色方塊
g.FillRectangle(new SolidBrush(item.Color) , intRectSize * 2 ,
intRectSize * (i * 2 + 1) + m_intHeight / 3 * 2 + 5
,intRectSize , intRectSize) ;
//寫文字
g.DrawString(intPercent.ToInt32().ToString() + "% " + item.Text ,
new Font("宋體" , intRectSize) ,
new SolidBrush(item.Color) , intRectSize * 4 ,
intRectSize * ( i * 2 + 1) + m_intHeight / 3 * 2 + 5) ;
}
//清空
pen.Dispose() ;
}
//清控對(duì)象
g.Dispose() ;
//保存為jpg格式
try
{
myBmp.Save(a_strFileName , System.Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw(new Exception("保存圖片失敗:" + e.ToString())) ;
}
myBmp.Dispose() ;
}
}
/// <summary>
/// 統(tǒng)計(jì)圖項(xiàng)目類
/// </summary>
/// <remarks>
/// 構(gòu)造一個(gè)統(tǒng)計(jì)圖項(xiàng)目,屬性有要顯示的文字、所占的百分比以及想顯示的顏色。
/// </remarks>
public class ChartItem : object
{
/// <summary>
/// 要顯示的文字
/// </summary>
private string m_strText ;
/// <summary>
/// 數(shù)量
/// </summary>
private int m_intCount ;
/// <summary>
/// 顏色
/// </summary>
private System.Drawing.Color m_objColor ;
//屬性
public string Text
{
get
{
return m_strText ;
}
set
{
m_strText = value ;
}
}
public int Count
{
get
{
return m_intCount ;
}
set
{
m_intCount = value ;
}
}
public System.Drawing.Color Color
{
get
{
return m_objColor ;
}
set
{
m_objColor = value ;
}
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public ChartItem()
{
m_strText = "" ;
m_intCount = 0 ;
m_objColor = Color.White ;
}
/// <summary>
/// 重載構(gòu)造函數(shù)
/// </summary>
/// <param name="a_strText"> </param>
/// <param name="a_intPercent"> </param>
/// <param name="a_objColor"> </param>
public ChartItem(string a_strText , int a_intCount , System.Drawing.Color a_objColor)
{
m_strText = a_strText ;
m_objColor = a_objColor ;
m_intCount = a_intCount ;
}
}
}