★搜Asp.net★(www.soAsp.net),为专业技术文档网站。
包括Asp.net开发技术文档·C#开发技术文档·Access/SQL Server数据库开发技术文档·VB.NET开发技术文档。
还包括·项目实战经验总结·开发经验技巧总结·项目开发心得。
柱型图分析小轿车月销售比例

   柱型图也称为条形图,它是网站开发中最常用的一种图表技术。本节通过一个实例来介绍,如何使用柱型图分析小轿车月销售情况。

程序开发步骤:

1)新建一个网站,其主页默认为Default.aspx

2)在该网站中添加一个Info.aspx页面,该页面主要用来使用柱型图显示各月份的小轿车销售情况。

3)程序主要代码如下。

       Info.aspx页面中定义了两个方法,分别为sumNum方法和CreateImage方法,其中sumNum方法用来计算各月份的轿车销售量总和,而CreateImage方法则用来调用Graphics对象的相关方法绘制柱型图。sumNum方法实现代码如下:

private int sumNum(int P_int_year)
    {
        string P_str_sum = "SELECT SUM(month1+month2+month3+month4+month5+month6+month7+month8+month9+month10+month11+month12) AS number FROM tb_01 WHERE yearID=" + P_int_year + "";
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
        SqlDataAdapter myda = new SqlDataAdapter(P_str_sum, sqlcon);
        DataSet myds = new DataSet();
        myda.Fill(myds);
        return Convert.ToInt32(myds.Tables[0].Rows[0][0].ToString());
    }
CreateImage
方法实现代码如下:

    private void CreateImage(int P_int_year)
    {
        int height = 400, width = 600;
        Bitmap image = new Bitmap(width, height);
        Graphics graphics = Graphics.FromImage(image);        //创建Graphics类对象
        try
        {
            graphics.Clear(Color.White);            //清空图片背景色
            Font font = new Font("Arial", 9, FontStyle.Regular);
            Font font1 = new Font("宋体", 20, FontStyle.Regular);
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
            graphics.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
            Brush brush1 = new SolidBrush(Color.Blue);
            graphics.DrawString("" + P_int_year + "小轿车月销售情况比例图", font1, brush1, new PointF(150, 30));
            //画图片的边框线
            graphics.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++)
            {
                graphics.DrawLine(mypen, x, 80, x, 366);
                x = x + 40;
            }
            Pen mypen1 = new Pen(Color.Blue, 2);
            graphics.DrawLine(mypen1, x - 480, 80, x - 480, 366);
            int y = 106;            //绘制纵向线条
            for (int i = 0; i < 10; i++)
            {
                graphics.DrawLine(mypen, 60, y, 540, y);
                y = y + 26;
            }
            graphics.DrawLine(mypen1, 60, y, 540, y);
            //x轴
        String[] n = {"  一月", "  二月", "  三月", "  四月", "  五月", "  六月", "  七月",
                     "  八月", "  九月", "  十月", "十一月", "十二月"};
            x = 60;
            for (int i = 0; i < 12; i++)
            {//设置文字内容及输出位置
                graphics.DrawString(n[i].ToString(), font, Brushes.Red, x, 374); 
                x = x + 40;
            }
            //y轴
            String[] m = {"100%", " 90%", " 80%", " 70%", " 60%", " 50%", " 40%", " 30%",
                     " 20%", " 10%", "  0%"};
            y = 98;
            for (int i = 0; i < 11; i++)
            {//设置文字内容及输出位置
                graphics.DrawString(m[i].ToString(), font, Brushes.Red, 25,y); 
                y = y + 26;
            }
            int[] Count = new int[12];
            string P_str_yearID = "SELECT * FROM tb_01 WHERE yearID=" + P_int_year + "";
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
            SqlDataAdapter myda = new SqlDataAdapter(P_str_yearID, sqlcon);
            DataSet myds = new DataSet();
            myda.Fill(myds);
            int P_int_num = sumNum(P_int_year);
            for (int j = 0; j < 12; j++)
            {
                Count[j] = Convert.ToInt32(myds.Tables[0].Rows[0][j + 1].ToString()) * 100 / P_int_num;
            }
            x = 70;            //显示柱状效果
            for (int i = 0; i < 12; i++)
            {
                SolidBrush mybrush = new SolidBrush(Color.Red);
                graphics.FillRectangle(mybrush, x, 366 - Count[i] * 26 / 10, 20, Count[i] * 26 / 10);
                x = x + 40;
            }
            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
            image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(MStream.ToArray());
        }
        finally
        {
            graphics.Dispose();
            image.Dispose();
        }
    } 

无标题页

无标题页
网站主页 | 广告业务 | 责任声明 | 联系我们 | 关于本站
2008 soAsp.net 使用前必读 京ICP备08009904号
本站为公益性网站,提供开发技术参考文章,不代表本站观点,不保证文章正确性。