C#绘制柱状图和折线图的方法
程序员文章站
2021-12-07 19:34:02
本文实例为大家分享了c#绘制柱状图和折线图的具体代码,供大家参考,具体内容如下运行效果如下图:设计上面的柱状图和折线图其实并没有什么难度,主要是各个坐标的计算,完全是精细活。首先在窗体在添加了一个ta...
本文实例为大家分享了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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C#实现图表中鼠标移动并显示数据
下一篇: C#基于Socket实现多人聊天功能