当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 从一个舆论调查的制作谈面向对象的编程思路(四)

ASP.NET
asp.net图片加水印
Asp.Net中页面运行时动态载入的UserControl内元素的事
ASP.NET底层架构探索之再谈.NET运行时(二)
借助封装类实现线程调用带参方法
面向对象设计思想(C#)
asp.net URL重写(URLRewriter) 简化版
GUID在.net里的使用,就用System.Guid结构
不要忽略c#中的using和as操作符
C#中ref和out的使用小结
C#的Web XML编程
asp.net2.0下 如何实现服务器端压缩包自解压
javascript如何调用C#后台代码中的过程 和ASP.NET调用
在ASP.NET中自动给URL加上超链接
ASP.NET 中处理页面“回退”的方法
ASP.NET的四种错误机制
asp.net跳转页面的三种方法比较
ASP.NET2.0中将GridView导出到Excel文件中
ASP.NET 2.0中GridView无限层复杂表头的实现
ASP.NET 2.0 中动态添加 GridView 模板列
十天学会ASP.net之第一天

ASP.NET 中的 从一个舆论调查的制作谈面向对象的编程思路(四)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 29 ::
收藏到网摘: n/a

public MyChart()
{
//
// TODO: Add Constructor Logic here
//
m_arrItems = new ArrayList() ;
m_strTitle = "" ;
m_objBackColor = Color.White ;
m_intWidth = 200 ;
m_intHeight = 200 ;
m_intChartType = ChartType.Pie ;
m_strUnit = "" ;
}

/// <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) ;

//写字