C# 绘制统计图大全(柱状图, 折线图, 扇形图)
程序员文章站
2023-12-31 21:50:34
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编...
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的;这里我们用就c# 制作三款最经典的统计图: 柱状图, 折线图和扇形图;既然是统计, 当然需要数据, 这里演示的数据存于sql server2000中, 三款统计图形都是动态生成. 其中柱状图我会附上制作步骤, 其他两款统计图直接附源码.
说明: 需求不一样, 统计图形绘制后的显示效果也不一样, 比如这里柱状图的主要需求是为了比较每一期报名人数与通过人数的差, 因此会把两根柱子放在一起会使比较结果一目了然. 因此大家可以根据需要灵活绘制.
一. 柱状图的绘制.
绘制步骤如下:
1. 定义绘图用到的类.
int height = 500, width = 700; bitmap image = new bitmap(width, height); graphics g = graphics.fromimage(image); pen mypen = new pen(brush, 1);
2. 绘制图框.
g.fillrectangle(brushes.whitesmoke, 0, 0, width, height);
3. 绘制横向坐标线
for (int i = 0; i < 14; i++) { g.drawline(mypen, x, 80, x, 340); x = x + 40; }
4. 绘制纵向坐标线
for (int i = 0; i < 9; i++) { g.drawline(mypen, 60, y, 620, y); y = y + 26; }
5. 绘制横坐标值
string[] n = { "第一期", "第二期", "第三期", "第四期", "全年" }; for (int i = 0; i < 7; i++) { g.drawstring(n[i].tostring(), font, brushes.blue, x, 348); x = x + 78; }
6. 绘制纵坐标值
string[] m = {"250","225", "200", "175", "150", "125", "100“}; for (int i = 0; i < 10; i++) { g.drawstring(m[i].tostring(), font, brushes.blue, 25, y); y = y + 26; }
7. 定义数组存储数据库中统计的数据
int[] count1 = new int[7]; //存储从数据库读取的报名人数 int[] count2 = new int[7]; //存储从数据库读取的通过人数
8. 从数据库中读取报名人数与通过人数
sqlconnection con = new sqlconnection( "server=(local);database=committeetraining;"); con.open(); string cmdtxt2 = "select * from ##count where company='" + ****+ "'"; sqldataadapter da = new sqldataadapter(cmdtxt2, con); dataset ds = new dataset(); da.fill(ds);
9. 将读取的数据存储到数组中
count1[0] = convert.toint32(ds.tables[0].rows[0][“count1”].tostring()); count1[1] = convert.toint32(ds.tables[0].rows[0][“count3”].tostring()); count2[0] = convert.toint32(ds.tables[0].rows[0][“count2”].tostring()); count2[1] = convert.toint32(ds.tables[0].rows[0]["count4"].tostring());
10.定义画笔和画刷准备绘图
x = 80; font font2 = new system.drawing.font( "arial", 10, fontstyle.bold); solidbrush mybrush = new solidbrush(color.red); solidbrush mybrush2 = new solidbrush(color.green);
11. 根据数组中的值绘制柱状图
(1)第一期报名人数
g.fillrectangle(mybrush, x, 340 - count1[0], 20, count1[0]); g.drawstring(count1[0].tostring(), font2, brushes.red, x, 340 - count1[0] - 15);
(2) 第一期通过人数
x = x + 20; g.fillrectangle(mybrush2, x, 340 - count2[0], 20, count2[0]); g.drawstring(count2[0].tostring(), font2, brushes.green, x, 340 - count2[0] - 15);
12. 将图形输出到页面.
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());
最终柱状图的效果图:
柱状图的完整代码:
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); g.drawstring(this.ddltaget.selecteditem.text + " " + this.ddlyear.selecteditem.text + " 成绩统计柱状图", font1, brush, new pointf(70, 30)); //画图片的边框线 g.drawrectangle(new pen(color.blue), 0, 0, image.width - 1, image.height - 1); pen mypen = new pen(brush, 1); //绘制线条 //绘制横向线条 int x = 100; for (int i = 0; i < 14; i++) { g.drawline(mypen, x, 80, x, 340); x = x + 40; } pen mypen1 = new pen(color.blue, 2); x = 60; g.drawline(mypen1, x, 80, x, 340); //绘制纵向线条 int y = 106; for (int i = 0; i < 9; i++) { g.drawline(mypen, 60, y, 620, y); y = y + 26; } g.drawline(mypen1, 60, y, 620, y); //x轴 string[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" }; x = 78; for (int i = 0; i < 7; i++) { g.drawstring(n[i].tostring(), font, brushes.blue, x, 348); //设置文字内容及输出位置 x = x + 78; } //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; } int[] count1 = new int[7]; int[] count2 = new int[7]; sqlconnection con = new sqlconnection("server=(local);database=committeetraining;uid=sa;pwd=**"); con.open(); string cmdtxt2 = "select * from ##count where company='" + this.ddltaget.selecteditem.text.trim() + "'"; sqldataadapter da = new sqldataadapter(cmdtxt2, con); dataset ds = new dataset(); da.fill(ds); count1[0] = convert.toint32(ds.tables[0].rows[0]["count1"].tostring()); count1[1] = convert.toint32(ds.tables[0].rows[0]["count3"].tostring()); count1[2] = convert.toint32(ds.tables[0].rows[0]["count5"].tostring()); count1[3] = convert.toint32(ds.tables[0].rows[0]["count7"].tostring()); count1[4] = count1[0] + count1[1]; count1[5] = count1[2] + count1[3]; count1[6] = convert.toint32(ds.tables[0].rows[0]["count9"].tostring()); count2[0] = convert.toint32(ds.tables[0].rows[0]["count2"].tostring()); count2[1] = convert.toint32(ds.tables[0].rows[0]["count4"].tostring()); count2[2] = convert.toint32(ds.tables[0].rows[0]["count6"].tostring()); count2[3] = convert.toint32(ds.tables[0].rows[0]["count8"].tostring()); count2[4] = count2[0] + count2[1]; count2[5] = count2[2] + count2[3]; count2[6] = convert.toint32(ds.tables[0].rows[0]["count10"].tostring()); //绘制柱状图. x = 80; 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 + 20; g.fillrectangle(mybrush2, x, 340 - count2[0], 20, count2[0]); g.drawstring(count2[0].tostring(), font2, brushes.green, x, 340 - count2[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 + 20; g.fillrectangle(mybrush2, x, 340 - count2[1], 20, count2[1]); g.drawstring(count2[1].tostring(), font2, brushes.green, x, 340 - count2[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 + 20; g.fillrectangle(mybrush2, x, 340 - count2[2], 20, count2[2]); g.drawstring(count2[2].tostring(), font2, brushes.green, x, 340 - count2[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 + 20; g.fillrectangle(mybrush2, x, 340 - count2[3], 20, count2[3]); g.drawstring(count2[3].tostring(), font2, brushes.green, x, 340 - count2[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 + 20; g.fillrectangle(mybrush2, x, 340 - count2[4], 20, count2[4]); g.drawstring(count2[4].tostring(), font2, brushes.green, x, 340 - count2[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 + 20; g.fillrectangle(mybrush2, x, 340 - count2[5], 20, count2[5]); g.drawstring(count2[5].tostring(), font2, brushes.green, x, 340 - count2[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); x = x + 20; g.fillrectangle(mybrush2, x, 340 - count2[6], 20, count2[6]); g.drawstring(count2[6].tostring(), font2, brushes.green, x, 340 - count2[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 { g.dispose(); image.dispose(); } }
二. 折线统计图的绘制
效果:
折线图的完整代码:
private void createimage() { int height = 480, width = 700; bitmap image = new bitmap(width, height); graphics g = graphics.fromimage(image); try { //清空图片背景色 g.clear(color.white); font font = new system.drawing.font("arial", 9, fontstyle.regular); font font1 = new system.drawing.font("宋体", 20, fontstyle.regular); font font2 = new system.drawing.font("arial", 8, fontstyle.regular); lineargradientbrush brush = new lineargradientbrush( new rectangle(0, 0, image.width, image.height), color.blue, color.blue, 1.2f, true); g.fillrectangle(brushes.aliceblue, 0, 0, width, height); brush brush1 = new solidbrush(color.blue); brush brush2 = new solidbrush(color.saddlebrown); g.drawstring(this.ddltaget.selecteditem.text + " " + this.ddlyear.selecteditem.text + " 成绩统计折线图", font1, brush1, new pointf(85, 30)); //画图片的边框线 g.drawrectangle(new pen(color.blue), 0, 0, image.width - 1, image.height - 1); pen mypen = new pen(brush, 1); pen mypen2 = new pen(color.red, 2); //绘制线条 //绘制纵向线条 int x = 60; for (int i = 0; i < 8; i++) { g.drawline(mypen, x, 80, x, 340); x = x + 80; } pen mypen1 = new pen(color.blue, 3); x = 60; g.drawline(mypen1, x, 82, x, 340); //绘制横向线条 int y = 106; for (int i = 0; i < 10; i++) { g.drawline(mypen, 60, y, 620, y); y = y + 26; } // y = 106; g.drawline(mypen1, 60, y - 26, 620, y - 26); //x轴 string[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" }; x = 45; for (int i = 0; i < 7; i++) { g.drawstring(n[i].tostring(), font, brushes.red, x, 348); //设置文字内容及输出位置 x = x + 77; } //y轴 string[] m = { "220人", " 200人", " 175人", "150人", " 125人", " 100人", " 75人", " 50人", " 25人"}; y = 100; for (int i = 0; i < 9; i++) { g.drawstring(m[i].tostring(), font, brushes.red, 10, y); //设置文字内容及输出位置 y = y + 26; } int[] count1 = new int[7]; int[] count2 = new int[7]; sqlconnection con = new sqlconnection("server=(local);database=committeetraining;uid=sa;pwd=eesoft"); con.open(); string cmdtxt2 = "select * from ##count where company='" + this.ddltaget.selecteditem.text.trim() + "'"; sqldataadapter da = new sqldataadapter(cmdtxt2, con); dataset ds = new dataset(); da.fill(ds); //报名人数 count1[0] = convert.toint32(ds.tables[0].rows[0]["count1"].tostring()); count1[1] = convert.toint32(ds.tables[0].rows[0]["count3"].tostring()); count1[2] = convert.toint32(ds.tables[0].rows[0]["count5"].tostring()); count1[3] = convert.toint32(ds.tables[0].rows[0]["count7"].tostring()); count1[6] = convert.toint32(ds.tables[0].rows[0]["count9"].tostring()); //全年 count1[4] = count1[0] + count1[1]; count1[5] = count1[2] + count1[3]; count2[0] = convert.toint32(ds.tables[0].rows[0]["count2"].tostring()); count2[1] = convert.toint32(ds.tables[0].rows[0]["count4"].tostring()); count2[2] = convert.toint32(ds.tables[0].rows[0]["count6"].tostring()); count2[3] = convert.toint32(ds.tables[0].rows[0]["count8"].tostring()); count2[6] = convert.toint32(ds.tables[0].rows[0]["count10"].tostring()); //全年 count2[4] = count2[0] + count2[1]; count2[5] = count2[2] + count2[3]; //显示折线效果 font font3 = new system.drawing.font("arial", 10, fontstyle.bold); solidbrush mybrush = new solidbrush(color.red); point[] points1 = new point[7]; points1[0].x = 60; points1[0].y = 340 - count1[0]; //从106纵坐标开始, 到(0, 0)坐标时 points1[1].x = 140; points1[1].y = 340 - count1[1]; points1[2].x = 220; points1[2].y = 340 - count1[2]; points1[3].x = 300; points1[3].y = 340 - count1[3]; points1[4].x = 380; points1[4].y = 340 - count1[4]; points1[5].x = 460; points1[5].y = 340 - count1[5]; points1[6].x = 540; points1[6].y = 340 - count1[6]; g.drawlines(mypen2, points1); //绘制折线 //绘制数字 g.drawstring(count1[0].tostring(), font3, brushes.red, 58, points1[0].y - 20); g.drawstring(count1[1].tostring(), font3, brushes.red, 138, points1[1].y - 20); g.drawstring(count1[2].tostring(), font3, brushes.red, 218, points1[2].y - 20); g.drawstring(count1[3].tostring(), font3, brushes.red, 298, points1[3].y - 20); g.drawstring(count1[4].tostring(), font3, brushes.red, 378, points1[4].y - 20); g.drawstring(count1[5].tostring(), font3, brushes.red, 458, points1[5].y - 20); g.drawstring(count1[6].tostring(), font3, brushes.red, 538, points1[6].y - 20); pen mypen3 = new pen(color.green, 2); point[] points2 = new point[7]; points2[0].x = 60; points2[0].y = 340 - count2[0]; points2[1].x = 140; points2[1].y = 340 - count2[1]; points2[2].x = 220; points2[2].y = 340 - count2[2]; points2[3].x = 300; points2[3].y = 340 - count2[3]; points2[4].x = 380; points2[4].y = 340 - count2[4]; points2[5].x = 460; points2[5].y = 340 - count2[5]; points2[6].x = 540; points2[6].y = 340 - count2[6]; g.drawlines(mypen3, points2); //绘制折线 //绘制通过人数 g.drawstring(count2[0].tostring(), font3, brushes.green, 61, points2[0].y - 15); g.drawstring(count2[1].tostring(), font3, brushes.green, 131, points2[1].y - 15); g.drawstring(count2[2].tostring(), font3, brushes.green, 221, points2[2].y - 15); g.drawstring(count2[3].tostring(), font3, brushes.green, 301, points2[3].y - 15); g.drawstring(count2[4].tostring(), font3, brushes.green, 381, points2[4].y - 15); g.drawstring(count2[5].tostring(), font3, brushes.green, 461, points2[5].y - 15); g.drawstring(count2[6].tostring(), font3, brushes.green, 541, points2[6].y - 15); //绘制标识 g.drawrectangle(new pen(brushes.red), 180, 390, 250, 50); //绘制范围框 g.fillrectangle(brushes.red, 270, 402, 20, 10); //绘制小矩形 g.drawstring("报名人数", font2, brushes.red, 292, 400); g.fillrectangle(brushes.green, 270, 422, 20, 10); g.drawstring("通过人数", font2, brushes.green, 292, 420); 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 { g.dispose(); image.dispose(); } }
三. 扇形统计图的绘制
扇形图完整代码:
private void createimage() { //把连接字串指定为一个常量 sqlconnection con = new sqlconnection("server=(local); database=committeetraining;uid=sa;pwd=**"); con.open(); string cmdtxt = selectstring; // "select * from ##count"; // //sqlcommand com = new sqlcommand(cmdtxt, con); dataset ds = new dataset(); sqldataadapter da = new sqldataadapter(cmdtxt, con); da.fill(ds); con.close(); float total = 0.0f, tmp; //转换成单精度。也可写成convert.toint32 total = convert.tosingle(ds.tables[0].rows[0][this.count[0]]); // total=convert.tosingle(ds.tables[0].rows[0][this.count[0]]); //设置字体,fonttitle为主标题的字体 font fontlegend = new font("verdana", 9); font fonttitle = new font("verdana", 10, fontstyle.bold); //背景宽 int width = 350; int bufferspace = 15; int legendheight = fontlegend.height * 10 + bufferspace; //高度 int titleheight = fonttitle.height + bufferspace; int height = width + legendheight + titleheight + bufferspace;//白色背景高 int pieheight = width; rectangle pierect = new rectangle(0, titleheight, width, pieheight); //加上各种随机色 arraylist colors = new arraylist(); random rnd = new random(); for (int i = 0; i < 2; i++) colors.add(new solidbrush(color.fromargb(rnd.next(255), rnd.next(255), rnd.next(255)))); //创建一个bitmap实例 bitmap objbitmap = new bitmap(width, height); graphics objgraphics = graphics.fromimage(objbitmap); //画一个白色背景 objgraphics.fillrectangle(new solidbrush(color.white), 0, 0, width, height); //画一个亮黄色背景 objgraphics.fillrectangle(new solidbrush(color.beige), pierect); //以下为画饼图(有几行row画几个) float currentdegree = 0.0f; //画通过人数 objgraphics.fillpie((solidbrush)colors[1], pierect, currentdegree, convert.tosingle(ds.tables[0].rows[0][this.count[1]]) / total * 360); currentdegree += convert.tosingle(ds.tables[0].rows[0][this.count[1]]) / total * 360; //未通过人数饼状图 objgraphics.fillpie((solidbrush)colors[0], pierect, currentdegree, ((convert.tosingle(ds.tables[0].rows[0][this.count[0]]))-(convert.tosingle(ds.tables[0].rows[0][this.count[1]]))) / total * 360); currentdegree += ((convert.tosingle(ds.tables[0].rows[0][this.count[0]])) - (convert.tosingle(ds.tables[0].rows[0][this.count[1]]))) / total * 360; //以下为生成主标题 solidbrush blackbrush = new solidbrush(color.black); solidbrush bluebrush = new solidbrush(color.blue); string title = " 机关单位成绩统计饼状图: " + "\n \n\n"; stringformat stringformat = new stringformat(); stringformat.alignment = stringalignment.center; stringformat.linealignment = stringalignment.center; objgraphics.drawstring(title, fonttitle, blackbrush, new rectangle(0, 0, width, titleheight), stringformat); //列出各字段与得数目 objgraphics.drawrectangle(new pen(color.red, 2), 0, height + 10 - legendheight, width, legendheight + 50); objgraphics.drawstring("----------------统计信息------------------", fontlegend, bluebrush, 20, height - legendheight + fontlegend.height * 1 + 1); objgraphics.drawstring("统计单位: " + this.ddltaget.selecteditem.text, fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 3 + 1); objgraphics.drawstring("统计年份: " + this.ddlyear.selecteditem.text, fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 4 + 1); objgraphics.drawstring("统计期数: " + this.ddlspan.selecteditem.text, fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 5 + 1); objgraphics.fillrectangle((solidbrush)colors[1], 5,height - legendheight + fontlegend.height * 8 + 1, 10, 10); objgraphics.drawstring("报名总人数: " + convert.tostring(convert.tosingle(ds.tables[0].rows[0][this.count[0]])), fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 7 + 1); objgraphics.fillrectangle((solidbrush)colors[0], 5, height - legendheight + fontlegend.height * 9 + 1, 10, 10); objgraphics.drawstring("通过总人数: " + convert.tostring(convert.tosingle(ds.tables[0].rows[0][this.count[1]])), fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 8 + 1); objgraphics.drawstring("未通过人数: " + ((convert.tosingle(ds.tables[0].rows[0][this.count[0]])) - (convert.tosingle(ds.tables[0].rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 9 + 1); objgraphics.drawstring("通过率: " + convert.tostring((convert.tosingle(ds.tables[0].rows[0][this.count[1]]) / convert.tosingle(ds.tables[0].rows[0][this.count[0]])) * 100)+ " %", fontlegend, blackbrush, 20, height - legendheight + fontlegend.height * 10 + 1); response.contenttype = "image/jpeg"; objbitmap.save(response.outputstream, system.drawing.imaging.imageformat.jpeg); objgraphics.dispose(); objbitmap.dispose(); }
这里的统计图直接输出到网页,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。