日期:2014-05-18 浏览次数:20997 次
private void Form1_Load(object sender, EventArgs e) { ///这里应该怎么调用,怎么样写!请各位帮忙!俺是菜鸟! } 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 = 35; if (width < left * 2 || height < top * 2) { g.DrawString("绘画区域太小",new Font("Tahoma",8),Brushes.Red,new PointF(0,0)); return bm; } if (chartTable == null) { 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) { if (highPoint <float.Parse(Convert.ToString(dr[0]))) { highPoint = float.Parse(Convert.ToString(dr[0])); } //if (highPoint < float.Parse(Convert.ToString(dr[1]))) //{ // highPoint = float.Parse(Convert.ToString(dr[1])); //} } try { //画大标题 g.DrawString(title, new Font("Tahoma", 12), Brushes.Black, new PointF(2, 2)); StringFormat drawFormat = new StringFormat(); drawFormat.FormatFlags = StringFormatFlags.DirectionVertical; g.DrawString("红--" + dt.Columns[0].ToString() + "]", new Font("Tahoma", 8), Brushes.Red, new PointF(2, top), drawFormat); //画条形图 float barWidth = (float.Parse(Convert.ToString(width)) - left) / (dt.Rows.Count + 1); PointF barOrigin = new PointF(left + barWidth, 0); float barHeigh = 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++) { //底部字体大小 barHeigh =int.Parse(Convert.ToString(dt.Rows[i][0])) * (height - 2 * top) / highPoint * 1; barOrigin.Y = height - barHeigh - top; g.FillEllipse(new SolidBrush(Color.Red), barOrigin.X - 3, barOrigin.Y - 3, 6, 6); pt1[i] = new PointF(barOrigin.X, barOrigin.Y); g.DrawString(dt.Rows[i][0].ToString(), new Font("Tahoma", topFontSize), Brushes.Red, new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2)); barHeigh =int.Parse(Convert.ToString(dt.Rows[i][1])) * (height - 2 * top) / highPoint * 1; barOrigin.Y = height - barHeigh - top; g.FillEllipse(new SolidBrush(Color.Blue), barOrigin.X - 3, barOrigin.Y - 3, 6, 6); pt2[i] = new PointF(barOrigin.X, barOrigin.Y); //顶部 g.DrawString(dt.Rows[i][1].ToString(), new Font("Tahoma", topFontSize), Brushes.Blue, new PointF(barOrigin.X, barOrigin.Y - 4 * topFontSize / 2)); barOrigin.X = barOrigin.X + barWidth; } if (dt.Rows.Count > 10) { int dis = dt.Rows.Count / 10; for (int i = 0; i < dt.Rows.Count; i++) { if (i % dis == 0)