ASP.NET画图全攻略(上) (3)
点击次数:13 次 发布日期:2008-11-26 12:29:04 作者:源代码网
|
源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 //设置边 源代码网推荐 源代码网推荐 g.DrawLine(new Pen(Color.Black,2),new Point(CHART_LEFT,CHART_TOP),new Point(CHART_LEFT,CHART_TOP + CHART_HEIGHT)); 源代码网推荐 源代码网推荐 g.DrawLine(new Pen(Color.Black,2),new Point(CHART_LEFT,CHART_TOP + CHART_HEIGHT),new Point(CHART_LEFT + CHART_WIDTH,CHART_TOP + CHART_HEIGHT)); 源代码网推荐 源代码网推荐 //画图例框和文字 源代码网推荐 源代码网推荐 g.DrawRectangle(new Pen(Color.Black,1),200,300,199,99); 源代码网推荐 源代码网推荐 g.DrawString("Legend",new Font("Tahoma",12,FontStyle.Bold),Brushes.Black,new PointF(200,300)); 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 //画图例 源代码网推荐 源代码网推荐 PointF boxOrigin = new PointF(210,330); 源代码网推荐 源代码网推荐 PointF textOrigin = new PointF(235,326); 源代码网推荐 源代码网推荐 for(int i=0;i<dt.Rows.Count;i++) 源代码网推荐 源代码网推荐 { 源代码网推荐 源代码网推荐 g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10); 源代码网推荐 源代码网推荐 g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10); 源代码网推荐 源代码网推荐 g.DrawString(dt.Rows[i][0].ToString() + " - " + dt.Rows[i][1].ToString(),new Font("Tahoma",10),Brushes.Black,textOrigin); 源代码网推荐 源代码网推荐 boxOrigin.Y += 15; 源代码网推荐 源代码网推荐 textOrigin.Y += 15; 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 //输出图形 源代码网推荐 源代码网推荐 bm.Save(target, ImageFormat.Gif); 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 //资源回收 源代码网推荐 源代码网推荐 bm.Dispose(); 源代码网推荐 源代码网推荐 g.Dispose(); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 public class ChartUtil 源代码网推荐 源代码网推荐 { 源代码网推荐 源代码网推荐 public ChartUtil() 源代码网推荐 源代码网推荐 { 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 public static Color GetChartItemColor(int itemIndex) 源代码网推荐 源代码网推荐 { 源代码网推荐 源代码网推荐 Color selectedColor; 源代码网推荐 源代码网推荐 switch(itemIndex) 源代码网推荐 源代码网推荐 { 源代码网推荐 源代码网推荐 case 0: 源代码网推荐 源代码网推荐 selectedColor = Color.Blue; 源代码网推荐 源代码网推荐 break; 源代码网推荐 源代码网推荐 case 1: 源代码网推荐 源代码网推荐 selectedColor = Color.Red; 源代码网推荐 源代码网推荐 break; 源代码网推荐 源代码网推荐 case 2: 源代码网推荐 源代码网推荐 selectedColor = Color.Yellow; 源代码网推荐 源代码网推荐 break; 源代码网推荐 源代码网推荐 case 3: 源代码网推荐 源代码网推荐 selectedColor = Color.Purple; 源代码网推荐 源代码网推荐 break; 源代码网推荐 源代码网推荐 default: 源代码网推荐 源代码网推荐 selectedColor = Color.Green; 源代码网推荐 源代码网推荐 break; 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 return selectedColor; 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 代码分析: 源代码网推荐 源代码网推荐 1.引入一些namespace 源代码网推荐 源代码网推荐 using System; 源代码网推荐 源代码网推荐 using System.IO;//用于文件存取 源代码网推荐 源代码网推荐 using System.Data;//用于数据访问 源代码网推荐 源代码网推荐 using System.Drawing;//提供画GDI+图形的基本功能 源代码网推荐 源代码网推荐 using System.Drawing.Text;//提供画GDI+图形的高级功能 源代码网推荐 源代码网推荐 using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能 源代码网推荐 源代码网推荐 using System.Drawing.Imaging;//提供画GDI+图形的高级功能 源代码网推荐 源代码网推荐 这些namespace将在后面被应用。 源代码网推荐 源代码网推荐 2.自定义一个namespace为Insight_cs.WebCharts,其中包括了两个类PieChart和BarChart,很清楚,class PieChart是为画饼图而建,class BarChart是为画条形图而建。由于class PieChart和class BarChar差不多,所以下面我们以饼图为例,进行代码分析。 源代码网推荐 源代码网推荐 3.类PieChart建立一个方法Render,此方法可以含一些参数。简单说明如下: 源代码网推荐 源代码网推荐 参数title,表示饼图上方的大标题文字。 源代码网推荐 源代码网推荐 参数subtitle,表示饼图上方的小标题文字。 源代码网推荐 源代码网推荐 参数width,height,表示了整个图形的大小。 源代码网推荐 源代码网推荐 参数charData是一个DataSet对象实例,用于画图使用。 源代码网推荐 源代码网推荐 参数target是Stream对象的实例,用于图形输出时使用。 源代码网推荐 源代码网推荐 4.为了增加可读性,定义一些常量: 源代码网推荐 源代码网推荐 const int SIDE_LENGTH = 400;//画布边长 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
