using System;
using System.Drawing;

namespace UUN
{
    public class 折线图
    {
        /// <summary>
        /// 生成图片的宽度
        /// </summary>
        public int 宽度;
        /// <summary>
        /// 生成图片的高度
        /// </summary>
        public int 高度;
        /// <summary>
        /// 包含点击量的数据
        /// </summary>
        public int[] 点击数据;
        /// <summary>
        /// 包含IP数的数据
        /// </summary>
        public int[] IP数据;
        /// <summary>
        /// 绘制在底部的文字信息
        /// </summary>
        public String[] 文字;
        private Color 颜色1 = Color.FromArgb(255, 0, 250);
        private Color 颜色2 = Color.FromArgb(36, 143, 213);
        private Color 颜色3=Color.FromArgb(3,194,61);
        /// <summary>
        /// 生成折线图
        /// </summary>
        /// <returns>生成结果</returns>
        public Image 生成()
        {
            Bitmap 位图 = new Bitmap(宽度 + 50, 高度 + 20);
            Graphics 绘图 = Graphics.FromImage(位图);
            绘图.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            绘图.FillRectangle(new SolidBrush(Color.FromArgb(255, 255, 255)), 0, 0, 宽度 + 50, 高度 + 20);
            绘图.DrawLine(new Pen(颜色1, 1), 50, 高度 - 1, 宽度 - 1 + 50, 高度 - 1);
            绘图.DrawLine(new Pen(颜色1, 1), 50, 0, 50, 高度);

            绘制表格(绘图);

            return 位图;
        }

        private void 绘制表格(Graphics 绘图)
        {
            for (int i = 0; i < 10; i++)
            {
                int 位置 = Convert.ToInt32(高度 * ((i * 10) / 100.00));
                Pen 画笔 = new Pen(颜色2, 1);
                画笔.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;
                绘图.DrawLine(画笔, 50, 位置, 宽度 - 1 + 50, 位置);
            }

            int 最大数 = (获取最大数(点击数据) / 100 + 1) * 100;
            for (int i = 10; i >= 0; i--)
            {
                int 绘制高度 = 高度 - Convert.ToInt32(高度 * ((i * 10) / 100.00));
                int 高度数据 = Convert.ToInt32(最大数 * ((i * 10) / 100.00));
                绘图.DrawString(高度数据.ToString(), new Font("Fixed", 9), new SolidBrush(颜色2), new PointF(0, 绘制高度));
            }
            for (int i = 0; i < 点击数据.Length; i++)
            {
                int 绘制宽度 = i * (宽度 / 文字.Length);
                绘图.DrawString(文字[i], new Font("Fixed", 9), new SolidBrush(颜色2), new PointF(绘制宽度 + 50, 高度));
            }

            int[] XS = new int[点击数据.Length];
            int[] YS = new int[点击数据.Length];
            for (int i = 0; i < 点击数据.Length; i++)
            {
                XS[i] = i * (宽度 / 点击数据.Length);
                YS[i] = 高度 - Convert.ToInt32(((点击数据[i] / Convert.ToDouble(最大数))) * 高度);
            }
            Point[] PS = new Point[XS.Length];
            for (int i = 0; i < XS.Length; i++)
                PS[i] = new Point(XS[i] + 50, YS[i]);
            绘图.DrawCurve(new Pen(颜色1, 1), PS);
            for (int i = 0; i < XS.Length; i++)
                绘图.FillEllipse(new SolidBrush(颜色1), XS[i] - 2 + 50, YS[i] - 2, 5, 5);
            for (int i = 0; i < XS.Length; i++)
                绘图.DrawString(点击数据[i].ToString(), new Font("Fixed", 9), new SolidBrush(颜色1), new PointF(XS[i] + 40, YS[i]));

            XS = new int[IP数据.Length];
            YS = new int[IP数据.Length];
            for (int i = 0; i < IP数据.Length; i++)
            {
                XS[i] = i * (宽度 / IP数据.Length);
                YS[i] = 高度 - Convert.ToInt32(((IP数据[i] / Convert.ToDouble(最大数))) * 高度);
            }
            PS = new Point[XS.Length];
            for (int i = 0; i < XS.Length; i++)
                PS[i] = new Point(XS[i] + 50, YS[i]);
            绘图.DrawCurve(new Pen(颜色3, 1), PS);
            for (int i = 0; i < XS.Length; i++)
                绘图.FillEllipse(new SolidBrush(颜色3), XS[i] - 2 + 50, YS[i] - 2, 5, 5);
            for (int i = 0; i < XS.Length; i++)
                绘图.DrawString(IP数据[i].ToString(), new Font("Fixed", 9), new SolidBrush(颜色3), new PointF(XS[i] + 40, YS[i]));
        }
        private int 获取最大数(int[] ZuiDaShuZu)
        {
            int ZuiDaZhi = 0;
            for (int i = 0; i < ZuiDaShuZu.Length; i++)
                if (ZuiDaShuZu[i] > ZuiDaZhi)
                    ZuiDaZhi = ZuiDaShuZu[i];
            return ZuiDaZhi;
        }
    }
}