.NET 页面中用代码画图用来表示百分比例[WEB学习示例]

本文介绍了一个使用C#绘制简单统计图表的方法。通过创建Bitmap对象并利用Graphics类进行绘图,包括填充背景、绘制文本、线条及矩形等元素,实现了一个基本的条形统计图表。

protected void Page_Load(object sender, EventArgs e)
    {

        int height = 600, width = 600;
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(height, width);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.Wheat);
        Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
        Font font1 = new System.Drawing.Font("隶书", 20, FontStyle.Regular);
        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.WhiteSmoke, 0, 0, width, height);
        Brush brush1 = new SolidBrush(Color.Blue);
        g.DrawString("" + "统计统计统计统计统计", font1, brush1, new PointF(100, 30));
        g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
        Pen mypen = new Pen(brush, 1);
        int x = 100;
        for (int i = 0; i < 11; 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 = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
        x = 62;
        for (int i = 0; i < 12; i++)
        { g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); x = x + 40; }
        string[] m = { "100%", "90%", "80%", "70%", "60%", "50%", "40%", "30%", "20%", "10%", "0%" };
        y = 85;
        for (int i = 0; i < 11; i++)
        { g.DrawString(m[i].ToString(), font, Brushes.Red, 25, y); y = y + 26; }
        //
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        Response.ClearContent();
        Response.BinaryWrite(ms.ToArray());
       
    }

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值