熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

如何在C#的WinForm中制作餅狀圖和柱狀圖

2013-11-13 09:54:44  來源: .NET編程 

  當我們的軟件需要各種餅狀圖和柱狀圖來表示數據時我們或許會想到用Offices中的圖形控件或是第三方控件但現在的第三方控件大都需要注冊有些免費的控件會有開發商的標記等而對於使用Offices的圖形控件來說並不能在程序中得於很好控制其使用的簡易程度也較低所以在這我給出在C#中使用GDI+實現餅狀圖和柱狀圖跟數據庫聯接顯示數據的方法

  using System;
using SystemIO;//用於文件存取
using SystemData;//用於數據訪問
using SystemDrawing;//提供畫GDI+圖形的基本功能
using SystemDrawingText;//提供畫GDI+圖形的高級功能
using SystemDrawingDrawingD;//提供畫高級二維矢量圖形功能
using SystemDrawingImaging;//提供畫GDI+圖形的高級功能
namespace BaseLayer
{
public class PieChart
{
public PieChart()
{
}
//Render是圖形大標題圖開小標題圖形寬度圖形長度餅圖的數據集和餅圖的數據集要表示出來的數據
public Image Render(string title string subTitle int width int height DataSet chartDataint DataLine)
{
const int SIDE_LENGTH = ;
const int PIE_DIAMETER = ;
DataTable dt = chartDataTables[];

  //通過輸入參數取得餅圖中的總基數
float sumData = ;
foreach(DataRow dr in dtRows)
{
sumData += ConvertToSingle(dr[DataLine]);
}
//產生一個image對象並由此產生一個Graphics對象
Bitmap bm = new Bitmap(widthheight);
Graphics g = GraphicsFromImage(bm);
//設置對象g的屬性
gScaleTransform((ConvertToSingle(width))/SIDE_LENGTH(ConvertToSingle(height))/SIDE_LENGTH);
gSmoothingMode = SmoothingModeDefault;
gTextRenderingHint = TextRenderingHintAntiAlias;

  //畫布和邊的設定
gClear(ColorWhite);
gDrawRectangle(PensBlackSIDE_LENGTHSIDE_LENGTH);
//畫餅圖標題
gDrawString(titlenew Font(Tahoma)BrushesBlacknew PointF());
//畫餅圖的圖例
gDrawString(subTitlenew Font(Tahoma)BrushesBlacknew PointF());
//畫餅圖
float curAngle = ;
float totalAngle = ;
for(int i=;i<dtRowsCount;i++)
{
curAngle = ConvertToSingle(dtRows[i][DataLine]) / sumData * ;

  gFillPie(new SolidBrush(ChartUtilGetChartItemColor(i))PIE_DIAMETERPIE_DIAMETERtotalAnglecurAngle);
gDrawPie(PensBlackPIE_DIAMETERPIE_DIAMETERtotalAnglecurAngle);
totalAngle += curAngle;
}
//畫圖例框及其文字
gDrawRectangle(PensBlack);
gDrawString(圖表說明new Font(TahomaFontStyleBold)BrushesBlacknew PointF());

  //畫圖例各項
PointF boxOrigin = new PointF();
PointF textOrigin = new PointF();
float percent = ;
for(int i=;i<dtRowsCount;i++)
{
gFillRectangle(new SolidBrush(ChartUtilGetChartItemColor(i))boxOriginXboxOriginY);
gDrawRectangle(PensBlackboxOriginXboxOriginY);
percent = ConvertToSingle(dtRows[i][DataLine]) / sumData * ;
gDrawString(dtRows[i][]ToString() + + dtRows[i][]ToString() + ( + percentToString() + %)new Font(Tahoma)BrushesBlacktextOrigin);
boxOriginY += ;
textOriginY += ;
}
//回收資源
gDispose();
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 = ;
const int CHART_TOP = ;
const int CHART_HEIGHT = ;
const int CHART_LEFT = ;
const int CHART_WIDTH = ;
DataTable dt = chartDataTables[];

  //計算最高的點
float highPoint = ;
foreach(DataRow dr in dtRows)
{
if(highPoint<ConvertToSingle(dr[]))
{
highPoint = ConvertToSingle(dr[]);
}
}
//建立一個Graphics對象實例
Bitmap bm = new Bitmap(widthheight);
try
{
Graphics g = GraphicsFromImage(bm);
//設置條圖圖形和文字屬性
gScaleTransform((ConvertToSingle(width))/SIDE_LENGTH(ConvertToSingle(height))/SIDE_LENGTH);
gSmoothingMode = SmoothingModeDefault;
gTextRenderingHint = TextRenderingHintAntiAlias;

  //設定畫布和邊
gClear(ColorWhite);
gDrawRectangle(PensBlackSIDE_LENGTHSIDE_LENGTH);
//畫大標題
gDrawString(titlenew Font(Tahoma)BrushesBlacknew PointF());
//畫小標題
gDrawString(subTitlenew Font(Tahoma)BrushesBlacknew PointF());
//畫條形圖
float barWidth = CHART_WIDTH / (dtRowsCount * );
PointF barOrigin = new PointF(CHART_LEFT + (barWidth / ));
float barHeight = dtRowsCount;
for(int i=;i<dtRowsCount;i++)
{
barHeight = ConvertToSingle(dtRows[i][]) * / highPoint * ;
barOriginY = CHART_TOP + CHART_HEIGHT barHeight;
gFillRectangle(new SolidBrush(ChartUtilGetChartItemColor(i))barOriginXbarOriginYbarWidthbarHeight);
barOriginX = barOriginX + (barWidth * );
}
//設置邊
gDrawLine(new Pen(ColorBlack)new Point(CHART_LEFTCHART_TOP)new Point(CHART_LEFTCHART_TOP + CHART_HEIGHT));
gDrawLine(new Pen(ColorBlack)new Point(CHART_LEFTCHART_TOP + CHART_HEIGHT)new Point(CHART_LEFT + CHART_WIDTHCHART_TOP + CHART_HEIGHT));
//畫圖例框和文字
gDrawRectangle(new Pen(ColorBlack));
gDrawString(圖表說明new Font(TahomaFontStyleBold)BrushesBlacknew PointF());

  //畫圖例
PointF boxOrigin = new PointF();
PointF textOrigin = new PointF();
for(int i=;i<dtRowsCount;i++)
{
gFillRectangle(new SolidBrush(ChartUtilGetChartItemColor(i))boxOriginXboxOriginY);
gDrawRectangle(PensBlackboxOriginXboxOriginY);
gDrawString(dtRows[i][]ToString() + + dtRows[i][]ToString()new Font(Tahoma)BrushesBlacktextOrigin);
boxOriginY += ;
textOriginY += ;
}
//輸出圖形
gDispose();
return bm;
}
catch
{
return bm;
}
}
}
public class ChartUtil
{
public ChartUtil()
{
}
public static Color GetChartItemColor(int itemIndex)
{
Color selectedColor;
switch(itemIndex)
{
case :
selectedColor = ColorBlue;
break;
case :
selectedColor = ColorRed;
break;
case :
selectedColor = ColorYellow;
break;
case :
selectedColor = ColorPurple;
break;
default:
selectedColor = ColorGreen;
break;
}
return selectedColor;
}
}
}

  以上是一個完整的WinForm中制作餅狀圖和柱狀圖源程序大家可以通過以上程序的更改做出能滿足自己程序的需要


From:http://tw.wingwit.com/Article/program/net/201311/11951.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.