Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:29:08 GDI+ 绘制折线图分析网站流量
GDI+ 绘制折线图分析网站流量
绘制折线图分析网站流量折线图是网站开发中经常用到的一种图表技术,折线图可以很直观地反映出相关数据的变化趋势。本节通过一个实例来介绍如何使用折线图分析网站的流量。实例运行结果如图1所示。程序开发步骤如下所示。(1)新建一个网站,命名为26_03,其主页默认为Default.aspx。(2)在该网站中添加一个Info.aspx页面,该页面主要用来使用折线图显示各月份的网站访问人数。(3)程序主要代码如下。图1 折线图分析网站流量Info.aspx页面中定义了一个方法CreateImage,该方法主要用来从数据库中提取数据,并根据提取的数据,调用Graphics对象的相关方法绘制一张反映网站流量情况的折线图。CreateImage方法实现代码如下:private void CreateImage(int P_int_year){int height = 400, width = 600;Bitmap image = new Bitmap(width, height);Graphics graphics = Graphics.FromImage(image);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);string P_str_sum = "SELECT * FROM tb_03 WHERE yearID=" + P_int_year + "";SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);sqlcon.Open();SqlCommand sqlcom = new SqlCommand(P_str_sum, sqlcon);SqlDataReader sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection);sqlread.Read();if (sqlread.HasRows){graphics.DrawString("" + P_int_year + "年各月份网站访问人数", font1, brush1, new PointF(130, 30));}sqlread.Close();//画图片的边框线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, 340);x = x + 40;}Pen mypen1 = new Pen(Color.Blue, 2);Pen mypen2 = new Pen(Color.Red, 2);graphics.DrawLine(mypen1, x - 480, 80, x - 480, 340);//绘制纵向线条int y = 106;for (int i = 0; i < 9; i++){graphics.DrawLine(mypen, 60, y, 540, y);y = y + 26;}graphics.DrawLine(mypen1, 60, y, 540, y);//x轴String[] n = {" 一月", " 二月", " 三月", " 四月", " 五月", " 六月", " 七月"," 八月", " 九月", " 十月", "十一月", "十二月"};x = 35;for (int i = 0; i < 12; i++){graphics.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置x = x + 40;}//y轴String[] m = { " 900人", " 800人", " 700人", " 600人", " 500人", " 400人", " 300人"," 200人", " 100人", " 0人"};y = 98;for (int i = 0; i < 10; i++){graphics.DrawString(m[i].ToString(), font, Brushes.Red, 20, y); //设置文字内容及输出位置y = y + 26;}int[] Count = new int[12];string P_str_yearID = "SELECT * FROM tb_03 WHERE yearID=" + P_int_year + "";SqlDataAdapter myda = new SqlDataAdapter(P_str_yearID, sqlcon);DataSet myds = new DataSet();myda.Fill(myds);for (int j = 0; j < 12; j++){Count[j] = Convert.ToInt32(myds.Tables[0].Rows[0][j + 1].ToString()) * 26 / 100;}SolidBrush mybrush = new SolidBrush(Color.Red);Point[] myPoint = new Point[12];myPoint[0].X = 60; myPoint[0].Y = 340 - Count[0];myPoint[1].X = 100; myPoint[1].Y = 340 - Count[1];myPoint[2].X = 140; myPoint[2].Y = 340 - Count[2];myPoint[3].X = 180; myPoint[3].Y = 340 - Count[3];myPoint[4].X = 220; myPoint[4].Y = 340 - Count[4];myPoint[5].X = 260; myPoint[5].Y = 340 - Count[5];myPoint[6].X = 300; myPoint[6].Y = 340 - Count[6];myPoint[7].X = 340; myPoint[7].Y = 340 - Count[7];myPoint[8].X = 380; myPoint[8].Y = 340 - Count[8];
赞