怎么在C#项目中利用GDI绘制一个雷达图
作者
这期内容当中小编将会给大家带来有关怎么在C#项目中利用GDI绘制一个雷达图,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
代码如下:
publicstaticclassRadarDemo { staticfloatmW=1200; staticfloatmH=1200; staticDictionary<string,float>mData=newDictionary<string,float> { //{"速度",77}, {"力量",72}, {"防守",110}, {"射门",50}, {"传球",80}, {"耐力",60} };//维度数据 staticfloatmCount=mData.Count;//边数 staticfloatmCenter=mW*0.5f;//中心点 staticfloatmRadius=mCenter-100;//半径(减去的值用于给绘制的文本留空间) staticdoublemAngle=(Math.PI*2)/mCount;//角度 staticGraphicsgraphics=null; staticintmPointRadius=5;//各个维度分值圆点的半径 staticinttextFontSize=18;//顶点文字大小px conststringtextFontFamily="MicrosoftYahei";//顶点字体 staticColorlineColor=Color.Green; staticColorfillColor=Color.FromArgb(128,255,0,0); staticColorfontColor=Color.Black; publicstaticvoidShow() { Bitmapimg=newBitmap((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(); } //绘制多边形边 privatestaticvoidDrawPolygon(Graphicsctx) { varr=mRadius/mCount;//单位半径 Penpen=newPen(lineColor); //画6个圈 for(vari=0;i<mCount;i++) { varpoints=newList<PointF>(); varcurrR=r*(i+1);//当前半径 //画6条边 for(varj=0;j<mCount;j++) { varx=(float)(mCenter+currR*Math.Cos(mAngle*j)); vary=(float)(mCenter+currR*Math.Sin(mAngle*j)); points.Add(newPointF{X=x,Y=y}); } ctx.DrawPolygon(pen,points.ToArray()); //break; } ctx.Save(); } //顶点连线 privatestaticvoidDrawLines(Graphicsctx) { for(vari=0;i<mCount;i++) { varx=(float)(mCenter+mRadius*Math.Cos(mAngle*i)); vary=(float)(mCenter+mRadius*Math.Sin(mAngle*i)); ctx.DrawLine(newPen(lineColor),newPointF{X=mCenter,Y=mCenter},newPointF{X=x,Y=y}); //break; } ctx.Save(); } //绘制文本 privatestaticvoidDrawText(Graphicsctx) { varfontSize=textFontSize;//mCenter/12; Fontfont=newFont(textFontFamily,fontSize,FontStyle.Regular); inti=0; foreach(variteminmData) { varx=(float)(mCenter+mRadius*Math.Cos(mAngle*i)); vary=(float)(mCenter+mRadius*Math.Sin(mAngle*i)-fontSize); if(mAngle*i>0&&mAngle*i<=Math.PI/2) { ctx.DrawString(item.Key,font,newSolidBrush(fontColor),x-ctx.MeasureString(item.Key,font).Width*0.5f,y+fontSize/*y+fontSize*/); } elseif(mAngle*i>Math.PI/2&&mAngle*i<=Math.PI) { ctx.DrawString(item.Key,font,newSolidBrush(fontColor),x-ctx.MeasureString(item.Key,font).Width,y/*y+fontSize*/); } elseif(mAngle*i>Math.PI&&mAngle*i<=Math.PI*3/2) { ctx.DrawString(item.Key,font,newSolidBrush(fontColor),x-ctx.MeasureString(item.Key,font).Width,y); } elseif(mAngle*i>Math.PI*3/2) { ctx.DrawString(item.Key,font,newSolidBrush(fontColor),x-ctx.MeasureString(item.Key,font).Width*0.5f,y-fontSize*0.5f); } else { ctx.DrawString(item.Key,font,newSolidBrush(fontColor),x,y/*y+fontSize*/); } i++; } ctx.Save(); } //绘制数据区域 privatestaticvoidDrawRegion(Graphicsctx) { inti=0; List<PointF>points=newList<PointF>(); foreach(variteminmData) { varx=(float)(mCenter+mRadius*Math.Cos(mAngle*i)*item.Value/100); vary=(float)(mCenter+mRadius*Math.Sin(mAngle*i)*item.Value/100); points.Add(newPointF{X=x,Y=y}); //ctx.DrawArc(newPen(lineColor),x,y,r,r,0,(float)Math.PI*2); i++; } //GraphicsPathpath=newGraphicsPath(); //path.AddLines(points.ToArray()); ctx.FillPolygon(newSolidBrush(fillColor),points.ToArray()); ctx.Save(); } //画点 privatestaticvoidDrawCircle(Graphicsctx) { //varr=mCenter/18; varr=mPointRadius; inti=0; foreach(variteminmData) { varx=(float)(mCenter+mRadius*Math.Cos(mAngle*i)*item.Value/100); vary=(float)(mCenter+mRadius*Math.Sin(mAngle*i)*item.Value/100); ctx.FillPie(newSolidBrush(fillColor),x-r,y-r,r*2,r*2,0,360); //ctx.DrawArc(newPen(lineColor),x,y,r,r,0,(float)Math.PI*2); i++; } ctx.Save(); } }
上述就是小编为大家分享的怎么在C#项目中利用GDI绘制一个雷达图了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注恰卡编程网行业资讯频道。
目录
推荐阅读
-
Web应用从零开始,初学者友好型开发教程
-
容器化最佳实践:Docker 与 Kubernetes 在微服务架构中的协同设计
-
AWS Cloud9 使用攻略:云端 IDE 如何无缝集成 Lambda 与 S3 服务?
-
Heroku vs AWS Elastic Beanstalk:快速部署 Web 应用的平台对比
-
Kubernetes 集群部署避坑:资源调度、服务发现与滚动更新策略
-
Docker 镜像优化指南:分层构建、瘦身技巧与多阶段编译实践
-
Postman 接口测试全流程:从 API 设计到自动化测试脚本编写
-
pytest 框架进阶:自定义 fixture、插件开发与持续集成集成方案
-
JUnit 5 新特性:参数化测试、扩展模型与微服务测试实践
-
Chrome DevTools 性能分析:FPS 监控、内存快照与网络请求优化指南