我看到一篇孟老大写的如何用C#生成柱状图
objGraphics.Clear(Color.White);
int[] arrValues={100,135,115,75};
string[] arrValueNames=new string[]{"单选题","填空题","多选题","读程题"};
objGraphics .DrawString ("试题库各类型题目分布情况",new Font("宋体",16),Brushes.Black,new PointF(5,5));
PointF symbolLeg=new PointF(360,16);
PointF descLeg=new PointF(360,16);
for (int i=0;i<arrValueNames.Length;i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)),symbolLeg.X,symbolLeg.Y ,20,10);
objGraphics.DrawRectangle(Pens.Black,symbolLeg.X,symbolLeg.Y ,20,10);
objGraphics.DrawString(arrValueNames[i].ToString(),new Font("宋体",10),Brushes.Black,descLeg);
symbolLeg .Y+=20;
descLeg.Y +=20;部分代码,也运行出来了 这里的数据都是定死了,那请问如何使的数据是从数据库里调出来呢?????使得这个图是动态的呢???
objGraphics.Clear(Color.White);
int[] arrValues={100,135,115,75};
string[] arrValueNames=new string[]{"单选题","填空题","多选题","读程题"};
objGraphics .DrawString ("试题库各类型题目分布情况",new Font("宋体",16),Brushes.Black,new PointF(5,5));
PointF symbolLeg=new PointF(360,16);
PointF descLeg=new PointF(360,16);
for (int i=0;i<arrValueNames.Length;i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)),symbolLeg.X,symbolLeg.Y ,20,10);
objGraphics.DrawRectangle(Pens.Black,symbolLeg.X,symbolLeg.Y ,20,10);
objGraphics.DrawString(arrValueNames[i].ToString(),new Font("宋体",10),Brushes.Black,descLeg);
symbolLeg .Y+=20;
descLeg.Y +=20;部分代码,也运行出来了 这里的数据都是定死了,那请问如何使的数据是从数据库里调出来呢?????使得这个图是动态的呢???
string[] arrValueNames=new string[]{"单选题","填空题","多选题","读程题"};
我把这个改成 int aa=Convert.ToInt16("select count([tm id]) from xuanze where xuanze.[type id='1']");
int bb=Convert.ToInt16("select count([tm id]) from xuanze where xuanze.[type id]='2'");
int cc=Convert.ToInt16("select count([tm id]) from xuanze where xuanze.[type id]='3'");
int dd=Convert.ToInt16("select count([tm id]) from xuanze where xuanze.[type id]='4'"); int[] arrValues={aa,bb,cc,dd};
string[] arrValueNames=new string[]{"单选题","填空题","多选题","读程题"}; //定义数组对象,用来描述图列
objGraphics .DrawString ("试题库各类型题目分布情况",new Font("宋体",16),Brushes.Black,new PointF(5,5)); //在画布(objBitMap对象)的坐标5,5处,用指定的Brush(画笔)对象和Font(字体)对象绘制统计图标题。 PointF symbolLeg=new PointF(360,16); //创建图列文字
PointF descLeg=new PointF(360,16);想把显示的东西后后台数据库连接 可是柱状图就显示不出来了 为什么呢
我刚找到孟老大的例子,=下测试
Convert.ToInt16("select count([tm id]) from xuanze where xuanze.[type id='1']");
从数据库里取东西可不是这样!!!
统计数据库里[xuanze]表中[typeid]为1的行数的代码像下面这样: try
{
string strCn = ...; //数据库的连接字串
string strSQL = "SELECT COUNT(*) FROM xuanze WHERE [typeid]='1'"; // SQL COMMAND
SqlConnection sqlCn = new SqlConnection(strCn);
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlCn);
sqlCmd.Connection.Open();
int a = Convert.ToInt32(sqlCmd.ExecuteScalar().ToString());
sqlCn.Close();
}
catch
{
}
至于统计多次,你可以写个方法private int GetCount(string typeid),调用4次不就可以了
或者分组统计:"SELECT [typeid], COUNT(*) FROM [xuanze] GROUP BY [typeid]"
然后用SqlDataReader sdr = sqlCmd.ExecuteScalar();
While(sdr.Read())
{
// 取值
}
List<int> arrValues = new List<int>();
// 存放Name的List
List<string> arrValueNames = new List<string>(); // 从数据库中取统计数据 string strCn = "DATA SOURCE=CSU-SILWOL;Initial Catalog=chen;uid = sa;pwd = '';Pooling=False";
//strCn改成你的连接字串
SqlConnection conn = new SqlConnection(strCn);
SqlCommand sqlCmd = new SqlCommand("SELECT [typename] AS [Name], COUNT(*) AS [Value] FROM [xuanze] GROUP BY [typeid]", conn);
// 你的数据表我不了解,我是假设[typename]字段是"单选题"、"填空题"这些内容,就是GROUP BY进行统计,语句你再自己改成符合你的表吧
sqlCmd.Connection.Open();
SqlDataReader sdr = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
while (sdr.Read())
{
arrValueNames.Add(sda["Name"].ToString());
arrValues.Add(int.Parse(sdr["Value"].ToString()));
}
sdr.Close();把这段放到孟老大代码的前面,注释掉老大的arrValueNames和arrValues定义,再把后面的arrValueNames.Length和arrValues.Length改成.Count这样子,不管你的分类数据有多少,都可以用了,不过老大写的绘图部分是固定400*200大小的,数据太多就有问题
当然你只有4个数据肯定看不出来,明天我把可以根据分类数多少来设定图片大小的全代码帖上来