C# 使用GDI绘制雷达图
时间:2019-11-22
本文章向大家介绍C# 使用GDI绘制雷达图,主要包括C# 使用GDI绘制雷达图使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
最近项目要用C#实现画一个雷达图,搜了搜网上竟然找不到C#画雷达图的解决方案,那么自己实现一个吧
实现效果如下图:
代码如下:
1 public static class RadarDemo 2 { 3 static float mW = 1200; 4 static float mH = 1200; 5 static Dictionary<string, float> mData = new Dictionary<string, float> 6 { 7 //{ "速度",77}, 8 { "力量", 72}, 9 { "防守", 110}, 10 { "射门", 50}, 11 { "传球", 80}, 12 { "耐力", 60 } 13 };//维度数据 14 static float mCount = mData.Count; //边数 15 static float mCenter = mW * 0.5f; //中心点 16 static float mRadius = mCenter - 100; //半径(减去的值用于给绘制的文本留空间) 17 static double mAngle = (Math.PI * 2) / mCount; //角度 18 static Graphics graphics = null; 19 static int mPointRadius = 5; // 各个维度分值圆点的半径 20 static int textFontSize = 18; //顶点文字大小 px 21 const string textFontFamily = "Microsoft Yahei"; //顶点字体 22 static Color lineColor = Color.Green; 23 static Color fillColor = Color.FromArgb(128, 255, 0, 0); 24 static Color fontColor = Color.Black; 25 26 public static void Show() 27 { 28 Bitmap img = new Bitmap((int)mW, (int)mH); 29 graphics = Graphics.FromImage(img); 30 graphics.Clear(Color.White); 31 img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/0.png", ImageFormat.Png); 32 DrawPolygon(graphics); 33 img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/1.png", ImageFormat.Png); 34 DrawLines(graphics); 35 img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/2.png", ImageFormat.Png); 36 DrawText(graphics); 37 img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/3.png", ImageFormat.Png); 38 DrawRegion(graphics); 39 img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/4.png", ImageFormat.Png); 40 DrawCircle(graphics); 41 img.Save($"{AppDomain.CurrentDomain.BaseDirectory}radar/5.png", ImageFormat.Png); 42 img.Dispose(); 43 graphics.Dispose(); 44 45 } 46 47 48 // 绘制多边形边 49 private static void DrawPolygon(Graphics ctx) 50 { 51 var r = mRadius / mCount; //单位半径 52 Pen pen = new Pen(lineColor); 53 //画6个圈 54 for (var i = 0; i < mCount; i++) 55 { 56 var points = new List<PointF>(); 57 var currR = r * (i + 1); //当前半径 58 //画6条边 59 for (var j = 0; j < mCount; j++) 60 { 61 var x = (float)(mCenter + currR * Math.Cos(mAngle * j)); 62 var y = (float)(mCenter + currR * Math.Sin(mAngle * j)); 63 points.Add(new PointF { X = x, Y = y }); 64 } 65 ctx.DrawPolygon(pen, points.ToArray()); 66 //break; 67 } 68 69 ctx.Save(); 70 } 71 72 //顶点连线 73 private static void DrawLines(Graphics ctx) 74 { 75 for (var i = 0; i < mCount; i++) 76 { 77 var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i)); 78 var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i)); 79 ctx.DrawLine(new Pen(lineColor), new PointF { X = mCenter, Y = mCenter }, new PointF { X = x, Y = y }); 80 //break; 81 } 82 ctx.Save(); 83 } 84 85 //绘制文本 86 private static void DrawText(Graphics ctx) 87 { 88 var fontSize = textFontSize;//mCenter / 12; 89 Font font = new Font(textFontFamily, fontSize, FontStyle.Regular); 90 91 int i = 0; 92 foreach (var item in mData) 93 { 94 var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i)); 95 var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) - fontSize); 96 97 if (mAngle * i > 0 && mAngle * i <= Math.PI / 2) 98 { 99 ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width * 0.5f, y + fontSize/* y + fontSize*/); 100 } 101 else if (mAngle * i > Math.PI / 2 && mAngle * i <= Math.PI) 102 { 103 ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width, y /*y + fontSize*/); 104 } 105 else if (mAngle * i > Math.PI && mAngle * i <= Math.PI * 3 / 2) 106 { 107 ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width, y); 108 } 109 else if (mAngle * i > Math.PI * 3 / 2) 110 { 111 ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width * 0.5f, y - fontSize * 0.5f); 112 } 113 else 114 { 115 ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x, y /* y + fontSize*/); 116 } 117 i++; 118 } 119 ctx.Save(); 120 } 121 122 //绘制数据区域 123 private static void DrawRegion(Graphics ctx) 124 { 125 int i = 0; 126 List<PointF> points = new List<PointF>(); 127 foreach (var item in mData) 128 { 129 var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / 100); 130 var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / 100); 131 132 points.Add(new PointF { X = x, Y = y }); 133 134 //ctx.DrawArc(new Pen(lineColor), x, y, r, r, 0, (float)Math.PI * 2); 135 i++; 136 } 137 138 139 //GraphicsPath path = new GraphicsPath(); 140 //path.AddLines(points.ToArray()); 141 142 ctx.FillPolygon(new SolidBrush(fillColor), points.ToArray()); 143 144 ctx.Save(); 145 } 146 147 //画点 148 private static void DrawCircle(Graphics ctx) 149 { 150 //var r = mCenter / 18; 151 var r = mPointRadius; 152 153 int i = 0; 154 foreach (var item in mData) 155 { 156 var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / 100); 157 var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / 100); 158 ctx.FillPie(new SolidBrush(fillColor), x - r, y - r, r * 2, r * 2, 0, 360); 159 //ctx.DrawArc(new Pen(lineColor), x, y, r, r, 0, (float)Math.PI * 2); 160 i++; 161 } 162 ctx.Save(); 163 } 164 165 }
把这个类粘贴到你的项目中,执行RadarDemo.Show();就会在你的根目录里生成雷达图了,为了方便理解怎么画出来的,我把画每一个步骤时的图片都保存下来了。可以自行运行查看
原文地址:https://www.cnblogs.com/chenmsg/p/11910154.html
- 腾讯高级副总裁郭凯天:打造腾讯智库分析互联网产业前沿问题
- Google Chrome 浏览器 开发者工具 使用教程
- 反向代理(Reverse Proxy)及 IIS 7 应用请求路由模块
- 2014腾讯“大数据连接的未来”高峰论坛在京召开
- 工作流、业务流程管理和SOA
- 面向对象设计的SOLID原则
- 用psake来简化自动化脚本的构建
- TESLA V100如何让质疑GPU的流言“失声”
- Web 前端性能优化相关内容解析
- Service Broker 无法工作的问题修复
- .NET代码快速转换成powershell代码
- 网站性能评分工具Yslow 使用教程
- Temp权限引起的WCF问题
- WordPress 中强制设置 特色图像 才能发表文章
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 一文透析SpringCloud,关于Bus消息总线,总算梳理清楚了
- LeetCode 102. 二叉树的层序遍历
- 剑指Offer LeetCode 面试题10- II. 青蛙跳台阶问题
- 剑指Offer LeetCode 面试题10- I. 斐波那契数列
- 蓝桥杯 1的个数
- 蓝桥杯-试题 算法训练 数据交换
- 解决mysql导入新数据库大小写问题(Table 'zup.Domain_System' doesn't exist)
- 蓝桥杯vip试题 报时助手
- 蓝桥杯-基础练习 查找整数
- 蓝桥杯-基础练习 数列排序
- 无线网络-何为ISM频段?
- 蓝桥杯vip测试题系统试题-算法提高 矩阵转置
- 蓝桥杯vip测试题系统-数组求和(解题思路以及解题代码,手画思路图虽然丑丑的)
- 蓝桥杯vip测试题-找零钱(解题思路以及解题代码)
- 剑指Office-二进制中1的个数