日期:2014-05-20 浏览次数:21211 次
// pic a picture box Bitmap bm = new Bitmap(pic.Width, pic.Height); g = Graphics.FromImage(bm); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 Pen p = new Pen(Brushes.Black, 2F); g.Clear(System.Drawing.Color.AliceBlue); try { int row = dgvtest.Rows.Count;//从datagridview 中取数据 if (row == 0) { //g.DrawString("没有可供分析的数据……", new Font("宋体", 17), Brushes.Maroon, new PointF(pic.Location.X + 100, pic.Location.Y + 30)); } else { // x Axis g.DrawLine(p, pic.Location.X + 30, pic.Location.Y + pic.Height - 40, pic.Width - 40, pic.Location.Y + pic.Height - 40); // Y Axis g.DrawLine(p, pic.Location.X + 30, pic.Location.Y + pic.Height - 40, pic.Location.X + 30, pic.Location.Y + 30); // g.DrawLine(Pens.DarkGray, pic.Location.X + 30, pic.Location.Y + 30, pic.Width - 40, pic.Location.Y + 30); // Y Axis // g.DrawLine(Pens.DarkGray, pic.Width - 40, pic.Location.Y + pic.Height - 40, pic.Width - 40, pic.Location.Y + 30); //原点 // g.DrawString("0", new Font("宋体", 9), Brushes.Black, new PointF(pic.Location.X + 25, pic.Location.Y + pic.Height - 38)); g.DrawString("产量(件)", new Font("宋体", 10), Brushes.Maroon, new PointF(pic.Location.X + 10, pic.Location.Y + 15)); g.DrawString("工序", new Font("宋体", 10), Brushes.Maroon, new PointF(pic.Width - 60, pic.Location.Y + pic.Height - 30)); int[] IntMV = GetMaxArrayValueAndAvgValue();//datagridview 的一列 int maxvalue = IntMV[0];//一列的最大值 int avgvalue = IntMV[1];一列的平均值 for (int i = 0; i < 4; i++) { g.DrawLine(Pens.LightSlateGray, pic.Location.X + 30, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) / 4 * (i + 1), pic.Width - 40, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) / 4 * (i + 1)); g.DrawString(maxvalue / 4 * (i + 1) + "", new Font("宋体", 12), Brushes.Maroon, pic.Location.X, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) / 4 * (i + 1) - 5); } int scale = (pic.Width - 100 - pic.Location.X) / row; for (int i = 0; i < row; i++) { if (i % 2 == 0) { Rectangle rec = new Rectangle(pic.Location.X + 30 + scale * i , pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * Convert.ToInt32(dgvtest[1, i].Value) / maxvalue, scale, (pic.Height - 120) * Convert.ToInt32(dgvtest[1, i].Value) / maxvalue); LinearGradientBrush lb = new LinearGradientBrush(rec, Color.LightBlue, Color.Blue, LinearGradientMode.Horizontal); g.FillRectangle(lb, rec); g.DrawString(dgvtest[1, i].Value.ToString(), new Font("宋体", 9), Brushes.Blue, pic.Location.X + 30 + scale * i , pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * Convert.ToInt32(dgvtest[1, i].Value) / maxvalue -10); g.DrawLine(Pens.Black, pic.Location.X + 30 + scale * (i+1), pic.Location.Y + pic.Height - 44, pic.Location.X + 30 + (i+1) * scale, pic.Location.Y + pic.Height - 36); g.DrawString(dgvtest[0, i].Value.ToString(), new Font("宋体", 9), Brushes.Blue, pic.Location.X + 30 + scale * i, pic.Location.Y + pic.Height - 35); } else { Rectangle rec = new Rectangle(pic.Location.X + 30 + scale * i, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * Convert.ToInt32(dgvtest[1, i].Value) / maxvalue, scale, (pic.Height - 120) * Convert.ToInt32(dgvtest[1, i].Value) / maxvalue); LinearGradientBrush lb = new LinearGradientBrush(rec, Color.LightBlue, Color.Blue, LinearGradientMode.Horizontal); g.FillRectangle(lb, rec); g.DrawString(dgvtest[1, i].Value.ToString(), new Font("宋体", 9), Brushes.Red, pic.Location.X + 30 + scale * i, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * Convert.ToInt32(dgvtest[1, i].Value) / maxvalue - 10); g.DrawLine(Pens.Black, pic.Location.X + 30 + scale * (i+1), pic.Location.Y + pic.Height - 42, pic.Location.X + 30 + (i+1) * scale, pic.Location.Y + pic.Height - 38); g.DrawString(dgvtest[0, i].Value.ToString(), new Font("宋体", 9), Brushes.Red, pic.Location.X + 30 + scale * i, pic.Location.Y + pic.Height - 35); } } //g.DrawLine(Pens.Red, pic.Location.X + 30, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * avgvalue / maxvalue, pic.Width - 40, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * avgvalue / maxvalue); //g.DrawString(avgvalue + "", new Font("宋体", 12), Brushes.Red, pic.Location.X, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * avgvalue / maxvalue - 5); //g.DrawString("AVG", new Font("宋体", 10), Brushes.Red, pic.Width - 40, pic.Location.Y + pic.Height - 40 - (pic.Height - 120) * avgvalue / maxvalue - 8); // g.DrawImage(bm, 0, 0); } } catch { } pic.Image = (Image)bm;
------解决方案--------------------
建议用dundas,方便简单,快捷,漂亮,这里就不给例子了,搜一下,N多!
------解决方案--------------------
这里有个手把手教程
http://hi.baidu.com/%C8%FC%C4%C9%D0%D0%D0%C7/blog/item/dca89efb91d232136d22eb91.html
.net使用DotNetCharting控件生成报表统计图总结,效果超炫
http://www.cnblogs.com/dreamof/archive/2008/07/18/1245887.html
------解决方案--------------------
做饼图之类图表的工具多了去了,其中以dundas的图表最好看
但是还要考虑下只是展现,还是需要打印,这个重要的特性。
这里有个FusionCharts的代码例子
http://files.cnblogs.com/babyt/FusionChartsHelper.zip
解压后,用VS2005,打开网站,找到这个文件夹后就能跑起来了。效果还不错。
------解决方案--------------------
/// <summary> /// 将进度数据转换为图片形式,柱状图,折线图,饼状图,图形样式由参数决定 /// \n绘制各款式完成量占总完成量比例图(饼状) /// </summary> /// <param name="title">图片标题</param> /// <param name="dataList">要展示的数据列表</param> /// <param name="unitStr">单位</param> /// <param name="width">图片宽</param> /// <param name="height">图片高,0为自适应</param> /// <returns></returns> public Image GetProgressDateImage(string title, IList<ProgressData> dataList, string unitStr, int width, int height) { if (dataList == null || dataList.Count == 0) return null; double total = 0.0; //总数量 foreach (ProgressData progressData in dataList) { total += progressData.ItemValue; } if (total == 0) return null; int itemCount = dataList.Count; int bmHeight = 178; //进度图的初始高度 if (height == 0) { for (int i = 9; i < itemCount; i++) bmHeight += 15; } int changeWidh = 0; Bitmap bm = new Bitmap(width + 50, bmHeight); Graphics g; g = Graphics.FromImage(bm); changeWidh = Convert.ToInt32(g.MeasureString(dataList[0].ItemName, new Font("宋体", 9)).Width); g.Clear(System.Drawing.Color.Snow); g.DrawString(title, new Font("宋体", 10), Brushes.Black, new Point(15, 5)); Point myRec = new Point(120, 35); Point myDec = new Point(130, 35); Point myTxt = new Point(132 + changeWidh, 35); Point myPercent = new Point(132 + changeWidh + 20, 35); g.DrawString("单位:件", new Font("宋体", 9), Brushes.Black, new Point(90, 20)); g.DrawString("占总完成量比", new Font("宋体", 8), Brushes.Blue, new PointF(170, 20)); for (int i = 0; i < itemCount; i++) { g.FillRectangle(new SolidBrush(OrderPublic.GetColor(i)), myRec.X, myRec.Y, 10, 10); //填充小方块 g.DrawRectangle(Pens.Black, myRec.X, myRec.Y, 10, 10); //绘制小方块 g.DrawString(dataList[i].ItemName, new Font("宋体", 9), Brushes.Black, myDec); //绘制小方块右边的文字 g.DrawString(dataList[i].ItemValue.ToString(), new Font("宋体", 9), Brushes.Green, myTxt); string percent = Math.Round(dataList[i].ItemValue / total * 100, 2) + "%"; g.DrawString(percent, new Font("宋体", 9), Brushes.Blue, myPercent); myRec.Y += 15; myDec.Y += 15; myTxt.Y += 15; myPercent.Y += 15; } //绘制扇型,并以相应色彩填充扇型,从而构成图Pie图 float fCurrentAngle = 0; float fStartAngle = 0; for (int i = 0; i < itemCount; i++) { //以下代码是获得要绘制扇型的开始角度 if (i == itemCount - 1) { fCurrentAngle = 360 - fStartAngle; } else { try { fCurrentAngle = Convert.ToSingle((dataList[i].ItemValue * 360) / total); } catch { } } //根据参数绘制扇型 g.DrawPie(Pens.Black, 10, 40, 105, 105, fStartAngle, fCurrentAngle); //以指定色彩填充绘制的扇型 g.FillPie(new SolidBrush(OrderPublic.GetColor(i)), 10, 40, 105, 105, fStartAngle, fCurrentAngle); fStartAngle += fCurrentAngle; } //画出图片的边框 Pen p = new Pen(System.Drawing.Color.Black, 2); g.DrawRectangle(p, 1, 1, width+changeWidh - 3, bmHeight - 2); return bm; }
------解决方案--------------------
//绘图区背景颜色 //oChartSpace.Charts[0].PlotArea.Interior.Color = "red"; //绘图区 // oChartSpace.Charts[0].PlotArea.Floor.Interior.Color = "green"; oChartSpace.Charts[0].SeriesCollection.Add(0); oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection.Add(); //柱状图颜色 //oChartSpace.Charts[0].SeriesCollection[0].Interior.Color = "red"; // ------------------------------------ // If you're charting a pie or a variation thereof percentages make a lot // more sense than values... // ------------------------------------ if (chartType == ChartChartTypeEnum.chChartTypePie || chartType == ChartChartTypeEnum.chChartTypePie3D || chartType == ChartChartTypeEnum.chChartTypeDoughnut) { oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasPercentage = true; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasValue = false; } // ------------------------------------ // Not so for other chart types where values have more meaning than // percentages. // ------------------------------------ else { oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasPercentage = false; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasValue = true; } // ------------------------------------ // Plug your own visual bells and whistles here // ------------------------------------ oChartSpace.Charts[0].SeriesCollection[0].Caption = String.Empty; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].Font.Name = "verdana"; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].Font.Size = 10; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].Font.Bold = true; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].Font.Color = "red"; oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].Position = ChartDataLabelPositionEnum.chLabelPositionCenter; if (chartType == ChartChartTypeEnum.chChartTypeBarClustered || chartType == ChartChartTypeEnum.chChartTypeBar3D || chartType == ChartChartTypeEnum.chChartTypeColumnClustered || chartType == ChartChartTypeEnum.chChartTypeColumn3D) { oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].Position = ChartDataLabelPositionEnum.chLabelPositionOutsideEnd; } oChartSpace.Charts[0].SeriesCollection[0].SetData(OWC11.ChartDimensionsEnum.chDimCategories, Convert.ToInt32(OWC11.ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr); oChartSpace.Charts[0].SeriesCollection[0].SetData(OWC11.ChartDimensionsEnum.chDimValues, Convert.ToInt32(OWC11.ChartSpecialDataSourcesEnum.chDataLiteral), chartValuesStr); return oChartSpace; } #region 属性设置 public string[] chartCategoriesArrValue { get { return chartCategoriesArr; } set { chartCategoriesArr = value; } } public string[] chartValuesArrValue { get { return chartValuesArr; } set { chartValuesArr = value; } } public string chartTypeValue { get { return chartTypeCh; } set { chartTypeCh = value; } } public string chartTitleValue { get { return chartTitle; } set { chartTitle = value; } } #endregion }
------解决方案--------------------
调用方法部分代码: private void Form1_Load(object sender, System.EventArgs e) { try { System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(aa)); thread.Start(); //ShowChart(); } catch {} } void aa() { while(true) { ShowChart(); System.Threading.Thread.Sleep(1000); } } //调用 首先需要在页面上放置一个pictureBox1来显示产生的统计图 public void ShowChart() { try { string ConnString = "Data Source=SQL服务器地址;Initial Catalog=数据库名;Persist Security Info=True;User ID=用户名;Password=密码"; DB.sqlDB db = new DB.sqlDB(ConnString); string sql = "查询字符串"; DataTable table = db.getTable(sql); if (table != null && table.Rows.Count > 0) { StringBuilder sb=new StringBuilder(); for (int i = 0; i < table.Rows.Count; i++) { if (i == 0) { sb.Append(table.Rows[i]["content"].ToString()); } else { sb.Append("," + table.Rows[i]["content"].ToString()); } } string[] CategoriesArr ={sb.ToString()}; sb=new StringBuilder(); for (int i = 0; i < table.Rows.Count; i++) { if (i == 0) { sb.Append(table.Rows[i]["cnt"].ToString()); } else { sb.Append("," + table.Rows[i]["cnt"].ToString()); } } string[] ValuesArr = { sb.ToString() }; ChartFactory chartFactory = new ChartFactory(); //初始化赋值 chartFactory.chartCategoriesArrValue = CategoriesArr; chartFactory.chartValuesArrValue = ValuesArr; chartFactory.chartTitleValue = "“龙源夜、网通情”欢送毕生晚会节目投票";//柱形图标注名称 chartFactory.chartTypeValue = "垂直柱状统计图";//图类型,在类中定义过 OWC11.ChartSpaceClass oChartSpace = chartFactory.BuildCharts(); string path = FileName; //产生图片并保存 页可以是png gif图片 oChartSpace.ExportPicture(path, "jpeg", this.Width-100, this.Height-100);//后面是图片的宽和高 //下面使用 FileStream 对象的原因是本例使用线程.每秒重新生成一个图,并将原图覆盖. //如果不使用 FileStream 将会出现共享冲突. FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); MemoryStream ms = new MemoryStream(bytes); System.Drawing.Image img=System.Drawing.Image.FromStream(ms); //本例使用pictureBox1显示图片 this.pictureBox1.Image=img; this.pictureBox1.Refresh(); } } catch(Exception e) { //MessageBox.Show(e.ToString()); } }
------解决方案--------------------
//使用GDI画图,虽然比那些控件实现起来要麻烦些,但能看到原理。 public partial class GDI画折线图_DrawImage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataSet ds = GetData("哈哈"); draw(this, ds); } } private DataSet GetData(string name) { using (SqlConnection conn = new SqlConnection("server=.;database=master;uid=sa;pwd=wsp")) { SqlCommand cmd = new SqlCommand("select * from Employees where name='" + name + "' order by month", conn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } //将dataset中的数据画在页面上 public void draw(Page page, DataSet ds) { //取得记录数量 int count = ds.Tables[0].Rows.Count; //生成Bitmap对象 Bitmap img = new Bitmap(650, 600); //生成绘图对象 Graphics g = Graphics.FromImage(img); //定义黑色画笔 Pen Bp = new Pen(Color.Black); //定义红色画笔 Pen Rp = new Pen(Color.Red); //定义银灰色画笔 Pen Sp = new Pen(Color.Silver); //定义大标题字体 Font Bfont = new Font("Arial", 12, FontStyle.Bold); //定义一般字体 Font font = new Font("Arial", 8); //绘制底色 g.Clear(Color.White); //定义黑色过渡型笔刷 SolidBrush brush = new SolidBrush(Color.Black); //定义蓝色过渡型笔刷 SolidBrush Bluebrush = new SolidBrush(Color.Blue); //绘制大标题 g.DrawString(ds.Tables[0].Rows[0]["name"].ToString() + "的销售曲线图", Bfont, brush, 40, 5); //取得总销售量 int nums = 0; for (int i = 0; i < count; i++) { nums += Convert.ToInt32(ds.Tables[0].Rows[i]["sale"]); } //绘制信息简报 string info = "本年度总销售量:" + nums.ToString(); g.DrawString(info, font, Bluebrush, 40, 25); //绘制图片边框 g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1); //画X坐标(横坐标与月份的比例40:1) g.DrawLine(Sp, new Point(40, 500), new Point(520, 500)); //绘制月份轴坐标标签 for (int i = 0; i <= 12; i++) { //从40开始画起 g.DrawString(i.ToString(), font, brush, 35 + 40 * i, 500); //画点(点之间Y坐标一致) g.DrawLine(Sp, new Point(40 + 40 * i, 500), new Point(40 + 40 * i, 497)); } g.DrawString("时间(月)", font, brush, 550, 500); //画Y坐标(纵坐标与销售量的比例40:500) g.DrawLine(Sp, new Point(40, 500), new Point(40, 80)); //绘制销售量轴坐标标签 for (int i = 1; i <= 10; i++) { g.DrawString((i * 500).ToString(), font, brush, 10, 500 - (i * 40) - 5); //画点(点之间X坐标一致) g.DrawLine(Sp, new Point(40, 500 - (i * 40)), new Point(43, 500 - (i * 40))); } //绘制竖坐标标题 g.DrawString("销售量(个)", font, brush, 10, 50); //根据点画折线图 if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (i == 0) //第一个点,起点从原坐标画起。 { int x = 40 + int.Parse(ds.Tables[0].Rows[0]["month"].ToString()) * 40; //获取对应Y轴第几个点 int n = int.Parse((double.Parse(ds.Tables[0].Rows[0]["sale"].ToString()) / 500).ToString()); int y = 500 - (n * 40); g.DrawLine(Rp, new Point(40, 500), new Point(x, y)); } else { //上个点的x、y坐标 int bx = 40 + int.Parse(ds.Tables[0].Rows[i-1]["month"].ToString()) * 40; int bn = int.Parse((double.Parse(ds.Tables[0].Rows[i-1]["sale"].ToString()) / 500).ToString()); int by = 500 - (bn * 40); //当前点的x、y坐标 int x = 40 + int.Parse(ds.Tables[0].Rows[i]["month"].ToString()) * 40; int n = int.Parse((double.Parse(ds.Tables[0].Rows[i]["sale"].ToString()) / 500).ToString()); int y = 500 - (n * 40); g.DrawLine(Rp, new Point(bx, by), new Point(x, y)); } } } //保存绘制的图片 MemoryStream stream = new MemoryStream(); img.Save(stream, ImageFormat.Jpeg); //图片输出 page.Response.Clear(); page.Response.ContentType = "image/jpeg"; page.Response.BinaryWrite(stream.ToArray()); } } //数据库结构: CREATE TABLE [Employees] ( [id] [int] IDENTITY (1, 1) NOT NULL , [name] [varchar] (25) NOT NULL , [month] [int] NULL , [sale] [money] NOT NULL ) INSERT [Employees] ( [id] , [name] , [month] , [sale] ) VALUES ( 1 , '哈哈' , 1 , 3000.0000 ) INSERT [Employees] ( [id] , [name] , [month] , [sale] ) VALUES ( 2 , '哈哈' , 2 , 2000.0000 ) INSERT [Employees] ( [id] , [name] , [month] , [sale] ) VALUES ( 3 , '哈哈' , 4 , 1500.0000 ) INSERT [Employees] ( [id] , [name] , [month] , [sale] ) VALUES ( 4 , '哈哈' , 5 , 3500.0000 ) INSERT [Employees] ( [id] , [name] , [month] , [sale] ) VALUES ( 5 , '哈哈' , 6 , 4000.0000 ) INSERT [Employees] ( [id] , [name] , [month] , [sale] ) VALUES ( 6 , 'sdf' , 5 , 4000.0000 ) go