|
|
Posted on
2004-12-08 13:00
冷风.net
阅读( 440)
评论()
收藏
举报
//*************************************************************************************************
//完成日期: 2004-11-11
//作者: 何利民
//程序功能: 繪制矩形圖表
//*************************************************************************************************
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.ComponentModel;
namespace ImagesSpaceName
  {
public class DrawRectangle : RectangleClass
 {
 定義類變量#region 定義類變量
protected ImagesSpaceName.Valign _pictureValign = ImagesSpaceName.Valign.top; //圖片顯示垂直方向位置
protected ImagesSpaceName.Align _pictureAlign = ImagesSpaceName.Align.center; //圖片顯示橫向位置
protected string _pictureTitle="冷風.net"; //圖片標題
protected string _pictureTitleColor = "#000000";//圖片標題顏色
protected int _pictureTitleSize = 14; //圖片標題大小
protected string _pictureTitleFamily="新細明體";//圖標標題名稱
protected bool _smallShow = true; //是否顯示右上角小矩形
protected int _smallWidth = 20; //右上角小矩形的寬度
protected int _smallHeight = 10; //右上角小矩形的高度
#endregion
 初使化購造函數#region 初使化購造函數
public DrawRectangle()
 {
}
#endregion
 定義屬性#region 定義屬性
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置圖片標題顏色")
]
public virtual string PictureTitleColor
 {
 get {return _pictureTitleColor;}
 set {_pictureTitleColor = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置圖片標題字體名稱")
]
public virtual string PictureTitleFamily
 {
 get {return _pictureTitleFamily;}
 set {_pictureTitleFamily = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置圖片標題大小")
]
public virtual int PictureTitleSize
 {
 get {return _pictureTitleSize;}
 set {_pictureTitleSize = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置圖片顯示垂直方向位置")
]
public ImagesSpaceName.Valign PictureValign
 {
 get {return _pictureValign;}
 set {_pictureValign = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置圖片顯示橫向位置")
]
public ImagesSpaceName.Align PictureAlign
 {
 get {return _pictureAlign;}
 set {_pictureAlign = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置圖片的標題")
]
public virtual string PictureTitle
 {
 get {return _pictureTitle;}
 set {_pictureTitle = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置右上角小矩形的寬度")
]
public virtual int SmallWidth
 {
 get {return _smallWidth;}
 set {_smallWidth = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置右上角小矩形的高度")
]
public virtual int SmallHeight
 {
 get {return _smallHeight;}
 set {_smallHeight = value;}
}
[
Bindable(true),
Category("drawing"),
DefaultValue(ImagesSpaceName.WaterTitle.bottom),
Description("獲得或設置右上角小矩形顯示與否")
]
public virtual bool SmallShow
 {
 get {return _smallShow;}
 set {_smallShow = value;}
}
#endregion
 繪制右上角的小矩形#region 繪制右上角的小矩形
private void DrawSmallRectangle(string[] arrayX)
 {
Point pos = new Point(topRightDot.X-10,topRightDot.Y); //取得基點
Pen pen = new Pen(new SolidBrush(ColorTranslator.FromHtml(this.RectangleBorderColor)),this.RectangleBorderSize);
Font font = new Font(this.XYFontFamily,this.XYFontSize);
int posY = pos.Y;
for(int i=0; i<arrayX.Length; i++)
 {
graphics.DrawRectangle(pen,pos.X,posY,SmallWidth,SmallHeight);
Color color;
if(this.RectangleBgColor==null)
color = GetColor(i);
else
color = ColorTranslator.FromHtml(this.RectangleBgColor);
graphics.FillRectangle(new SolidBrush(color),pos.X,posY,SmallWidth,SmallHeight);
graphics.DrawString(arrayX[i],font,new SolidBrush(ColorTranslator.FromHtml(this.XYFontColor)),pos.X+SmallWidth+5,posY);
posY += SmallHeight+20;
}
}
#endregion
 繪制標題#region 繪制標題
private void DrawTitle()
 {
int x = 0;
int y = 0;
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
Font font = new Font(PictureTitleFamily,PictureTitleSize);
SizeF sizeF = graphics.MeasureString(PictureTitle,font);
if(PictureValign==ImagesSpaceName.Valign.bottom)
y = (int)Height- (int)sizeF.Height;
if(PictureAlign==ImagesSpaceName.Align.center)
x = ((int)Width-(int)sizeF.Width)/2;
else if(PictureAlign==ImagesSpaceName.Align.right)
x = (int)Width-(int)sizeF.Width;
graphics.DrawString(PictureTitle,font,new SolidBrush(ColorTranslator.FromHtml(PictureTitleColor)),x,y);
}
#endregion
 繪圖#region 繪圖
 /**//// <summary>
/// 繪制圖形
/// </summary>
/// <param name="arrayX">X軸數據數組</param>
/// <param name="arrayY">Y軸數據數組</param>
private void Drawing(string[] arrayX, int[] arrayY)
 {
if(BgGround==null)
 {
bitmap = new Bitmap((int)Width,(int)Height);
graphics = Graphics.FromImage(bitmap);
graphics.Clear(ColorTranslator.FromHtml(BgColor));
}
else
 {
Bitmap bg;
try
 {
bg = new Bitmap(BgGround);
if(BgGroundAlpha<250) bg = ImageAlpha(bg,ColorTranslator.FromHtml(BgColor),BgGroundAlpha);
}
catch
 {
throw new Exception("背景圖片地址不正確!");
}
bitmap = new Bitmap(bg,(int)Width,(int)Height);
graphics = Graphics.FromImage(bitmap);
}
SetDrawArea(); //設置繪圖區
DrawingRuler(); //顯示坐標
DrawingGrid(arrayY);//繪制網格
DrawTitle(); //繪制標題
if(SmallShow) DrawSmallRectangle(arrayX);//繪制右上角的小矩形
int iXlen = arrayX.Length; //X軸上共有多少段
int iX = (bottomRightDot.X - bottomLeftDot.X-10)/iXlen; //X軸上每小段的寬度,其中10表箭頭多出的部份
int iYlen = arrayY[0]; //Y軸上共有多少段
for(int i=0; i<arrayY.Length; i++)
 {
if(iYlen<arrayY[i]) iYlen = arrayY[i];
}
int iY = (bottomLeftDot.Y - topLeftDot.Y-10)/iYlen; //Y軸上每小段的寬度,其中10表箭頭多出的部份
//繪制X軸上的小段
Pen pen = new Pen(new SolidBrush(ColorTranslator.FromHtml(XYColor)),XYSize);
Font font = new Font(XYFontFamily,XYFontSize);
for(int i=0; i<iXlen; i++)
 {
Point x = new Point(bottomLeftDot.X +(i+1)*iX,bottomLeftDot.Y);
graphics.DrawLine(pen,x,new Point(x.X,x.Y+5)); //繪制小段,其中5表小段的長度
graphics.DrawString(arrayX[i],font,new SolidBrush(ColorTranslator.FromHtml(XYFontColor)),bottomLeftDot.X+i*iX+iX/4,bottomLeftDot.Y+5);
//繪制矩形條
this.RectangleWidth = iX/2;
this.RectangleHeight = arrayY[i]*iY;
this.RectangleTitle = arrayY[i].ToString();
Point posX = new Point(bottomLeftDot.X+i*iX+(iX-this.RectangleWidth)/2,bottomLeftDot.Y-this.RectangleHeight);
Rectangle tang = new Rectangle(posX,new Size(this.RectangleWidth,this.RectangleHeight));
Image img = Image.FromStream(this.DrawingRectangle(i));
graphics.DrawImage(img,tang,0,0,this.RectangleWidth,this.RectangleHeight,GraphicsUnit.Pixel);
}
if(Alpha<250) bitmap = ImageAlpha(bitmap,ColorTranslator.FromHtml(BgColor),Alpha);
this.SaveImage(bitmap,ImageFormat.Jpeg);
}
#endregion
 繪制矩形圖表 CreateRectangle#region 繪制矩形圖表 CreateRectangle
 /**//// <param name="arrayX">橫坐標數據</param>
/// <param name="arrayY">昱坐標數據</param>
public void CreateRectangle(string[] arrayX, int[] arrayY)
 {
Drawing(arrayX,arrayY);
}
 /**//// <param name="ht">哈希表</param>
public void CreateRectangle(System.Collections.Hashtable ht)
 {
try
 {
int iMax = ht.Count;
string[] arrayX = new string[iMax];
int[] arrayY = new int[iMax];
System.Collections.IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
int i = 0;
while(myEnumerator.MoveNext())
 {
arrayX[i] = myEnumerator.Key.ToString();
arrayY[i] = (int)float.Parse(myEnumerator.Value.ToString());
i++;
}
Drawing(arrayX,arrayY);
}
catch
 {
throw new Exception("提Hashtable中的數據不對!");
}
}
 /**//// <param name="table">數據表</param>
/// <param name="x">X軸數據在數據表中的字段序號</param>
/// <param name="y">Y軸數據在數據表中的字段序號</param>
public void CreateRectangle(System.Data.DataTable table, int x, int y)
 {
try
 {
int iMax = table.Rows.Count;
string[] arrayX = new string[iMax];
int[] arrayY = new int[iMax];
for(int i=0; i<iMax; i++)
 {
arrayX[i] = table.Rows[i][x].ToString();
arrayY[i] = (int)float.Parse(table.Rows[i][y].ToString());
}
Drawing(arrayX,arrayY);
}
catch
 {
throw new Exception("提供數據表中的數據不對!");
}
}
 /**//// <param name="collection">集合</param>
public void CreateRectangle(System.Collections.Specialized.NameValueCollection collection)
 {
try
 {
int iMax = collection.Count;
string[] arrayX = new string[iMax];
int[] arrayY = new int[iMax];
for(int i=0; i<iMax; i++)
 {
arrayX[i] = collection.GetKey(i).ToString();
arrayY[i] = (int)float.Parse(collection.GetValues(i).ToString());
}
Drawing(arrayX,arrayY);
}
catch
 {
throw new Exception("提供集合中的數據表不對!");
}
}
#endregion
}
}
|