最近项目要用C#实现画一个雷达图,搜了搜网上竟然找不到C#画雷达图的解决方案,那么自己实现一个吧

实现效果如下图:

代码如下:

     public static class RadarDemo
{
static float mW = ;
static float mH = ;
static Dictionary<string, float> mData = new Dictionary<string, float>
{
//{ "速度",77},
{ "力量", },
{ "防守", },
{ "射门", },
{ "传球", },
{ "耐力", }
};//维度数据
static float mCount = mData.Count; //边数
static float mCenter = mW * 0.5f; //中心点
static float mRadius = mCenter - ; //半径(减去的值用于给绘制的文本留空间)
static double mAngle = (Math.PI * ) / mCount; //角度
static Graphics graphics = null;
static int mPointRadius = ; // 各个维度分值圆点的半径
static int textFontSize = ; //顶点文字大小 px
const string textFontFamily = "Microsoft Yahei"; //顶点字体
static Color lineColor = Color.Green;
static Color fillColor = Color.FromArgb(, , , );
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 = ; i < mCount; i++)
{
var points = new List<PointF>();
var currR = r * (i + ); //当前半径
//画6条边
for (var j = ; 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 = ; 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 = ;
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 > && mAngle * i <= Math.PI / )
{
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 / && 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 * / )
{
ctx.DrawString(item.Key, font, new SolidBrush(fontColor), x - ctx.MeasureString(item.Key, font).Width, y);
}
else if (mAngle * i > Math.PI * / )
{
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 = ;
List<PointF> points = new List<PointF>();
foreach (var item in mData)
{
var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / );
var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / ); 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 = ;
foreach (var item in mData)
{
var x = (float)(mCenter + mRadius * Math.Cos(mAngle * i) * item.Value / );
var y = (float)(mCenter + mRadius * Math.Sin(mAngle * i) * item.Value / );
ctx.FillPie(new SolidBrush(fillColor), x - r, y - r, r * , r * , , );
//ctx.DrawArc(new Pen(lineColor), x, y, r, r, 0, (float)Math.PI * 2);
i++;
}
ctx.Save();
} }

把这个类粘贴到你的项目中,执行RadarDemo.Show();就会在你的根目录里生成雷达图了,为了方便理解怎么画出来的,我把画每一个步骤时的图片都保存下来了。可以自行运行查看

C# 使用GDI绘制雷达图的更多相关文章

  1. 【带着canvas去流浪(6)】绘制雷达图

    目录 一. 任务说明 二. 重点提示 三. 示例代码 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文 ...

  2. Emgu-WPF 激光雷达研究-绘制雷达图

    原文:Emgu-WPF 激光雷达研究-绘制雷达图 硬件:Hokuyo URG04LX 环境:VS2017- win10- 64  Emgu_3.2.0.2682 语言:C#  WPF   数据解析参考 ...

  3. 带着canvas去流浪系列之六 绘制雷达图

    [摘要] 用canvas原生API实现百度Echarts基本图表. 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvas ...

  4. 利用matlibplot绘制雷达图

    之前在一些数据分析案例中看到用 Go 语言绘制的雷达图,非常的漂亮,就想着用matlibplot.pyplot也照着画一个,遗憾的是matlibplot.pyplot模块中没有直接绘制雷达图的函数,不 ...

  5. 利用d3.js绘制雷达图

    利用d3,js将数据可视化,能够做到数据与代码的分离.方便以后改动数据. 这次利用d3.js绘制了一个五维的雷达图.即将多个对象的五种属性在一张图上对照. 数据写入data.csv.数据类型写入typ ...

  6. Mesh绘制雷达图(UGUI)

    参考资料:http://www.cnblogs.com/jeason1997/p/5130413.html ** 描述:雷达图 刷新 radarDate.SetVerticesDirty(); usi ...

  7. Python绘制雷达图(俗称六芒星)

    原文链接:https://blog.csdn.net/Just_youHG/article/details/83904618 背景 <Python数据分析与挖掘实战> 案例2–航空公司客户 ...

  8. wepy绘制雷达图

    代码如下: <style lang='less'> .radar-canvas2 { width: 690rpx; height: 420rpx; } </style> < ...

  9. R语言绘图:雷达图

    使用fmsb包绘制雷达图 library("fmsb") radarfig <- rbind(rep(90, 4), rep(60, 4), c(86.17, 73.96, ...

随机推荐

  1. java基础集合简介Map(三)下

    --Map接口简介 今天来看一看map集合,map映射接口,用于存放键值对,<key,value>,通过key来查找value,顾名思义key不能为空,唯一且不重复,不然底层怎么查呢! 可 ...

  2. Django3.0 异步通信初体验(小结)

    2019年12月2日,Django终于正式发布了3.0版本.怀着无比的期待,我们来尝试一下吧! (附ASGI官方文档地址:https://asgi.readthedocs.io/en/latest/e ...

  3. 在 Java 中如何比较日期?

    在 Java 中有多种方法可以比较日期,日期在计算机内部表示为(long型)时间点--自1970年1月1日以来经过的毫秒数.在Java中,Date是一个对象,包含多个用于比较的方法,任何比较两个日期的 ...

  4. android只设置部分控件随着软键盘的出现而腾出空间

    转载请标明出处:https://www.cnblogs.com/tangZH/p/12013685.html 在项目过程中,出现了一个需求,软键盘要顶起部分控件,而另一部分控件不动. 关于这种需求,我 ...

  5. webpack4 配置

    package.json 开发环境/生产环境 webpack.config.js

  6. 一个经典的代码--Convert char to int in C and C++

    前记 写程序,就像建房子,对于高超的建筑师来说,是要有一些好的素材的.作为一个程序员,见了好用的素材存起来,以备后面需要,也是一门很好的修养. 实例代码 一个char 转int的经典代码,这里分享一下 ...

  7. Cordova搭建,所遇到问题处理

    环境:NodeJs.[Android SDK | IOS] 安装:npm install -g cordova 过程: 1.创建一个项目:cordova create myApp 2.选择平台: co ...

  8. python集合和eval的使用

    python集合和eval的使用 创建集合 使用工厂方法 set()和 frozenset(): >>> s = set('cheeseshop') >>> s s ...

  9. Python 和 JS 有什么相似?

    Python 是一门运用很广泛的语言,自动化脚本.爬虫,甚至在深度学习领域也都有 Python 的身影.作为一名前端开发者,也了解 ES6 中的很多特性借鉴自 Python (比如默认参数.解构赋值. ...

  10. Redis 命令执行全过程分析

    今天我们来了解一下 Redis 命令执行的过程.我们曾简单的描述了一条命令的执行过程,本篇文章展示深入说明一下,加深大家对 Redis 的了解. 如下图所示,一条命令执行完成并且返回数据一共涉及三部分 ...