当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 从一个舆论调查的制作谈面向对象的编程思路(四)
/// <summary>
/// 重载构造函数
/// </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>
/// 添加一个新的统计图项目
/// </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("对象类型错误,要加入的必须是ChartItem对象")) ;
}
}
/// <summary>
/// 产生统计图图片
/// </summary>
/// <param name="a_strFileName">要保存的文件名 </param>
/// <remarks>
/// a_strFileName必须是绝对物理路径
/// </remarks>
public void Create(string a_strFileName)
{
//如果没有定义统计图项,则抛出异常
if (m_arrItems.Count == 0)
{
throw(new Exception("没有定义统计图项")) ;
}
//根据不同统计图类型选择函数
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)
{
//绘图准备,新建一个image对象,一个graphics对象
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) ;
//如果没有任何回答,则显示没有结果
if (this.m_intTotalCount == 0)
{
g.DrawString("没有统计数字!" , 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) ;
//画统计图项目的矩形
//计算每个矩形的宽度
int intWidth = m_intWidth / (m_arrItems.Count * 2 - 1) ;
//定义一个一个像素宽的黑色的笔
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] ;
//计算所占百分比
float intPercent = (float)Decimal.Divide(item.Count * 100 , m_intTotalCount) ;
//计算矩形高度
int intHeight = (int)(m_intHeight/ 3 * 2 * intPercent / 100 - 10) ;
//计算矩形坐标
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) ;
//写字
评论 (0) All