最近项目要用c#实现画一个雷达图,搜了搜网上竟然找不到c#画雷达图的解决方案,那么自己实现一个吧
实现效果如下图:
代码如下:
public static class radardemo { static float mw = 1200; static float mh = 1200; static dictionary<string, float> mdata = new dictionary<string, float> { //{ "速度",77}, { "力量", 72}, { "防守", 110}, { "射门", 50}, { "传球", 80}, { "耐力", 60 } };//维度数据 static float mcount = mdata.count; //边数 static float mcenter = mw * 0.5f; //中心点 static float mradius = mcenter - 100; //半径(减去的值用于给绘制的文本留空间) static double mangle = (math.pi * 2) / mcount; //角度 static graphics graphics = null; static int mpointradius = 5; // 各个维度分值圆点的半径 static int textfontsize = 18; //顶点文字大小 px const string textfontfamily = "microsoft yahei"; //顶点字体 static color linecolor = color.green; static color fillcolor = color.fromargb(128, 255, 0, 0); static color fontcolor = color.black; public static void show() { bitmap img = new bitmap((int)mw, (int)mh); graphics = graphics.fromimage(img); graphics.clear(color.white); img.save($"{appdomain.currentdomain.basedirectory}radar/0.png", imageformat.png); drawpolygon(graphics); img.save($"{appdomain.currentdomain.basedirectory}radar/1.png", imageformat.png); drawlines(graphics); img.save($"{appdomain.currentdomain.basedirectory}radar/2.png", imageformat.png); drawtext(graphics); img.save($"{appdomain.currentdomain.basedirectory}radar/3.png", imageformat.png); drawregion(graphics); img.save($"{appdomain.currentdomain.basedirectory}radar/4.png", imageformat.png); drawcircle(graphics); img.save($"{appdomain.currentdomain.basedirectory}radar/5.png", imageformat.png); img.dispose(); graphics.dispose(); } // 绘制多边形边 private static void drawpolygon(graphics ctx) { var r = mradius / mcount; //单位半径 pen pen = new pen(linecolor); //画6个圈 for (var i = 0; i < mcount; i++) { var points = new list<pointf>(); var currr = r * (i + 1); //当前半径 //画6条边 for (var j = 0; j < mcount; j++) { var x = (float)(mcenter + currr * math.cos(mangle * j)); var y = (float)(mcenter + currr * math.sin(mangle * j)); points.add(new pointf { x = x, y = y }); } ctx.drawpolygon(pen, points.toarray()); //break; } ctx.save(); } //顶点连线 private static void drawlines(graphics ctx) { for (var i = 0; i < mcount; i++) { var x = (float)(mcenter + mradius * math.cos(mangle * i)); var y = (float)(mcenter + mradius * math.sin(mangle * i)); ctx.drawline(new pen(linecolor), new pointf { x = mcenter, y = mcenter }, new pointf { x = x, y = y }); //break; } ctx.save(); } //绘制文本 private static void drawtext(graphics ctx) { var fontsize = textfontsize;//mcenter / 12; font font = new font(textfontfamily, fontsize, fontstyle.regular); int i = 0; foreach (var item in mdata) { var x = (float)(mcenter + mradius * math.cos(mangle * i)); var y = (float)(mcenter + mradius * math.sin(mangle * i) - fontsize); if (mangle * i > 0 && mangle * i <= math.pi / 2) { ctx.drawstring(item.key, font, new solidbrush(fontcolor), x - ctx.measurestring(item.key, font).width * 0.5f, y + fontsize/* y + fontsize*/); } else if (mangle * i > math.pi / 2 && mangle * i <= math.pi) { ctx.drawstring(item.key, font, new solidbrush(fontcolor), x - ctx.measurestring(item.key, font).width, y /*y + fontsize*/); } else if (mangle * i > math.pi && mangle * i <= math.pi * 3 / 2) { ctx.drawstring(item.key, font, new solidbrush(fontcolor), x - ctx.measurestring(item.key, font).width, y); } else if (mangle * i > math.pi * 3 / 2) { ctx.drawstring(item.key, font, new solidbrush(fontcolor), x - ctx.measurestring(item.key, font).width * 0.5f, y - fontsize * 0.5f); } else { ctx.drawstring(item.key, font, new solidbrush(fontcolor), x, y /* y + fontsize*/); } i++; } ctx.save(); } //绘制数据区域 private static void drawregion(graphics ctx) { int i = 0; list<pointf> points = new list<pointf>(); foreach (var item in mdata) { var x = (float)(mcenter + mradius * math.cos(mangle * i) * item.value / 100); var y = (float)(mcenter + mradius * math.sin(mangle * i) * item.value / 100); points.add(new pointf { x = x, y = y }); //ctx.drawarc(new pen(linecolor), x, y, r, r, 0, (float)math.pi * 2); i++; } //graphicspath path = new graphicspath(); //path.addlines(points.toarray()); ctx.fillpolygon(new solidbrush(fillcolor), points.toarray()); ctx.save(); } //画点 private static void drawcircle(graphics ctx) { //var r = mcenter / 18; var r = mpointradius; int i = 0; foreach (var item in mdata) { var x = (float)(mcenter + mradius * math.cos(mangle * i) * item.value / 100); var y = (float)(mcenter + mradius * math.sin(mangle * i) * item.value / 100); ctx.fillpie(new solidbrush(fillcolor), x - r, y - r, r * 2, r * 2, 0, 360); //ctx.drawarc(new pen(linecolor), x, y, r, r, 0, (float)math.pi * 2); i++; } ctx.save(); } }
把这个类粘贴到你的项目中,执行radardemo.show();就会在你的根目录里生成雷达图了,为了方便理解怎么画出来的,我把画每一个步骤时的图片都保存下来了。可以自行运行查看
总结
以上所述是小编给大家介绍的c# 使用gdi绘制雷达图的实例,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论