柱状图代码:这是画的图,所以要放在一个“容器”中,我放在的是pictureBox控件中;在方法最后一句就是放在pictureBox1的。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Data.SqlClient; namespace 统计图 { public partial class MYtest : Form { public MYtest() { InitializeComponent(); } private void MYtest_Load(object sender, EventArgs e) { CreateImage(); } private void CreateImage() { int height = 500, width = 700; Bitmap image = new Bitmap(width, height); //创建Graphics类对象 Graphics g = Graphics.FromImage(image); try { //清空图片背景色 g.Clear(Color.White); Font font = new Font("Arial", 10, FontStyle.Regular); Font font1 = new Font("宋体", 20, FontStyle.Bold); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.BlueViolet, 1.2f, true); g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height); // Brush brush1 = new SolidBrush(Color.Blue); //this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + g.DrawString(" 测试自动", font1, brush, new PointF(70, 30)); //画图片的边框线 g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1); AdjustableArrowCap aac = new AdjustableArrowCap(5, 2);//添加的箭头 Pen mypen = new Pen(brush, 1); //设置画笔 //绘制线条 //绘制竖向线条 Pen mypen2 = new Pen(Color.Blue, 2); x = 60; mypen2.CustomStartCap = aac; g.DrawLine(mypen2, x, 80, x, 340); //绘制横向线条 Pen mypen1 = new Pen(Color.Blue, 2); y = 340; mypen1.CustomEndCap = aac; g.DrawLine(mypen1, 60, y, 620, y); //x轴 x = 85; for (int i = 0; i < 7; i++) { int dt = DateTime.Now.Month; dt = dt + 6 + i; if (dt > 12) dt = dt - 12; g.DrawString(dt.ToString() + "月", font, Brushes.Blue, x, 348); //设置文字内容及输出位置 x = x + 60; } g.DrawString("月份", font, Brushes.Blue, 600, 348); //y轴 String[] m = {"250","225", "200", "175", "150", "125", "100", " 75", " 50", " 25", " 0"}; y = 72; for (int i = 0; i < 10; i++) { g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y); //设置文字内容及输出位置 y = y + 26; } g.DrawString("产量/kg", font, Brushes.Blue, 25, 60); int[] Count1 = new int[7]; int[] Count2 = new int[7]; //柱状图的柱子 for (int i = 0; i < 7; ++i) { Count1[i] = i + 100; } for (int i = 0; i < 7; ++i) { Count2[i] = i + 100; } //绘制柱状图. x = 90; Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold); SolidBrush mybrush = new SolidBrush(Color.Red); SolidBrush mybrush2 = new SolidBrush(Color.Green); //第一期 g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]); g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15); //第二期 x = x + 60; g.FillRectangle(mybrush, x, 340 - Count1[1], 20, Count1[1]); g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 340 - Count1[1] - 15); //第三期 x = x + 60; g.FillRectangle(mybrush, x, 340 - Count1[2], 20, Count1[2]); g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 340 - Count1[2] - 15); //第四期 x = x + 60; g.FillRectangle(mybrush, x, 340 - Count1[3], 20, Count1[3]); g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 340 - Count1[3] - 15); //上半年 x = x + 60; g.FillRectangle(mybrush, x, 340 - Count1[4], 20, Count1[4]); g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 340 - Count1[4] - 15); //下半年 x = x + 60; g.FillRectangle(mybrush, x, 340 - Count1[5], 20, Count1[5]); g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 340 - Count1[5] - 15); //全年 x = x + 60; g.FillRectangle(mybrush, x, 340 - Count1[6], 20, Count1[6]); g.DrawString(Count1[6].ToString(), font2, Brushes.Red, x, 340 - Count1[6] - 15); //绘制标识 Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular); g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); //绘制范围框 g.FillRectangle(Brushes.Red, 270, 410, 20, 10); //绘制小矩形 g.DrawString("报名人数", font3, Brushes.Red, 292, 408); //g.FillRectangle(Brushes.Green, 270, 430, 20, 10); //g.DrawString("通过人数", font3, Brushes.Green, 292, 428); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //Response.ClearContent(); //Response.ContentType = "image/Jpeg"; //Response.BinaryWrite(ms.ToArray()); } finally { pictureBox1.Image = Image.FromHbitmap(image.GetHbitmap()); g.Dispose(); image.Dispose(); } } } }
呈现出来的效果: