using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
namespace TChart.ImageChart
{
	/// <summary>
	/// 走势图
	/// </summary>
    /// <remarks>
    /// 作者: Kingthy 
    /// 日期: 2007-09-11
    /// MSN:  Kingthy@gmail.com
    /// 转载请注明原作者,当你有更新修改时如果方便的希望能发一份给我.谢谢
    /// </remarks>
	public class MovementImage : IDisposable
	{
        /// <summary>
        /// 
        /// </summary>
        public MovementImage()
		{
			InitParameter();
        }

        #region 属性定义
        /// <summary>
		/// 初始化参数
		/// </summary>
		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;
        /// <summary>
        /// 返回或设置X轴间隔
        /// </summary>
        public int Xspace
        {
            get
            {
                return m_Xspace;
            }
            set
            {
                m_Xspace = value;
            }
        }
		private string m_Title;
		/// <summary>
		/// 标题
		/// </summary>
		public string Title
		{
			get
			{
				return m_Title;
			}
			set
			{
				m_Title = value;
			}
		}

        private int m_Width;
        /// <summary>
        /// 返回图片的宽度
        /// </summary>
        public int Width
        {
            get
            {
                return m_Width;
            }
        }

        private int m_Height;
        /// <summary>
        /// 返回或设置图片的高度
        /// </summary>
        public int Height
        {
            get
            {
                return m_Height;
            }
            set
            {
                m_Height = value;
            }
        }

		private int m_XSpaceWidth;
		/// <summary>
		/// X轴的间距
		/// </summary>
		public int XSpaceWidth
		{
			get
			{
				return m_XSpaceWidth;
			}
			set
			{
				m_XSpaceWidth = value;
			}
		}

        private int m_YSpaceWidth;
        /// <summary>
        /// Y轴的间距
        /// </summary>
        public int YSpaceWidth
        {
            get
            {
                return m_YSpaceWidth;
            }
            set
            {
                m_YSpaceWidth = value;
            }
        }

        private Color m_TitleColor;
        /// <summary>
        /// 标题的颜色
        /// </summary>
        public Color TitleColor
        {
            get
            {
                return m_TitleColor;
            }
            set
            {
                m_TitleColor = value;
            }
        }

        private Font m_TitleFont;
        /// <summary>
        /// 标题的字体
        /// </summary>
        public Font TitleFont
        {
            get
            {
                return m_TitleFont;
            }
            set
            {
                m_TitleFont = value;
            }
        }

        private Color m_BackgroundColor;
        /// <summary>
        /// 背景颜色
        /// </summary>
        public Color BackgroundColor
        {
            get
            {
                return m_BackgroundColor;
            }
            set
            {
                m_BackgroundColor = value;
            }
        }

        private Color m_XAxisColor;
        /// <summary>
        /// X轴的颜色
        /// </summary>
        public Color XAxisColor
        {
            get
            {
                return m_XAxisColor;
            }
            set
            {
                m_XAxisColor = value;
            }
        }

        private Color m_YAxisColor;
        /// <summary>
        /// Y轴的颜色
        /// </summary>
        public Color YAxisColor
        {
            get
            {
                return m_YAxisColor;
            }
            set
            {
                m_YAxisColor = value;
            }
        }

        private decimal m_XAxisMaxValue;
        /// <summary>
        /// 返回X轴数据的最大值(供给Painter调用)
        /// </summary>
        public decimal XAxisMaxValue
        {
            get
            {
                return m_XAxisMaxValue;
            }
        }

        private System.Drawing.Image m_Picture;
        /// <summary>
        /// 返回图片数据
        /// </summary>
        public System.Drawing.Image Picture
        {
            get
            {
                return m_Picture;
            }
            set
            {
                m_Picture = value;
            }
        }
        /// <summary>
        /// 画布
        /// </summary>
        private Graphics m_Graphics;
        #endregion

        #region 设置作图器
        private MovementPainterBase m_Painter;
        /// <summary>
        /// 作图器
        /// </summary>
        public MovementPainterBase Painter
        {
            get
            {
                return m_Painter;
            }
            set
            {
                m_Painter = value;
                //重置作图器的关联图像为当前图
                if (value != null) m_Painter.Image = this;
            }
        }
        #endregion

        #region 释放内容资源
        /// <summary>
        /// 释放内容资源
        /// </summary>
        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 取得最大值
        /// <summary>
        /// 获取X轴中的最大值
        /// </summary>
        /// <param name="xAxes"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 获取所有X轴列中的最大值
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private decimal GetMaxXAxisValue(List<XAxisItemList> list)
        {
            decimal max = 0;
            foreach (XAxisItemList item in list)
            {
                if (item.Count != 0) max = Math.Max(max, GetMaxXAxisValue(item));
            }
            return max;
        }
        #endregion
        
        #region 画图
        /// <summary>
        /// 初始化图片
        /// </summary>
        /// <param name="xAxes">X轴的数据</param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 画一条走势图
        /// </summary>
        /// <param name="xAxes"></param>
        public void Draw(XAxisItemList xAxes)
        {
            //取得最大值
            m_XAxisMaxValue = GetMaxXAxisValue(xAxes);

            if (!InitImage(xAxes)) return;

            //画值
            Painter.DrawValue(m_Graphics, xAxes);
        }

        /// <summary>
        /// 画多条走势图
        /// </summary>
        /// <param name="list"></param>
        public void Draw(List<XAxisItemList> 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
    }
}
