C# 画曲线图

C#画曲线图思路:因为画图时是以左上角为起点,所以曲线画出之后是刚好是反方向

1。画曲线图(画完后旋转到正方向)

2。画背景网格

3。合并图层

 

 Response.Clear(); // 加载测试数据 DataTable tbl = new DataTable(); tbl.Columns.Add("price"); tbl.Columns.Add("count"); Random ran = new Random(); DataRow row = null; int count = 0; int price = 0; for (int i = 0; i < 10; i++) { row = tbl.NewRow(); row["price"] = ran.Next(0, 250); count = ran.Next(0, 800); if (i != 0) { if (count < Convert.ToInt32(tbl.Rows[i - 1]["count"])) { i--; continue; } } row["count"] = count; tbl.Rows.Add(row); } int width = 800; int height = 270; // 画曲线 Bitmap bmp = new Bitmap(width, height); Pen pen = new Pen(Color.Black); Graphics grap = Graphics.FromImage(bmp); Point[] points = new Point[tbl.Rows.Count]; int _x; int _y; // 曲线坐标数组 for (int i = 0; i < tbl.Rows.Count; i++) { int.TryParse(tbl.Rows[i]["price"].ToString(), out _x); int.TryParse(tbl.Rows[i]["count"].ToString(), out _y); points[i] = new Point(_y, _x); } grap.DrawLines(pen, points); // 画曲线 bmp.RotateFlip(RotateFlipType.Rotate180FlipX); // 反转图片 // 画背景 Pen penbg = new Pen(Color.Black, 1); Bitmap bmpbg = new Bitmap(width, height); Graphics grapbg = Graphics.FromImage(bmpbg); // 画横轴 for (int j = 0, i = 5; i >= 0; i--, j++) { int var = i * 50; grapbg.DrawString(Convert.ToString(j * 50), new Font("宋体", 12), Brushes.Black, new PointF(10, var - 3)); grapbg.DrawLine(penbg, new Point(45, var), new Point(800, var)); } // 画纵轴 for (int j = 0, i = 0; i < 8; i++, j++) { int var = i * 100 + 45; grapbg.DrawLine(penbg, new Point(var, 0), new Point(var, 250)); if (j == 0) { continue; } grapbg.DrawString(Convert.ToString(j * 50), new Font("宋体", 12), Brushes.Black, new PointF(var - 10, 250)); } // 合并图层 grapbg.DrawImage(bmp, new Point(45, -19)); // 保存到存储流 System.IO.MemoryStream ms = new System.IO.MemoryStream(); bmpbg.Save(ms, System.Drawing.Imaging.ImageFormat.Png); // 输出内容 Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length); Response.End();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(手计)