using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace TChart.ImageChart
{
    /// <summary>
    /// 作图器的基础类
    /// </summary>
    /// <remarks>
    /// 作者: Kingthy 
    /// 日期: 2007-09-11
    /// MSN:  Kingthy@gmail.com
    /// 转载请注明原作者,当你有更新修改时如果方便的希望能发一份给我.谢谢
    /// </remarks>
    public abstract class MovementPainterBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        public MovementPainterBase(MovementImage image)
        {
            this.m_Image = image;
        }

        private MovementImage m_Image;
        /// <summary>
        /// 返回此作图器的相关链走势图
        /// </summary>
        public MovementImage Image
        {
            get
            {
                return m_Image;
            }
            set
            {
                m_Image = value;
            }
        }

        #region 取得坐标
        /// <summary>
        /// 获取某值在Y轴上的位置
        /// </summary>
        /// <param name="value">当前点的值</param>
        /// <returns>点坐标</returns>
        protected int GetYPosition(decimal value)
        {
            //取得Y轴坐标为0的位置
            int y = Image.Height - Image.YSpaceWidth;
            //取得Y轴的总高度
            int h = Image.Height - Image.YSpaceWidth * 2;

            //取得比率
            int percent = 0;
            if (Image.XAxisMaxValue != 0)
            {
                percent = (int)((value / Image.XAxisMaxValue) * h);
            }
            return y - percent;
        }
        #endregion

        /// <summary>
        /// 画XY轴线
        /// </summary>
        /// <param name="graphics">画布</param>
        /// <param name="xAxes">X轴的数据</param>
        public virtual void DrawXYAxisLine(System.Drawing.Graphics graphics, XAxisItemList xAxes)
        {
            int i, p, h;
            StringFormat format = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };
            int tmpXSpaceWidth;
            //tmpXSpaceWidth = Image.XSpaceWidth;
            tmpXSpaceWidth = Image.Xspace;

            //画Y轴线
            Point p1 = new Point(tmpXSpaceWidth, Image.YSpaceWidth);
            Point p2 = new Point(tmpXSpaceWidth, Image.Height - Image.YSpaceWidth);
            graphics.DrawLine(new Pen(Image.YAxisColor), p1, p2);

            //画等分线[Y轴默认为10等分]
            h = (p2.Y - p1.Y) / 10;         //等分线的间距

            //0和10不画线
            for (i = 1; i <= 9; i++)
            {
                p = p1.Y + h * i;
                graphics.DrawLine(new Pen(Image.YAxisColor), new Point(p1.X - 3, p), new Point(p1.X, p));
            }


            //画X轴线
            //点[X = 左右间距 + X轴列的间距总数, Y = 图片高度 - Y轴间距]
            Point p3 = new Point(Image.XSpaceWidth * xAxes.Count + tmpXSpaceWidth * 2, Image.Height - Image.YSpaceWidth);
            graphics.DrawLine(new Pen(Image.XAxisColor), p2, p3);


            //画等分线
            for (i = 1; i <= xAxes.Count; i++)
            {
                    p = p1.X + Image.XSpaceWidth * i;

                //重置p3点
                p3.X = p;
                p3.Y = p2.Y + 3;
                graphics.DrawLine(new Pen(Image.XAxisColor), new Point(p, p2.Y), p3);

                //添加X轴标题
                p3.Y = p2.Y + Image.YSpaceWidth / 2;
                XAxisItem item = xAxes[i - 1];
                if (item != null && !string.IsNullOrEmpty(item.Title))
                {
                    graphics.DrawString(item.Title, item.TitleFont, new SolidBrush(item.TitleColor), p3, format);
                }
            }
        }

        /// <summary>
        /// 画标题
        /// </summary>
        /// <param name="graphics"></param>
        public virtual void DrawTitle(System.Drawing.Graphics graphics)
        {
            StringFormat format = new StringFormat
            {
                Alignment = StringAlignment.Near,
                LineAlignment = StringAlignment.Near
            };

            if (!string.IsNullOrEmpty(Image.Title))
            {
                graphics.DrawString(Image.Title, Image.TitleFont, new SolidBrush(Image.TitleColor), new Point(3,3), format);
            }
        }

        /// <summary>
        /// 画走势线
        /// </summary>
        /// <param name="graphics">画布</param>
        /// <param name="xAxes">X轴数据</param>
        public abstract void DrawValue(System.Drawing.Graphics graphics, XAxisItemList xAxes);
    }
}
