首先,建立绘图类,如下:
/// <summary>
/// 绘制柱状图
/// </summary>
/// <param name="ds">DataTable中第一列为数据列,第二列为对应的名称</param>
/// <param name="xName">X轴名称</param>
/// <param name="yName">Y轴名称</param>
/// <returns>位图对象</returns>
public Bitmap GetDrawRectangleForStat(DataSet ds, string xName, string yName)
{
Bitmap cBmp = new Bitmap(471, 367); //创建画布宽度为471,高度为367的Bitmap实例
Graphics cGraphic;
Color[] cColor = new Color[] { Color.Red, Color.Blue, Color.Green, Color.Gray, Color.LightCoral, Color.Gold, Color.Brown, Color.Cyan, Color.Lime, Color.Peru, Color.Magenta, Color.Sienna, Color.Chocolate, Color.PeachPuff, Color.Orange, Color.DarkGoldenrod, Color.Olive, Color.DarkSeaGreen, Color.SpringGreen, Color.Teal, Color.CadetBlue, Color.RoyalBlue, Color.DeepPink, Color.LightGray, Color.MediumVioletRed, Color.Orchid, Color.MediumSlateBlue, Color.White, Color.LightSteelBlue, Color.DarkSlateGray, Color.GreenYellow, Color.DarkKhaki, Color.RosyBrown, Color.Navy, Color.Indigo, Color.HotPink }; //36种颜色
Pen cPen;
SolidBrush cSolidBrush;
Font bFont = new Font("Tahoma", 14, FontStyle.Bold); //轴标字体
Font sFont = new Font("Tahoma", 14, FontStyle.Bold); //统计数值字体
Font fFont = new Font("Tahoma", 14, FontStyle.Bold); //统计项值字体
StringFormat cFormat = new StringFormat();
RectangleF cRect;
int RowNum = 0;
int iLoop = 0;
int i = 0;
int cValue = 0;
int MaxValue = 0;
//定义Y轴,Y上(42,15)~Y下(42,317),Y轴长从17到317共300
int xPu = 42; //Y轴上(Up)点X坐标
int yPu = 15; //Y轴上(Up)点Y坐标
int xPd = 42; //Y轴下(Down)点X坐标
int yPd = 317; //Y轴下(Down)点Y坐标
//定义X轴,X左(42,317)~X右(448,317),X轴上从42到448共406
int xPl = 42; //X轴左(Left)点X坐标
int yPl = 317; //X轴左(Left)点Y坐标
int xPr = 448; //X轴右(Right)点X坐标
int yPr = 317; //X轴右(Right)点Y坐标
//定义柱图
//int xStart = 57; //首根柱图开始的X轴坐标
//int cWidth = 20; //柱图宽度
//int cSpace = 15; //柱图间距
int xStart = 67; //首根柱图开始的X轴坐标
int cWidth = 40; //柱图宽度
int cSpace = 25; //柱图间距
cGraphic = Graphics.FromImage(cBmp);
cGraphic.Clear(Color.Snow);
//画轴线
cPen = new Pen(Color.Black, 3);
cSolidBrush = new SolidBrush(Color.Black);
cGraphic.DrawLine(cPen, xPu, yPu, xPd, yPd); //Y轴
cGraphic.DrawLine(cPen, xPl, yPl, xPr, yPr); //X轴
//画轴向
cGraphic.DrawLine(cPen, xPu, yPu - 3, xPu - 4, yPu + 3); //Y轴向
cGraphic.DrawLine(cPen, xPu, yPu - 3, xPu + 4, yPu + 3);
cGraphic.DrawLine(cPen, xPr + 3, yPr, xPr - 4, yPr - 3); //X轴向
cGraphic.DrawLine(cPen, xPr + 3, yPr, xPr - 4, yPr + 3);
//画轴标
cFormat.FormatFlags = StringFormatFlags.NoClip;
cGraphic.DrawString(yName, bFont, cSolidBrush, 5, 45, cFormat); //Y轴标
cGraphic.DrawString(xName, bFont, cSolidBrush, 392, 332, cFormat); //X轴标
//画轴心值
cGraphic.DrawString("0", sFont, cSolidBrush, xPd - 5, yPd + 3, cFormat);
RowNum = ds.Tables[0].Rows.Count - 1;
for (i = 0; i <= RowNum; i++)
{
if (MaxValue < Int32.Parse(ds.Tables[0].Rows[i][0].ToString()))
{
MaxValue = Int32.Parse(ds.Tables[0].Rows[i][0].ToString());
}
//画柱图
cPen = new Pen(cColor[i], 3);
cSolidBrush = new SolidBrush(cColor[i]);
cValue = Int32.Parse(ds.Tables[0].Rows[i][0].ToString()) * ((yPd - yPu) / MaxValue);
cGraphic.DrawRectangle(cPen, xStart + (cWidth + cSpace) * iLoop, yPd - cValue, cWidth, cValue - 2); //减2的目的:使柱图的下边沿不挡住X轴
cGraphic.FillRectangle(cSolidBrush, xStart + (cWidth + cSpace) * iLoop, yPd - cValue, cWidth, cValue - 2);
//画柱图统计Y轴值
cGraphic.DrawString(ds.Tables[0].Rows[i][0].ToString(), sFont, cSolidBrush, xStart + (cWidth + cSpace) * iLoop - 2, yPd - cValue - 20, cFormat);
//画柱图统计X轴值
cRect = new RectangleF(xStart + (cWidth + cSpace) * iLoop, yPd + 1, cWidth + 10, 40);
cGraphic.DrawString(ds.Tables[0].Rows[i][1].ToString(), fFont, cSolidBrush, cRect, cFormat);
iLoop += 1;
}
return cBmp;
}
然后,在Windows Mobile的窗体中加入PictureBox控件,调用绘图方法,将图形展现到PictureBox控件中。


