using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.Drawing; using System.Drawing.Drawing2D; using System.Data; using System.Windows.Forms; namespace WXX { class DrawZhiFang { //double[] a = new double[] { 4, 5, 5, 6, 4, 6, 8, 4, 5, 6, 7, 8, 4, 6, 6, 4, 5, 8, 4, 7, 8, 6, 4, 4, 5, 2, 3, 9 };//数据集 public Bitmap bMap = new Bitmap(500, 500); public void drawZhiFang(string s) { string cdstr = s; string sql; Connection con = new Connection(); if (cdstr == "") { sql = "select RB0 from " + con.table; cdstr = "RB0"; } else sql = "select " + cdstr + " from " + con.table ; DataTable dt = new GetDatatable().getDt(sql); double[] dou = new double[dt.Rows.Count]; try { for (int f = 0; f < dt.Rows.Count; f++) dou[f] = Convert.ToDouble(dt.Rows[f][cdstr].ToString().Substring(0, 6));//获取dbf文件中的电阻; } catch (Exception) { cdstr= "RB0"; MessageBox.Show(s+"数据不存在,请重新选择"); } #region 这里的数据是为测试方便才使用的值,因为当前数据前两个有异常; double[] d=new double[dou.Length-2]; for (int o = 0; o < dou.Length - 2;o++ ) d[o]=dou[o+2]; #endregion double max = d.Max();//数据集中最大值; double min = d.Min();//数据集中最小值 double r = max - min;//极差值 int count=d.Count(); int zhushu;//设置数据分组后的组数 if (count > 20 && count < 50) zhushu = 6; else if (count >= 50 && count < 100) zhushu = 7; else if (count >= 100 && count < 200) zhushu = 8; else if (count >= 200 && count < 500) zhushu = 9; else if (count >= 500 && count < 1000) zhushu = 10; else zhushu = 11; double h = r / zhushu;//组距=极差/组数 //int fencount = 0; int[] fenzu=new int[zhushu];//定义一个存储小组数量的数组(即分成zhushu个小组,每小组里成员的数量形成的数组) for (int i = 0; i < count; i++) { for (int j = 0; j < zhushu; j++) if (d[i] >= (min + j * h) && d[i] < (min + (j + 1) * h)) fenzu[j]++; }//获取个各个分组中的数据数 //开始画图 //画图初始化 Graphics gph=Graphics.FromImage(bMap); gph.Clear(Color.White); PointF cPt=new PointF(40,420);//中心点 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)};//X轴三角形 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)};//Y轴三角形 gph.DrawString("数据直方图",new Font("宋体",14), Brushes.Black,new PointF(cPt.X+12,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("X",new Font("宋体",12),Brushes.Black,new PointF(cPt.Y+12,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("电阻"+s+"的阻值",new Font("宋体",12),Brushes.Black,new PointF(6,7)); for(int i=1;i<=fenzu.Length;i++ ) { //画Y轴刻度 if(i<11) { gph.DrawString((i*10).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轴项目 gph.DrawString((min+(i-1)*h).ToString(), new Font("宋体", 8), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 5)); // gph.DrawString(fenzu[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 20)); // if (fenzu[i - 1].Length > 2) // gph.DrawString(fenzu[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 35)); //画点 //gph.DrawEllipse(Pens.Black,cPt.X+i*30-1.5F,cPt.Y-d[i-1]*3-1.5F,3,3); //gph.FillEllipse(new SolidBrush(Color.Black),cPt.X+i*30-1.5F,cPt.Y-d[i-1]*3+1.5F,3,3); Color[] color = new Color[] { Color.Red,Color.Green,Color.Gray,Color.GreenYellow,Color.Gold}; int c = i % color.Count(); SolidBrush mybrush = new SolidBrush(color[c]);//定义不同的颜色的笔刷; //画矩形 gph.FillRectangle(mybrush,cPt.X+i*30,cPt.Y-fenzu[i-1]*3,30,fenzu[i-1]*3); mybrush.Dispose(); //画数值 gph.DrawString(fenzu[i-1].ToString(),new Font("宋体",11),Brushes.Black,new PointF(cPt.X+i*30,cPt.Y-fenzu[i-1]*3)); //画折线 //if(i>1) // gph.DrawLine(Pens.Red,cPt.X+(i-1)*30,cPt.Y-d[i-2]*3,cPt.X+i*30,cPt.Y-d[i-1]*3); } 显示在pictureBox1控件中 //this.pictureBox1.Image=bMap; 保存输出图片 //bMap.Save("c://test.bmp"); } } }