`

图表的绘制

J# 
阅读更多
//图表的绘制兵保存为gif格式
  int height = 440, width = 600;
            Bitmap image = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(image);        
            g.Clear(Color.White);
            Font font = new Font("Arial", 9, FontStyle.Regular);
            Font font1 = new Font("宋体", 20, FontStyle.Regular);
            Font font2 = new Font("Arial", 8, FontStyle.Regular);

            try
            {
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
                g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
                Brush brush1 = new SolidBrush(Color.Blue);
                Brush brush2 = new SolidBrush(Color.SaddleBrown);
                g.DrawString("各年份的销售量", font1, brush1, new PointF(130, 30));
                g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
                Pen mypen = new Pen(brush, 1);
                Pen mypen2 = new Pen(Color.Red, 2);
                int x = 60;
                for (int i = 0; i < 12; i++)
                {
                    g.DrawLine(mypen, x, 80, x, 340);
                    x = x + 40;
                }
                Pen mypen1 = new Pen(Color.Blue, 2);
                g.DrawLine(mypen1, x - 480, 80, x - 480, 340);
                int y = 106;
                for (int i = 0; i < 9; i++)
                {
                    g.DrawLine(mypen, 60, y, 540, y);
                    y = y + 26;
                }
                g.DrawLine(mypen1, 60, y, 540, y);
                String[] n = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
                x = 55;
                for (int i = 0; i < 12; i++)
                {
                    g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348);
                    x = x + 40;
                }
                String[] m = { "3000", "6000", "9000", "12000", "15000", "18000", "21000", "24000", "27000" };
                y = 100;
                for (int i = 8; i >= 0; i--)
                {
                    g.DrawString(m[i].ToString(), font, Brushes.Red, 25, y);
                    y = y + 26;
                }
                int[] count = new int[4];
                for (int j = 0; j < 4; j++)
                {
                    count[j] = Convert.ToInt32(dtTable.Rows[j][1].ToString()) * 26 / 3000;
                }
                SolidBrush mybrush = new SolidBrush(Color.Red);
                Point[] points = new Point[4];
                points[0].X = 60; points[0].Y = 340 - count[0];
                points[1].X = 100; points[1].Y = 340 - count[1];
                points[2].X = 140; points[2].Y = 340 - count[2];
                points[3].X = 180; points[3].Y = 340 - count[3];
                g.DrawLines(mypen2, points);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                string ImagePath = (Server.MapPath(".")) + "\\Images//ShowData.gif";
                FileStream fs = new FileStream(ImagePath, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Flush();
                bw.Write(ms.ToArray());
                bw.Close();
                fs.Close();
                string s = "<img src='./Images/ShowData.gif   '/>";                         
                //Response.ClearContent();
                //Response.ContentType = "image/Gif";
                //Response.BinaryWrite(ms.ToArray()); 
                //string s = "<img src='"+im+" '/>"; 
                PlaceHolder1.Controls.Add (new LiteralControl (s));  
            }
            catch (Exception ms)
            {
                throw (ms);
            }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics