當我們的軟件需要各種餅狀圖和柱狀圖來表示數據時,我們或許會想到用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中制作餅狀圖和柱狀圖源程序,大家可以通過以上程序的更改,做出能滿足自己程序的需要。