C#绘制柱状图和折线图的方法
本文实例为大家分享了C#绘制柱状图和折线图的具体代码,供大家参考,具体内容如下
运行效果如下图:
设计上面的柱状图和折线图其实并没有什么难度,主要是各个坐标的计算,完全是精细活。首先在窗体在添加了一个tabControl控件来切换柱状图和折线图的显示,在tabPage1中显示柱状图,在tabPage2中显示折线图。然后在各自的Page页属性中定义Paint事件,具体实现过程可以从下面的代码中看到。
代码如下:
添加头文件:
using System.Drawing.Drawing2D;
tabPage1的Paint事件(画柱状图):
private void tabPage1_Paint(object sender, PaintEventArgs e) { BackColor = Color.White; //标题 Graphics g = tabPage1.CreateGraphics(); Font f = new Font("宋体", 24, FontStyle.Regular); Pen p = new Pen(Color.Blue); g.DrawString("报名及考试统计柱状图", f, p.Brush, 200, 20); //画表格 for (int i = 0; i <= 9; i++) { g.DrawLine(p, 30, 90 + 31 * i, 620, 90 + 31 * i); } for (int i = 1; i <= 14; i++) { g.DrawLine(p, 30 + 42 * i, 60, 30 + 42 * i, 370); } Pen MyPen = new Pen(Color.Blue, 2); Point p1 = new Point(30, 60); Point p2 = new Point(30, 370); Point p3 = new Point(30, 370); Point p4 = new Point(620, 370); g.DrawLine(MyPen, p1, p2); g.DrawLine(MyPen, p3, p4); //红色图形部分 Pen drawPen = new Pen(Color.Red, 1); SolidBrush mybrush = new SolidBrush(Color.Red); e.Graphics.DrawRectangle(drawPen, 30 + 21, 370 - 41, 21, 41); e.Graphics.FillRectangle(mybrush, 30 + 21, 370 - 41, 21, 41); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10, 21, 31 * 4 + 10); e.Graphics.FillRectangle(mybrush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10, 21, 31 * 4 + 10); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20, 21, 31 * 2 + 20); e.Graphics.FillRectangle(mybrush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20, 21, 31 * 2 + 20); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20, 21, 31 * 1 + 20); e.Graphics.FillRectangle(mybrush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20, 21, 31 * 1 + 20); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25, 21, 31 * 5 + 25); e.Graphics.FillRectangle(mybrush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25, 21, 31 * 5 + 25); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7, 21, 31 * 4 + 7); e.Graphics.FillRectangle(mybrush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7, 21, 31 * 4 + 7); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 12 + 21, 60, 21, 370 - 60); e.Graphics.FillRectangle(mybrush, 30 + 42 * 12 + 21, 60, 21, 370 - 60); //绿色图形部分 Pen drawPen2 = new Pen(Color.Green, 1); SolidBrush brush = new SolidBrush(Color.Green); e.Graphics.DrawRectangle(drawPen2, 30 + 42, 370 - 31, 21, 31); e.Graphics.FillRectangle(brush, 30 + 42, 370 - 31, 21, 31); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 3, 370 - 31 * 2 - 15, 21, 31 * 2 + 15); e.Graphics.FillRectangle(brush, 30 + 42 * 3, 370 - 31 * 2 - 15, 21, 31 * 2 + 15); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 5, 370 - 31 - 10, 21, 41); e.Graphics.FillRectangle(brush, 30 + 42 * 5, 370 - 31 - 10, 21, 41); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 7, 370 - 16, 21, 16); e.Graphics.FillRectangle(brush, 30 + 42 * 7, 370 - 16, 21, 16); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 9, 370 - 31 * 3 - 20, 21, 31 * 3 + 20); e.Graphics.FillRectangle(brush, 30 + 42 * 9, 370 - 31 * 3 - 20, 21, 31 * 3 + 20); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 11, 370 - 31 * 1 - 28, 21, 31 * 1 + 28); e.Graphics.FillRectangle(brush, 30 + 42 * 11, 370 - 31 * 1 - 28, 21, 31 * 1 + 28); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 13, 370 - 31 * 5 - 15, 21, 31 * 5 + 15); e.Graphics.FillRectangle(brush, 30 + 42 * 13, 370 - 31 * 5 - 15, 21, 31 * 5 + 15); //图上的文字部分 Font font2 = new Font("宋体", 10, FontStyle.Regular); g.DrawString("第一期", font2, p.Brush, 30 + 21, 375); g.DrawString("第二期", font2, p.Brush, 30 + 42 * 2 + 21, 375); g.DrawString("第三期", font2, p.Brush, 30 + 42 * 4 + 21, 375); g.DrawString("第四期", font2, p.Brush, 30 + 42 * 6 + 21, 375); g.DrawString("上半年", font2, p.Brush, 30 + 42 * 8 + 21, 375); g.DrawString("下半年", font2, p.Brush, 30 + 42 * 10 + 21, 375); g.DrawString("全年统计", font2, p.Brush, 30 + 42 * 12 + 21, 375); //图上数字部分 g.DrawString("25", font2, p.Brush, 10, 370 - 35); g.DrawString("50", font2, p.Brush, 10, 370 - 35 * 2); g.DrawString("75", font2, p.Brush, 10, 370 - 34 * 3); g.DrawString("100", font2, p.Brush, 5, 370 - 33 * 4); g.DrawString("125", font2, p.Brush, 5, 370 - 33 * 5); g.DrawString("150", font2, p.Brush, 5, 370 - 32 * 6); g.DrawString("175", font2, p.Brush, 5, 370 - 32 * 7); g.DrawString("200", font2, p.Brush, 5, 370 - 32 * 8); g.DrawString("225", font2, p.Brush, 5, 370 - 32 * 9); g.DrawString("250", font2, p.Brush, 5, 370 - 32 * 10); //红色数 Pen pen2 = new Pen(Color.Red); g.DrawString("39", font2, pen2.Brush, 30 + 21, 370 - 41 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10 - 15); g.DrawString("71", font2, pen2.Brush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20 - 15); g.DrawString("40", font2, pen2.Brush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20 - 15); g.DrawString("150", font2, pen2.Brush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7 - 15); g.DrawString("261", font2, pen2.Brush, 30 + 42 * 12 + 21, 60 - 15); //绿色数 Pen pen3 = new Pen(Color.Green); g.DrawString("39", font2, pen2.Brush, 30 + 21, 370 - 41 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10 - 15); g.DrawString("71", font2, pen2.Brush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20 - 15); g.DrawString("40", font2, pen2.Brush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20 - 15); g.DrawString("150", font2, pen2.Brush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7 - 15); g.DrawString("261", font2, pen2.Brush, 30 + 42 * 12 + 21, 60 - 15); //最下面的矩形框 e.Graphics.DrawRectangle(p, 30 + 42 * 2 + 30, 400, 42 * 7, 31 * 2); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 5, 410, 21, 10); e.Graphics.FillRectangle(mybrush, 30 + 42 * 5, 410, 21, 10); g.DrawString("报名人数", font2, pen2.Brush, 30 + 42 * 6, 410); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 5, 440, 21, 10); e.Graphics.FillRectangle(brush, 30 + 42 * 5, 440, 21, 10); g.DrawString("通过人数", font2, pen3.Brush, 30 + 42 * 6, 440); }
tabPage2的Paint事件(画折线图):
private void tabPage2_Paint(object sender, PaintEventArgs e) { BackColor = Color.White; //标题 Graphics g = tabPage2.CreateGraphics(); Font f = new Font("宋体", 24, FontStyle.Regular); Pen p = new Pen(Color.Blue); g.DrawString("报名及考试统计折线图", f, p.Brush, 200, 20); //画表格 for (int i = 0; i <= 9; i++) { g.DrawLine(p, 30, 90 + 31 * i, 620, 90 + 31 * i); } for (int i = 1; i <= 7; i++) { g.DrawLine(p, 30 + 84 * i, 60, 30 + 84 * i, 370); } Pen MyPen = new Pen(Color.Blue, 2); Point p1 = new Point(30, 60); Point p2 = new Point(30, 370); Point p3 = new Point(30, 370); Point p4 = new Point(620, 370); g.DrawLine(MyPen, p1, p2); g.DrawLine(MyPen, p3, p4); //绘制折线 Pen pen1 = new Pen(Color.Red, 2); Pen pen2 = new Pen(Color.Green,2); //红色折线 Point a1, a2, a3, a4, a5, a6, a7; a1 = new Point(30,370-31-20); a2 = new Point(30+84*1,370-(31*4+9)); a3 = new Point(30 + 84 * 2,370-(31*2+28)); a4 = new Point(30 + 84 * 3, 370 - (31 * 1 + 20)); a5 = new Point(30 + 84 * 4, 370 - (31 * 5 + 21)); a6 = new Point(30 + 84 * 5, 370 - (31 * 4 + 10)); a7 = new Point(30 + 84 * 6, 60); Point[] points = { a1,a2,a3,a4,a5,a6,a7}; g.DrawLines(pen1, points); //绿色折线 Point b1, b2, b3, b4, b5, b6, b7; b1 = new Point(30,370-(31*1+1)); b2 = new Point(30+84*1,370-(31*2+15)); b3 = new Point(30 + 84 * 2, 370 - (31 * 1 + 10)); b4 = new Point(30 + 84 * 3, 370 - (31 * 0 + 15)); b5 = new Point(30 + 84 * 4, 370 - (31 * 3 + 15)); b6 = new Point(30 + 84 * 5, 370 - (31 * 1 + 29)); b7 = new Point(30 + 84 * 6, 370 - (31 * 5 + 14)); Point[] points2 = { b1, b2, b3, b4, b5, b6, b7 }; g.DrawLines(pen2,points2); //图上数字部分 Font font2 = new Font("宋体", 10, FontStyle.Regular); g.DrawString("25", font2, pen1.Brush, 10, 370 - 35); g.DrawString("50", font2, pen1.Brush, 10, 370 - 35 * 2); g.DrawString("75", font2, pen1.Brush, 10, 370 - 34 * 3); g.DrawString("100", font2, pen1.Brush, 5, 370 - 33 * 4); g.DrawString("125", font2, pen1.Brush, 5, 370 - 33 * 5); g.DrawString("150", font2, pen1.Brush, 5, 370 - 32 * 6); g.DrawString("175", font2, pen1.Brush, 5, 370 - 32 * 7); g.DrawString("200", font2, pen1.Brush, 5, 370 - 32 * 8); g.DrawString("225", font2, pen1.Brush, 5, 370 - 32 * 9); g.DrawString("250", font2, pen1.Brush, 5, 370 - 32 * 10); //文字 g.DrawString("第一期", font2, pen1.Brush, 15, 375); g.DrawString("第二期", font2, pen1.Brush, 15 + 84 * 1, 375); g.DrawString("第三期", font2, pen1.Brush, 15 + 84 * 2, 375); g.DrawString("第四期", font2, pen1.Brush, 15 + 84 * 3, 375); g.DrawString("上半年", font2, pen1.Brush, 15 + 84 * 4, 375); g.DrawString("下半年", font2, pen1.Brush, 15 + 84 * 5, 375); g.DrawString("全年统计", font2, pen1.Brush, 15 + 84 * 6, 375); //折线图上的数字 g.DrawString("39", font2, pen1.Brush, 30, 370 - 31 - 20 - 15); g.DrawString("111", font2, pen1.Brush, 30 + 84 * 1, 370 - (31 * 4 + 9) - 15); g.DrawString("71", font2, pen1.Brush, 30 + 84 * 2, 370 - (31 * 2 + 28) - 15); g.DrawString("40", font2, pen1.Brush, 30 + 84 * 3, 370 - (31 * 1 + 20) - 15); g.DrawString("150", font2, pen1.Brush, 30 + 84 * 4, 370 - (31 * 5 + 21) - 15); g.DrawString("111", font2, pen1.Brush, 30 + 84 * 5, 370 - (31 * 4 + 10) - 15); g.DrawString("261", font2, pen1.Brush, 30 + 84 * 6, 60 - 15); g.DrawString("26", font2, pen2.Brush, 30, 370 - (31 * 1 + 1) - 15); g.DrawString("68", font2, pen2.Brush, 30 + 84 * 1, 370 - (31 * 2 + 15) - 15); g.DrawString("35", font2, pen2.Brush, 30 + 84 * 2, 370 - (31 * 1 + 10) - 15); g.DrawString("14", font2, pen2.Brush, 30 + 84 * 3, 370 - (31 * 0 + 15) - 15); g.DrawString("94", font2, pen2.Brush, 30 + 84 * 4, 370 - (31 * 3 + 15) - 15); g.DrawString("49", font2, pen2.Brush, 30 + 84 * 5, 370 - (31 * 1 + 29) - 15); g.DrawString("143", font2, pen2.Brush, 30 + 84 * 6, 370 - (31 * 5 + 14) - 15); //最下面的矩形框 SolidBrush mybrush = new SolidBrush(Color.Red); SolidBrush brush = new SolidBrush(Color.Green); e.Graphics.DrawRectangle(pen1, 30 + 42 * 2 + 30, 400, 42 * 7, 31 * 2); e.Graphics.DrawRectangle(pen1, 30 + 42 * 5, 410, 21, 10); e.Graphics.FillRectangle(mybrush, 30 + 42 * 5, 410, 21, 10); g.DrawString("报名人数", font2, pen1.Brush, 30 + 42 * 6, 410); e.Graphics.DrawRectangle(pen2, 30 + 42 * 5, 440, 21, 10); e.Graphics.FillRectangle(brush, 30 + 42 * 5, 440, 21, 10); g.DrawString("通过人数", font2, pen2.Brush, 30 + 42 * 6, 440); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)