用Graphics绘制柱状图

来源:互联网 发布:双卡手机都是4g网络吗 编辑:程序博客网 时间:2024/04/28 14:32
 HuoClassLibrary.DbHelperOleDb db = new HuoClassLibrary.DbHelperOleDb("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath("/db/jbh_newyear.mdb"));        public string[] X = new string[7];        public string[] Y = new string[7];        public void ProcessRequest(HttpContext context)        {            GetTime();            Bitmap bm = new Bitmap(700, 350);            Graphics g = Graphics.FromImage(bm);            g.Clear(Color.Snow);            g.DrawString(" 杭州骏宝行抽奖码报名详情", new Font("黑体", 16), Brushes.Black, new Point(220, 15));            Point myRec = new Point(600,30);            Point myDec = new Point(835, 700);            g.DrawString("单位:个", new Font("宋体", 11), Brushes.Black, new Point(615, 20));            for (int i = 0; i < X.Length; i++)            {                if (Y[i] != null)                {                    if (Y[i] != "0")                    {                        //绘制小方块                        g.DrawRectangle(Pens.Black, myRec.X, myRec.Y + 10, 20, 10);                        //填充小方块                        g.FillRectangle(new SolidBrush(GetColor(i)), myRec.X, myRec.Y + 10, 20, 10);                        if (X[i] != null)                        {                            g.DrawString(X[i].ToString(), new Font("宋体", 9), Brushes.Black, new Point(623, myRec.Y + 10));                        }                    }                }                myDec.Y += 15;                myRec.Y += 15;            }            int iBarWidth = 70;            int scale = 1;            for (int i = 0; i <Y.Length; i++)            {                if (Y[i] != null)                {                    if (Y[i] != "0")                    {                        int width=(Convert.ToInt32(Y[i].ToString()) * scale) / 5 + 5;                        int y = 350 - (Convert.ToInt32(Y[i].ToString()) * scale) /5;                        g.DrawRectangle(Pens.Black, (i * iBarWidth) + 50, y, 20, width);                        //绘制Bar图                        g.FillRectangle(new SolidBrush(GetColor(i)), (i * iBarWidth) + 50, y, 20, width);                        //以指定的色彩填充Bar图                        g.DrawString(Y[i].ToString(), new Font("宋体", 9), Brushes.Black, (i * iBarWidth) + 50, y-25);                    }                }            }            Pen p = new Pen(Color.Black, 2);            g.DrawRectangle(Pens.Black, 1, 1, 698, 348);            bm.Save(context.Response.OutputStream, ImageFormat.Gif);        }        public Color GetColor(int i)        {            if (i % 7 == 0)            {                return Color.Red;            }            if (i % 7 == 1)            {                return Color.Blue;            }            if (i % 7 == 2)            {                return Color.DarkSlateGray;            }            if (i % 7 == 3)            {                return Color.Gray;            }            if (i % 7 == 4)            {                return Color.Green;            }            if (i % 7 == 5)            {                return Color.Pink;            }            if (i % 7 == 6)            {                return Color.PapayaWhip;            }            else            {                return Color.Gold;            }        }        //获取时间列表        public void GetTime()        {            DateTime time = Convert.ToDateTime("2015/2/1");            DateTime endtime = Convert.ToDateTime("2015/2/7");            int i = 0;            while (time <= endtime)            {                X[i] = time.ToString("yyyy/M/d");                string sql = "select * from active_play where dttime like '%2015/2/" + (i + 1) + "%' and tid>1";                var dt = db.ExecuteQuery(sql);                Y[i] = dt.Rows.Count.ToString();                i++;                time = time.AddDays(1);            }        }        public bool IsReusable        {            get            {                return false;            }        }



0 0
原创粉丝点击