说明:此示例代码在我做的一个项目中 不过还是可以学习一下
一:直角坐标系显示数据
先看效果图:
代码:
private List<int> Conuts = new List<int>(); public List<int> Conuts1 { get { return Conuts; } set { Conuts = value; } } protected void Page_Load(object sender, EventArgs e) { StaffSystemDataContext dc = new StaffSystemDataContext(); var Deps = dc.PropertyMappings.Where(p => p.PropertyName.Equals("DepartmentID")); foreach (var dep in Deps) { var Pros = dc.Projects.Where(p => p.DepartmentID.Equals(dep.PropertyValue)); Conuts1.Add(Pros.Count()); } //画图初始化 Bitmap bMap = new Bitmap(500, 500); Graphics gph = Graphics.FromImage(bMap); gph.Clear(Color.White); //中心点 PointF cPt = new PointF(40, 420); //X轴三角形 PointF[] xPt = new PointF[3] { new PointF(cPt.Y + 15, cPt.Y), new PointF(cPt.Y, cPt.Y - 8), new PointF(cPt.Y, cPt.Y + 8) }; //Y轴三角形 PointF[] yPt = new PointF[3] { new PointF(cPt.X, cPt.X - 15), new PointF(cPt.X - 8, cPt.X), new PointF(cPt.X + 8, cPt.X) }; //图表标题 gph.DrawString("部门项目汇总图表", new Font("宋体", 14), Brushes.Black, new PointF(cPt.X + 60, cPt.X)); //画X轴 gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y); gph.DrawPolygon(Pens.Black, xPt); gph.FillPolygon(new SolidBrush(Color.Black), xPt); gph.DrawString("部门", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y + 10, cPt.Y + 10)); //画Y轴 gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X); gph.DrawPolygon(Pens.Black, yPt); gph.FillPolygon(new SolidBrush(Color.Black), yPt); gph.DrawString("项目数", new Font("宋体", 12), Brushes.Black, new PointF(0, 7)); //画Y轴刻度 for (int i = 1; i <= 10; i++) { gph.DrawString(i.ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6)); gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30); } //画X轴项目 int a = 1; foreach (var dep in Deps) { gph.DrawString(dep.PropertyMeaning.Substring(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + a * 30 - 5, cPt.Y + 5)); gph.DrawString(dep.PropertyMeaning.Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + a * 30 - 5, cPt.Y + 20)); gph.DrawString(dep.PropertyMeaning.Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + a * 30 - 5, cPt.Y + 35)); a++; } for (int i = 1; i <= Deps.Count(); i++) { //画点 gph.DrawEllipse(Pens.Black, cPt.X + i * 30 - 1.5F, cPt.Y - Conuts1[i - 1] * 30 - 1.5F, 3, 3); gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30 - 1.5F, cPt.Y - Conuts1[i - 1] * 30 - 1.5F, 3, 3); //画数值 gph.DrawString(Conuts1[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30, cPt.Y - Conuts1[i - 1] * 30)); //画折线 if (i > 1) gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - Conuts1[i - 2] * 30, cPt.X + i * 30, cPt.Y - Conuts1[i - 1] * 30); } System.IO.MemoryStream ms = new System.IO.MemoryStream(); bMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.ClearContent(); Response.ContentType = "image/Png"; Response.BinaryWrite(ms.ToArray()); gph.Dispose(); bMap.Dispose(); Response.End(); }
注意:这里的坐标都是从左上角开始算 而且必须使用最后的一小段代码将其转化为图片
二:绘制圆形效果
StaffSystemDataContext dc = new StaffSystemDataContext(); var Deps = dc.PropertyMappings.Where(p => p.PropertyName.Equals("DepartmentID")); //创建画图对象 int width = 800, height = 800; Bitmap bitmap = new Bitmap(width, height); Graphics g = Graphics.FromImage(bitmap); try { //清空背景色 g.Clear(Color.White); Pen pen1 = new Pen(Color.Red); Brush brush1 = new SolidBrush(Color.PowderBlue); Brush brush2 = new SolidBrush(Color.Blue); Brush brush3 = new SolidBrush(Color.Wheat); Brush brush4 = new SolidBrush(Color.Orange); Brush brush5 = new SolidBrush(Color.Gray); Brush brush6 = new SolidBrush(Color.Red); Brush brush7 = new SolidBrush(Color.White); Brush[] brs = new Brush[] { brush1, brush2, brush3, brush4, brush5, brush6 }; Font font1 = new Font("Courier New", 16, FontStyle.Bold); Font font2 = new Font("Courier New", 8); g.FillRectangle(brush7, 0, 0, width, height); //绘制背景图 g.DrawString("员工部门比例饼形图", font1, brush2, new Point(80, 20)); //书写标题 int piex = 100, piey = 60, piew = 200, pieh = 200, sum = 0; var Pros = dc.Projects.Where(p => p.DepartmentID != null); sum = Pros.Count(); int a = 0; float j = 0; foreach (var dep in Deps) { var Prods = dc.Projects.Where(p => p.DepartmentID.Equals(dep.PropertyValue)); //计算角度 float angle = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(Prods.Count())); //绘制员工所占比例 //FieldInfo fi = type.GetField("ddd" + i.ToString()); //temp += fi.GetValue(this).ToString(); //Type type = this.GetType(); //Brush fi = (Brush)type.GetField("brush" + a.ToString()); g.FillPie(brs[a], piex, piey, piew, pieh, j, angle); a++; j = j + angle; } //绘制标识 g.DrawRectangle(pen1, 50, 280, 400, 300); //绘制范围框 //绘制小矩形 int le = 320, b = 0; foreach (var dep in Deps) { var Prods = dc.Projects.Where(p => p.DepartmentID.Equals(dep.PropertyValue)); g.FillRectangle(brs[b], 90, le, 20, 10); g.DrawString("" + dep.PropertyMeaning + "员工占公司总人数比例:" + Convert.ToSingle(Prods.Count()) * 100 / Convert.ToSingle(sum) + "%", font2, brs[b], 120, le); le += 40; b++; } } catch (Exception ex) { throw new Exception("有错!!!"); } System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.ClearContent(); Response.ContentType = "image/Png"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); bitmap.Dispose(); Response.End();
时间: 2024-10-13 01:16:49