1、C#绘制曲线图和柱状图 矩形图 //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集 public Image Render(string title, int width, int height, DataTable chartTable) { Bitmap bm = new Bitmap(width, height); Graphics g = Graphics.FromImage(bm); g.Clear(Color.White)
2、 DataTable dt = chartTable; const int top = 30; const int left = 35; if (width < left * 2 || height < top * 2) { g.DrawString("绘图区域太小", new Font("Tahoma", 8), Brushes.Blue, new PointF(0, 0));
3、 return bm; } //计算最高的点 float highPoint = 1; foreach (DataRow dr in dt.Rows) { if (highPoint < Convert.ToSingle(dr[0])) { highPoint = Convert.ToSingle(dr[0]);
4、 } if (highPoint < Convert.ToSingle(dr[1])) { highPoint = Convert.ToSingle(dr[1]); } } try { //画大标题 g.DrawString(title, new Font("Tahoma", 1
5、2), Brushes.Black, new PointF(2, 2)); StringFormat drawFormat = new StringFormat(); drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; g.DrawString("[红--" + dt.Columns[0].ToString() + "]", new Font("Tahoma", 8), Bru
6、shes.Red, new PointF(2, top), drawFormat); g.DrawString("[蓝--" + dt.Columns[1].ToString() + "]", new Font("Tahoma", 8), Brushes.Blue, new PointF(17, top), drawFormat); //画条形图 float barWidth = (Convert.ToSingle(width) - left)
7、/ (dt.Rows.Count * 3 + 1); PointF barOrigin = new PointF(left + barWidth, 0); float barHeight = dt.Rows.Count; float topFontSize = (barWidth / highPoint.ToString().Length); if (topFontSize > 2*top/3)
8、 { topFontSize = 2*top/3; } if (topFontSize < 5) { topFontSize = 5; } for (int i = 0; i < dt.Rows.Count; i++) { //底部字体的大小
9、 float bottomFontSize = (2 * barWidth / dt.Rows[2].ToString().Length) + 2; if (bottomFontSize > 2 * top / 3) { bottomFontSize = 2 * top / 3; } barHeight = Convert.ToSingle(dt.Rows[
10、0]) * (height - 2 * top) / highPoint * 1; barOrigin.Y = height - barHeight - top; g.FillRectangle(new SolidBrush(Color.Red), barOrigin.X, barOrigin.Y, barWidth, barHeight); //柱状图底部 g.DrawString(dt.Rows[2].ToString(),
11、 new Font("Tahoma", bottomFontSize), Brushes.Black, new PointF(barOrigin.X, height - top)); //柱状图顶部 g.DrawString(dt.Rows[0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red, new PointF(barOrigin.X, ba
12、rOrigin.Y - 3*topFontSize/2)); barOrigin.X = barOrigin.X + barWidth; barHeight = Convert.ToSingle(dt.Rows[1]) * (height - 2 * top) / highPoint * 1; barOrigin.Y = height - barHeight - top; g.FillRectangle(new So
13、lidBrush(Color.Blue), barOrigin.X, barOrigin.Y, barWidth, barHeight); //柱状图顶部 g.DrawString(dt.Rows[1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue, new PointF(barOrigin.X, barOrigin.Y - 3 * topFontSize/2));
14、 barOrigin.X = barOrigin.X + (barWidth * 2); } //设置边 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top), new Point(left, height - top)); g.DrawLine(new Pen(Color.Blue, 2), new Point(left, he
15、ight - top), new Point(left + width, height - top)); g.Dispose(); return bm; } catch { return bm; } }K线图 //Render是图形大标题,图开小标题,图形宽度,图形长度,饼图的数据集和饼图的数据集
16、 public Image Render(string title, int width, int height, DataTable chartTable) { Bitmap bm = new Bitmap(width, height); Graphics g = Graphics.FromImage(bm); g.Clear(Color.White); const int top = 30; const int left =
17、35; if (width < left * 2 || height < top * 2) { g.DrawString("绘图区域太小" ,new Font("Tahoma", 8), Brushes.Blue, new PointF(0, 0)); return bm; } if (chartTable == null) {
18、 g.DrawString("没有数据", new Font("Tahoma", 7), Brushes.Blue, new PointF(0, 0)); return bm; } DataTable dt = chartTable; //计算最高的点 float highPoint = 1; foreach (DataRow dr in dt.Rows)
19、 { if (highPoint < Convert.ToSingle(dr[0])) { highPoint = Convert.ToSingle(dr[0]); } if (highPoint < Convert.ToSingle(dr[1])) { highPoint = Convert.ToSingle(dr[1]);
20、 } } //建立一个Graphics对象实例 try { //画大标题 g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(2, 2)); StringFormat drawFormat = new StringFormat(); d
21、rawFormat.FormatFlags = StringFormatFlags.DirectionVertical; g.DrawString("[红--" + dt.Columns[0].ToString() + "]", new Font("Tahoma", 8), Brushes.Red, new PointF(2, top), drawFormat); g.DrawString("[蓝--" + dt.Columns[1].ToString() + "]", new F
22、ont("Tahoma", 8), Brushes.Blue, new PointF(17, top), drawFormat); //画条形图 float barWidth = (Convert.ToSingle(width) - left) / (dt.Rows.Count + 1); PointF barOrigin = new PointF(left + barWidth , 0); float barH
23、eight = dt.Rows.Count; float topFontSize = 7; float bottomFontSize = 7; PointF[] pt1 = new PointF[dt.Rows.Count]; PointF[] pt2 = new PointF[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++)
24、 { //底部字体的大小 barHeight = Convert.ToSingle(dt.Rows[0]) * (height - 2 * top) / highPoint * 1; barOrigin.Y = height - barHeight - top; g.FillEllipse(new SolidBrush(Color.Red), barOrigin.X - 3, barOrigin.Y - 3, 6, 6);
25、 pt1 = new PointF(barOrigin.X, barOrigin.Y); //顶部 g.DrawString(dt.Rows[0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red, new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2));
26、 barHeight = Convert.ToSingle(dt.Rows[1]) * (height - 2 * top) / highPoint * 1; barOrigin.Y = height - barHeight - top; g.FillEllipse(new SolidBrush(Color.Blue), barOrigin.X - 3, barOrigin.Y - 3, 6, 6); pt2 = new PointF(barOrigin.X, bar
27、Origin.Y); //顶部 g.DrawString(dt.Rows[1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue, new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2)); barOrigin.X = barOrigin.X + barWidth;
28、 } if (dt.Rows.Count > 10) { int dis = dt.Rows.Count / 10; for (int i = 0; i < dt.Rows.Count; i++) { if (i % dis == 0) { g
29、DrawLine(new Pen(Color.Blue, 2), new PointF(left + (i + 1) * barWidth, height - top + 5), new PointF(left + (i + 1) * barWidth, height - top - 3)); //底部 g.DrawString(dt.Rows[2].ToString(), new Font("Tahom
30、a", bottomFontSize), Brushes.Black, new PointF(left + (i + 1) * barWidth, height - top)); } else { g.DrawLine(new Pen(Color.Gray, 1), new PointF(left + (i+1) *
31、 barWidth, height - top + 3), new PointF(left + (i+1) * barWidth, height - top - 3)); } } } else { for (int i = 0; i < dt.Rows.Count; i++)
32、 { g.DrawLine(new Pen(Color.Gray, 1), new PointF(left + (i + 1) * barWidth, height - top + 3), new PointF(left + (i + 1) * barWidth, height - top - 3)); } } //绘制曲线
33、 g.DrawLines(new Pen(new SolidBrush(Color.Red), 1), pt1); g.DrawLines(new Pen(new SolidBrush(Color.Blue),1), pt2); //设置边 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, top), new Point(left, height - top));
34、 g.DrawLine(new Pen(Color.Blue, 2), new Point(left, height - top), new Point(left + width, height - top)); g.Dispose(); return bm; } catch { return bm; } }






