面向对象编程:舆论调查的制作谈面向对象的编程思路



public MyChart
{
//
// TODO: Add Constructor Logic here
//
m_arrItems = ArrayList ;
m_strTitle = \"\" ;
m_objBackColor = Color.White ;
m_Width = 200 ;
m_Height = 200 ;
m_ChartType = ChartType.Pie ;
m_strUnit = \"\" ;
}

/// <summary>
/// 重载构造
/// </summary>
/// <param name=\"a_strTitle\"> </param>
/// <param name=\"a_objBackColor\"> </param>
/// <param name=\"a_Width\"> </param>
/// <param name=\"a_Height\"> </param>
/// <param name=\"a_ChartType\"> </param>
/// <param name=\"a_strUnit\"> </param>
public MyChart( a_strTitle , .Drawing.Color a_objBackColor ,
a_Width , a_Height , ChartTypea_ChartType , a_strUnit)
{
m_arrItems = ArrayList ;
m_strTitle = a_strTitle ;
m_objBackColor = a_objBackColor ;
m_Width = a_Width ;
m_Height = a_Height ;
m_ChartType = a_ChartType ;
m_TotalCount = 0 ;
m_strUnit = a_strUnit ;
}

/// <summary>
/// 添加个新统计图项目
/// </summary>
/// <param name=\"a_objItem\"> </param>
public void AddItem(object a_objItem)
{
(a_objItem is MyClass.Util.ChartItem)
{
m_arrItems.Add(a_objItem) ;
m_TotalCount ((ChartItem)a_objItem).Count ;
}

{
throw( Exception(\"对象类型要加入必须是ChartItem对象\")) ;
}
}

/// <summary>
/// 产生统计图图片
/// </summary>
/// <param name=\"a_strFileName\">要保存文件名 </param>
/// <remarks>


///a_strFileName必须是绝对物理路径
/// </remarks>
public void Create( a_strFileName)
{
//如果没有定义统计图项则抛出异常
(m_arrItems.Count 0)
{
throw( Exception(\"没有定义统计图项\")) ;
}


//根据区别统计图类型选择
switch(m_ChartType)
{
ChartType.Bar:
DrawBar(a_strFileName) ;
;

ChartType.Pie:
DrawPie(a_strFileName) ;
;

ChartType.Curve:
DrawCurve(a_strFileName) ;
;

default:
DrawPie(a_strFileName) ;
;
}

}

/// <summary>
/// 画条形图
/// </summary>
/// <param name=\"a_strFileName\">要保存图片名称</param>
protected void DrawBar( a_strFileName)
{
//绘图准备新建个image对象,个graphics对象
.Drawing.Image myBmp = Bitmap(m_Width , m_Height ) ;
.Drawing.Graphics g = Graphics.FromImage(myBmp) ;

//填充背景
g.FillRectangle( .Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_Width , m_Height) ;

//如果没有任何回答则显示没有结果
(this.m_TotalCount 0)
{
g.DrawString(\"没有统计数字!\" , Font(\"宋体\" , m_Width / 14) ,
SolidBrush(Color.Red) , (m_Width - m_Width / 8 * 6) / 2 ,
m_Height / 2 - m_Width / 8) ;
}

{



//写题目

//g.DrawString(m_strTitle , .Drawing.Font(\"黑体\" , m_Width / 30) ,
// .Drawing.SolidBrush(Color.Black) , (m_Width - m_strTitle.Length * (m_Width / 30))/2, 10 ,
//.Drawing.StringFormat.GenericDefault) ;




//画统计图项目矩形
//计算每个矩形宽度
Width = m_Width / (m_arrItems.Count * 2 - 1) ;

//定义个像素宽黑色
.Drawing.Pen pen = .Drawing.Pen( .Drawing.SolidBrush(Color.Black) , 1) ;

for ( i = 0 ; i < m_arrItems.Count ; i )
{
ChartItem item = (ChartItem)m_arrItems[i] ;

//计算所占百分比
float Percent = (float)Decimal.Divide(item.Count * 100 , m_TotalCount) ;

//计算矩形高度
Height = ()(m_Height/ 3 * 2 * Percent / 100 - 10) ;

//计算矩形坐标
ix = Width * i * 3 / 2 + Width ;
iy = m_Height /3 * 2- Height ;


//画矩形
g.FillRectangle( .Drawing.SolidBrush(item.Color) ,
ix , iy , Width , Height + 10) ;

//写字
//计算字体大小
FontSize = Width / this.TotalCount..Length ;
//限制字体大小不能超过12
FontSize = FontSize > 12 ? 12 : FontSize ;
g.DrawString(item.Count. + m_strUnit ,
.Drawing.Font(\"宋体\" , FontSize) ,
.Drawing.SolidBrush(item.Color) ,


ix , iy - FontSize * 2) ;

//画图例

//计算字体大小
FontSize = m_Height / 3 / m_arrItems.Count / 2 - 1 ;
FontSize = FontSize > 12 ? 12 : FontSize ;
g.FillRectangle( .Drawing.SolidBrush(item.Color) , 20 ,
m_Height / 3 * 2 + FontSize * (i * 2 + 1) + 5 ,
FontSize , FontSize) ;
g.DrawString( Percent.ToInt32. + \"%\" ,
.Drawing.Font(\"宋体\" , FontSize) ,
.Drawing.SolidBrush(item.Color) ,
20 + FontSize ,
m_Height /3 * 2 + FontSize * (i * 2 + 1) + 5) ;
g.DrawString(item.Text,
.Drawing.Font(\"宋体\" , FontSize) ,
.Drawing.SolidBrush(item.Color) , 80 ,
m_Height / 3 * 2 + FontSize * (i * 2 + 1) + 5) ;

}

//画标志线
g.DrawLine(pen , 0 , 10 , 0 , m_Height / 3 * 2 + 10) ;
g.DrawLine(pen , 0 , m_Height / 3 * 2 + 10 , m_Width ,
m_Height / 3 * 2 + 10) ;
for ( i = 0 ; i < 10 ; i)
{
g.DrawLine( pen , 0 , m_Height / 3 * 2 / 10 * i + 10 ,
5 , m_Height / 3 * 2 / 10 * i + 10) ;


}

//写单位
//g.DrawString(\"单位:\" + m_strUnit , .Drawing.Font(\"宋体\" , m_Width/40) ,
// SolidBrush(Color.Black) , 10 , 10) ;

//清空
pen.Dispose ;
}

//清控对象
g.Dispose ;

//保存为jpg格式
try
{
myBmp.Save(a_strFileName , .Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw( Exception(\"保存图片失败:\" + e.)) ;
}

myBmp.Dispose ;

}

/// <summary>
/// 画饼形图
/// </summary>
/// <param name=\"a_strFileName\"> </param>
/// <remarks>
/// 这段很难懂需要根据图片大小决定饼图及文字大小所以所有计算思路方法
/// 都是试验出来
/// </remarks>
protected void DrawPie( a_strFileName)
{
//绘图准备新建个image对象,个graphics对象
.Drawing.Image myBmp = Bitmap(m_Width , m_Height ) ;
.Drawing.Graphics g = Graphics.FromImage(myBmp) ;

//填充背景
g.FillRectangle( .Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_Width , m_Height) ;

//如果没有任何回答则显示没有结果
(this.m_TotalCount 0)
{
g.DrawString(\"没有统计数字!\" , Font(\"宋体\" , m_Width / 14) ,
SolidBrush(Color.Red) , (m_Width - m_Width / 8 * 6) / 2 ,
m_Height / 2 - m_Width / 8) ;
}

{
//定义个浮点变量记住上个项目结束角度
Angel = 180 ;

//画饼形图


for ( i = 0 ; i < m_arrItems.Count ; i )
{
ChartItem item = (ChartItem)m_arrItems[i] ;

//开始时从180度开始

//计算所占百分比
float Percent = (float)Decimal.Divide(item.Count * 100 , m_TotalCount) ;
(i m_arrItems.Count - 1)
{
g.FillPie( SolidBrush(item.Color) , 0 , 0 , m_Width * 2 / 3 ,
m_Height * 2 / 3 , Angel , 540 - Angel) ;
}

{
g.FillPie( SolidBrush(item.Color) , 0 , 0 , m_Width * 2 / 3,
m_Height * 2 / 3 , Angel , 360 * Percent/100) ;
Angel ()(360 * Percent/100) ;
}

//画饼图右边图例百分数
//计算矩形大小
RectSize = m_Height / 30 ;

//画颜色方块
g.FillRectangle( SolidBrush(item.Color) , m_Width * 2 / 3 + RectSize ,
RectSize * (i * 2 + 1) + m_Height / 2 - m_arrItems.Count * 2 * RectSize
,RectSize , RectSize) ;

//写百分数
g.DrawString(item.Count. + m_strUnit ,
Font(\"宋体\" , RectSize) ,
SolidBrush(item.Color) ,
m_Width * 2 / 3 + RectSize * 3 ,
RectSize * (i * 2 + 1) + m_Height / 2


- m_arrItems.Count * 2 * RectSize);

//画饼图下方图例文字
//计算矩形大小
RectSize = m_Height / 3 / (m_arrItems.Count * 2) - 1 ;
RectSize = RectSize > 12 ? 12 : RectSize ;

//画颜色方块
g.FillRectangle( SolidBrush(item.Color) , RectSize * 2 ,
RectSize * (i * 2 + 1) + m_Height / 3 * 2
,RectSize , RectSize) ;
//写文字
g.DrawString(Percent.ToInt32. + \"%\" + item.Text ,
Font(\"宋体\" , RectSize) ,
SolidBrush(item.Color) , RectSize * 4 ,
RectSize * ( i * 2 + 1) + m_Height / 3 * 2) ;

}
}
//清控对象
g.Dispose ;

//保存为jpg格式
try
{
myBmp.Save(a_strFileName , .Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw( Exception(\"保存图片失败:\" + e.)) ;
}

myBmp.Dispose ;

}

/// <summary>
/// 画折线图
/// </summary>
/// <param name=\"a_strFileName\"> </param>
protected void DrawCurve( a_strFileName)
{
//绘图准备新建个image对象,个graphics对象
.Drawing.Image myBmp = Bitmap(m_Width , m_Height ) ;
.Drawing.Graphics g = Graphics.FromImage(myBmp) ;

//填充背景
g.FillRectangle( .Drawing.SolidBrush(m_objBackColor) , 0 , 0 , m_Width , m_Height) ;



//如果没有任何回答则显示没有结果
(this.m_TotalCount 0)
{
g.DrawString(\"没有统计数字!\" , Font(\"宋体\" , m_Width / 14) ,
SolidBrush(Color.Red) , (m_Width - m_Width / 8 * 6) / 2 ,
m_Height / 2 - m_Width / 8) ;
}

{
//定义个像素宽黑色
.Drawing.Pen pen = .Drawing.Pen( .Drawing.SolidBrush(Color.Black) , 1) ;

//画标志线
g.DrawLine(pen , 0 , 10 , 0 , m_Height / 3 * 2 + 10) ;
g.DrawLine(pen , 0 , m_Height / 3 * 2 + 10 , m_Width , m_Height / 3 * 2 + 10) ;
for ( i = 0 ; i < 10 ; i)
{
g.DrawLine( pen , 0 , m_Height / 3 * 2 / 10 * i + 10 , 5 , m_Height / 3 * 2 / 10 * i + 10) ;
}

//写单位
g.DrawString(\"单位:\" + m_strUnit , .Drawing.Font(\"宋体\" , m_Width/40) ,
SolidBrush(Color.Black) , 10 , 10) ;

//画折线

//计算宽度
Width = m_Width / (m_arrItems.Count * 2) ;

//定义两个变量记住上个点x , y
ix = 0 ;
iy = 0 ;

for ( i = 0 ; i < m_arrItems.Count ; i )
{
ChartItem item = (ChartItem)m_arrItems[i] ;

//计算所占百分比
float Percent = (float)Decimal.Divide(item.Count * 100 , m_TotalCount) ;

//计算点高度
Height = ()(m_Height / 3 * 2 * Percent / 100) ;

//画点
g.FillEllipse( SolidBrush(item.Color) , Width * (i * 2 + 1) ,


m_Height / 3 * 2 - Height , 10 , 10) ;

//连接线
//定义笔如果是升则为蓝颜色否则是红色
(iy > m_Height /3 * 2 - Height + 5)
{
pen = Pen( SolidBrush(Color.Blue) , 3) ;
}

{
pen = Pen( SolidBrush(Color.Red) , 3) ;
}
(i != 0)
{
g.DrawLine(pen , ix , iy , Width * (i * 2 + 1) + 5 , m_Height /3 * 2 - Height + 5) ;
}
ix = Width * (i * 2 + 1) + 5 ;
iy = m_Height / 3 * 2 - Height + 5 ;

//画饼图下方图例文字
//计算矩形大小
RectSize = m_Height / 3 / (m_arrItems.Count * 2) - 1 ;
RectSize = RectSize > 12 ? 12 : RectSize ;

//画颜色方块
g.FillRectangle( SolidBrush(item.Color) , RectSize * 2 ,
RectSize * (i * 2 + 1) + m_Height / 3 * 2 + 5
,RectSize , RectSize) ;
//写文字
g.DrawString(Percent.ToInt32. + \"%\" + item.Text ,
Font(\"宋体\" , RectSize) ,
SolidBrush(item.Color) , RectSize * 4 ,


RectSize * ( i * 2 + 1) + m_Height / 3 * 2 + 5) ;
}

//清空
pen.Dispose ;
}

//清控对象
g.Dispose ;

//保存为jpg格式
try
{
myBmp.Save(a_strFileName , .Drawing.Imaging.ImageFormat.JPEG) ;
}
catch(Exception e)
{
throw( Exception(\"保存图片失败:\" + e.)) ;
}

myBmp.Dispose ;



}

}

/// <summary>
/// 统计图项目类
/// </summary>
/// <remarks>
///构造个统计图项目属性有要显示文字、所占百分比以及想显示颜色
/// </remarks>
public ChartItem : object
{
/// <summary>
/// 要显示文字
/// </summary>
private m_strText ;

/// <summary>
/// 数量
/// </summary>
private m_Count ;


/// <summary>
/// 颜色
/// </summary>
private .Drawing.Color m_objColor ;

//属性
public Text
{
get
{
m_strText ;
}

, {
m_strText = value ;
}
}


public Count
{
get
{
m_Count ;
}

{
m_Count = value ;
}


}
public .Drawing.Color Color
{
get
{
m_objColor ;
}

{
m_objColor = value ;
}
}

/// <summary>
/// 构造
/// </summary>
public ChartItem
{
m_strText = \"\" ;
m_Count = 0 ;
m_objColor = Color.White ;
}

/// <summary>
/// 重载构造
/// </summary>
/// <param name=\"a_strText\"> </param>
/// <param name=\"a_Percent\"> </param>
/// <param name=\"a_objColor\"> </param>
public ChartItem( a_strText , a_Count , .Drawing.Color a_objColor)
{
m_strText = a_strText ;
m_objColor = a_objColor ;
m_Count = a_Count ;
}
}
}
Tags:  php面向对象编程 什么是面向对象编程 java面向对象编程 面向对象编程

延伸阅读

最新评论

发表评论