当我们的软件需要各种饼状图和柱状图来表示数据时,我们或许会想到用offices中的图形控件或是第三方控件,但现在的第三方控件大都需要注册,有些免费的控件会有开发商的标记等。而对于使用offices的图形控件来说,并不能在程序中得于很好控制,其使用的简易程度也较低,所以在这我给出在c#中使用gdi+实现饼状图和柱状图跟数据库联接显示数据的方法。
using system;
using system.io;//用于文件存取
using system.data;//用于数据访问
using system.drawing;//提供画gdi+图形的基本功能
using system.drawing.text;//提供画gdi+图形的高级功能
using system.drawing.drawing2d;//提供画高级二维,矢量图形功能
using system.drawing.imaging;//提供画gdi+图形的高级功能
namespace baselayer
{
public class piechart
{
public piechart()
{
}
//render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集要表示出来的数据
public image render(string title, string subtitle, int width, int height, dataset chartdata,int dataline)
{
const int side_length = 400;
const int pie_diameter = 200;
datatable dt = chartdata.tables[0];
//通过输入参数,取得饼图中的总基数
float sumdata = 0;
foreach(datarow dr in dt.rows)
{
sumdata += convert.tosingle(dr[dataline]);
}
//产生一个image对象,并由此产生一个graphics对象
bitmap bm = new bitmap(width,height);
graphics g = graphics.fromimage(bm);
//设置对象g的属性
g.scaletransform((convert.tosingle(width))/side_length,(convert.tosingle(height))/side_length);
g.smoothingmode = smoothingmode.default;
g.textrenderinghint = textrenderinghint.antialias;
//画布和边的设定
g.clear(color.white);
g.drawrectangle(pens.black,0,0,side_length-1,side_length-1);
//画饼图标题
g.drawstring(title,new font(“tahoma”,14),brushes.black,new pointf(5,5));
//画饼图的图例
g.drawstring(subtitle,new font(“tahoma”,12),brushes.black,new pointf(7,35));
//画饼图
float curangle = 0;
float totalangle = 0;
for(int i=0;i<dt.rows.count;i++)
{
curangle = convert.tosingle(dt.rows[i][dataline]) / sumdata * 360;
g.fillpie(new solidbrush(chartutil.getchartitemcolor(i)),100,65,pie_diameter,pie_diameter,totalangle,curangle);
g.drawpie(pens.black,100,65,pie_diameter,pie_diameter,totalangle,curangle);
totalangle += curangle;
}
//画图例框及其文字
g.drawrectangle(pens.black,200,300,199,99);
g.drawstring(“图表说明”,new font(“tahoma”,12,fontstyle.bold),brushes.black,new pointf(200,300));
//画图例各项
pointf boxorigin = new pointf(210,330);
pointf textorigin = new pointf(235,326);
float percent = 0;
for(int i=0;i<dt.rows.count;i++)
{
g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),boxorigin.x,boxorigin.y,20,10);
g.drawrectangle(pens.black,boxorigin.x,boxorigin.y,20,10);
percent = convert.tosingle(dt.rows[i][dataline]) / sumdata * 100;
g.drawstring(dt.rows[i][1].tostring() + ” – ” + dt.rows[i][0].tostring() + ” (” + percent.tostring(“0”) + “%)”,new font(“tahoma”,10),brushes.black,textorigin);
boxorigin.y += 15;
textorigin.y += 15;
}
//回收资源
g.dispose();
return (image) bm;
}
}
//画条形图
public class barchart
{
public barchart()
{
}
//render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集
public image render(string title, string subtitle, int width, int height, dataset chartdata)
{
const int side_length = 400;
const int chart_top = 75;
const int chart_height = 200;
const int chart_left = 50;
const int chart_width = 300;
datatable dt = chartdata.tables[0];
//计算最高的点
float highpoint = 0;
foreach(datarow dr in dt.rows)
{
if(highpoint<convert.tosingle(dr[0]))
{
highpoint = convert.tosingle(dr[0]);
}
}
//建立一个graphics对象实例
bitmap bm = new bitmap(width,height);
try
{
graphics g = graphics.fromimage(bm);
//设置条图图形和文字属性
g.scaletransform((convert.tosingle(width))/side_length,(convert.tosingle(height))/side_length);
g.smoothingmode = smoothingmode.default;
g.textrenderinghint = textrenderinghint.antialias;
//设定画布和边
g.clear(color.white);
g.drawrectangle(pens.black,0,0,side_length-1,side_length-1);
//画大标题
g.drawstring(title,new font(“tahoma”,14),brushes.black,new pointf(5,5));
//画小标题
g.drawstring(subtitle,new font(“tahoma”,12),brushes.black,new pointf(7,35));
//画条形图
float barwidth = chart_width / (dt.rows.count * 2);
pointf barorigin = new pointf(chart_left + (barwidth / 2),0);
float barheight = dt.rows.count;
for(int i=0;i<dt.rows.count;i++)
{
barheight = convert.tosingle(dt.rows[i][0]) * 200 / highpoint * 1;
barorigin.y = chart_top + chart_height – barheight;
g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),barorigin.x,barorigin.y,barwidth,barheight);
barorigin.x = barorigin.x + (barwidth * 2);
}
//设置边
g.drawline(new pen(color.black,2),new point(chart_left,chart_top),new point(chart_left,chart_top + chart_height));
g.drawline(new pen(color.black,2),new point(chart_left,chart_top + chart_height),new point(chart_left + chart_width,chart_top + chart_height));
//画图例框和文字
g.drawrectangle(new pen(color.black,1),200,300,199,99);
g.drawstring(“图表说明”,new font(“tahoma”,12,fontstyle.bold),brushes.black,new pointf(200,300));
//画图例
pointf boxorigin = new pointf(210,330);
pointf textorigin = new pointf(235,326);
for(int i=0;i<dt.rows.count;i++)
{
g.fillrectangle(new solidbrush(chartutil.getchartitemcolor(i)),boxorigin.x,boxorigin.y,20,10);
g.drawrectangle(pens.black,boxorigin.x,boxorigin.y,20,10);
g.drawstring(dt.rows[i][1].tostring() + ” – ” + dt.rows[i][0].tostring(),new font(“tahoma”,10),brushes.black,textorigin);
boxorigin.y += 15;
textorigin.y += 15;
}
//输出图形
g.dispose();
return bm;
}
catch
{
return bm;
}
}
}
public class chartutil
{
public chartutil()
{
}
public static color getchartitemcolor(int itemindex)
{
color selectedcolor;
switch(itemindex)
{
case 0:
selectedcolor = color.blue;
break;
case 1:
selectedcolor = color.red;
break;
case 2:
selectedcolor = color.yellow;
break;
case 3:
selectedcolor = color.purple;
break;
default:
selectedcolor = color.green;
break;
}
return selectedcolor;
}
}
}
以上是一个完整的winform中制作饼状图和柱状图源程序,大家可以通过以上程序的更改,做出能满足自己程序的需要。
如何在C#的WinForm中制作饼状图和柱状图-.NET教程,C#语言
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何在C#的WinForm中制作饼状图和柱状图-.NET教程,C#语言
相关推荐
-      VS2010的aspx文件中的html代码的格式化方法
-      .net 反序题目的详细解答第1/2页
-      asp.net创建html文本文件实例
-      比较完整的 asp.net 学习流程
-      官网 Ext direct包中.NET版的问题
-      C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页
-      c# 连接字符串数据库服务器端口号 .net状态服务器端口号
-      asp.net教程:简单的C#图片上传代码或C#文件上传代码