C#技术员售后时间绘图

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Imaging;//引用线命名空间

namespace YXServicesSystem
{
    public partial class UC技术员售后时间绘图 : UserControl
    {
        int housmin = 9;
        int housmax = 18;


        int rectangleWidth = 118;
        int rectangleHeight = 15;
        int rectangleEndWidth = 2;

        Color color空闲 = Color.Green;
        Color color排班 = Color.Yellow;
        Color color出发 = Color.Blue;
        Color color延迟出发 = Color.Turquoise;
        Color color延迟完成 = Color.Red;

        Thread threadDraw = null;

        public UC技术员售后时间绘图()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            this.Disposed += new EventHandler(UC技术员售后表_Disposed);
        }

        void UC技术员售后表_Disposed(object sender, EventArgs e)
        {
            if (threadDraw != null) { threadDraw.Abort(); threadDraw = null; }
        }

        private void UC技术员售后时间绘图_Load(object sender, EventArgs e)
        {
            //threadDraw = new Thread(but绘制);//实例化线程
            //threadDraw.Start();//开始线程
        }

        void but绘制(object o)
        {
            while (true)//线程多少秒刷新一次
            {
                //DataTable dtjs = data技术员();
                //Draw(dtjs);
                Thread.Sleep(1000 * 50);
            }

        }
        void get_list()
        {
            Bitmap b = draw基本图片();
            //将颜色时间存在集合里面
            string sql = "select ServicesEmployee_ENO,ServicesList_ExpectStart,ServicesList_ExpectEnd,ServicesList_Speed from ServicesList,ServicesEmployee";
            sql += " where ServicesList_NO=ServicesEmployee_SLNO and ServicesList_IsSolve=0 and ServicesEmployee_ENO='ENO20150603003000030142'";
            DataTable dt = new DataTable();
            dt = DataSystem.ClassData.GetDataSet(sql).Tables[0];
            List<Class技术员状态> jslist = new List<Class技术员状态>();
            Color color = color空闲;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["ServicesList_Speed"].ToString())
                {
                    case "0":
                        color = color排班;
                        break;
                    case "20":
                        color = color出发;
                        break;
                    case "31":
                        color = color延迟出发;
                        break;
                    default:
                        color = color空闲;
                        break;
                    
                }
                jslist.Add(new Class技术员状态(color, DateTime.Parse(dt.Rows[i]["ServicesList_ExpectStart"].ToString()), DateTime.Parse(dt.Rows[i]["ServicesList_ExpectEnd"].ToString())));
            }


            Bitmap b2 = draw颜色图片(b, jslist);

            this.pictureBox1.Image = b2;
        }
        /// <summary>
        /// 绘制基本图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button绘制_Click(object sender, EventArgs e)
        {

            get_list();


        }
        //<summary>
        //开始画图
        //</summary>
        Bitmap draw基本图片()
        {
            int housSub = housmax - housmin;
            Bitmap b = new Bitmap(housSub * rectangleWidth + rectangleEndWidth, rectangleHeight);//定义一个图片b的大小
            Graphics g = Graphics.FromImage(b);//绘图面板
            g.Clear(Color.Transparent);//Transparent空,让图片没被挖空的部分颜色为Transparent
            for (int i = 0; i < housSub; i++)
            {
                Rectangle r = new Rectangle(i * rectangleWidth, 0, rectangleWidth, rectangleHeight);
                g.DrawImage(YXServicesSystem.Properties.Resources.刻度, r);//这是一张显示部分被挖空了的图片
                Rectangle r0 = new Rectangle(housSub * rectangleWidth, 0, rectangleEndWidth, rectangleHeight);
                g.DrawImage(YXServicesSystem.Properties.Resources.尾部, r0);
            }

            return b;//返回没有被修改过,底色全部一样的图(基本图)
        }
        /// <summary>
        /// 定义一个返回图的方法来绘制颜色(改变基本图颜色)
        /// </summary>
        /// <param name="baseImgae">给方法一张基本图</param>
        /// <param name="js">传一个集合,我们要处理的状态(颜色,时间)</param>
        /// <returns></returns>
        Bitmap draw颜色图片(Bitmap baseImgae, List<Class技术员状态> js)
        {

            Bitmap b = new Bitmap(baseImgae.Width, baseImgae.Height);
            Graphics g = Graphics.FromImage(b);//绘图面板

            DateTime time = DateTime.Now.Date;

 

            g.Clear(color空闲);//让画板的底色为Green,这样就让为空的图片显示出为空的部分颜色为Green
            for (int i = 0; i < js.Count; i++)
            {

                Color color_1 = js[i].ColorName;
                DateTime timemin = js[i].StartTime;
                DateTime timemax = js[i].EndTime;
                int start = (timemin.Hour - time.Hour - housmin) * 118;
                MessageBox.Show(start.ToString());
                double sum = timemax.Subtract(timemin).TotalMinutes * 2;

                if (start < 0) { start = 0; }
                if (start > baseImgae.Width) { start = baseImgae.Width; }
                Rectangle r = new Rectangle(start, 0, (int)sum, 15);//定义一个矩形
                g.FillRectangle(new SolidBrush(color_1), r);
            }

            g.DrawImage(baseImgae, 0, 0, baseImgae.Width, baseImgae.Height);
            return b;
        }
    }
    /// <summary>
    /// 定义一个类接收颜色和时间
    /// </summary>
    class Class技术员状态
    {
        public Color ColorName = Color.Black;
        public DateTime StartTime = DateTime.Now;
        public DateTime EndTime = DateTime.Now;

        public Class技术员状态()
        {
        }
        /// <summary>
        /// 构造器 构造一个方法接收颜色和时间
        /// </summary>
        /// <param name="ColorName"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        public Class技术员状态(Color ColorName, DateTime StartTime, DateTime EndTime)
        {
            this.ColorName = ColorName;
            this.StartTime = StartTime;
            this.EndTime = EndTime;
        }
    }
}

posted @ 2016-01-28 11:18  有妖气  阅读(159)  评论(0编辑  收藏  举报