楼主: keaide

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

[复制链接]
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
11#
 楼主| 发表于 2006-9-23 14:17 | 只看该作者
//返回结果
return arrResult ;
}

/// <summary>
/// 取得激活的调查id
/// </summary>
public static string GetActiveSurveyID()
{
string strResult = "" ;

MyClass.Util.MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "select top 1 id , surveyid from survey

where active=1 order by id desc" ;
myCommand.CommandType = System.Data.CommandType.Text ;

try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
if (myReader.Read())
{
strResult = myReader["surveyid"].ToString() ;
}
else
{
throw(new Exception("没有激活的调查") ;
}

myReader.Close() ;

}
catch(Exception e)
{
throw(new Exception("从数据库中取出调查失败:" +

e.ToString())) ;
}
finally
{
myConn.Close() ;
}

//返回结果
return strResult ;

}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
12#
 楼主| 发表于 2006-9-23 14:17 | 只看该作者
/// <summary>
/// 激活调查
/// </summary>
/// <param name="a_strSurveyID">调查编号 </param>
public static void ActiveSurvey(string a_strSurveyID)
{
MyClass.Util.MyConnection myConn = new MyClass.Util.MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.CommandText = "update survey set active=0 ;"
+ "update survey set

active=1 where surveyid='" + a_strSurveyID + "'" ;
myCommand.CommandType = System.Data.CommandType.Text ;

try
{
myConn.Open() ;
myCommand.ActiveConnection = myConn ;
myCommand.ExecuteNonQuery() ;
}
catch(Exception exp)
{
throw(new Exception("保存激活调查到数据库出错:" +

exp.ToString()));
}
finally
{
myConn.Close() ;
}
}

}
}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
13#
 楼主| 发表于 2006-9-23 14:18 | 只看该作者
现在你是不是发现已经可以用这个类来进行舆论调查的操作了?但这个类里还没有任何的页面html

输出,所以现在的问题就是做页面显示层了,但现在又面临一个问题,那就是如何显示调查结果的问题,

比较流行的做法是用图表来表示,可以根据需要选择条形图、饼性图或折线图等等,那么如果在这个调查

类里来做也可以,但实在太不上算,应该单独做这么一个图表类,可以根据条件画这种图表,下面就是这

个类的定义:

namespace MyClass.Util
{
using System;
using System.Collections ;
using System.Drawing ;


/// <summary>
/// MyChart是一个统计图类,可以生成饼形图、条形图和折线图
/// </summary>
public class MyChart : object
{

/// <summary>
/// 枚举类型
/// </summary>
/// <remarks>
/// 三种类型,饼形图是pie , 条形图是bar , 折线图是curve
/// </remarks>
public enum ChartType
{
Pie = 0 ,
Bar = 1 ,
Curve = 2
};

/// <summary>
/// 统计图标题
/// </summary>
private string m_strTitle ;

/// <summary>
/// 统计图项目数组,数组元素是ChartItem类
/// </summary>
/// <remarks>
/// 所有统计图项目的百分比和等于100。
/// </remarks>
private ArrayList m_arrItems ;

/// <summary>
/// 计量单位
/// </summary>
private string m_strUnit ;

/// <summary>
/// 生成统计图的宽度
/// </summary>
/// <remarks>
/// 默认是200
/// </remarks>
private int m_intWidth ;

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
14#
 楼主| 发表于 2006-9-23 14:18 | 只看该作者
/// <summary>
/// 生成统计图的高度
/// </summary>
/// <remarks>
/// 默认是200
/// </remarks>
private int m_intHeight ;

/// <summary>
/// 生成统计图的背景色
/// </summary>
private System.Drawing.Color m_objBackColor ;

/// <summary>
/// 统计图类型
/// </summary>
/// <remarks>
/// 默认是饼形图
/// </remarks>
private ChartType m_intChartType ;

/// <summary>
/// 总数
/// </summary>
private int m_intTotalCount ;

/// <summary>
/// 总数,只读
/// </summary>
public int TotalCount
{
get
{
return m_intTotalCount ;
}
}

/// <summary>
/// 统计图的标题
/// </summary>
public string Title
{
get
{
return m_strTitle ;
}
set
{
m_strTitle = value ;
}
}

/// <summary>
/// 统计图项数组
/// </summary>
/// <remarks>
/// 这个属性只读,若要添加项目,用AddItem函数
/// </remarks>
public ArrayList Items
{
get
{
return m_arrItems ;
}
}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
15#
 楼主| 发表于 2006-9-23 14:18 | 只看该作者
/// <summary>
/// 统计图宽度
/// </summary>
public int Width
{
get
{
return m_intWidth ;
}
set
{
m_intWidth = value ;
}
}

/// <summary>
/// 计量单位
/// </summary>
public string Unit
{
get
{
return m_strUnit ;
}
set
{
m_strUnit = value ;
}
}
/// <summary>
/// 统计图高度
/// </summary>
public int Height
{
get
{
return m_intHeight ;
}
set
{
m_intHeight = value ;
}
}

/// <summary>
/// 统计图背景色
/// </summary>
public System.Drawing.Color BackColor
{
get
{
return m_objBackColor ;
}
set
{
m_objBackColor = value ;
}
}


/// <summary>
/// 图表类型,这个决定生成的图片采用什么形式
/// </summary>
/// <remarks>
/// Pie : 饼形图
/// Bar: 条形图
/// Curve: 折线图
public ChartType Type
{
get
{
return m_intChartType ;
}
set
{
m_intChartType = value ;
}
}
/// <summary>
/// 构造函数
/// </summary>

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
16#
 楼主| 发表于 2006-9-23 14:19 | 只看该作者
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对象") ;
}
}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
17#
 楼主| 发表于 2006-9-23 14:19 | 只看该作者
/// <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

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
18#
 楼主| 发表于 2006-9-23 14:19 | 只看该作者
{

//写题目

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

//计算所占百分比
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) ;

//写字
//计算字体大小
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) ;

//画图例

//计算字体大小
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) ;

}

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
19#
 楼主| 发表于 2006-9-23 14:20 | 只看该作者
//画标志线
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() ;
}

//清控对象
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>
/// 这段程序很难懂,因为需要根据图片大小决定饼图及文字的大小,所以所有计算方法
/// 都是试验出来的。
/// </remarks>
protected void DrawPie(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)

使用道具 举报

回复
论坛徽章:
456
ITPUB年度最佳版主
日期:2011-12-28 15:24:18马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14ITPUB年度最佳版主
日期:2014-02-19 10:05:27优秀写手
日期:2013-12-18 09:29:09ITPUB社区千里马徽章
日期:2013-06-09 10:15:34ITPUB年度最佳版主
日期:2013-01-30 17:30:25版主9段
日期:2012-07-03 02:21:03
20#
 楼主| 发表于 2006-9-23 14:20 | 只看该作者
//如果没有任何回答,则显示没有结果
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
{
//定义一个浮点变量,记住上一个项目结束的角度
int intAngel = 180 ;

//画饼形图
for (int i = 0 ; i < m_arrItems.Count ; i ++)
{
ChartItem item = (ChartItem)m_arrItems ;

//开始时从180度开始

//计算所占百分比
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) ;
}

//画饼图右边图例百分数
//计算矩形大小
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) ;

//写百分数
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);

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

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表