图形绘制之二: 折线图

本文介绍了一种使用C#绘制1997年至2006年出生人口性别比例图表的方法,通过数据库获取数据并利用GDI+进行图表绘制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
        this.CreateImage();
    }
    private void CreateImage()
    {
        int height = 440, width = 600;
        Bitmap image = new Bitmap(width, height);
        Graphics g = Graphics.FromImage(image);

        try
        {
            //清空图片背景色
            g.Clear(Color.White);

            Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
            Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
            Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
            LinearGradientBrush brush = new 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("1997年-2006年出生人口的男女比例", 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);
            Pen mypen2 = new Pen(Color.Red, 2);
            //绘制线条
            //绘制纵向线条
            int x = 60;
            for (int i = 0; i < 10; i++)
            {
                g.DrawLine(mypen, x, 80, x, 340);
                x = x + 50;
            }
            Pen mypen1 = new Pen(Color.Blue, 2);
            g.DrawLine(mypen1, x - 500, 80, x - 500, 340);

            //绘制横向线条
            int y = 106;
            for (int i = 0; i < 9; i++)
            {
                g.DrawLine(mypen, 60, y, 560, y);
                y = y + 26;
            }
            g.DrawLine(mypen1, 60, y, 560, y);

            //x轴
            String[] n = {"1997年", "1998年", "1999年", "2000年", "2001年", "2002年",
                     "2003年", "  2004年", "2005年", "2006年"};
            x = 45;
            for (int i = 0; i < 10; i++)
            {
                g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置
                x = x + 50;
            }

            //y轴
            String[] m = {"2200人", " 2000人", " 1800人", "1600人", " 1400人", " 1200人", " 1000人", " 800人",
                     " 600人"};
            y = 106;
            for (int i = 0; i < 9; i++)
            {
                g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //设置文字内容及输出位置
                y = y + 26;
            }

            int[] Count1 = new int[10];
            int[] Count2 = new int[10];
            SqlConnection Con = new SqlConnection(ConfigurationManager.AppSettings["ConSql"]);
            Con.Open();
            string cmdtxt2 = "SELECT * FROM tb_13_55 WHERE Year<=2006 and Year>=1997";
            SqlDataAdapter da = new SqlDataAdapter(cmdtxt2 ,Con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            for (int j = 0; j < 10; j++)
            {
                if (ds.Tables[0].Rows.Count == 0)
                {
                    Count1[j] = 0;
                }
                else
                {
                    Count1[j] = Convert.ToInt32(ds.Tables [0].Rows [j][0].ToString ()) * 13 / 100;
                }
            }
            for (int k = 0; k < 10; k++)
            {
                if (ds.Tables[0].Rows.Count == 0)
                {
                    Count2[k] = 0;
                }
                else
                {
                    Count2[k] = Convert.ToInt32(ds.Tables[0].Rows[k][1].ToString()) * 13 / 100;
                }
            }

            //显示折线效果
            SolidBrush mybrush = new SolidBrush(Color.Red);
            Point[] points1 = new Point[10];
            points1[0].X = 60; points1[0].Y = 390 - Count1[0];
            points1[1].X = 110; points1[1].Y = 390 - Count1[1];
            points1[2].X = 160; points1[2].Y = 390 - Count1[2];
            points1[3].X = 210; points1[3].Y = 390 - Count1[3];
            points1[4].X = 260; points1[4].Y = 390 - Count1[4];
            points1[5].X = 310; points1[5].Y = 390 - Count1[5];
            points1[6].X = 360; points1[6].Y = 390 - Count1[6];
            points1[7].X = 410; points1[7].Y = 390 - Count1[7];
            points1[8].X = 460; points1[8].Y = 390 - Count1[8];
            points1[9].X = 510; points1[9].Y = 390 - Count1[9];
            g.DrawLines(mypen2, points1);  //绘制折线

            Pen mypen3 = new Pen(Color.Black, 2);
            Point[] points2 = new Point[10];
            points2[0].X = 60; points2[0].Y = 390 - Count2[0];
            points2[1].X = 110; points2[1].Y = 390 - Count2[1];
            points2[2].X = 160; points2[2].Y = 390 - Count2[2];
            points2[3].X = 210; points2[3].Y = 390 - Count2[3];
            points2[4].X = 260; points2[4].Y = 390 - Count2[4];
            points2[5].X = 310; points2[5].Y = 390 - Count2[5];
            points2[6].X = 360; points2[6].Y = 390 - Count2[6];
            points2[7].X = 410; points2[7].Y = 390 - Count2[7];
            points2[8].X = 460; points2[8].Y = 390 - Count2[8];
            points2[9].X = 510; points2[9].Y = 390 - Count2[9];
            g.DrawLines(mypen3, points2);  //绘制折线

            //绘制标识
            g.DrawRectangle(new Pen(Brushes.Red), 150, 370, 250, 50);  //绘制范围框
            g.FillRectangle(Brushes.Red, 250, 380, 20, 10);  //绘制小矩形
            g.DrawString("女孩", font2, Brushes.Red, 270, 380);
            g.FillRectangle(Brushes.Black, 250, 400, 20, 10);
            g.DrawString("男孩", font2, Brushes.Black, 270, 400);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }
}

转载于:https://www.cnblogs.com/ziyiFly/archive/2008/09/04/1283832.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值