|
|
Posted on
2004-12-08 12:53
冷风.net
阅读( 401)
评论()
收藏
举报
//*************************************************************************************************
//完成日期: 2004-11-22
//作者: 何利民
//程序功能: 繪制曲線圖表
//*************************************************************************************************
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.ComponentModel;
namespace ImagesSpaceName
  {
public class DrawLine : ImageArea
 {
 定義類變量#region 定義類變量
protected int _lineSize=1; //曲線大小
protected string _lineColor="#000000"; //曲線顏色
#endregion
 初使化構造函數#region 初使化構造函數
public DrawLine()
 {
}
#endregion
 定義曲線屬性#region 定義曲線屬性
 /**//// <summary>
/// 曲線大小
/// </summary>
public virtual int LineSize
 {
 get {return _lineSize;}
 set {_lineSize=value;}
}
 /**//// <summary>
/// 曲線顏色
/// </summary>
public virtual string LineColor
 {
 get {return _lineColor;}
 set {_lineColor=value;}
}
#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);//繪制網格
int iXlen = arrayX.Length; //X軸上共有多少段
int iX = (bottomRightDot.X - bottomLeftDot.X-10)/iXlen; //X軸上每小段的寬度,其中5表箭頭多出的部份
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表箭頭多出的部份
Pen pen = new Pen(new SolidBrush(ColorTranslator.FromHtml(LineColor)),LineSize);
Font font = new Font(XYFontFamily,XYFontSize);
for(int i=1; i<iXlen; i++)
 {
//繪制坐標上的小段及文字
if(ShowXYTitle)
 {
Point x = new Point(bottomLeftDot.X +i*iX,bottomLeftDot.Y);
graphics.DrawLine(new Pen(new SolidBrush(ColorTranslator.FromHtml(XYColor)),XYSize),x,new Point(x.X,x.Y+5)); //繪制小段,其中5表小段的長度
graphics.DrawString(arrayX[i-1],font,new SolidBrush(ColorTranslator.FromHtml(XYFontColor)),bottomLeftDot.X+(i-1)*iX+iX/4,bottomLeftDot.Y+5);
if(i==iXlen-1)
graphics.DrawString(arrayX[i],font,new SolidBrush(ColorTranslator.FromHtml(XYFontColor)),bottomLeftDot.X+i*iX+iX/4,bottomLeftDot.Y+5);
}
//繪制曲線
Point pos1 = new Point(bottomLeftDot.X +(i-1)*iX,bottomLeftDot.Y - arrayY[i-1]*iY);
Point pos2 = new Point(bottomLeftDot.X +i*iX,bottomLeftDot.Y - arrayY[i]*iY);
graphics.DrawLine(pen,pos1,pos2);
//是否在當前點顯示文字
if(ShowPointTitle)
 {
graphics.DrawString(arrayY[i-1].ToString(),font,new SolidBrush(Color.Black),pos1);
if(i==iXlen-1)
graphics.DrawString(arrayY[i].ToString(),font,new SolidBrush(Color.Black),pos2);
}
}
if(Alpha<250) bitmap = ImageAlpha(bitmap,ColorTranslator.FromHtml(BgColor),Alpha);
this.SaveImage(bitmap,ImageFormat.Jpeg);
}
#endregion
 外部調用方法#region 外部調用方法
 /**//// <summary>
/// 繪制曲線圖
/// </summary>
/// <param name="arrayX">x坐標值</param>
/// <param name="arrayY">y坐標值</param>
public void CreateLine(string[] arrayX, int[] arrayY)
 {
Drawing(arrayX,arrayY);
}
#endregion
}
}
|