using System; using System.Collections.Generic; using System.Text; using System.Drawing.Imaging; using System.Drawing; using System.IO; namespace TChart.ImageChart { /// /// 走势图 /// /// /// 作者: Kingthy /// 日期: 2007-09-11 /// MSN: Kingthy@gmail.com /// 转载请注明原作者,当你有更新修改时如果方便的希望能发一份给我.谢谢 /// public class MovementImage : IDisposable { /// /// /// public MovementImage() { InitParameter(); } #region 属性定义 /// /// 初始化参数 /// private void InitParameter() { m_Title = null; m_Width = 0; m_Height = 200; m_Xspace = 4; m_XSpaceWidth = 30; m_YSpaceWidth = 30; m_XAxisMaxValue = 0; m_TitleColor = Color.Black; m_TitleFont = new Font(new FontFamily("Arial"), 9, FontStyle.Regular); m_BackgroundColor = Color.White; m_XAxisColor = Color.Black; m_YAxisColor = Color.Black; m_Painter = new RectanglePainter(this); m_Picture = null; } private int m_Xspace; /// /// 返回或设置X轴间隔 /// public int Xspace { get { return m_Xspace; } set { m_Xspace = value; } } private string m_Title; /// /// 标题 /// public string Title { get { return m_Title; } set { m_Title = value; } } private int m_Width; /// /// 返回图片的宽度 /// public int Width { get { return m_Width; } } private int m_Height; /// /// 返回或设置图片的高度 /// public int Height { get { return m_Height; } set { m_Height = value; } } private int m_XSpaceWidth; /// /// X轴的间距 /// public int XSpaceWidth { get { return m_XSpaceWidth; } set { m_XSpaceWidth = value; } } private int m_YSpaceWidth; /// /// Y轴的间距 /// public int YSpaceWidth { get { return m_YSpaceWidth; } set { m_YSpaceWidth = value; } } private Color m_TitleColor; /// /// 标题的颜色 /// public Color TitleColor { get { return m_TitleColor; } set { m_TitleColor = value; } } private Font m_TitleFont; /// /// 标题的字体 /// public Font TitleFont { get { return m_TitleFont; } set { m_TitleFont = value; } } private Color m_BackgroundColor; /// /// 背景颜色 /// public Color BackgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; } } private Color m_XAxisColor; /// /// X轴的颜色 /// public Color XAxisColor { get { return m_XAxisColor; } set { m_XAxisColor = value; } } private Color m_YAxisColor; /// /// Y轴的颜色 /// public Color YAxisColor { get { return m_YAxisColor; } set { m_YAxisColor = value; } } private decimal m_XAxisMaxValue; /// /// 返回X轴数据的最大值(供给Painter调用) /// public decimal XAxisMaxValue { get { return m_XAxisMaxValue; } } private System.Drawing.Image m_Picture; /// /// 返回图片数据 /// public System.Drawing.Image Picture { get { return m_Picture; } set { m_Picture = value; } } /// /// 画布 /// private Graphics m_Graphics; #endregion #region 设置作图器 private MovementPainterBase m_Painter; /// /// 作图器 /// public MovementPainterBase Painter { get { return m_Painter; } set { m_Painter = value; //重置作图器的关联图像为当前图 if (value != null) m_Painter.Image = this; } } #endregion #region 释放内容资源 /// /// 释放内容资源 /// public void Dispose() { if (m_Picture == null) m_Picture.Dispose(); m_Picture = null; if (m_Graphics != null) m_Graphics.Dispose(); m_Graphics = null; } #endregion #region 取得最大值 /// /// 获取X轴中的最大值 /// /// /// private decimal GetMaxXAxisValue(XAxisItemList xAxes) { if (xAxes.Count == 0) return 0; decimal max = xAxes[0].Value; for (int i = 1; i < xAxes.Count; i++) { max = Math.Max(max, xAxes[i].Value); } return max; } /// /// 获取所有X轴列中的最大值 /// /// /// private decimal GetMaxXAxisValue(List list) { decimal max = 0; foreach (XAxisItemList item in list) { if (item.Count != 0) max = Math.Max(max, GetMaxXAxisValue(item)); } return max; } #endregion #region 画图 /// /// 初始化图片 /// /// X轴的数据 /// private bool InitImage(XAxisItemList xAxes) { if (xAxes == null) return false; if (xAxes.Count == 0) return false; //求长(左右距 + X轴数量级 * 间距) this.m_Width = this.XSpaceWidth * 3 + this.XSpaceWidth * xAxes.Count; //初始化图片 m_Picture = new Bitmap(this.Width, this.Height); m_Graphics = Graphics.FromImage(m_Picture); //填充背景色 m_Graphics.FillRectangle(new SolidBrush(this.BackgroundColor), 0, 0, this.Width, this.Height); //画标题 Painter.DrawTitle(m_Graphics); //画X,Y轴坐标 Painter.DrawXYAxisLine(m_Graphics, xAxes); return true; } /// /// 画一条走势图 /// /// public void Draw(XAxisItemList xAxes) { //取得最大值 m_XAxisMaxValue = GetMaxXAxisValue(xAxes); if (!InitImage(xAxes)) return; //画值 Painter.DrawValue(m_Graphics, xAxes); } /// /// 画多条走势图 /// /// public void Draw(List list) { if (list == null || list.Count == 0) return; //取得最大值 m_XAxisMaxValue = GetMaxXAxisValue(list); XAxisItemList xAxes = list[0]; if (!InitImage(xAxes)) return; //画值 foreach (XAxisItemList item in list) { Painter.DrawValue(m_Graphics, item); } } #endregion } }