问题描述
///<summary>///根据货物所占百分比画饼图///</summary>///<paramname="objgraphics">Graphics类对象</param>///<paramname="M_str_sqlstr">SQL语句</param>///<paramname="M_str_table">表名</param>///<paramname="M_str_Num">数据表中货物数</param>///<paramname="M_str_tbGName">数据表中货物名称</param>///<paramname="M_str_title">饼图标题</param>publicvoiddrawPic(Graphicsobjgraphics,stringM_str_sqlstr,stringM_str_table,stringM_str_Num,stringM_str_tbGName,stringM_str_title){DataSetmyds=datacon.getds(M_str_sqlstr,M_str_table);floatM_flt_total=0.0f,M_flt_tmp;intM_int_iloop;for(M_int_iloop=0;M_int_iloop<myds.Tables[0].Rows.Count;M_int_iloop++){M_flt_tmp=Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]);M_flt_total+=M_flt_tmp;}Fontfontlegend=newFont("verdana",9),fonttitle=newFont("verdana",10,FontStyle.Bold);//设置字体intM_int_width=275;//白色背景宽constintMc_int_bufferspace=15;intM_int_legendheight=fontlegend.Height*(myds.Tables[0].Rows.Count+1)+Mc_int_bufferspace;intM_int_titleheight=fonttitle.Height+Mc_int_bufferspace;intM_int_height=M_int_width+M_int_legendheight+M_int_titleheight+Mc_int_bufferspace;//白色背景高intM_int_pieheight=M_int_width;Rectanglepierect=newRectangle(0,M_int_titleheight,M_int_width,M_int_pieheight);//加上各种随机色Bitmapobjbitmap=newBitmap(M_int_width,M_int_height);//创建一个bitmap实例objgraphics=Graphics.FromImage(objbitmap);ArrayListcolors=newArrayList();Randomrnd=newRandom();for(M_int_iloop=0;M_int_iloop<myds.Tables[0].Rows.Count;M_int_iloop++)colors.Add(newSolidBrush(Color.FromArgb(rnd.Next(255),rnd.Next(255),rnd.Next(255))));objgraphics.FillRectangle(newSolidBrush(Color.White),0,0,M_int_width,M_int_height);//画一个白色背景objgraphics.FillRectangle(newSolidBrush(Color.LightYellow),pierect);//画一个亮黄色背景//以下为画饼图(有几行row画几个)floatM_flt_currentdegree=0.0f;for(M_int_iloop=0;M_int_iloop<myds.Tables[0].Rows.Count;M_int_iloop++){objgraphics.FillPie((SolidBrush)colors[M_int_iloop],pierect,M_flt_currentdegree,Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num])/M_flt_total*360);M_flt_currentdegree+=Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num])/M_flt_total*360;}//以下为生成主标题SolidBrushblackbrush=newSolidBrush(Color.Black);StringFormatstringFormat=newStringFormat();stringFormat.Alignment=StringAlignment.Center;stringFormat.LineAlignment=StringAlignment.Center;objgraphics.DrawString(M_str_title,fonttitle,blackbrush,newRectangle(0,0,M_int_width,M_int_titleheight),stringFormat);objgraphics.DrawRectangle(newPen(Color.Black,2),0,M_int_height-M_int_legendheight,M_int_width,M_int_legendheight);for(M_int_iloop=0;M_int_iloop<myds.Tables[0].Rows.Count;M_int_iloop++){objgraphics.FillRectangle((SolidBrush)colors[M_int_iloop],5,M_int_height-M_int_legendheight+fontlegend.Height*M_int_iloop+5,10,10);objgraphics.DrawString(((String)myds.Tables[0].Rows[M_int_iloop][M_str_tbGName])+"——"+Convert.ToString(Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num])*100/M_flt_total)+"%",fontlegend,blackbrush,20,M_int_height-M_int_legendheight+fontlegend.Height*M_int_iloop+1);}objgraphics.DrawString("总货物数是:"+Convert.ToString(M_flt_total),fontlegend,blackbrush,5,M_int_height-fontlegend.Height);stringP_str_imagePath=Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\")).LastIndexOf("\"));P_str_imagePath+=@"Imageimage"+DateTime.Now.ToString("yyyyMMddhhmss")+".jpg";objbitmap.Save(P_str_imagePath,ImageFormat.Jpeg);objgraphics.Dispose();objbitmap.Dispose();}
解决方案
解决方案二:
DataSetmyds=datacon.getds(M_str_sqlstr,M_str_table);//返回数据集floatM_flt_total=0.0f,M_flt_tmp;//声明float类型变量,并对M_flt_total初始化为0intM_int_iloop;//声明int变量//遍历数据集合myds的第一个表的数据行for(M_int_iloop=0;M_int_iloop<myds.Tables[0].Rows.Count;M_int_iloop++){M_flt_tmp=Convert.ToSingle(myds.Tables[0].Rows[M_int_iloop][M_str_Num]);//把列值转换成Single值M_flt_total+=M_flt_tmp;//M_flt_total记录该列的合计}
解决方案三:
Fontfontlegend=newFont("verdana",9),fonttitle=newFont("verdana",10,FontStyle.Bold);//设置字体intM_int_width=275;//白色背景宽constintMc_int_bufferspace=15;//声明int常量intM_int_legendheight=fontlegend.Height*(myds.Tables[0].Rows.Count+1)+Mc_int_bufferspace;intM_int_titleheight=fonttitle.Height+Mc_int_bufferspace;intM_int_height=M_int_width+M_int_legendheight+M_int_titleheight+Mc_int_bufferspace;//白色背景高intM_int_pieheight=M_int_width;Rectanglepierect=newRectangle(0,M_int_titleheight,M_int_width,M_int_pieheight);//声明并实例化矩形
解决方案四:
//加上各种随机色Bitmapobjbitmap=newBitmap(M_int_width,M_int_height);//创建一个bitmap实例objgraphics=Graphics.FromImage(objbitmap);//通过图片创建Graphics对象ArrayListcolors=newArrayList();//声明并实例化数组Randomrnd=newRandom();//声明并实例化随机数产生器//变量数据集第一个表,添加画刷for(M_int_iloop=0;M_int_iloop<myds.Tables[0].Rows.Count;M_int_iloop++)colors.Add(newSolidBrush(Color.FromArgb(rnd.Next(255),rnd.Next(255),rnd.Next(255))));objgraphics.FillRectangle(newSolidBrush(Color.White),0,0,M_int_width,M_int_height);//画一个白色背景objgraphics.FillRectangle(newSolidBrush(Color.LightYellow),pierect);//画一个亮黄色背景