效果图
public partial class 折线图 : Form { string[] strYue = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; string[] strShuLiang = { "100", "68", "87", "67", "10", "50", "90", "60", "20", "30", "78", "56" }; DataTable dt = new DataTable(); public 折线图() { InitializeComponent(); DataColumn dc = new DataColumn("YueFen", typeof(string)); dt.Columns.Add(dc); DataColumn dc1 = new DataColumn("ShuLiang", typeof(string)); dt.Columns.Add(dc1); //加载数据 for (int i = 0; i < 12; i++) { DataRow dr = dt.NewRow(); dr["YueFen"] = strYue[i]; dr["ShuLiang"] = strShuLiang[i]; dt.Rows.Add(dr); } comboBox1.SelectedItem = "上半年"; btn_ChaXun_Click(null,null); } private void btn_ChaXun_Click(object sender, EventArgs e) { List<Point> pos = new List<Point>(); int i1, i2; Bitmap bitM = new Bitmap(this.panel1.Width, this.panel1.Height); Graphics g = Graphics.FromImage(bitM); g.Clear(Color.White); Brush b = new SolidBrush(Color.Blue); Font f = new Font("Arial", 9, FontStyle.Regular); Pen p = new Pen(b); //绘制横线 //找出折线统计图的0,0点 int ZheX = 40; int ZheY = 0; int y = 40; for (int i = 0; i < 6; i++) { g.DrawLine(new Pen(Color.Red), 40, y, 260, y); g.DrawString(i * 20 + "", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 10, this.panel1.Height - y - 32); y += 40; } ZheY = y - 40; //绘制竖线 for (int i = 0; i < 6; i++) { g.DrawLine(new Pen(Color.Red), ZheX + 40 * i, 20, 40 + 40 * i, 240); // g.DrawString(i * 20 + "", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 10, this.panel1.Height - y - 32); if (comboBox1.Text == "上半年") { g.DrawString(strYue[i], new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 20 + 40 * i, ZheY + 8); } else { g.DrawString(strYue[6 + i], new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 20 + 40 * i, ZheY + 8); } } if (comboBox1.Text == "上半年") { //加载右侧List listBox1.Items.Clear(); for (int i = 0; i < 6; i++) { listBox1.Items.Add(strYue[i] + " : " + strShuLiang[i] + "箱"); } i1 = 0; i2 = 6; } else { //加载右侧List listBox1.Items.Clear(); for (int i = 6; i < 12; i++) { listBox1.Items.Add(strYue[i] + " : " + strShuLiang[i] + "箱"); } i1 = 6; i2 = 12; } for (int i = i1; i < i2; i++) { //根据新的0,0点和横线的增量获取新的Y值 //新的Y值=数量/绘制的文字数量的增量(0,20,40) * 40(绘制的图中的px的增量,40px) decimal d = (decimal.Parse(dt.Rows[i]["ShuLiang"].ToString()) / 20) * 40; int d1 = (int)d; Point p1; if (i1 >= 6) { p1 = new Point((i + 1-6) * 40, ZheY - d1); } else { p1 = new Point((i + 1) * 40, ZheY - d1); } pos.Add(p1); } //连线 for (int i = 0; i < 5; i++) { g.DrawLine(p, pos[i], pos[i + 1]); } this.panel1.BackgroundImage = bitM; } }